views:

311

answers:

3

I want to display images inside single select when it appears inside dialog.

alt text

How can I do it?

+2  A: 

You can override everything and create it from scratch. First create an xml describing the content you want. For example:

<!-- test.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
 android:background="@color/white">
 <ImageView android:id="@+id/ImageView01"
  android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
  android:src="@drawable/facebook"></ImageView>
 <TextView android:id="@+id/TextView01" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_toRightOf="@+id/ImageView01"
  android:text="Facebook" 
            android:textSize="40sp"></TextView>
 <RadioButton android:id="@+id/RadioButton01"
  android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
  android:layout_toRightOf="@+id/TextView01"></RadioButton>
</RelativeLayout>

Then build this xml using the alert dialog. You can let the activity handle your dialog and just override the onCreateDialog like this:

@Override
protected Dialog onCreateDialog(int id) {
    View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.test, null);
    return new AlertDialog.Builder(MainActivity.this).setView(view).setTitle("Share")
                    .create();
}

You can then override onClick to store the state in your StaticPreferences files. This way you'll have complete control over all the elements in your view.

One final piece of advice, when creating the xml make sure that you create as flat a layout as possible so that the code is optimized. You can also play with the list layout to create something like this.

shanka
Wrong answer, in my question layout mention, in your xml layout you are making radiobutton sub child of relative layout, making radio button child of other than radiogroup do not work.In this case your answer fails.
Faisal khan
@Faisal khan: He was just giving an example.
Macarse
A: 

I did not worked on it. But I have an Idea to you have combine the Three Examples on API Demos.

  1. AlertDialogs
  2. SingleChoice Adapter
  3. Efficient Adapter

the steps you have to do are:

  • Create AlertDialog
  • Create List Adapter Class for the view you want to achieve.
  • Use the SetAdapter Method to set the ListAdapter.

Sample Code:

new AlertDialog.Builder(MainActivity.this).setTitle("Share")setAdapter(new Listadapterclass(),new DialogInterface.OnClickListener(){

                            @Override
                            public void onClick(DialogInterface arg0,
                                    int arg1) {
                                // TODO Auto-generated method stub
        // the click event on the list item selected                        
                            }})
                .create();

Hope It works.

Praveen Chandrasekaran
not working how we have radio button working in one group with inflat
Faisal khan
+1  A: 

Here you have working solution. Not mine, but I used described technique and it worked. Do not forget about img.setBounds(), it is necessary!

http://mgmblog.com/2010/06/10/arrayadapter-and-alertdialog-for-single-choice-items/

PS: original post does not display radiobutton, but my layout does. My single row XML layout file (animal_row.xml is missing in example) is here:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/miastorow_text"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#000"
/>
tomash
awesome, thanks.
Faisal khan