Here i'm giving example how to display list of items in AlertDialog.
ScreenShots:
ScreenShots:
activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button_alertDialogList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="98dp"
android:text="Dialog List" />
</RelativeLayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AlertDialog with List</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="names">Mobile Platforms</string>
<string-array name="mobileplatforms">
<item >Android</item>
<item >iPhone</item>
<item >BlackBerry</item>
</string-array>
</resources>
MainActivity.java:
package com.ram.alertdialogwithlist;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button showDialog = (Button) findViewById(R.id.button_alertDialogList);
showDialog.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
final String[] names = getResources().getStringArray(
R.array.mobileplatforms);
AlertDialog.Builder builder = new AlertDialog.Builder(
MainActivity.this);
builder.setTitle(R.string.names);
builder.setItems(R.array.mobileplatforms,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int index) {
// TODO Auto-generated method stub
switch (index) {
case 0:
Toast.makeText(getApplicationContext(),
names[index] + " Selected",
Toast.LENGTH_LONG).show();
break;
case 1:
Toast.makeText(getApplicationContext(),
names[index] + " Selected",
Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(getApplicationContext(),
names[index] + " Selected",
Toast.LENGTH_LONG).show();
break;
}
}
});
AlertDialog alertList = builder.create();
alertList.show();
}
});
}
}
No comments:
Post a Comment