1. Create Project
2. Create promptdialog xml file
3. attach AlertDialog.Builder to AlertDialog
1. Create new Project
Open your tool( Eclipse) go to File-->New --> Android Project
2. Create Android Layout File
Right click on your project select New --> Android XML file name it to promptdialog.xml
bellow this code
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/background_material_light" tools:context="com.example.simpltutorial.MainActivity" > <Button android:id="@+id/btnprompt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="134dp" android:text="Prompt" /> </RelativeLayout>
In layout promptdialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Your Name Here" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> </LinearLayout>
In MainActivity.java follow this code it will alert box and allow user input and then toast result.
package com.mkyong.android; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { final Context context = this; private Button button; private EditText result; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // components from main.xml button = (Button) findViewById(R.id.buttonPrompt); result = (EditText) findViewById(R.id.editTextResult); // add button listener button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // get prompts.xml view LayoutInflater li = LayoutInflater.from(context); View promptsView = li.inflate(R.layout.prompts, null); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( context); // set prompts.xml to alertdialog builder alertDialogBuilder.setView(promptsView); final EditText userInput = (EditText) promptsView .findViewById(R.id.editTextDialogUserInput); // set dialog message alertDialogBuilder .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // get user input and set it to result // edit text result.setText(userInput.getText()); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { dialog.cancel(); } }); // create alert dialog AlertDialog alertDialog = alertDialogBuilder.create(); // show it alertDialog.show(); } }); } }
1 Comments
Customize Android Search Dialog Using Library
ReplyDeleteIn this tutorial, we are going to learn how to implement and customize android search dialog in our android application. Search functionality is one of the major features most android applications have.
http://www.tellmehow.co/customize-android-search-dialog/