How to add EditText to an AlertDialog in Android
Step 1. Create a layout activity_main.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity"
android:background="#c9d3ff"
>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Say Hello"
/>
<TextView
android:id="@+id/tv_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50dp"
android:textColor="#597aff"
android:layout_below="@id/btn"
/>
</RelativeLayout>
Step 2. Create a drawble layout custom alertdialog android with layout alertdialog_custom_view.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dialog_rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity"
android:background="#e9f1ba"
>
<TextView
android:id="@+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirmation."
android:padding="5dp"
android:background="#d5dda6"
android:textSize="15dp"
/>
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Input your name."
android:textSize="15dp"
android:layout_below="@id/dialog_title"
/>
<Button
android:id="@+id/dialog_positive_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:layout_alignParentRight="true"
android:background="#dde5ad"
android:layout_below="@id/et_name"
/>
<Button
android:id="@+id/dialog_negative_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:layout_toLeftOf="@id/dialog_positive_btn"
android:background="#dde5ad"
android:layout_marginRight="3dp"
android:layout_alignBaseline="@id/dialog_positive_btn"
/>
</RelativeLayout>
Step 3. Create a class Mainactivity.class
public class MainActivity extends Activity {Result add EditText to an AlertDialog in Android
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
final TextView tv_message = (TextView) findViewById(R.id.tv_message);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alertdialog_custom_view,null);
builder.setCancelable(false);
builder.setView(dialogView);
Button btn_positive = (Button) dialogView.findViewById(R.id.dialog_positive_btn);
Button btn_negative = (Button) dialogView.findViewById(R.id.dialog_negative_btn);
final EditText et_name = (EditText) dialogView.findViewById(R.id.et_name);
final AlertDialog dialog = builder.create();
btn_positive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Dismiss the alert dialog
dialog.cancel();
String name = et_name.getText().toString();
Toast.makeText(getApplication(),
"Submitted name : " + name, Toast.LENGTH_SHORT).show();
// Say hello to the submitter
tv_message.setText("Hello " + name + "!");
}
});
// Set negative/no button click listener
btn_negative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Dismiss/cancel the alert dialog
//dialog.cancel();
dialog.dismiss();
Toast.makeText(getApplication(),
"No button clicked", Toast.LENGTH_SHORT).show();
}
});
// Display the custom alert dialog on interface
dialog.show();
}
});
}
}
No comments:
Post a Comment