Android dialog listview for single selection

 

We are not going to use any thing from xml file,

Please copy and paste below code:

package com.pr.testtwo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class TestTwoActivity extends Activity {
/** Called when the activity is first created. */
private Gallery gallery;
private int selectedItem;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final CharSequence[] digitList = { “One”, “Two”, “Three” };
AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setIcon(R.drawable.icon);
alt_bld.setTitle(“Select one Digit”);
selectedItem = -1;
alt_bld.setSingleChoiceItems(digitList , -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
selectedItem = item;
}
});
alt_bld.setPositiveButton(“OK”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(),
“Selected digit: ” + digitList [selectedItem],
Toast.LENGTH_SHORT).show();

}
});
alt_bld.setNegativeButton(“Cancel”,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});

AlertDialog alert = alt_bld.create();
alert.show();
}
}

Advertisements

Leave a comment

Filed under Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s