This question already has answers here:
ImageView image not showing after loading with Picaso and linking to layout file
(4 answers)
Closed 3 years ago.
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?
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);
Related
I want to display images in horizontal Gridview in single row on top of the screen. It should have multiple images and should scroll accordingly when the images is focused. The focused images should be positioned at center of the screen.
Thanks
Main_activity.xml******
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<android.support.v17.leanback.widget.HorizontalGridView
android:layout_height="80dp"
android:layout_width="wrap_content"
android:id="#+id/horview"
android:background="#color/colorPrimaryDark"
>
<!--
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/_image"/>-->
</android.support.v17.leanback.widget.HorizontalGridView>
</RelativeLayout>
Main activity.java****
package com.example.farhanm.horizontal2;
import android.support.v17.leanback.widget.HorizontalGridView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
private View hview;
private ImageView imageView;
private HorizontalGridView hgview;
private RelativeLayout relativeLayout;
private int[] images = {R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark, R.color.lb_action_text_color,
R.color.lb_basic_card_bg_color, R.color.colorAccent};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// relativeLayout=(RelativeLayout)findViewById(R.id.relative1);
hgview=(HorizontalGridView)findViewById(R.id.horview);
for (int i = 0; i < images.length; i++) {
hview =getLayoutInflater().inflate(R.layout.hview, null);
imageView = (ImageView)hview.findViewById(R.id._image);
imageView.setImageResource(images[i]);
// relativeLayout.addView(imageView);
hgview.addView(imageView);
}
}
}
Instead of using this one, you can try below code
<android.support.v17.leanback.widget.HorizontalGridView
android:layout_height="80dp"
android:layout_width="wrap_content"
android:id="#+id/horview"
android:background="#color/colorPrimaryDark">
Add RecyclerView into a Linearlayout in your XML:
<LinearLayout
android:id="#+id/image_LAY"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp" />
</LinearLayout>
Use LinearLayoutmanager and set its orientation to horizontal
final LinearLayoutManager layoutManager = new LinearLayoutManager(myContext);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
ImageRecyclerView.setLayoutManager(layoutManager);
Create an RecyclerView adapter to add images horizontally. And add Array of images to Recyclerview Adapter.
RecyclerViewAdapter = new STOffsiteRVAdapter(aImageInfos, Context);
ImageRecyclerView.setAdapter(RecyclerViewAdapter);
I have an app in which i want to assign a bitmap named original to an image view whose id is img1 in the layout by taking the position of the image view in the layout(here it is child at position 0). When i click the save button,i want to assign bitmap named original to image view whose id is img1 in the layout.I know we can assign by using ourImageView.setImageBitmap(bitmaptobeassigned). But in my app,my situation is to assign the bitmap original by means of View object child only.Please help me. My main class is
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Test extends Activity
{
Button save;
Bitmap original;
FrameLayout frame;
ImageView background;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
save=(Button)findViewById(R.id.button3);
frame=(FrameLayout)findViewById(R.id.frame);
background=(ImageView)findViewById(R.id.img1);
background.setOnTouchListener(new myTouchListener());
original=BitmapFactory.decodeResource(getResources(), R.drawable.parrot);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View child=frame.getChildAt(0);
//Here i obtained image view as a child.But I do not know how to assign bitmap to it.
//please help me how to write code here.
}
});
}
}
and my xml file is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/frame"
>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
I have found my answer.my main class is
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Test extends Activity
{
Button save;
Bitmap original;
FrameLayout frame;
ImageView background;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
save=(Button)findViewById(R.id.button3);
frame=(FrameLayout)findViewById(R.id.frame);
background=(ImageView)findViewById(R.id.img1);
background.setOnTouchListener(new myTouchListener());
original=BitmapFactory.decodeResource(getResources(), R.drawable.parrot);
//background.setImageBitmap(original);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView child=(ImageView) frame.getChildAt(0);
child.setImageBitmap(original);
}
});
}
}
and my xml is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/frame"
>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
How do I dynamically add image buttons on the click of a button? I need to add the image buttons in 2x3 format and it should be horizontally scrollable when it goes out of the screen.
package com.example.dynamic;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
LinearLayout linearLayout1;
LinearLayout linearLayout2;
int flag=0;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_main);
linearLayout1 = (LinearLayout) findViewById(R.id.linearLayout1);
linearLayout2 = (LinearLayout) findViewById(R.id.linearLayout2);
}
public void onClick(View v){
ImageView image = new ImageView(MainActivity.this);
image.setBackgroundResource(R.drawable.ic_launcher);
if(flag==0){
linearLayout1.addView(image);
flag=1;
}
else{
linearLayout2.addView(image);
flag=0;
}
}
}
and this is the layout I used:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Button" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</ScrollView>
</LinearLayout>
The problem with this is that, when the number of image button exceeds the screen width, it is not scrollable. Another potential problem is that it might be possible to scroll the 2 rows separately, and not together. and that is not desirable. Please help me, I'm a beginner and pardon me for any mistakes while asking this.
Сurrently there is no horizontal scrollview class in android. You can try some custom scroll views here, here or here. When you will choose which one class to use, you should create adapter, or add items directly into this scroll view. You dont need any layouts inside scroll view.
I am trying to put image in TextView. I had done it using image span but my problem is i cannot put onClickListener in every image (in same TextView, there are multipal image in same TextView). Please suggest me how can i do that.
make a custom.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/thumbnail_view"
android:src="#drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/message_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/thumbnail_view"
android:textSize="18sp"
android:text="MyText" />
</RelativeLayout>
then in main.xml , include this custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<include
android:id="#+id/customView"
layout="#layout/custom"/>
</LinearLayout>
This is my mainActivity.class
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private String TAG = MainActivity.class.getSimpleName();
ImageView img;
ImageView img1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView txt = (TextView)findViewById(R.id.message_view);
img = (ImageView) findViewById(R.id.thumbnail_view);
img1 = (ImageView) findViewById(R.id.thumbnail_view1);
img.setOnClickListener(this);
img1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v== img){
// do something for img
}
else if (v== img1){
//do something for img1
}
}
}
make a custom view instead. That would be much easier.
I am developing an application for children. Basic maths for children to learn with fun.
In this app I am using apple image for displaying on the screen. for addition when they give 1st input and second input it should show apple images as specified in 1st and 2nd input.
Is any one know solution for this or any one having source code thn help me
this is the way you can do it ,quite simple though :)
your xml should look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/input1" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/input2" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Result"/>
<HorizontalScrollView
android:id="#+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linear1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="#+id/scroll2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear2"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="#+id/scroll3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear3"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
and you Activity looks like this
package com.example.stackanswer;
import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class AppleActivity extends Activity {
private LinearLayout linear1;
private LinearLayout linear2;
private LinearLayout linear3;
private HorizontalScrollView scrollbar1;
private HorizontalScrollView scrollbar2;
private HorizontalScrollView scrollbar3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apple);
final EditText input1 = (EditText)findViewById(R.id.input1);
final EditText input2 = (EditText)findViewById(R.id.input2);
scrollbar1 = (HorizontalScrollView)findViewById(R.id.scroll1);
scrollbar2 = (HorizontalScrollView)findViewById(R.id.scroll2);
scrollbar3 = (HorizontalScrollView)findViewById(R.id.scroll3);
linear1 = (LinearLayout)findViewById(R.id.linear1);
linear2 = (LinearLayout)findViewById(R.id.linear2);
linear3 = (LinearLayout)findViewById(R.id.linear3);
Button output = (Button)findViewById(R.id.button);
output.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String input1A = input1.getText().toString();
String input1B = input2.getText().toString();
int value1 = Integer.parseInt(input1A);
int value2 = Integer.parseInt(input1B);
Log.i("1",""+value1);
Log.i("2",""+value2);
linear1.removeAllViews();
linear2.removeAllViews();
linear3.removeAllViews();
linear1.setScrollContainer(true);
ImageView image ;
for(int i=0;i<value1;i++){
image = new ImageView(getApplicationContext());
image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) );
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear1.addView(image);
scrollbar1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
for(int i=0;i<value2;i++){
image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear2.addView(image);
scrollbar2.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
int sum = value1+value2;
for(int i=0;i<sum;i++){
image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear3.addView(image);
scrollbar3.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}
});
}
}
Complete source code here