There are times when you need to display a dialog window to get a confirmation from the user. In this case , you can override the onCreateDialog() protected method defined in the Activity base class to display a dialog window. The following Try It Out shows you how.
1. Using Android Studio , create a new Android project and name it Dialog. When presented with the option , name the main activity DialogActivity.
2.Add the following theme in bold to the AndroidMainfest.xml file. Be sure to change all instances of "com.android" to whatever package name your project is using.
<?xml version="1.0" encoding="utd-8"?>
<manifest xmlns:android"http://schemas.android.com/apk/res/android" package="com.android.dialog">
<application
android:allowBackup="true"
android:icon:="@mipmap/ic_launcher"
android:label="@String/app_name"
android:supportRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".DialogActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHAR" />
< / intent-filter>
</activity>
</application>
</manifest>
3. Compare your DialogActivity.java file to this:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snacbar;
import android.support.v7.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.MenuItem;
public class DialogActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle saveIntanceState)
{
super.onCreate(saveInstaceState);
setContentView(R.layout.activity_dialog);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fat = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnclickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
Snackbar.make(view, "Replace with your own action" ,
Snackbar.LENGTH_LONG)
.setAction("Action",null).show();
}
});
}
@Override
public boolean onCreateOpationsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu_dialog, menu);
}
@Override
public boolean onOpationItemSelected(MenuItem item)
{
//Handle action bar Item clicks here. The action Bar will
//automatically handle clicks on the Home/Up button, so long
//as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//notinspection SimplifiableIfStatement
if ( id == R.id.action_settings)
{
return true;
}
return super.onOpationsItemSelected(item);
}
}
4. Press Shift+F9 to debug the application on the Android emulator. Click the button to display the dialog.
How It Works
Android uses the Appcompat.Dialog theme to draw your standard activity as a free-floating dialog box.It would be very easy to modify this dialog to add some buttons if you needed to provide an OK or Cancel choice.
Notice also that theme is applied to the Activity , not the project. Therefore , you could have a project with multiple activities , and apply the dialog theme to just one of them.
1. Using Android Studio , create a new Android project and name it Dialog. When presented with the option , name the main activity DialogActivity.
2.Add the following theme in bold to the AndroidMainfest.xml file. Be sure to change all instances of "com.android" to whatever package name your project is using.
<?xml version="1.0" encoding="utd-8"?>
<manifest xmlns:android"http://schemas.android.com/apk/res/android" package="com.android.dialog">
<application
android:allowBackup="true"
android:icon:="@mipmap/ic_launcher"
android:label="@String/app_name"
android:supportRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".DialogActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Dialog" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHAR" />
< / intent-filter>
</activity>
</application>
</manifest>
3. Compare your DialogActivity.java file to this:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snacbar;
import android.support.v7.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.MenuItem;
public class DialogActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle saveIntanceState)
{
super.onCreate(saveInstaceState);
setContentView(R.layout.activity_dialog);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fat = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnclickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
Snackbar.make(view, "Replace with your own action" ,
Snackbar.LENGTH_LONG)
.setAction("Action",null).show();
}
});
}
@Override
public boolean onCreateOpationsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu_dialog, menu);
}
@Override
public boolean onOpationItemSelected(MenuItem item)
{
//Handle action bar Item clicks here. The action Bar will
//automatically handle clicks on the Home/Up button, so long
//as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//notinspection SimplifiableIfStatement
if ( id == R.id.action_settings)
{
return true;
}
return super.onOpationsItemSelected(item);
}
}
4. Press Shift+F9 to debug the application on the Android emulator. Click the button to display the dialog.
How It Works
Android uses the Appcompat.Dialog theme to draw your standard activity as a free-floating dialog box.It would be very easy to modify this dialog to add some buttons if you needed to provide an OK or Cancel choice.
Notice also that theme is applied to the Activity , not the project. Therefore , you could have a project with multiple activities , and apply the dialog theme to just one of them.
Comments
Post a Comment