ImageView is small after rotation of device - android

After rotation of device -> Image (id=list_image2) remains small like it was at portrate mode
Seems it is because I change count of Columns gridView.setNumColumns(3);, but viewholder still contains previous imageview with previous width and height, also I tryed to make gridView.invalidate(); in onConfigurationChanged...but no result
added to activity in Manifest
android:configChanges="keyboardHidden|orientation|screenSize"
Here is item_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_row_root"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="1dip"
android:paddingRight="1dip"
<ImageView
android:id="#+id/list_image2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:minWidth="#dimen/grid_item_width"
android:minHeight="#dimen/grid_item_height"
android:src="#drawable/ic_action_cloud"
android:scaleType="fitXY"
/>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:layout_alignBottom="#+id/list_image2"
android:layout_alignTop="#+id/list_image2"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="#+id/likes_RelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/field_likes"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_alignBottom="#+id/list_image2"
android:layout_alignRight="#+id/list_image2"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp" >
<TextView
android:id="#+id/likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#android:color/white"
android:text="265465" />
<ImageView
android:id="#+id/btn_favoutites"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/likes"
android:src="#drawable/btn_rating_star_on_normal_holo_dark"
android:text="Button" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/btn_RelativeLayout"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBottom="#+id/likes_RelativeLayout"
android:layout_alignLeft="#+id/likes_RelativeLayout"
android:layout_alignRight="#+id/likes_RelativeLayout"
android:layout_alignTop="#+id/likes_RelativeLayout"
android:background="#drawable/field_like_clickable_style" >
</RelativeLayout>
Here is my adapter:
publ
ic class LazyAdapter2 extends BaseAdapter{
public LazyAdapter2(Context context, ArrayList<WallImage> d) {
mContext = context;
data=d;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = ImageLoader.getInstance();
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.resetViewBeforeLoading(true)
.imageScaleType(ImageScaleType.EXACTLY)// default
.build();
}
public View getView( int position, View convertView, ViewGroup parent) {
final ViewHolderItem viewHolder;
if(convertView==null)
{
convertView = inflater.inflate(R.layout.item_grid, null);
viewHolder = new ViewHolderItem();
viewHolder.thumb_image=(ImageView)convertView.findViewById(R.id.list_image2); // thumb image
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolderItem) convertView.getTag();
}
WallImage imageObject = data.get(position);
viewHolder.likes.setText(imageObject.views);
imageLoader.displayImage(imageObject.preview, viewHolder.thumb_image, options);
return convertView;
}
}

Not just invalidate but how about like this with Handler.post()?:
new Handler(getMainLooper()).post(new Runnable() {
#Override
public void run() {
gridView.invalidate();
}
});

Maybe this is due to the use of convertView in your getView method?
It keeps the original layout and don't recreate it when orientation change.
You could add to your ViewHolderItem class a boolean member that indicates the screen orientation.
And when you recycle the view, you test your viewHolder orientation. If it's different from the current screen orientation, don't recycle it.

Solved by changing of item_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_row_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="1dip"
android:paddingRight="1dip"
>
<ImageView
android:id="#+id/list_image2"
android:layout_width="match_parent"
android:layout_height="#dimen/grid_item_height"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:scaleType="centerCrop"
android:src="#android:color/transparent"
/>
after doing of ImageView android:layout_height="#dimen/grid_item_height" => after rotation of screen image size remains constant, but to resize imege in ImageView I use android:scaleType="centerCrop" after that image is slighly cutted , but problem is solved

Try imageScaleType(ImageScaleType.IN_SAMPLE_INT)
and
imageLoader.displayImage(imageObject.preview, viewHolder.thumb_image, options);
viewHolder.thumb_image.setScaleType(ImageView.ScaleType.FILL_XY);

Related

Android ListView doesn't show first item

I have gone through this & this SO questions but unable to figure out why the first item of my list view is not visible. I have checked the size of arraylist and it's correct.
if there is 2 items then it shows only 1.
if there is 1 item then it shows 0.
listview file:
<?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">
<ListView
android:id="#+id/videoListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp" />
</RelativeLayout>
listview item xml file:
<?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_content"
android:paddingTop="10dp">
<ImageView
android:layout_width="150dp"
android:layout_height="90dp"
android:id="#+id/videoImageView"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:id="#+id/titleTextView"
android:layout_marginRight="10dp"
android:layout_toRightOf="#+id/videoImageView"
android:layout_marginLeft="10dp"
android:textSize="12sp"
android:textColor="#000000"
android:ellipsize="end"
android:maxLines="2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:id="#+id/timeTextView"
android:layout_alignStart="#+id/titleTextView"
android:layout_alignLeft="#+id/titleTextView"
android:layout_below="#+id/titleTextView"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/downloadImageView"
android:layout_alignStart="#+id/timeTextView"
android:layout_alignLeft="#+id/timeTextView"
android:layout_below="#+id/timeTextView"
android:layout_alignBottom="#+id/videoImageView"
android:src="#drawable/ic_vector_download"
android:layout_marginTop="5dp"/>
<ProgressBar
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/downloadProgressBarView"
android:layout_alignStart="#+id/timeTextView"
android:layout_alignLeft="#+id/timeTextView"
android:layout_below="#+id/timeTextView"
android:layout_alignBottom="#+id/videoImageView"
android:layout_marginTop="5dp"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/shareImageView"
android:layout_below="#+id/timeTextView"
android:layout_alignBottom="#+id/videoImageView"
android:layout_toRightOf="#+id/downloadImageView"
android:layout_marginTop="5dp"
android:layout_marginLeft="7dp"
android:src="#drawable/ic_vector_share"/>
</RelativeLayout>
adapter class:
public class OfflineVideoAdapter extends ArrayAdapter<OfflineVideo> {
Context context;
OfflineVideoDBHelper db;
List<OfflineVideo> list;
public OfflineVideoAdapter(Context context, int resource, List<OfflineVideo> list) {
super(context, resource, list);
this.context = context;
this.list = list;
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final OfflineVideoHolder holder;
if (convertView == null) {
holder = new OfflineVideoHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.video_list_item, parent, false);
holder.video = (ImageView)convertView.findViewById(R.id.videoImageView);
holder.title = (TextView)convertView.findViewById(R.id.titleTextView);
holder.publishedAt = (TextView)convertView.findViewById(R.id.timeTextView);
holder.delete = (ImageView)convertView.findViewById(R.id.downloadImageView);
holder.share = (ImageView)convertView.findViewById(R.id.shareImageView);
convertView.setTag(holder);
} else {
holder = (OfflineVideoHolder)convertView.getTag();
}
final OfflineVideo offlineVideo = getItem(position);
Picasso.with(context).load(offlineVideo.imageURL).into(holder.video);
holder.title.setText(offlineVideo.title);
holder.publishedAt.setText(CommonUtils.calculateTime(offlineVideo.publishedAt));
holder.delete.setImageResource(R.drawable.ic_vector_delete);
return convertView;
}
}
Change the ListView attribute layout_width and layout_height to Fill_Parent
it worked for me.
I was having the same issue because my ListView layout_width and layout_height was "match_constraint" and the toolbar was overlapping the first item. Just adjust the height of the ListView. It works for me.

Android. two imageViews that fits screen perfectly

Im working on a ListView and each row has two square imageViews. The drawable is given dynamically by url. The problem is that the picture does not fit screen.
the image from url is already square but i want the image to scale up or down to fit the screen.
right now it looks like first screenshot but i want them to look like the second one.
<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="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
>
<ImageView
android:id="#+id/list_item_image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<ImageView
android:id="#+id/list_item_image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
</LinearLayout>
this is Adapter
public class ListAdapter extends ArrayAdapter<ListItem>{
private ArrayList<ListItem> items;
private Activity context;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public ListAdapter(Activity context, int textViewResourceId, ArrayList<ListItem> items)
{
super(context, textViewResourceId, items);
this.items = items;
this.context = context;
inflater = (LayoutInflater)context.getLayoutInflater();
imageLoader = new ImageLoader(context);
}
public static class ViewHolder{
public ImageView image1;
public ImageView image2;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View rowView = convertView;
ViewHolder holder;
if (rowView == null) {
rowView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.image1 = (ImageView)rowView.findViewById(R.id.list_item_image1);
holder.image2 = (ImageView)rowView.findViewById(R.id.list_item_image2);
rowView.setTag(holder);
}
else
holder = (ViewHolder)rowView.getTag();
imageLoader.DisplayImage("http://dev.ts.hs.kr/jjhoon713/images/1400023976194990.jpg", holder.image1);
imageLoader.DisplayImage("http://dev.ts.hs.kr/jjhoon713/images/1400023976194990.jpg", holder.image2);
return rowView;
}
}
try 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="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/list_item_image1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitXY"
/>
<ImageView
android:id="#+id/list_item_image2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitXY"
/>
</LinearLayout>
Try with below code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_margin="5dp"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2"
>
<ImageView
android:id="#+id/list_item_image1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:scaleType="centerCrop"
/>
<ImageView
android:id="#+id/list_item_image2"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher"
android:scaleType="centerCrop"
/>
</LinearLayout>
take a linear layout with weightsum=2 and assign 1 weight to each imageview like below:
<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="wrap_content" // you can hardcode height if your view
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="#+id/imageview1"
android:layout_width="0dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:layout_height="wrap_content" // can change to fill parent if you hardcode the height of your view
android:src= ""// your image if static, else set dynamically
android:scaleType="fitxy"/>
<ImageView
android:id="#+id/imageview2"
android:layout_height=""// can change to fill parent if you hardcode the height of your view
android:layout_width="0dp"
android:adjustViewBounds="true"
android:layout_weight="1"
android:src="wrap_content" // your image if static, else set dynamically
android:scaleType="fitxy"/>
</LinearLayout>
my problem was that layout width of list_view.xml was wrap content.
since parent of list items are list_view so weight didnt work.
i changed them to fill_parent and now it works well

Set different icons for each row

Above is the main screen of my app with a custom adapter for listview. How do I put a different icon for each row in the listview? I tried putting an ImageView in the xml but it overwrites my whole screen only showing an icon.
MainAcitivty 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:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/hp_color"
tools:context=".MainActivity" >
<TextView
android:id="#+id/cityText"
style="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="28sp" />
<ImageView
android:id="#+id/condIcon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_below="#id/cityText"
android:contentDescription="weather icon" />
<TextView
android:id="#+id/condDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/condIcon"
android:textSize="20sp" />
<ListView
android:id="#+id/mainListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<ImageButton
android:id="#+id/btnPicturePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="picturePage"
android:src="#drawable/photo" />
Custom adapter:
public class WeatherAdapter extends ArrayAdapter<String> {
private final Activity context;
private ArrayList<String> weatherData;
static class ViewHolder {
public TextView text;
public ImageView image;
}
public WeatherAdapter(Activity context, ArrayList<String> weatherData) {
super(context, R.layout.list, weatherData);
this.context = context;
this.weatherData = weatherData;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.rowTextView);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
ArrayList<String> s = new ArrayList<String>(weatherData);
String []sa = new String [s.size()];
sa = s.toArray(sa);
holder.text.setText(sa[position]);
if (sa[position].startsWith("Air pressure")) {
rowView.setBackgroundResource(R.color.skyblue) ;
}
return rowView;
}
List xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#color/white" >
</TextView>
Your list.xml should be something similar to this (taken from one my files)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:gravity="center_vertical" android:orientation="horizontal"
android:background="#drawable/xml_pressed_bg" android:clickable="true" android:layout_width="fill_parent"
android:layout_height="54dip"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ttshow="http://schemas.android.com/apk/res-auto">
<ImageView android:id="#+id/imgThumb" android:layout_width="42.0dip" android:layout_height="42.0dip" android:scaleType="fitXY"
android:src="#drawable/sample_dj" />
<TextView android:textSize="14.0sp" android:textColor="#ff666666" android:id="#+id/txtName"
android:layout_width="180dip" android:layout_height="wrap_content"
android:singleLine="true" android:shadowColor="#99ffffff" style="#style/TextShadowLight"
android:text="Hello" android:paddingLeft="10dip"/>
</LinearLayout>
Every row item has an image (imgThumb). This needs to be changed or set to a static image, depending on your needs. This is done in the adapters' getView() method.
imgThumb.setImageResource(R.drawable.my_image);
Private TypedArray img;
img=getResource.obtainTypedArray(R.array.imgIcon)
if (sa[position].startsWith("Air pressure")) {
rowView.setBackgroundResource(R.color.skyblue) ;
holder.image.setImageResource(img.gerResourceId[0]);
}else if (sa[position].startsWith("Air Condition")) {
holder.image.setImageResource(img.gerResourceId[1]);
}

ImageView doesn't fill the height of its layout

I wish to add an ImageView inside a ListView. I would like the image to fill the layout without distorsion (basically, fill the height and adapt its width).
This is what I get now:
I have put a grey background to the ImageView to make it easier to see. In red, it's the size I wish to get.
Here is my code:
Project_form.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/name_project"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="3dp"
android:layout_alignParentLeft="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="25dp" >
</TextView>
<Button
android:id="#+id/button_project"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btn_custom"
android:layout_centerVertical="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignParentRight="true"
android:padding="3dp"
android:text=" details "
android:textSize="20dp"
android:visibility="invisible" />
<ImageView
android:id="#+id/imagesstep"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#color/grey"
android:adjustViewBounds="false"
android:layout_centerVertical="true"
android:padding="3dp"
android:layout_alignParentRight="true"
android:visibility="invisible" />
</RelativeLayout>
method getView() of my Adapter :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row==null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.project_form, parent, false);
}
TextView text = (TextView) row.findViewById(R.id.name_project);
text.setText(this.objects[position].getName());
int IDImage=this.objects[position].getIDImage();
int IDPlay = this.objects[position].getIDPlay();
text.setCompoundDrawablesWithIntrinsicBounds(IDImage, 0, 0, 0);
text.setCompoundDrawablePadding(7);
ImageView image = (ImageView)
row.findViewById(R.id.imagesstep);
image.setVisibility(View.VISIBLE);
image.setImageResource(IDPlay);
if (position == selectedPosition) {
row.setBackgroundColor(Color.parseColor("#8000ff00"));
}
else {
row.setBackgroundColor(Color.TRANSPARENT);
}
return row;
}
I tried to play with different parameters such as ScaleType, android:adjustViewBounds and layout_width but the height of my image never changed. I also tried to do it programatically (something like image.setMinHeight(parent.getHeight()) but it failed too.
Could anyone point me out what is going wrong?
Many thanks
Try changing the RelativeLayout of the layout_height to fill_parent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<TextView ... />
<Button .. />
<ImageView ... />
</RelativeLayout>
Alternatively you could set the minHeight attribute of the ImageView to ?android:attr/listPreferredItemHeight
<ImageView
...
android:minHeight="?android:attr/listPreferredItemHeight" />
Remove the padding from your imageview

Android gridview adjusting to screen size

The grid view of my application has 3 rows and 3 columns. I want this to fill the screen ,irrespective of the screen size of the device.
I tried getting window size and setting the layoutparams accordingly. But this is not giving a perfect alignment as it works with weightsum in linearlayout.
So can we use weightsum for gridview or is there any other android property that can be used.
Thanks a lot for time and response.
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/text1view"
android:background="#drawable/home_bg"
android:gravity="center"
android:horizontalSpacing="10dip"
android:numColumns="3"
android:stretchMode="columnWidth" />
each grid item is
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="75dip"
android:layout_margin="0dip"
android:padding="0dip" >
</ImageView>
<TextView
android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/grid_item_image"
android:gravity="center_horizontal"
android:padding="0dip"
android:textColor="#000000"
android:textSize="10sp" >
</TextView>
code of the adapter :
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.griditems, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.grid_item_text);
holder.image = (ImageView) convertView
.findViewById(R.id.grid_item_image);
// if it's not recycled, initialize some attributes
holder.image.setImageResource(gridItemIds[position]);
holder.text1.setText(gridTitles[position]);
int h = mContext.getResources().getDisplayMetrics().densityDpi;
// holder.image.setLayoutParams(new LayoutParams(h-50,h-50));
convertView.setLayoutParams(new GridView.LayoutParams(h - 45,
h - 39));
// holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
// holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
// holder.image.setPadding(20, 20, 20, 20);
// convertView.setLayoutParams(new GridView.LayoutParams(Utils
// .getLayoutParameter(), Utils.getLayoutParameter()+50));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new ModuleManager().startModuleACtivity(position, mContext);
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView text1;
ImageView image;
}
pls see the line
convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));
i arrived at this value with some trials on different size emulator.
But i dont think its a right way to define height and width.
As far as i understood your question, as you have not posted any code, what you are trying to do is, you want gridview to appear on full screen.
Firstly you must be using some layout to put your gridview in. Do the following:
Make height and width of the layout to fill parent:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
then, set height and width of your gridview to fill_parent as well.
Well, this is just a supposed solution that you might have been working this way. putting some code would be much better. Hope this works for you.
Update:
Well the problem may lie here:
convertView.setLayoutParams(new GridView.LayoutParams(h-45, h-39));
change to:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
convertView.setLayoutParams(new GridView.LayoutParams(params));
Hope this works for you.
#preetha: first of all look at this example i used this and my layout did not affect irrespective of devise height and width....
http://developer.android.com/resources/tutorials/views/hello-gridview.html
set the following in xml
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
hope this helps...
Here I only changed width and height as match parent and wrap content.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView5"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingTop="5dp"
android:text="Category"
android:textSize="20dp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:src="#drawable/iconclothcolor" />
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView3"
android:layout_marginTop="2dp"
android:textSize="16dp"
android:textColor="#F37E98"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Shirt" />
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView4"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Gender - Male" />
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView8"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Quantity - 10" />
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView6"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Size - S" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/textView10"
android:textSize="20dp"
android:text="Offer" />
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView7"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textColor="#F37E98"
android:text="Price - 1500 Rs" />
</LinearLayout>
</androidx.cardview.widget.CardView>
And in the Mainclass add this -->
GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
recyclerView.setLayoutManager(gridLayoutManager);
To adjusting Gridview to screen size
In onCreate take rowHeight :
root = binding.root
view.onGlobalLayout {
rowHeight =
root.measuredHeight / currentPageNumberOfRow
fillView(appsList)
}
In adapter:
override fun getView(position: Int, ConvertView: View?, parent: ViewGroup?): View? {
var mConvertView = ConvertView
if (layoutInflater == null) {
layoutInflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as
layoutInflater!!.inflate(R.layout.item_app_shortcut, null)
}
if (mConvertView == null) {
mConvertView =
layoutInflater!!.inflate(R.layout.item_child_launcher_app, null)
//Add this:
mConvertView.layoutParams = AbsListView.LayoutParams(GridView.AUTO_FIT, rowHeight)
}
Dont forget set row image:
android:layout_width="0dp"
android:layout_height="0dp"
To fit on screen

Categories

Resources