I want to show a radio button and next to it show a ImageView with image loaded by Picaso. I tried putting the ImageView object after loading it next to the radio button in Relative layout but only the radio button is showing up but not the the image. I am new to android and any help is appreciated. can anyone point out the issue?
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 = {"https://homepages.cae.wisc.edu/~ece533/images/airplane.png",
"https://homepages.cae.wisc.edu/~ece533/images/arctichare.png",
"https://homepages.cae.wisc.edu/~ece533/images/baboon.png"};
@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;
//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);
RadioGroup ll = new RadioGroup(this);
RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(View.generateViewId());
ll.addView(rdbtn);
((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" >
</RadioGroup>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myImage" />
</RelativeLayout>
Give height
and weight
to ImageView
. I have tested it and works fine,
<?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">
<!--<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" >
</RadioGroup>
<ImageView
android:id="@+id/myImage"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_toEndOf="@+id/radiogroup"/>
</RelativeLayout>
It seems your ImageView
is overlapping over RadioGroup
! It's my recommendation for activity_main.xml
file: app:srcCompat="@android:drawable/sym_def_app_icon"
will replace with your image .
Try this:
<?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"
>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="190dp"
android:layout_marginBottom="8dp"/>
<RadioGroup
android:layout_below="@id/viewPager"
android:id="@+id/radiogroup"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Demo"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Demo"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Demo"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Demo"/>
</RadioGroup>
<ImageView
android:layout_marginTop="20dp"
android:layout_below="@id/radiogroup"
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/sym_def_app_icon"
android:contentDescription="TODO" />
</RelativeLayout>
Try to change your Layout because radiobutton overlaps imageview
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RadioGroup>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="@+id/myImage" />
</LinearLayout>
It seems your ImageView is overlapping over RadioGroup! It's my recommensation for activity_main.xml file:
<?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" >
</RadioGroup>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myImage"
android:layout_toEndOf="@+id/radiogroup" />
</RelativeLayout>/>
You can edit it on your own.
Hope it works!
P.S: I have edited activity_main.xml file out of IDE and may have some typo, so correct it if you found some :)