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);
Related
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);
I'm new to Android, and was following the tutorial on the following site:
http://www.androidauthority.com/how-to-build-an-image-gallery-app-718976/
Pretty much got everything to work, got lazy and used the image he provided, just used two of the images and copy to 8. However, can't figure out why two title of the image (img2 and img3) is missing.... they should be doing the same thing
My activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.peter.recycleviewtest.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/imagegallery"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.constraint.ConstraintLayout>
cell_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_height="match_parent"
android:id="#+id/img"
android:adjustViewBounds="true"
android:layout_width="match_parent" />
<TextView
android:id="#+id/title"
android:layout_gravity="center"
android:textColor="#000"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
main_activity
package com.example.peter.recycleviewtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private final String image_titles[] = {
"Img1A",
"Img2B",
"Img3C",
"Img4D",
"Img5E",
"Img6F",
"Img7G",
"Img8H",
};
private final Integer image_ids[] = {
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.img4,
R.drawable.img5,
R.drawable.img6,
R.drawable.img7,
R.drawable.img8,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.imagegallery);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(),2);
recyclerView.setLayoutManager(layoutManager);
ArrayList<CreateList> createLists = prepareData();
MyAdapter adapter = new MyAdapter(getApplicationContext(), createLists);
recyclerView.setAdapter(adapter);
}
private ArrayList<CreateList> prepareData(){
ArrayList<CreateList> theimage = new ArrayList<>();
for(int i = 0; i< image_titles.length; i++){
CreateList createList = new CreateList();
createList.setImage_title(image_titles[i]);
Log.d("DEBUG", "Title added: " + image_titles[i]);
createList.setImage_ID(image_ids[i]);
theimage.add(createList);
}
return theimage;
}
}
adaptor
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private ArrayList<CreateList> galleryList;
private Context context;
public MyAdapter(Context context, ArrayList<CreateList> galleryList) {
this.galleryList = galleryList;
this.context = context;
}
#Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cell_layout, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(MyAdapter.ViewHolder viewHolder, int i) {
Log.d("DEBUG", "onBindView added: " + galleryList.get(i).getImage_title());
viewHolder.title.setText(galleryList.get(i).getImage_title());
viewHolder.img.setScaleType(ImageView.ScaleType.CENTER_CROP);
viewHolder.img.setImageResource((galleryList.get(i).getImage_ID()));
}
#Override
public int getItemCount() {
return galleryList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView title;
private ImageView img;
public ViewHolder(View view) {
super(view);
title = (TextView)view.findViewById(R.id.title);
img = (ImageView) view.findViewById(R.id.img);
}
}
}
Pretty much identical to the tutorial sample... but don't know why this missing title is happening. Thank you in advance.
I believe this is a combination of two things: the use of adjustViewBounds in your itemView layout and the internal behavior of RecyclerView.
Your itemView layout, stripped down, is:
<LinearLayout orientation=vertical>
<ImageView height=match_parent/>
<TextView>
</LinearLayout>
With this layout, I would expect to never see the image title. This is because you are giving the first child of the LinearLayout all of the available height. That leaves no room for the TextView.
However, since you also specified adjustViewBounds="true", you've given yourself a chance. If the image inside your ImageView isn't as tall as the parent, then it will shrink and now you'll have room for the TextView.
It looks like your two images have different dimensions, so sometimes this will happen. Sometimes it will not.
The best way to handle this is to re-write your itemView's layout. I suggest either using a LinearLayout and the layout_weight attribute, or using FrameLayout and overlaying the image title on top of the image.
LinearLayout solution
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#000"
android:textStyle="bold"/>
</LinearLayout>
FrameLayout solution
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:textColor="#000"
android:textStyle="bold"/>
</FrameLayout>
I searched a lot to find a direct and clear solution for I think a popular problem but unfortunately I couldn't find it.
We want to have a list of cardviews so each card bind to a specific data and each card has a list inside that shows meta or detail data about its parent.
So we have a nested list inside a cardview.
With a simple search, we know that we should use expanded list view which parents items are cardviews and we must have another layout for its child.
So when you click on cards a list of items appears below of your cards on the root.
But we want to show child list inside of cards?
And there is no access to the something like child list id or any other things to refer to a transition animation and change of layout.
So the clear question is how to add a listview inside a cardview?
i follow my work by using tablelayout and table row. and prefer not to use external libraries for stability and version problems.
this is what i mean.
Image that describe my question
If you don't want to use an external library, you can make your CardView expand like this:
Layout file for an expandable CardView
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
>
<TextView
android:id="#+id/textView_name"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold" />
<!--My dropdown Button -->
<RelativeLayout
android:id="#+id/button"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="end"
android:layout_alignParentRight="true"
android:gravity="center"
>
<View
android:layout_width="12dp"
android:layout_height="12dp"
android:background="#drawable/triangle"
android:layout_alignParentRight="false"
android:layout_alignParentEnd="false" />
</RelativeLayout>
</RelativeLayout>
<!--The layout below is my ExpandableLayout -->
<LinearLayout
android:id="#+id/expandableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<android.support.v7.widget.AppCompatImageView
android:id="#+id/imageView_Owner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView_Owner"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView_OwnerUrl"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
My ExpandableRecyclerAdapter Class
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
import java.util.List;
public class ExpandableRecyclerAdapter extends RecyclerView.Adapter<ExpandableRecyclerAdapter.ViewHolder> {
private List<Repo> repos;
private SparseBooleanArray expandState = new SparseBooleanArray();
private Context context;
public ExpandableRecyclerAdapter(List<Repo> repos) {
this.repos = repos;
//set initial expanded state to false
for (int i = 0; i < repos.size(); i++) {
expandState.append(i, false);
}
}
#Override
public ExpandableRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
this.context = viewGroup.getContext();
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.expandable_card_row, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ExpandableRecyclerAdapter.ViewHolder viewHolder, final int i) {
viewHolder.setIsRecyclable(false);
viewHolder.tvName.setText(repos.get(i).getName());
viewHolder.tvOwnerLogin.setText("Owner: " +repos.get(i).getOwner().getLogin());
viewHolder.tvOwnerUrl.setText(repos.get(i).getOwner().getUrl());
Picasso.with(context)
.load(repos.get(i).getOwner().getImageUrl())
.resize(500, 500)
.centerCrop()
.into(viewHolder.ivOwner);
//check if view is expanded
final boolean isExpanded = expandState.get(i);
viewHolder.expandableLayout.setVisibility(isExpanded?View.VISIBLE:View.GONE);
viewHolder.buttonLayout.setRotation(expandState.get(i) ? 180f : 0f);
viewHolder.buttonLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
onClickButton(viewHolder.expandableLayout, viewHolder.buttonLayout, i);
}
});
}
#Override
public int getItemCount() {
return repos.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvName,tvOwnerLogin, tvOwnerUrl;
private ImageView ivOwner;
public RelativeLayout buttonLayout;
public LinearLayout expandableLayout;
public ViewHolder(View view) {
super(view);
tvName = (TextView)view.findViewById(R.id.textView_name);
tvId = (TextView)view.findViewById(R.id.textView_id);
tvUrl = (TextView)view.findViewById(R.id.textView_url);
tvOwnerLogin = (TextView)view.findViewById(R.id.textView_Owner);
tvOwnerUrl = (TextView)view.findViewById(R.id.textView_OwnerUrl);
ivOwner = (ImageView) view.findViewById(R.id.imageView_Owner);
buttonLayout = (RelativeLayout) view.findViewById(R.id.button);
expandableLayout = (LinearLayout) view.findViewById(R.id.expandableLayout);
}
}
private void onClickButton(final LinearLayout expandableLayout, final RelativeLayout buttonLayout, final int i) {
//Simply set View to Gone if not expanded
//Not necessary but I put simple rotation on button layout
if (expandableLayout.getVisibility() == View.VISIBLE){
createRotateAnimator(buttonLayout, 180f, 0f).start();
expandableLayout.setVisibility(View.GONE);
expandState.put(i, false);
}else{
createRotateAnimator(buttonLayout, 0f, 180f).start();
expandableLayout.setVisibility(View.VISIBLE);
expandState.put(i, true);
}
}
//Code to rotate button
private ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
animator.setDuration(300);
animator.setInterpolator(new LinearInterpolator());
return animator;
}
}
In your Activity/Fragment, set up RecyclerView like this
private RecyclerView recyclerView;
private List<Repo> data;
recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
//fetch data and on ExpandableRecyclerAdapter
recyclerView.setAdapter(new ExpandableRecyclerAdapter(data));
So, its quite simple to create an expandable CardView without external Library. The code is almost exactly the same as using a normal CardView with RecyclerView, the main change is setting the View.GONE/View.VISIBLE on button click.
You can use ExpandLayout,and then add it in cardview.
<com.kyo.expandablelayout.ExpandableLayout
android:id="#+id/expandlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp" >
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:contentDescription="#null"
android:scaleType="centerCrop"
android:src="#drawable/child"
app:canExpand="true" />
</com.kyo.expandable.ExpandableLayout>
I want to write a class which dynamically add the views like button or TextView or ...
I did it but there is some problem
1- I want to use an option that means I want to use scroll or I want to show all of the views in the screen.
1-1 I want to use scroll (Vertical or Horizontal) to show all of the views.
1-2 I want to use android:layout_weight to fit the views in the same size which are beautiful on screen on any size of screen but I don't know how.
m=number of rows
n=number of columns
2- I don't know is it a good method for describing a class for considering the Object Oriented Concepts
In XML file there is only a RelativeLayout.
the Class:
import android.app.ActionBar;
import android.content.Context;
import android.text.Layout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import java.util.ArrayList;
public class MakeTwoDimensionArrayView {
private ScrollView scrollView;
private LinearLayout mainLinearLayout;
private LinearLayout [] subLinearLayout;
MakeTwoDimensionArrayView(Context c, int m, int n, ArrayList<?> V){
scrollView=new ScrollView(c);
mainLinearLayout=new LinearLayout(c);
mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
mainLinearLayout.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
subLinearLayout=new LinearLayout[m];
scrollView.addView(mainLinearLayout);
for(int i=0; i<m; i++){
subLinearLayout[i]=new LinearLayout(c);
subLinearLayout[i].setOrientation(LinearLayout.HORIZONTAL);
subLinearLayout[i].setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
for(int j=0; j<n; j++){
int t=i*n+j;
View v= (View) V.get(t);
v.setTag(t+"");
subLinearLayout[i].addView(v);
}
mainLinearLayout.addView(subLinearLayout[i]);
}
}
public ScrollView getLayout(){
return scrollView;
}
}
The main
public class MainActivity extends AppCompatActivity {
MakeTwoDimensionArrayView makeButton;
ArrayList <Button> buttons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int m=3, n=2;
buttons=new ArrayList<Button>();
for (int i=0; i<m*n; i++){
Button b=new Button(this);
buttons.add(b);
}
makeButton=new MakeTwoDimensionArrayView(this, m, n, buttons);
setContentView(makeButton.getLayout());
}
}
You should implement horizontalScrollView within scrollView. and in that horizontalScrollView add buttons. then it will be both vertically and horizontally scrollable. But for that you should know about ScrollView and HorizontalScrollView. because they have only one direct child layout. This is an XML file.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="vertical" >
<TextView
android:id="#+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft"
android:paddingTop="5dp"
android:textSize="17dip" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/itemContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/item1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item1" />
<Button
android:id="#+id/item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item2" />
<Button
android:id="#+id/item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item3" />
<Button
android:id="#+id/item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableBottom="#drawable/ic_launcher"
android:text="Item3" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
This is only demo for you to understand how scrollView and horizontalScrollView works in an xml if you want to implement dynamically then change in your MakeTwoDimensionArrayView class and in onCreate() of activity class. if still you have question then let me know.
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.