We can create a dialog with Edittext and other views like Button, CheckBoxes, RadioButtons etc.For this we need to Create A xml layout and and inflate it in AlertDialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editTextKeywordsToBlock"
android:hint="Enter 1 or more keywords. Use space berween two keywords"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:id="@+id/buttonBlockByKeyword"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="SAVE"
/>
<Button
android:id="@+id/buttonCancelBlockKeyword"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cancel"
/>
</LinearLayout>
</LinearLayout>
And inflate this Layout at run time like following
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.block_by_keyword);
dialog.setTitle("Keyword To Block");
final EditText editTextKeywordToBlock=(EditText)dialog.findViewById(R.id.editTextKeywordsToBlock);
Button btnBlock=(Button)dialog.findViewById(R.id.buttonBlockByKeyword);
Button btnCancel=(Button)dialog.findViewById(R.id.buttonCancelBlockKeyword);
dialog.show();
Android Tutorial
Option Menu:
Option Menu is a drop down menu containing options, and appears when a users clicks on Menu button.For Ex:
We can create an Option Menu with following :
- Create a menu xml
- Register the menu in Activity
- Write code to Handle the Clicks on menu items
1: Create xml for menu
Create a new folder named "menu" in res folder (if menu folder is not there in res folder) inside this Menu folder create .xml file here I have created option.xml xml for the option menu in above Image <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/ChangeColor"
android:icon="@drawable/setting"
android:title="Settings"
/>
<item android:id="@+id/phoneInformation"
android:icon="@drawable/phone"
android:title="My Phone Information" />
<item android:id="@+id/callInfo"
android:icon="@drawable/callinfo"
android:title="In and Out Call Info" />
<item android:id="@+id/email"
android:icon="@drawable/mail"
android:title="Mail to Developer" />
</menu>
android:id
- A resource ID that's unique to the item, which allows the application can recognize the item when the user selects it.
android:icon
- An image to use as the item's icon.
android:title
- A tittle to show.
2: Register In Activity
Override onCreateOptionMenu method and inflate the .xml (here options.xml) inside method @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
3: Handle Click Events
When the user selects an item from the options menu (including action items in the action bar), the system calls your activity's onOptionsItemSelected()
method. This method passes the MenuItem
selected. You can identify the item by calling getItemId()
, which returns the unique ID for the menu item (defined by the android:id
attribute in the menu resource To Handle click events override onOptionsItemSelected method @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.ChangeColor:
// write code to execute when clicked on this option
return true;
case R.id.phoneInformation:
// write code to execute when clicked on this option
return true;
case R.id.callInfo:
// write code to execute when clicked on this option
return true;
case R.id.email:
// write code to execute when clicked on this option
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Option Menu Full Source Code
public classMainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Create Option Menu @Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
// Handle click events @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.ChangeColor:
// write code to execute when clicked on this option
return true;
case R.id.phoneInformation:
// write code to execute when clicked on this option
return true;
case R.id.callInfo:
// write code to execute when clicked on this option
return true;
case R.id.email:
// write code to execute when clicked on this option
return true;
default:
return super.onOptionsItemSelected(item);
}
}}
In Android an Activity can be started for some results, means an Activity can return some result to the Parent/Previous Activity.An Activity can return String, Int, float, boolean etc as Result.In this post I have two Activities MainActivity and SecondActivity.Second Activty returns a String to the First MainActivity as Result.From MainActivity we start the Second Activity to get Result with follwing method
startActivityForResult(Intent intent, int requestCode);
to get the Result back we need to override the method..
onActivityResult(int requestCode, int resultCode, Intent data)
the data object (last parameter of above method) contains the returned Result, we need to fetch the result from data(object of Intent).
See the Execution Flow
First Screen: Click on get Message (MainActivity)
Second Screen: Enter the Message " I am much Busy" and click on Submit Message (Second Activity)
3rd Screen : See the returned message MainActivity , you can see the result is being returned to Previous Activity i.e. MainActivity (Which have started the activity for result)
Code:
activtymain.xml
<LinearLayout 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:gravity="center_vertical" android:orientation="vertical" > <TextView android:id="@+id/textViewMessage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textColor="#FF0000" android:textSize="20dp" android:text="Message" /> <Button android:id="@+id/button1" android:layout_marginTop="20dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Get Message" android:onClick="getMessage" /></LinearLayout>
layout2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical" > <EditText android:id="@+id/editTextMessage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:textColor="#FF0000" android:textSize="20sp" android:hint="Enter The Message" /> <Button android:layout_marginTop="20dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="Submit Message" android:onClick="submitMessage" /></LinearLayout>
MainActivity.java
public class MainActivity extends Activity { TextView textViewMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // get The refference of The textView textViewMessage=(TextView)findViewById(R.id.textViewMessage); } // Method to handle the Click Event on GetMessage Button public void getMessage(View V) { // Create The Intent and Start The Activity to get The message Intent intentGetMessage=new Intent(this,SecondActivity.class); startActivityForResult(intentGetMessage, 2);// Activity is started with requestCode 2 } // Call Back method to get the Message form other Activity override the method @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); // check if the request code is same as what is passed here it is 2 if(requestCode==2) { // fetch the message String String message=data.getStringExtra("MESSAGE"); // Set the message string in textView textViewMessage.setText(message); } }}
SecondActivity.java
public class SecondActivity extends Activity { EditText editTextMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout2); // Get the Refference of Edit Text editTextMessage=(EditText)findViewById(R.id.editTextMessage); } public void submitMessage(View V) { // get the Entered message String message=editTextMessage.getText().toString(); Intent intentMessage=new Intent(); // put the message to return as result in Intent intentMessage.putExtra("MESSAGE",message); // Set The Result in Intent setResult(2,intentMessage); // finish The activity finish(); } }