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>
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>
MainActivity.java
package com.cfsuman.me.androidcodesnippets; import android.app.AlertDialog; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import android.view.LayoutInflater; import android.widget.EditText; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get reference of widgets from XML layout simpulan RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl); simpulan 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) { // Build an AlertDialog AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); LayoutInflater inflater = getLayoutInflater(); View dialogView = inflater.inflate(R.layout.alertdialog_custom_view,null); // Specify alert dialog is not cancelable/not ignorable builder.setCancelable(false); // Set the custom layout as alert dialog view builder.setView(dialogView); // Get the custom alert dialog view widgets reference Button btn_positive = (Button) dialogView.findViewById(R.id.dialog_positive_btn); Button btn_negative = (Button) dialogView.findViewById(R.id.dialog_negative_btn); simpulan EditText et_name = (EditText) dialogView.findViewById(R.id.et_name); // Create the alert dialog simpulan AlertDialog dialog = builder.create(); // Set positive/yes button click listener 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(); } }); } }




- How to add a cancel button on AlertDialog
- How to create an AlertDialog with custom layout
- How to dismiss AlertDialog
- How to use AlertDialog Builder
- How to create AlertDialog with theme
- How to create MultiChoice AlertDialog
- How to use Toast.makeText
- How to change EditText cursor color
- How to remove underline from EditText
- How to create a round Button
Komentar
Posting Komentar