Make a Recyclerview of 9 items with GridLayoutManager fit the screen - java

I have a Grid that was generated by a RecyclerView and GridLayoutManager , so in my case I will always have only 9 items on the Grid, so I want them to fill the entire screen. Below is how they look on my screen:
This is the activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
then, the single_item.xml is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:padding="1dp"
>
<ImageView
android:id="#+id/image_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
/>
</RelativeLayout>
How I attached the Gridlayoutmanager :
rv = findViewById(R.id.rv);
ImageAdapter imageAdapter = new ImageAdapter(allBitmaps, MainActivity.this);
mLayoutManager = new GridLayoutManager(MainActivity.this, 3);
rv.setLayoutManager(mLayoutManager);
rv.setAdapter(imageAdapter);
So I want the grids to fit well the entire screen, how best can this be done ?

Here is the scenario:
Measure the width & height of the RecycerView before attaching the adapter to it:
Get the desired RecyclerView item width & height by dividing the width & height of the previous step by 3; as you have 3 items horizontally and vertically.
Pass the item width & height to the RecyclerView adapter as parameters and store them as fields.
In activity onCreate():
recyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if (recyclerView.getViewTreeObserver().isAlive())
recyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
float width = (float) recyclerView.getWidth() / 3;
float height = (float) recyclerView.getHeight() / 3;
ImageAdapter adapter = new ImageAdapter(context, width, height, allBitmaps);
recyclerView.setAdapter(adapter);
return true;
}
});
Set the ImageView width & height in the ViewHolder:
class ViewHolder extends RecyclerView.ViewHolder {
ImageView image;
public ViewHolder(#NonNull View itemView) {
super(itemView);
image = itemView.findViewById(R.id.image);
image.getLayoutParams().width = (int) itemWidth;
image.getLayoutParams().height = (int) itemHeight;
}
}
I tested this with just an ImageView as a list item layout:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
Portrait preview:
Landscape preview:

I think if you want those images to fill up the screen, you need to modify the width and height of the image inside your single item.xml

Related

Center crop dont work in gridlayoutmaneger Android

I have grid view which is using GridLayoutManager and the RecyclerView
grid has 3 rows which are working fine as you see,[![enter image description here][1]][1]
but for some reason, image views scale type won't work on any of the items and those items you see work fine because the resolution already is square mean 512*512 but if you see the last item that has low resolution it have white space in left and right.
that's my code from a setup list
int getori = getResources().getConfiguration().orientation;
if (getori == Configuration.ORIENTATION_PORTRAIT) {
gl = new GridLayoutManager(getBaseContext(), 3);
} else if (getori == Configuration.ORIENTATION_LANDSCAPE){
gl = new GridLayoutManager(getBaseContext(), 6);
}
gg.setHasFixedSize(true);
gg.setFitsSystemWindows(true);
gg.setLayoutManager(gl);
gg.setAdapter(startpostsystem);
that's my grid adapter code
#Override
public void onBindViewHolder(Grid_view holder, final int position) {
holder.im.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String getfiledop = fall[position].getAbsolutePath();
Intent is = new Intent(cm,Open_post_item.class);
is.putExtra("req",12);
cm.startActivity(is);
and this is my grid view items layout code.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="140dp">
<ImageView
android:id="#+id/imageView18"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
app:srcCompat="#drawable/sunbow100" />
I tried to use :
android:scaleType="centerCrop"
android:adjustViewBounds="true"
or fitxy it make it worse
view bounds won't work either.
if i add more images like 800*400 or ... it will have white space
so where is problem where I do wrong.
try using android:src in place of app:srcCompat
<ImageView
android:id="#+id/imageView18"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="#drawable/sunbow100" />

Dynamically adding views in grid layout doesn't take the size specified

I am creating an app in which I want to dynamically add items to grid layout and want them to look like this:
What I did to achieve this is given as follows:
activity_main.xml
<ScrollView 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"
android:fillViewport="true"
tools:context=".MainActivity">
<LinearLayout
android:weightSum="1"
android:id="#+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<GridLayout
android:id="#+id/myGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#FFFFFF"
android:layout_weight="1">
</GridLayout>
</LinearLayout>
</ScrollView>
item_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/item"
android:background="#color/itemBackground"
android:layout_weight="4"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/cImage"
android:layout_marginTop="20dp"
android:layout_marginRight="40dp"
android:layout_marginLeft="40dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:src="#drawable/icon"/>
<TextView
android:id="#+id/cText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:textSize="20sp"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:layout_marginBottom="10dp"
android:text="ORAL CARE"/>
</LinearLayout>
MainActivity.java: (Code adding items in grid layout)
gridLayout = (GridLayout) findViewById(R.id.myGrid);
myArray = new String[]{"pink","blue","red","orange","green","purple","white","black","brown","other"};
int total = myArray.length;
int column = 2;
int row = total / column;
gridLayout.setAlignmentMode(GridLayout.ALIGN_BOUNDS);
gridLayout.setColumnCount(column);
gridLayout.setRowCount(row + 1);
LinearLayout linearLayoutItem;
Display display = getWindowManager().getDefaultDisplay();
int displayWidth = display.getWidth();
int displayHeight = display.getHeight();
for(int i =0, c = 0, r = 0; i < total; i++, c++)
{
if(c == column)
{
c = 0;
r++;
}
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.item_layout, gridLayout, false);
TextView cText = (TextView) v.findViewById(R.id.cText);
ImageView cImage = (ImageView) v.findViewById(R.id.cImage);
cText.setText(myArray[i]);
cImage.setImageResource(R.drawable.icon);
v.setMinimumWidth(displayWidth/2); // setting width and height
v.setMinimumHeight(displayWidth/2);
gridLayout.addView(v);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.height = GridLayout.LayoutParams.WRAP_CONTENT;
param.width = GridLayout.LayoutParams.WRAP_CONTENT;
param.rightMargin = 20;
param.topMargin = 20;
param.setGravity(Gravity.CENTER);
param.columnSpec = GridLayout.spec(c);
param.rowSpec = GridLayout.spec(r);
This code is adding the views in grid layout perfectly. But the problem is that it is not adjusting the size of view according to the screen, as shown:
I have tried to make the view's width equal to half the space of screen but it doesn't seem to work. The image which I am using in imageView is large but I have tried experimenting with the size of the imageView as well but it doesn't work.
What I want is to:
Adjust the views (the gray colored views with image and text) according to the size of the screen on which it is run. As width of grid layout is match_parent.
Height of the view to be in ratio with width of the view.
Size of the image in the view to be in ratio with the size of the view.
Thanks in advance. Any help would be appreciated.

RecyclerView Item does not wrap height of ListView

So I have this activity, which looks like this:
<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.jonas.trainingslog1.WorkoutDataActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.constraint.ConstraintLayout
...
</android.support.constraint.ConstraintLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/layout">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
As you can see, the activity has an RecyclerView which items look like this:
<android.support.constraint.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<ListView
android:id="#+id/lst"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="30dp"
app:layout_constraintTop_toBottomOf="#+id/lst"/>
The Height of the ListView in the RecyclerView Item should only be as tall as its content, so I set it to wrap_content. The problem is, this only works as long as I have set the height of the RecyclerView Item Layout to match_parent, as soon as I change it to wrap_content the ListView shows only one item. How ever I cant leave the height of the RecyclerView Item as match_parent because then every item of the RecyclerView has a lot of unused space.
Anyone hows how to fix this problem? I dont understand this behavior of the xml files. I would really appreciate help.
Add this to you listview...set the height of the listview dynamically
public static void setListViewHeightBasedOnChildren(final ListView listView) {
listView.post(new Runnable() {
#Override
public void run() {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = listView.getPaddingTop() + listView.getPaddingBottom();
int listWidth = listView.getMeasuredWidth();
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(
View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
totalHeight += listItem.getMeasuredHeight();
Log.d("listItemHeight " + listItem.getMeasuredHeight(), "********");
}
Log.d("totalHeight " + totalHeight, "********");
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = (totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)));
listView.setLayoutParams(params);
listView.requestLayout();
}
});
}
add this to your recyclerview adapter
One option is to use a vertical linear layout where you put all your content of the page. Then set the height of the views (including the recyclerview) to 0dp and add a layout weight.

ImageView inside ListView is ignoring all layout dimensions and filling the whole screen

I'm trying to create a set of 4 dynamically updating listviews of icons.
To do this, I've put my 4 listviews inside a gridview, and I'm using a custom layout for each listview item with a single imageview inside it. I'm handling changing the icon inside a custom adapter. I'm using the Picasso library to handle the icons, and all my icons are stored locally in drawable as .png files.
I'm not getting any errors, but when I run the app, all I get is a single icon that takes up the whole screen. I have a small imageview and a textview above the gridview that contains the lists of icons, but the one icon pushes even that out of the way.
This is my layout preview, and a screenshot of the app running in an emulator. (the result is the same if I run on my phone).
preview,
running
(apologies for not embedding, I'm new to SO, and don't have 10 rep yet haha)
I've tried resizing the image at runtime inside my custom adapter. I've tried setting the maximum width of the listview - both at runtime (since I want to adapt to different screen dimensions), and in the xml files. I've tried resizing the source .png files outside of android studio. Nothing's worked.
My activity xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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.sta.tomov0.MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tomoFace"
android:src="#drawable/office38"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_marginBottom="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_below="#+id/tomoFace"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="false"
android:layout_marginBottom="50dp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:rowCount="1"
android:id="#+id/gridLayout"
android:layout_below="#+id/textView"
android:columnCount="5">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewA"
android:layout_row="0"
android:layout_column="0" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewB"
android:layout_row="0"
android:layout_column="1" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewC"
android:layout_row="0"
android:layout_column="2" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listViewD"
android:layout_column="3"
android:layout_row="0" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="4"
android:layout_row="0"
android:onClick="addTask"
android:src="#android:drawable/stat_notify_more" />
</GridLayout>
</RelativeLayout>
My listview layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listImageView"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
and my activity.java file:
public class MainActivity extends AppCompatActivity {
ImageView tomoface;
ListView listViewA;
ArrayList arrayListA = new ArrayList<Integer>();
Boolean aExists = false;
ListView listViewB;
ArrayList arrayListB = new ArrayList<Integer>();
Boolean bExists = false;
ListView listViewC;
ArrayList arrayListC = new ArrayList<Integer>();
Boolean cExists = false;
ListView listViewD;
ArrayList arrayListD = new ArrayList<Integer>();
Boolean dExists = false;
Button addButton;
TextView tomoText;
File tomoFiles;
File taskFiles;
String currentCommunity = "Dep";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Paper.init(getApplicationContext());
tomoface = (ImageView) findViewById(R.id.tomoFace);
tomoface.setImageResource(R.drawable.favourite15);
tomoText = (TextView) findViewById(R.id.textView);
listViewA = (ListView) findViewById(R.id.listViewA);
listViewB = (ListView) findViewById(R.id.listViewB);
listViewC = (ListView) findViewById(R.id.listViewC);
listViewD = (ListView) findViewById(R.id.listViewD);
tomoFiles = getDir("TomoFiles", MODE_PRIVATE);
taskFiles = new File(tomoFiles, "taskFiles");
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width =(size.x)/5;
ViewGroup.LayoutParams params = listViewA.getLayoutParams();
params.height = width;
params.width = width;
listViewA.setLayoutParams(params);
listViewB.setLayoutParams(params);
listViewC.setLayoutParams(params);
listViewD.setLayoutParams(params);
/*
If there is no data saved, the following code creates a list of (empty) lists to hold TaskClass objects
The 4 sub lists each represent one category.
If there is data saved, the following code retrieves that data, and creates 4 new ArrayLists,
each containing the iconIds of the TaskClass objects in the corresponding position of the corresponding TaskClass arraylist.
These ArrayLists of ids are then used to populate the 4 listviews.
*/
ArrayList<ArrayList<TaskClass>> listList = Paper.book(currentCommunity).read("listList", new ArrayList<ArrayList<TaskClass>>());
if (listList.size() == 0){
listList.add(new ArrayList<TaskClass>());
listList.add(new ArrayList<TaskClass>());
listList.add(new ArrayList<TaskClass>());
listList.add(new ArrayList<TaskClass>());
} else {
for(TaskClass t:listList.get(0)){
arrayListA.add(t.getIcon());
}
for(TaskClass t:listList.get(1)){
arrayListB.add(t.getIcon());
}
for(TaskClass t:listList.get(2)){
arrayListC.add(t.getIcon());
}
for(TaskClass t:listList.get(3)){
arrayListD.add(t.getIcon());
}
}
Integer[] intArrayA = new Integer[arrayListA.size()];
for (int i = 0; i < arrayListA.size(); i++){
intArrayA[i] =(Integer) arrayListA.get(i);
}
ArrayAdapter adapterA = new CustomArrayAdapter(getApplicationContext(),intArrayA);
listViewA.setAdapter(adapterA);
Integer[] intArrayB = new Integer[arrayListB.size()];
for (int i = 0; i < arrayListB.size(); i++){
intArrayB[i] =(Integer) arrayListB.get(i);
}
ArrayAdapter adapterB = new CustomArrayAdapter(getApplicationContext(),intArrayB);
listViewB.setAdapter(adapterB);
Integer[] intArrayC = new Integer[arrayListC.size()];
for (int i = 0; i < arrayListC.size(); i++){
intArrayC[i] =(Integer) arrayListC.get(i);
}
ArrayAdapter adapterC = new CustomArrayAdapter(getApplicationContext(),intArrayC);
listViewC.setAdapter(adapterC);
Integer[] intArrayD = new Integer[arrayListD.size()];
for (int i = 0; i < arrayListD.size(); i++){
intArrayD[i] =(Integer) arrayListD.get(i);
}
ArrayAdapter adapterD = new CustomArrayAdapter(getApplicationContext(),intArrayD);
listViewD.setAdapter(adapterD);
}
//TODO: create a custom adapter that will display icons correctly
public class CustomArrayAdapter extends ArrayAdapter{
private final Context context;
private final Integer[] values;
public CustomArrayAdapter(Context context, Integer[] values) {
super(context, -1, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View iconView = inflater.inflate(R.layout.iconlayout, parent, false);
ImageView imageView = (ImageView) iconView.findViewById(R.id.listImageView);
int s =(int) values[position];
imageView.setImageResource(s);
//get the device width, to calculate icon width, and then set icon width accordingly
//TODO: setting icon width not working
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width =(size.x)/5;
Uri uri = Uri.parse("android.resource://com.example.sta.tomov0/drawable/"+s);
Picasso.with(context).load(uri).resize(width,width).fit().centerCrop().into(imageView);
return iconView;
}
}
}
A note: I have a custom data management class that returns arraylists. That's tested separately and working fine. The arraylists that are being fed to the adapter are int arraylists of the drawable references.
Help! What am I doing wrong? I've been searching through SO all day and trying different things. The solutions posted in these questions haven't helped T-T:
How to set ImageView width in android ListView?
ImageView in ListView that expands maximum width
(that was a bit difficult to implement, and just created other problems - it seems like overkill to create a custom view class just for displaying an icon?)
Give fixed height to your Imageview in My listview layout:
Say Something like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="120dp"
android:id="#+id/listImageView"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false" />
</RelativeLayout>
I'm assuming #drawable/office38 is the image shown in your screenshot (taking up the whole screen)?
Try setting the dimensions of this image to something small, eg:
android:layout_width="50dp"
android:layout_height="50dp"
I know this may not be what you are actually looking to do, but at least it will show you if the problem is with that first ImageView or not and will free up space below it for the TextView and the GridLayout. It could be that the drawable is massive, and as you aren't specifying a set width/height or any scale options, it takes up all the space it can.
Also, have a look at http://developer.android.com/reference/android/widget/ImageView.html#attr_android:scaleType which describes the different scaling options you have with an ImageView.
GOT IT.
sorry guys. It was a really dumb problem after all.
My listview adapter is working fine it turns out. The problem was in the imageview that was outside of the lists (tomoface) - for some reason, I decided to set it to a different drawable on runtime, and hadn't scaled that at all haha.

How to Increase the size of Image View in Android

I am working on a project that requires me to implement two horizontal scroll views with an image view in between them. I would like to extend the size of the image view to fill up the gap in between the scroll view. I went through the sample code, but it doesn't seem to mention anywhere in the size of the image view. I've enclosed the code below after the Image describing my problem.
public class Gallery2DemoActivity extends Activity {
private Gallery gallery,gallery1;
private ImageView imgView;
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery1 = (Gallery)findViewById(R.id.examplegallery1);
gallery1.setAdapter(new AddImgAdp(this));
imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(Imgid[position]);
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(110, 100));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
This is my XML main file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/examplegallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Below is my Attributes file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="GalleryTheme">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>
Vinner,
The issue here is really two issues:
The size of the ImageView is based on the parent, which means that it shares its width and height.
The Image is resized to match the most limiting dimension (width).
The easiest way to handle this is to set the ScaleType of the ImageView. There are a number of Scaling Types, but the one you probably want is Center Crop. In code, this is done by view_name.setScaleType(ImageView.ScaleType.CENTER_CROP). You may also do this in XML for your ImageView with the attribute android:scaleType="centerCrop". Mind you, this will resize your Image and maintain your aspect ratio, but it will cut off the sides.
Just add the above attribute to your ImageView in your XML, (probably under your layout_weight), and it should do what you want.
Hope this helps,
FuzzicalLogic
will you try below code... ?
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1" />
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:src="#drawable/ic_launcher"
android:layout_weight="1"/>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/examplegallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"/>
</LinearLayout>
and if it do not work, try setting your gallery height to some dp, it will work.. Thanks
The size of the ImageView is based on the parent, which means that it shares its width and height. Also, the Image is resized to match the most limiting dimension (width).
It looks like in the AddImgAdp class, you set the ImageView's height and width to specific pixel sizes.
imgView.setLayoutParams(new Gallery.LayoutParams(110, 100)); //width = "110px", height="100px"
I personally would put the layout as a RelativeLayout, align the first gallery with the top, the second gallery with the bottom, nd have the ImageView set to align above the second gallery. Then make the image:
imgView.setLayoutParams( new Gallery.LayoutParams(
Gallery.LayoutParams.FILL_PARENT,
Gallery.LayoutParams.FILL_PARENT) );
If you set the ImageView to fill_parent with a LinearLayout, it will fill to the bottom of the screen and the second gallery will be off the screen.
Also know that by filling in all the space you will most likely distort the picture. Consider all of your resizing options: http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

Categories