Set Fixed GridView Row Height - android

I am having all sorts of issues with row heights in GridView.
I would like to set the size of the items (height/width) in XML and then just have the GridView autofit as many of them as it can with no stretching. If it can't fit the next element it should just add padding around the current number of elements it was able to fit.
Currently I get 2 columns (which to me almost seems fixed size) and the rows get stretched. Could someone please help explain what is happening and how to achieve what I want?
GridView:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/main_grid"
android:numColumns="auto_fit"
android:gravity="center"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:stretchMode="none"
android:background="#drawable/main_grid_background">
</GridView>
GridItem (I want it 320x320 as I later insert a background image into it which looks odd if its not a perfect square).
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320dp"
android:layout_height="320dp"
android:padding="10dp" >
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="#+id/label"
android:layout_marginTop="5dp"
android:textColor="#color/black"
android:textSize="15sp"
android:visibility="invisible"
android:layout_centerInParent="true" >
</TextView>
</RelativeLayout>
Java:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.main_grid_item, null);
} else {
gridView = (View) convertView;
}
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
//SET TEXT AND BACKGROUND IMAGE HERE
//gridView.setBackgroundResource(R.drawable.main_grid_item_import);
return gridView;
}

In the CustomAdapter
v.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, rowHigh));
rowHigh is the dimension you must change

So, this is not a perfect answer but this is how I got it to work. Ideas borrowed from this thread.
All of this works assuming I know my image sizes, which are 320x320. Also, in GridView I had to set android:columnWidth="320dp" or it would not work. If someone has a better idea please post it... for now I am moving on.
Grid Item XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="#+id/label"
android:textColor="#color/black"
android:layout_alignLeft="#+id/grid_item_image"
android:layout_alignTop="#+id/grid_item_image"
android:layout_alignRight="#+id/grid_item_image"
android:layout_alignBottom="#+id/grid_item_image"
android:gravity="center"
android:textSize="15sp"
android:visibility="invisible">
</TextView>
</RelativeLayout>
GridView XML:
<GridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/main_grid"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="320dp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:stretchMode="columnWidth"
android:background="#drawable/main_grid_background">
</GridView>

public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec;
if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
// The great Android "hackatlon", the love, the magic.
// The two leftmost bits in the height measure spec have
// a special meaning, hence we can't use them to describe height.
heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
} else {
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
In XML:
<MyGridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/main_grid"
android:gravity="center"/>

Related

Unable to scroll LinearLayout in ScrollView

I want to make a layout that display several items, but I cannot make them fully unfold, so I use a ScrollView. However, I find that I can only scroll the first GridView, the last two item can not be scroll up.
What I need is to display two GridView fully and all items can be scrolled,but I do not know how to deal with the problem.
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.ifchan.reader.AllClassActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/all_class_tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorToolbar"
app:title="All Class"
app:titleTextColor="#ffffff"/>
<ScrollView
android:id="#+id/all_class_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/all_class_tool_bar"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/all_class_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="male"
android:textSize="20dp"/>
<GridView
android:id="#+id/all_class_male_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"/>
<TextView
android:id="#+id/all_class_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="female"
android:textSize="20dp"/>
<GridView
android:id="#+id/all_class_female_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Use Recycler View or a non scrollable gridview if you want to use scrollview.
public class NonScrollGridView extends GridView{
public NonScrollGridView (Context context) {
super(context);
}
public NonScrollGridView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollGridView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
<NonScrollGridView
android:id="#+id/all_class_female_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"/>
You put GridViews into ScrollView. It will not work as you expected.
Try Using GridLayoutManager with RecyclerView and put it in NestedScrollView
You can check this question for detailed explanations.
If you want to use GridView anyway, you can use this ExpandableHeightGridView
which extends GridView

Items in a Listview don't look like my item layout

I'm building an app for a news website, I have created a ListView and a layout for each item, which has quite a bit of white space, but on the list view all the white space is removed.
Here's what I mean: http://imgur.com/a/mLuuE
This is an item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="175dp"
android:gravity="center"
android:layout_weight="0"
android:id="#+id/article_layout"
android:background="#drawable/gradientdemo">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Titolo dell'articolo"
android:textSize="#dimen/abc_text_size_headline_material"
android:layout_gravity="left"
android:id="#+id/article_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="3/9/16 - Autore"
android:layout_gravity="left"
android:id="#+id/article_info" />
</LinearLayout>
</LinearLayout>
This is my custom adapter:
public class ArticleAdapter extends ArrayAdapter<Article> {
public ArticleAdapter(Context context, int textViewResourceId){
super(context, textViewResourceId);
}
public ArticleAdapter(Context context, int resource, List<Article> items ){
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.article_item, null);
}
Article article = (Article) getItem(position);
TextView title = (TextView) v.findViewById(R.id.article_title);
TextView info = (TextView) v.findViewById(R.id.article_info);
if( title != null ) {
title.setText( article.getTitle() );
}
if( info != null ) {
info.setText( article.getAuthor() );
}
return v;
}
}
Hope it's readable, I'm kind of new to android development.
Your cell's LineaerLayout:
android:layout_height="0dp"
android:layout_weight="1"
change to:
android:layout_height="wrap_content"
you don't need layout_weight
Delete the android:layout_weight="1" and add a correct height, e.g. android:layout_height="60dp":
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center|bottom"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Titolo dell'articolo"
android:textSize="#dimen/abc_text_size_headline_material"
android:layout_gravity="left"
android:id="#+id/article_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="3/9/16 - Autore"
android:layout_gravity="left"
android:id="#+id/article_info" />
</LinearLayout>
in the linearLayout use wrapcontent, erase layout_weigth and add weigthsum=1
Then in textViews change height to 0dp and add to each one layout_weight=0.5
I think this will work
I fixed this by changing this line in the adapter
v = vi.inflate(R.layout.article_item, null);
to this one:
v = vi.inflate(R.layout.article_item, parent, false);

Android: Listview in ScrollView with dynamic height

currently I am developing a settings menu in a scrollview. The basic structure looks like the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollview_settings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/paddingSmall">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent_white_settings"
android:paddingTop="#dimen/paddingMedium"
android:paddingBottom="#dimen/paddingMedium">
<ImageView
android:id="#+id/logo"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:src="#drawable/newspaper_logo_color"
android:layout_alignParentRight="true"
android:layout_margin="#dimen/paddingBig"
android:adjustViewBounds="true"/>
</RelativeLayout>
<!-- Layout News Uebersicht -->
<include layout="#layout/separator_line_grey"/>
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#color/buttonHoverBackground"
android:textSize="#dimen/sliding_image_kicker_font_size"
android:paddingLeft="#dimen/paddingLarge"
android:paddingTop="#dimen/paddingMedium"
android:paddingBottom="#dimen/paddingMedium"
android:text="#string/news_overview"
android:textScaleX="#dimen/letter_spacing"
android:textStyle="bold"
android:gravity="center_vertical"
android:background="#color/settings_header_color"
android:layout_gravity="center_vertical"/>
<include layout="#layout/separator_line_grey"/>
<!-- News-Overview Issues -->
<LinearLayout
android:id="#+id/issue_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/transparent_white_settings">
<de.basecom.noznewsapp.views.NewsScrollListView
android:id="#+id/listview_issues"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:divider="#null"
android:dividerHeight="0dp" />
</LinearLayout>
<!-- Layout Einstellungen -->
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#color/buttonHoverBackground"
android:textSize="#dimen/sliding_image_kicker_font_size"
android:paddingLeft="#dimen/paddingLarge"
android:paddingTop="#dimen/paddingMedium"
android:paddingBottom="#dimen/paddingMedium"
android:text="#string/settings"
android:textStyle="bold"
android:textScaleX="#dimen/letter_spacing"
android:gravity="center_vertical"
android:background="#color/settings_header_color"
android:layout_gravity="center_vertical"/>
<include layout="#layout/separator_line_grey"/>
<!-- Einstellungs- Werte -->
<LinearLayout
android:id="#+id/settings_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/transparent_white_settings">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/paddingLarge">
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/receive_pushmessages"
android:textAllCaps="true"
android:textScaleX="#dimen/letter_spacing"
android:textSize="#dimen/sliding_image_kicker_font_size"
android:layout_centerVertical="true"
android:textColor="#color/buttonBackground"/>
<Switch
android:id="#+id/receive_pushmessages_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/empty_string"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
For the listview I used a custom implementation, to give it a fix height and make it able to scroll within the scrollview (note: I have to use the listview within the scrollview, because I have lots of elements, so that using a linearlayout wouldn't be nice).
public class CustomListView extends ListView {
public CustomListView(Context context) {
super(context);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context, AttributeSet attrs,int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
Each item of the listview contains a label and a gridview, which is all shown correctly. But now I want to implement the functionality, that the user can click on the label to show and hide the visibility of the gridview.
But if I click on the label and the gridview is shown, the height of the listview doesn't adapt to the new open element. It always stays at the same height and if I open an element, the elements below are no longer visible. This also even happens, if I call the requestLayout() method of the listview to trigger its onMeasure again.
Anybody got an idea what I am doing wrong?
EDIT: Updated my xml file :)
Finally I was only able to solve this issue by replacing Listview with a LinearLayout and add my items in code to the Layout.

Gridview multiline Textview cut off

I have a GridView and each cell has an ImageView with TextView under it. Unfortunately if the TextView has more than one line the text gets cut off. I have tried everything but I cant find a solution.
It seems that the row height of the GridView is the problem and not the actual text because you can see half of the text in the Textview.
Here is my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter());
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
startActivity(new Intent(Main.this, Acronyms.class));
}
});
}
public class ImageAdapter extends BaseAdapter {
private Integer[] iconImg = {
R.drawable.acronyms, R.drawable.acronyms,
R.drawable.acronyms
};
private String[] iconTitle = {
"Acronyms", "Cardiac Strips",
"Test 3"
};
public int getCount() {
return iconImg.length;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int arg0) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
view = inflater.inflate(R.layout.icon, null);
TextView textView = (TextView) view.findViewById(R.id.icon_text);
textView.setText(iconTitle[position]);
ImageView imageView = (ImageView) view.findViewById(R.id.icon_image);
imageView.setImageResource(iconImg[position]);
} else {
view = convertView;
}
return view;
}
}
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#a3e6ff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="25"
android:text="Example"
android:textColor="#248fb7"
android:textSize="40dp"
android:typeface="serif" android:gravity="center_horizontal"/>
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="15dp" >
</GridView>
</LinearLayout>
and my icon.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_icon_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="#+id/icon_image"
android:layout_width="90dp"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/icon_text"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:maxLines="2"
android:singleLine="false"
android:text="Samples Samples"
android:textColor="#FFFFFF"
android:textSize="15dp"
android:typeface="serif" />
</LinearLayout>
And here is a screenshot:
I resolved using, when i define it
TextView.setLines(2);
in the xml the textview is
android:layout_width="wrap_content"
android:layout_height="wrap_content"
[EDIT1]
You could try using a RelativeLayout instead of a Linear Layout for the icon.xml.
If this doesnt work then I would then move to a static height TextView. From looking at your screenshot, it looks like you will always use the same image, and the text is either going to be 1 line or 2. Just make the text height static to allow for 2 lines.
[ORIGINAL]
I think the problem is in your linear layout definition for your icon.xml. In your definition, you have the layout having "match_parent" as the width and height parameters. You should, since these are to essentially be subviews within the gridview be "wrap_content". Here is what I think it should be
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_icon_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<ImageView android:id="#+id/icon_image"
android:layout_width="90dp"
android:layout_height="wrap_content" />
<TextView android:id="#+id/icon_text"
android:layout_width="90dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:maxLines="2"
android:singleLine="false"
android:text="Samples Samples"
android:textColor="#FFFFFF"
android:textSize="15dp"
android:typeface="serif" />
</LinearLayout>
Change ConstraintLayout for LinearLayout.
Make sure you are using ConstraintLayout and if possible change by LinearLayout. For long texts, Constraint is not very good.

android How to stretch rows in the gridview to fill screen?

I have simple GridView (with one column and six rows), that displays ImageView with TextView in the cell (I create adapter). How to stretch rows to fill entire screen height?? Now I have some space below cells...
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<LinearLayout android:layout_height="fill_parent"
android:layout_width="150dp"
android:orientation="vertical"
android:id="#+id/left">
<include layout="#layout/menu_grid"></include>
</LinearLayout>
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="#+id/right">
<ImageView android:src="#drawable/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/imageMain"
android:layout_gravity="center">
</ImageView>
</LinearLayout>
menu_grid.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<GridView android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="#+id/gridView"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="1"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:gravity="center"></GridView>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="fill_horizontal">
<ImageView android:src="#drawable/icon"
android:layout_height="70dp" android:id="#+id/imageIcon"
android:layout_width="70dp" android:layout_gravity="center_horizontal"></ImageView>
<TextView android:id="#+id/textIcon" android:text="TextView"
android:layout_gravity="center" android:layout_height="wrap_content"
android:layout_width="wrap_content"></TextView>
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
//....some code
//....
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item, null);
TextView text = (TextView) view.findViewById(R.id.textIcon);
text.setText(labels[position]);
ImageView image = (ImageView) view.findViewById(R.id.imageIcon);
image.setImageResource(icons[position]);
} else {
view = convertView;
}
return view;
}
}
As of what i understood, the question is- if i have 6 rows of data to be filled in the gridView, how can i make these 6 rows fill the whole screen, instead of showing 6 rows and some empty space under the last row.
To achieve this you must set minimum height of the gridView item layout (in your case its item.xml) to (height of the screen)/6, where 6 is the (no.of rows you have to fill). This minimum height can be set in the adapter you are using.
To find the height of the screen of the device:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
find these values in the Activity and make them public static and use them in the adapter, i had a problem in finding these values in the adapter class
then to set minimum height:
view = inflater.inflate(R.layout.item, null);
view.setMinimumHeight(GridViewActivity.height/6);
I tried this after seeing the post, and it worked perfectly.
If you just want to increase the space between the lines You can use padding it should work.
you can set padding through xml or programmatically based on your requirement.
// this you can set inside your view.
android:paddingLeft="5px"
android:paddingRight="5px"
android:paddingTop="10px"
android:paddingBottom="10px"
// this you can set to your any widgets like TextView/Layouts/Buttons etc..
setPadding(top, left, bottom, right);
Extend a layout say Framelayout then override the onMeasure method.
public class CameraView extends FrameLayout
public TextView titleView;
public CameraView(Context context,AttributeSet attrs) {
super(context, attrs)
titleView = rootView.findViewById(R.id.titleView);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}
You can then use this view in your BaseAdapter and set the layout_width and layout_height to match_parent
I hope I am understanding you correctly, if so it is as easy as changing your GridView to fill_parent instead of wrap_content.
Change
<GridView android:layout_height="wrap_content"
to
<GridView android:layout_height="fill_parent"
If this is not the case then it is probably the layout height on the LinearLayout of the item.xml that you want to modify.

Categories

Resources