Custom row Layout for a Multiple Choice Dialog - android

I would like to implement a Custom Multiple Choice Dialog, so following the directions from this answer, this is what I did:
I created the xml for my row, which is a CheckedTextView
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#000000"
android:fontFamily="sans-serif-condensed"
style="?android:textAppearanceSmall"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:clickable="true"/>
Now, my Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setAdapter(
new ArrayAdapter<String>(getActivity(),
R.layout.dialog_list_item, tables), null)
// Set the action buttons
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alerDialog.show();
As you can see, I have not implemented an setOnItemClickListener yet, but I do not know how though. However, the Dialog looks fine. How can I call a ClickListener for each of the CheckedTextView?
Than you very much

The way that #Rahul Gupta suggested works after implementing a ListView and OnItemClickListener, but if the ListView contains too many items, the Views that are not shown are going to be "checked" as well. I suppose the Views are not yet generated until the user actually scrolls down and see the rest of the items.
The way that I made it work was creating a Layout with a single ListView and setting the value of choice mode to CHOICE_MODE_MULTIPLE. In that case, I don't have to deal with each item, but I can retrieve an array of the items selected by using listview.getCheckedItemPositions()
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater li = LayoutInflater.from(getActivity());
View view= li.inflate(R.layout.listview_dialog, null);
builder.setView(view);
builder.setTitle(title);
listView = (ListView) view.findViewById(R.id.listview);
ArrayAdapter<String> ad = new ArrayAdapter<String>(getActivity(), R.layout.dialog_list_item , R.id.text2, tables);
listView.setAdapter(ad);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setDivider(null);
builder.create();

Yes, you can implement thee onClickListener for your CheckedTextView.
You just have to find its reference. Now as it is in your dialog, you will have to do this :-
CheckedTextView dialogCheckedTextView = (CheckedTextView ) alertDialog.findViewById(R.id.yourcheckedtextboxid);
dialogCheckedTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO
}
});

Related

How to create an alert dialog with app icons next to the items(apps)

I have created an alert dialog with a list of apps (in text). So when I click on any of them, that particular app launches. I did this using AlertDialog fragment and using this code:
apps = new String[]{"App1","App2","App3"};
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose the app");
builder.setItems(apps, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
//Code to launch the apps
}
});
return builder.create();
Is it possible to display the app icons along with the app names? If yes, what changes should I make to the code?
How do I get the app icons of installed apps on the phone?
1.Create custom layout for dialog like you want then
Layout custom_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/image"/>
</RelativeLayout>
2.Make adapter with your text , icon(i.e items object )and your custom layout
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, R.layout.custom_dialog.xml);
3.set this adapter to your dialog like this
builder.setAdapter(adapter , new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
Update:
Best option is to use listview in activity with android:theme="#android:style/Theme.Dialog" so that activity will look like dialog(as u want list of apps in dialog only) and also it will be easy to handle the data in listview compared to dialog with adapter.This will fulfill all your requirement in easy and smart way.
call your app like this
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);
i got reference from this
add your icon dynamically like this
see this answer
I'd create a new xml layout, then inflate it using:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflate.inflate(R.layout.your_file, null,false);
And then on AlertDialog
builder.setView(view)
I'd add a list view to layout and then handle it like every other listview. Use view.findViewById to get references.
How get list of apps? http://javatechig.com/android/how-to-get-list-of-installed-apps-in-android

How to set tittle for spinner in android

For spinner how to set tittle ,i tried with 'prompt' in both xml and activity file ,it is showing as tittle for dropdown list after clicking on spinner but i want to give tittle for spinner how to do it,help me.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setPrompt(getResources().getString(R.string.hello_world));
Try this :
spinner.setPrompt("Select Things");
I ended up using a Button instead. While a Button is not a Spinner, the behavior is easy to customize.
First create the Adapter as usual:
String[] items = new String[] {"One", "Two", "Three"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, items);
Note that I am using the simple_spinner_dropdown_item as the layout id. This will help create a better look when creating the alert dialog.
In the onClick handler for my Button I have:
public void onClick(View w) {
new AlertDialog.Builder(this)
.setTitle("the prompt")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO: user specific action
dialog.dismiss();
}
}).create().show();
}
And that's it!
use that
<Spinner
android:id="#+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/gender"
android:prompt="#string/Gender_prompt1"
/>
and string.xml
<string name="Gender_prompt1">Choose a Gender</string>

Multiple choice alert dialog with custom row layout

I need to create an AlertDialog with multiple choice items but I'm having some trouble trying to set a custom layout file to the internal ListView.
For single choice items I use a constructor that takes a ListAdapter as parameter and this way I can set the proper layout resource for each row:
builder.setSingleChoiceItems(new ArrayAdapter<String>(getActivity(),
R.layout.list_item_single_choice_answer, items), checkedItem,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
checkedItem = which;
toggleEditTextAnswer(checkedItem == (items.length - 1));
dialog.dismiss();
}
});
The problem is that there's no constructor for setMultiChoiceItems that accepts a ListAdapter as parameter when creating a multiple choice list.
I need to set a custom layout for each row because I use drawable selectors for setting the row background and text color.
Any ideas?
PS. here is the AlertDialog source code for more information.
https://android.googlesource.com/platform/frameworks/base.git/+/android-4.2.2_r1/core/java/android/app/AlertDialog.java
Well, I know I should create a custom Dialog but right now I don't have the time to do it ... so this is how I hacked this problem:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Set the adapter
builder.setAdapter(
new ArrayAdapter<String>(getActivity(),
R.layout.list_item_multiple_choice_answer, items), null)
// Set the action buttons
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
ListView listView = alertDialog.getListView();
listView.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.list_item_multiple_choice_answer, items));
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
CheckedTextView checkedTextView = (CheckedTextView) view;
checkedItems[position] = !checkedTextView.isChecked();
}
});
listView.setDivider(null);
listView.setDividerHeight(-1);
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
setCheckedItems(((AlertDialog) dialog).getListView());
}
});
alertDialog.show();
First I set the adapter with the items and the instead of calling setMultiChoiceItems I get the ListView object from the Dialog and then configure it myself.
I would recommend that you create your own dialog class like this:
Customizing dialog by extending Dialog or AlertDialog
How to create a Custom Dialog box in android?
This way you will have full control over your dialog and you can customize it any way you want.
Also if you still have issues with your list view after that you can customize your list view items completely: (You can only affect the background and text in a small way through xml and selectors without doing your custom implementation)
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Try it out, it may seem hard but when you do it once it will be piece of cake and will do wonders for you in your future development projects.

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();

How to display an arraylist in AlertDialog

i'm trying to get an arraylist on alert dialog but i can see the list items only if i click them. any idea whats wrong with the below code .any suggestions pls....
ArrayList<String> matches = data.getStringArrayListExtra(
RecognizerIntent.EXTRA_RESULTS);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final ArrayAdapter<String> aa1=new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_single_choice, matches);
builder.setSingleChoiceItems(aa1, 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
numbers.add(aa1.getItem(item));
aa.notifyDataSetChanged();
dialog.dismiss();
}
});
alert = builder.create();
alert.show();
I believe this is caused by a bug when using the default themes and AlertDialog.Builder.
You should be able to work around it by copying the android.R.layout.simple_list_item_single_choice layout xml out of the platform and creating a local layout file with the android:textColor properties overridden to something other than themed text color attributes.
Just use this one
android.R.layout.simple_spinner_dropdown_item
simple dialog here try this. Just you have to pass string or charsequence array to it shows simple dialog..

Categories

Resources