Android input dialog


When you create an application that needs to display an input dialog to the user, this Android tip might be useful to you. In this Android tip, I help you to create an input dialog that shows up to allow the user to input text into the text box (EditText). The input text will be passed to the activity before the input dialog is dismissed by pressing the OK button.
Before you can display the input dialog to the user and pass the input text to its parent activity (MainActivity), you create a class that extends the DialogFragment class. In this example, the name of the class is InputDiaglogFragment. In this class, you create an interface (OnResultDialog). In the interface, you define a method called onGetDialogResult that will be implemented in the parent activity. This method is called from the InputDiaglogFragment class (before you call the dismiss() method to close the dialog) to pass the input text to the activity. You should note that to be able to call the onGetDialogResult method that is implemented in the activity, you need to cast the activity to OnResultDialog when the dialog is attached to its parent activity.




package com.example.inputdialog;
import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class InputDialogFragment extends DialogFragment{
private OnResultDialog mCallback;
@Override public void onAttach(Activity activity) {
super.onAttach(activity);
// Cast the parent activity to OnResultDialog so that you are able
// call the onGetDialogResult implemented the activity
mCallback = (OnResultDialog)activity;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Set dialog title
getDialog().setTitle("Input dialog box");
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.dialoglayout, container, false);
// Create a reference to the EditText
final EditText txtInput=(EditText)view.findViewById(R.id.txtinput);
// Add click listener to the button
// Create a reference to the Button
Button bt=(Button)view.findViewById(R.id.btok);
// Add click listener to the button
bt.setOnClickListener(new OnClickListener(){
public void onClick(View view){
// Pass the input text to the parent activity
mCallback.onGetDialogResult(txtInput.getText());
dismiss(); //close the dialog
}
});
return view;
}

public interface OnResultDialog{
public void onGetDialogResult(Object result);
}

}

Here is the layout file (dialoglayout.xml) that is required to display an input text box and a button on the input dialog. This file is placed in the res/layout directory.

<?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" >
    
  <EditText
   android:id="@+id/txtinput"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:hint="Enter a value"
   android:layout_marginBottom="20dp"
   android:layout_marginTop="20dp"
     />
  <Button
   android:id="@+id/btok"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text="OK"
     />

</LinearLayout>


To display the input dialog, in the MainActivity, you create an instance of the InputDialogFragment and call the show() method.



package com.example.inputdialog;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity implements InputDialogFragment.OnResultDialog {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     //create dialog instance
     InputDialogFragment df=new InputDialogFragment();
     //show the dialog
     df.show(getFragmentManager(), "df");     
   }
   // Implementation of the onGetDialogResult method of the OnResultInterface
   // defined in InputDialogFragment class
   public void onGetDialogResult(Object result){
   // Display the input text from the input dialog in LogCat windows
   Log.i("Main", result.toString());
   }

}



Android-input-dialog

Posted by: Dara | post date: 12-10-2014 | Subject: Android Apps Development

write programming tip




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.