Android: Alert Dialog with CheckedTextView - android

Ok guys..I have a problem...
I need to create a dialog with 3 CheckedTextView. And when i open a dialog, second item should be checked...How to do that ? Code ...
#Override
protected Dialog onCreateDialog (int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Something..");
builder.setInverseBackgroundForced(true);
ListView listView = new ListView(this);
listView .setAdapter(new ArrayAdapter(Settings.this, android.R.layout.select_dialog_multichoice, new String {a ,b, c}));
builder.setView(listView );

http://developer.android.com/guide/topics/ui/dialogs.html
check adding check boxes and radio buttons ,
One method would be ,
Have a flag through which you can decide
which to check next time when you popup the dialog and set the radio button in code ,
get radio button view and use toggle()
Check radio buttons and checkbox,
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
Another method would be to use CheckBoxPreference.
Hope it helps.

Try this listView.getAdapter().getItem(1);. if your 2nd list item is check box, then set it checked by setChecked(true);

Related

Alert dialog with multiple choice to be checked dynamically android

My code fetches data from server and made it to display in the alert dialog box with listview, multiple choice option but now, i want every data to be pre checked before loading into listview and mark checked if the condition satisfies is this possible in alert dialog.
//this is my code for loading the value into alertdialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select Category List");
final ListView modeList = new ListView(getActivity());
// Creating a button - Load More
Button btnDone = new Button(getActivity());
btnDone.setText("Done");
btnDone.setTextColor(getActivity().getResources().getColor( android.R.color.white));
btnDone.setBackgroundResource(R.drawable.logout_btn);
final ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_multiple_choice,
android.R.id.text1, EavesList);
modeList.addFooterView(btnDone);
modeList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
modeList.setAdapter(modeAdapter);
builder.setView(modeList);
My question: Is it possible to check the condition and mark checked before loading multiple choice in alert dialog. if possible, then please respective tutorial or any code will be helpful.
you can use
setItemChecked() method of ListView for the purpose.
you can use this, after
modeList.setAdapter(modeAdapter);
and put your condition here, and then setItemChecked(position, value); to set the item as checked! if it satisfies your condition.
Assuming your list is of type CharacterSequence[] or can be converted to one, you can use
AlertDialog.setMultiChoiceItems(CharacterSequence[] list, boolean[] checkedItems, OnMultiChoiceClickListener listener)
Pass the EavesList as a CharacterSequence[] as first parameter, a boolean[] specifying which list items are to be checked while creating the Dialog as the second and the listener for when the user toggles the items as the third parameter.

Android Listview dialog doesn't disapear after choosing an item

I've tried to create a list view dialog to display a list of choose. My code is shown below:
LayoutInflater factory=LayoutInflater.from(this);
final View stuckLevelDialogView=factory.inflate(R.layout.report_stuck_dialog, null);
final ListView stuckLevelListViewForDialog=(ListView)stuckLevelDialogView.findViewById(R.id.report_stuck_dialog_listview);
final String[] stuckLevelList=new String[]{"1 - You can move freely","2 - You have to be aware of your movement","3 - You can move slowly","4 - There is a traffic jam","5 - There is a serious traffic jam"};
ArrayAdapter<String> adapterForDialog=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stuckLevelList);
stuckLevelListViewForDialog.setAdapter(adapterForDialog);
final AlertDialog.Builder stuckLevelDialog=new AlertDialog.Builder(this);
stuckLevelDialog.setTitle("What stuck level is this point?");
stuckLevelDialog.setView(stuckLevelDialogView);
stuckLevelDialog.show();
However, when I choose an option, the onItemClick is executed, but the listview dialog doesn't disappear, I have to press back button manually. I've tried to debug the code for a whole day, but it has not been solved yet. Please help me. Thank in advanced!
I think you need to dismiss() the dialog in your onItemClick listener as below:
stuckLevelListViewForDialog.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> p_arg0, View p_arg1,
int p_arg2, long p_arg3) {
stuckLevelDialog.dismiss();
}
});
Use stuckLevelDialog.dismiss; at the end of onItemClick.
You can set setSingleChoiceItems in your alert dialog box with your items list, which will show a list with a radio buttons. If you want to add buttons you can else once user select any items you can dismiss the dialog.
new AlertDialog.Builder(this)
.setSingleChoiceItems(array, -1, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// here you can do your functionality and can dismiss dialog as well
dialog.dismiss();
}
})
.show();

Android: How to set the selection in a single choice AlertDialog?

I created a single choice AlertDialog with "Apply" and "Cancel" buttons. When the user presses "Cancel", I would like to set the selected item back to what it was when the dialog was first shown (I have the value stored already).
I know how to set the selected item when the dialog is first created in setSingleChoiceItems() but is there a way to set it again later?
I would like to be compatible with API level 7, so I'd rather not use onPrepareDialog().
Well, nobody could find a solution to my problem, so I settled for a dialog which doesn't use buttons. When the user presses an item in the list, that selection is applied and the dialog is dismissed. No need to remember previous choices or support cancelling.
You can create your dialog on the spot rather than going through showDialog and onCreateDialog. This way its always using the current selection we give to setSingleChoiceItems.
//where we display the dialog
showDialog(MYDIALOG);
AlertDialog.Builder builder = new Builder(this);
String[] choices = {"A","B","C"};
builder.setSingleChoiceItems(choices,current_choice,null);
builder.setPositiveButton("Select",null);
builder.setNegativeButton("Cancel",null);
builder.show();
...
protected Dialog onCreatDialog(int id) {
if (id==MYDIALOG) {
AlertDialog.Builder builder = new Builder(this);
String[] choices = {"A","B","C"};
builder.setSingleChoiceItems(choices,current_choice,null);
builder.setPositiveButton("Select",null);
builder.setNegativeButton("Cancel",null);
return builder.show();
}
return null;
}
In Altert Dialog For creating single choice have you used single choice list view or radio group?
If You have used Single Choice Listviwe then no need to worry.
IF you have used Radio Group then you have to keep track of selected Radio Button Using Global Varialbe in your activity.
What you want to do when user click on cancel.
when user click on cancel than do you want to remove selected item from list if yes then you have to do the following.
In Your listitemseleted listener
alb.setSingleChoiceItems(items, 0, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
/* which argument is the position of the item in
array items you have to remove this item from items array
Make your items array global variable.since in java array are
immutable you have to use arraylist.Arraylist can be converted
to array.*/
}
});
hope this will help if any question feel free to ask.

Android opening context menu after button click

I want to open context menu when I click a button, but also I have to know which list item is focused when I click the button. Do you know how to do that? What code should be in onclick method?
I was looking for the same, and found that instead of context menu, you should use Dialogs
final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
If you really want to do it for whatever reason... (in my case, out of laziness)
During onCreate of your activity or somewhere before your user can touch the button, do registerForContextMenuon that button. Then in the actual button onClick handler, call openContextMenu(View).
For example, I have a button declared in xml like
<Button
android:id="#+id/btn_help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onHelp"
android:text="#string/help_btn_text" />
in my onCreate
registerForContextMenu(findViewById(R.id.btn_help));
and in onHelp function
public void onHelp(View v) {
openContextMenu(v);
}
this works because the View v is the same as the view registered for context menu.
First thing, you should register the view by calling registerForContextMenu(View view). Second, override the onCreateContextMenu() to add the menus and lastly, override the onContextItemSelected() to put logic on each menu.
First of all, you should know why you should use ContextMenu. The functionality of ContextMenu of a View is similar to the right-click menu on a PC, which means the "available operations" on some item.
According to your description, I think what you actually need is a customized Dialog with a list, which is displayed when clicking the Button and is also able to get the focused item of your ListView. Then you can save the registration of ContextMenu for some View that really needs the menu:)

What is the process of creating an AlertDialog with ListView in Android?

I want to know the process or order of creating the AlertDialog. The order of I asking this question is that I suppose to filter and disable some list item in the AlertDialog. It this must be dynamically. So I chose overwrite the onPrepareDialog(int id, Dialog dialog) method.
First I create a AlertDialog in the onCreateDialog(int id) method
protected Dialog onCreateDialog(int id) {
--------
builder.setMultiChoiceItems(itemsId, checkedItems, mListenter);
---------
}
protected void onPrepareDialog(int id, Dialog dialog) {
-----------
ListView mListView = ((AlertDialog)dialog).getListView();
mListView.setItemChecked(0, false);
mListView.invalidateViews();
View view = mListView.getChildAt(0);
-----------
}
But these code not work. The first item was still be checked after I check it before.
And the ChildView is null when the first time display the dialog, why?
How to filer some item in ListView and how to disable but show some of items.
Check and see if my answer over at: How to update array of items in an AlertDialog list built with AlertDialog.builder after creation fits the bill. It works for me, at least.

Categories

Resources