Show/Display Android Activity as a Dialog
Show/Display Android Activity as a Dialog. Here We will learn the simplest way to display an Android activity as a Dialog. When you will display the android activity as a dialog, it will work as a modal dialog. That means user can’t access it’s parent activity unless until the dialog activity will be destroyed. Once the dialog activity will close, then only it’s parent activity will get focus.
Android Activity as a Dialog Example with Source Code
Here we will discuss about How to show an Android Activity as a Dialog with example. We have taken 2 activity in the attached example to show an activity as a dialog. In the example when we will click on a button from the first activities, then we will show the second activity as a dialog. We added one Edittext and 2Buttons in the second activity for the test purpose only.
ActivityOne.Java File.
package com.jitesh.androidactivityasadialog;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ActivityOne extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button buttonYes = (Button) findViewById(R.id.button_show_activity_two);
buttonYes.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//open second activity(ActivityTwo)as a dialog here
Intent intent = new Intent(getBaseContext(),ActivityTwo.class);
startActivity(intent);
}
});
}
}
ActivityTwo.java File
package com.jitesh.androidactivityasadialog;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ActivityTwo extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// add below line if you want to remove title for the activity dialog
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_two);
final Button buttonYes = (Button) findViewById(R.id.button_ok);
buttonYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
EditText editText = (EditText) findViewById(R.id.edit_text);
String str = editText.getEditableText().toString();
if (0 != str.length()) {
Toast.makeText(getBaseContext(), "Hello Mr." + str,
Toast.LENGTH_SHORT).show();
}
}
});
final Button buttonNo = (Button) findViewById(R.id.button_cancel);
buttonNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
main.xml file for ActivityOne.java File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0F7732"
android:orientation="vertical" >
<Button
android:id="@+id/button_show_activity_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Show Activity as a Dialog"
android:textColor="#4E091E" />
</LinearLayout>
activity_two.xml file for ActivityTwo.java File
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DA7896"
android:orientation="vertical" >
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dip"
android:text="Enter Your Name" />
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text_view"
android:gravity="left"
android:singleLine="true"
android:textStyle="normal" >
</EditText>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_text"
android:layout_centerHorizontal="true"
android:background="#5B942C"
android:orientation="horizontal" >
<Button
android:id="@+id/button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:text="Ok"
android:textColor="#4E091E" >
</Button>
<Button
android:id="@+id/button_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dip"
android:layout_toRightOf="@+id/button_yes"
android:text="Cancel"
android:textColor="#4E091E" >
</Button>
</LinearLayout>
</RelativeLayout>
AndroidManifest.xml for the Example
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jitesh.androidactivityasadialog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.jitesh.androidactivityasadialog.ActivityOne"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ActivityTwo" android:theme="@android:style/Theme.Dialog">
</activity>
</application>
</manifest>
Download the source code from the link Activity as a dialog
No comments:
Post a Comment