0

I have an ImageView Object with image loaded from URL with Picaso. and i would like to show this image next to a radio button as part of a radio button group. I like do this programmatically as the Imageview object is in code. In the code I just add the ImageView to the RadioGroup with addView. Is this the right way to go?

MainActivity.java

package com.example.test.imageslider;
import androidx.core.content.ContextCompat;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.view.View;
import android.view.ViewGroup;
import com.squareup.picasso.Picasso;


public class MainActivity extends AppCompatActivity {

    ViewPager viewPager;

    private String[] imageUrls = {"http://localhost:8080/test.jpg"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //viewPager = (ViewPager) findViewById(R.id.viewPager);

        //ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);

        //viewPager.setAdapter(viewPagerAdapter);

        ImageView imageView = new ImageView(this);
        Picasso.get()
                .load(imageUrls[0])
                .fit()
                .centerCrop()
                .into(imageView);
        RadioGroup ll = new RadioGroup(this);
        RadioButton rdbtn = new RadioButton(this);
        rdbtn.setId(View.generateViewId());
        ll.addView(rdbtn);
        ll.addView(imageView);
        ((ViewGroup) findViewById(R.id.radiogroup)).addView(ll);
    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test.imageslider.MainActivity">



    <!--<androidx.viewpager.widget.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:layout_marginBottom="8dp"/> -->


    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>

    </RadioGroup>

</RelativeLayout>

I did not to try to run this yet as I do not how to link the ImageView object in the code with the one with the layout file. Can someone show how to do this?

  • It isn't accidentally called `RadioGroup`. It keeps only `RadioButton`s. You can create your own class which will contain `RadioButton`'s and `ImageView`'s, but I think the easiest way will be to put ImageView next to your RadioButton's. LinearLayout as a child of `RadioGroup` won't do either – SkypeDogg Sep 11 '19 at 07:39
  • I understand but in one other stack overflow https://stackoverflow.com/questions/11081919/radio-button-with-image-as-an-option-instead-of-text it does the same thing without program in layout file. I want to know how to do this programmatically. – Vamsi Krishna Reddy Sep 11 '19 at 07:45
  • Please look at the first comment below that answer. – SkypeDogg Sep 11 '19 at 07:48
  • Thank you for this. you are right! I am new to android :) – Vamsi Krishna Reddy Sep 11 '19 at 08:02
  • No problem :) You're going to get more and more knowledge, and that's the best thing about it. – SkypeDogg Sep 11 '19 at 08:12
  • Please don't repeat questions. Simply editing this post with any new information you had, any new code you'd tried, or an explanation of why any posted answers didn't work, would've bumped it to the top of the active queue. I've closed this as a duplicate of the newer one, since you apparently got your answer there, but, in the future, please just edit the original. – Mike M. Sep 11 '19 at 09:05

1 Answers1

0

If you are reffering to your xml

You must add an ID to your Image in xml

  <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/myImage"
       />

In your activity

 ImageView imageView;

 //this will set the image to your ImageView in your xml
 imageView = (ImageView)findViewById(R.id.myImage);
 Picasso.get()
            .load(imageUrls[0])
            .fit()
            .centerCrop()
            .into(imageView);
Cyrille Con Morales
  • 918
  • 1
  • 6
  • 21