In activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="95dp"
android:layout_marginTop="62dp"
android:textSize="19sp"
android:text="**********" />
<Button
android:id="@+id/set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="24dp"
android:text="Set Name" />
</RelativeLayout>
In dialog_box.xml
<?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" >
<EditText
android:id="@+id/name_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/ok"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@+id/name_edit_text"
android:layout_marginTop="50dp"
android:layout_toLeftOf="@+id/cancel"
android:text="Ok" />
<Button
android:id="@+id/cancel"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/ok"
android:layout_alignBottom="@+id/ok"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="Cancel" />
</RelativeLayout>
MainActivity.java
package com.rajeshvijayakumar.safr;
public class MainActivity extends Activity implements OnClickListener {
private TextView mNameTextView;
private Button mSetButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameTextView = (TextView) findViewById(R.id.name);
mSetButton = (Button) findViewById(R.id.set);
mSetButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.set:
Intent intent = new Intent(this, NameDialogActivity.class);
startActivityForResult(intent, 1);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data.getExtras().containsKey("name")) {
String name = data.getExtras().getString("name");
mNameTextView.setText(name);
}
}
}
NameDialogActivity.java
package com.rajeshvijayakumar.safr;
public class NameDialogActivity extends Activity implements OnClickListener {
private EditText mNameEditText;
private Button mOk;
private Button mCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_box);
mNameEditText = (EditText) findViewById(R.id.name_edit_text);
mOk = (Button) findViewById(R.id.ok);
mCancel = (Button) findViewById(R.id.cancel);
mOk.setOnClickListener(this);
mCancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = getIntent();
switch (v.getId()) {
case R.id.ok:
String name = mNameEditText.getText().toString();
intent.putExtra("name", name);
setResult(RESULT_OK, intent);
break;
case R.id.cancel:
setResult(RESULT_CANCELED, intent);
break;
}
finish();
}
}
In manifest.xml add theme as @android:style/Theme.Dialog for NameDialogActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rajeshvijayakumar.safr"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.rajeshvijayakumar.safr.MainActivity"
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=".NameDialogActivity"
android:theme="@android:style/Theme.Dialog" />
</application>
</manifest>
Output :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="95dp"
android:layout_marginTop="62dp"
android:textSize="19sp"
android:text="**********" />
<Button
android:id="@+id/set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="24dp"
android:text="Set Name" />
</RelativeLayout>
In dialog_box.xml
<?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" >
<EditText
android:id="@+id/name_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/ok"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@+id/name_edit_text"
android:layout_marginTop="50dp"
android:layout_toLeftOf="@+id/cancel"
android:text="Ok" />
<Button
android:id="@+id/cancel"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/ok"
android:layout_alignBottom="@+id/ok"
android:layout_alignParentRight="true"
android:layout_marginRight="14dp"
android:text="Cancel" />
</RelativeLayout>
MainActivity.java
package com.rajeshvijayakumar.safr;
public class MainActivity extends Activity implements OnClickListener {
private TextView mNameTextView;
private Button mSetButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameTextView = (TextView) findViewById(R.id.name);
mSetButton = (Button) findViewById(R.id.set);
mSetButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.set:
Intent intent = new Intent(this, NameDialogActivity.class);
startActivityForResult(intent, 1);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data.getExtras().containsKey("name")) {
String name = data.getExtras().getString("name");
mNameTextView.setText(name);
}
}
}
NameDialogActivity.java
package com.rajeshvijayakumar.safr;
public class NameDialogActivity extends Activity implements OnClickListener {
private EditText mNameEditText;
private Button mOk;
private Button mCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_box);
mNameEditText = (EditText) findViewById(R.id.name_edit_text);
mOk = (Button) findViewById(R.id.ok);
mCancel = (Button) findViewById(R.id.cancel);
mOk.setOnClickListener(this);
mCancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = getIntent();
switch (v.getId()) {
case R.id.ok:
String name = mNameEditText.getText().toString();
intent.putExtra("name", name);
setResult(RESULT_OK, intent);
break;
case R.id.cancel:
setResult(RESULT_CANCELED, intent);
break;
}
finish();
}
}
In manifest.xml add theme as @android:style/Theme.Dialog for NameDialogActivity
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rajeshvijayakumar.safr"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.rajeshvijayakumar.safr.MainActivity"
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=".NameDialogActivity"
android:theme="@android:style/Theme.Dialog" />
</application>
</manifest>
Output :