0

I want to show the images with radio buttons and show this with radio group as user would be able to choose the option by reading the text and viewing the image. How can I make this possible?

Here is the code by which radio buttons are created and also image view dynamically, but these are created in radio group when I click the radio button to choose it shows error as image view can not be cast to radio button and this makes the image view as a child in radio group also and it is not working but showing perfect.

public RadioGroup showNationalCan(RadioGroup rg,Context context,ImageView iv,String voterNA){

    //candidate address  as punjab kpk etc

    if(conn==null){

    }
    try{
        RadioButton rb;
        Statement st=conn.createStatement();
        ResultSet rs=st.executeQuery("select * from AM_NATIONAL where ca_na=N'"+voterNA+"'");
        while (rs.next()) {
            rb=new RadioButton(context);
            rb.setTextColor(Color.BLACK);
            rb.setId(rs.getInt(1));
            rb.setText(rs.getString(2)+"\n"+rs.getString(3)+"\n");

            iv=new ImageView(context);
            byte[] photo=rs.getBytes(4);
            Bitmap bitmap;
            bitmap=BitmapFactory.decodeByteArray(photo, 0, photo.length);
            iv.setImageBitmap(bitmap);
            iv.setEnabled(false);


            rg.addView(iv);
            rg.addView(rb);
        }
        rs.close();
        st.close();

    } catch (SQLException e){
        e.printStackTrace();
    }
    return rg;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Awais
  • 1,157
  • 9
  • 15
  • why my question is down vote? i need reason please. is there any issue regarding my question? i need to know. may this will help me. because this is not fear to do this without any justification. – Awais Sep 02 '14 at 13:47

2 Answers2

3

You can set image by drawableRight property. You will be more details from think link.

http://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableRight

<RadioButton
        android:id="@+id/radio0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:drawableRight="@drawable/ic_launcher"
        android:text="A" >
    </RadioButton>

You can use this to dynamically add radio button.

RadioGroup rgp= (RadioGroup) findViewById(R.id.radiogroup);
        RadioGroup.LayoutParams rprms;

        for(int i=0;i<3;i++){
              RadioButton radioButton = new RadioButton(this);
              radioButton.setText("new"+i);
              radioButton.setId(i);
              radioButton.setChecked(true);
              radioButton.setCompoundDrawables(drwable_left, drwable_top, drwable_right,                 drwable_bottom);
              rprms= new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
              rgp.addView(radioButton, rprms);
        } 

You can set bitmap in setCompoundDrawables

setCompoundDrawables(new BitmapDrawable(your_left_bitmap), new BitmapDrawable(your_top_bitmap), new BitmapDrawable(your_right_bitmap), new BitmapDrawable(your_bottom_bitmap));

Update :

 Bitmap drawable = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

              radioButton.setCompoundDrawables(new BitmapDrawable(drawable), new BitmapDrawable(drawable), new BitmapDrawable(drawable), new BitmapDrawable(drawable));
Dhruba Bose
  • 446
  • 2
  • 7
  • but the radio buttons are created dynamically and image views also by the database which is in sql and i connect them through jdbc. i want to set them programatically – Awais Sep 02 '14 at 14:26
  • thanks for your answer dear @DhrubaBose this is very appreciable because no one answer it. but setCompundDrawables method is used when we have images in android drawable not for the images which we set direct from the database as i do. and my issue was that due to image view when we click radio group to get radio button app crashed. – Awais Sep 03 '14 at 11:44
  • You can use bitmap in setCompoundDrawables method. Check my update. – Dhruba Bose Sep 03 '14 at 12:14
  • Check my update. Set a bitmap in setCompoundDrawables() method. – Dhruba Bose Sep 03 '14 at 14:10
  • dear i am not taking the image from drawable resource it is from database directly thats why it creates the error and i have already post the answer my work is done and there is no issue. i am very thankful to you for your kind cooperation by your opinion i was able to get by problem. thanks for the help dear. – Awais Sep 03 '14 at 15:58
0

Here is my own answer which is working now absolutely perfect. I answered it here because others would be able to do that and able to set images direct from the external database like SQL also.

rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                for(int i=0; i<rg.getChildCount();i++)
                {
                    RadioButton rbtn=(RadioButton)rg.getChildAt(i+1);
                    if(rbtn.getId()==checkedId)
                    {
                        serial=rbtn.getId();
                        flag=true;
                        return;
                    }
                    else
                    {
                        rbtn.setChecked(false);
                    }
                }

            }
        });

The issue was there that radio group have image view in it as child and this would not be clicked as radio buttons. I just increment the i with i+1 as it will get the radio button every time.

halfer
  • 19,824
  • 17
  • 99
  • 186
Awais
  • 1,157
  • 9
  • 15