Android gridView item width issue - android

I Created GridView with CheckedTextView as item.
Here my Gridview in XML
<GridView
android:id="#+id/grid_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip"
android:gravity="center"
android:choiceMode="multipleChoice"/>
Here GridView item in XML
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/checkedTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/gray_color"
android:background="#drawable/shape_rectangle"/>
But it displaying two item in a row and each item displaying with extra space like below
how to avoid the extra space in each item in GridView ?

try using match_parent in the item's width & height, not sure what was the problem but ive also encountered this one, tell me if this fixed it, otherwise ill look into my old projects and get you another solution

Related

Trying to get textView to stick to gridView scroll

I have a fragment with a textView giving instructions, and below a gridView. I wanted for the textView to scroll upwards with the gridView. In other words: for textView to disappear off screen when gridView is scrolling.
My idea of how to accomplish this is to give the gridView a fixed height that would make the entire layout scrollable - including the textView. However, up until I cannot achieve this. Is there a way to do this?
Layout no scroll:
When scrolled:
Any help/advice would be appreciated.
My layout XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/LightGrey"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/chooseLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="18dp"
android:text="#string/chooseLine"
android:textAppearance="?android:attr/textAppearanceLarge" />
<GridView
android:id="#+id/gridLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="130dp"
android:fitsSystemWindows="false"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:paddingTop="0dp"
android:stretchMode="columnWidth"
android:verticalSpacing="25dp" />
</LinearLayout>
Achieving this is is not going to be trivial. What you need is ListView's capability to add a header view, but unfortunately, GridView doesn't offer that functionality.
What I have done in the past to solve this problem is to convert the GridView to a ListView, and then add the header to the ListView. I created a wrapper adapter that takes the original adapter and combines a horizontal row's worth of grid cells into a single list row.
The tricky parts include: dynamically adapting the number of columns in a row based on the width of the screen, accounting for all combinations of view types within a row and remaining empty columns in the last row, and handling click interactions properly.
Put the whole LinearLayout in a ScrollView (How to use ScrollView in Android?) and for the GridView set android:layout_height="wrap_content"

Listview height

I have listview that loads all audio files from sd card...there is alot of files..but listview looks prety bad..every row height automatically resizes depends of item name that shows in row..background is white and text is grey so it is hard to notice it...I want to set every row height to same so it looks like this
here is my code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/PhoneMusicList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
In your group row.xml
Instead of android:layout_height="wrap_content" for the listview row height give a fixed height..This will solve your problem......
Something like this
android:layout_height="50dp"
Use the customize list view. Create a layout for one list view item. Set the height in that. Apply adapter in .java file. Then it works fine. I done the same thing and it works for me.

how to fit a large layout within a single screen with multiple listviews

i am working with a project in which what i have to do that there are two lists and the layout is very large you can say that it is just double to a normal layout. i have to add scroll view to show whole layout but the problem is that there can't be a list within a scroll view if i do so then there is ambiguity to compiler that which is scrolling if it is list or scroll view . Now what should i do help me thanks in advance ...
I am also attaching an image which is half of my layout....
From your query i designed one sample XML file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffeabc" >
</LinearLayout>
</LinearLayout>
</ScrollView>
In above you will have Scroll View for larger height Views and also you have equal proportion of list View. Thing you have to concentrate is on android:layout_weight , you should check how to play using Weight attributes
Edit :
Okay one thing you can do is that , We can use Header footer concept of List View - make ImageView and 2nd List View as footer of 1st List View so that ImageView and 2nd ListView will always comes Below 1st List View..
For Header Footer concept check this Link.

Inflating a ListView with different number of ImageViews in each row

I'm starting to work with the Android LayoutInflater and I need some help.
I have a ListActivity which uses a layout defined in the following XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
This ListView is filled with an ArrayAdapter which generates each row by inflating another XML like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/item_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="20dip"
android:textColor="#FFF"
/>
<ImageView
android:id="#+id/item_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginRight="6dip"
android:src="#drawable/example_icon"
/>
</LinearLayout>
</LinearLayout>
This is working fine, but now I want to show a variable number of icons for each item (so, each row of the ListView could have a different number of ImageViews under the TextView).
Maybe I have to define another XML for the list of icons and somehow build the ListView from nested layouts? I don't know what's the better way to do this. Any suggestions?
Thank you!
Edit: This is an example of what im trying to do: http://tinypic.com/r/2s9yniw/7
You'll just have to include a number of images in the XML layout for each row, and set the visibility of these to GONE when you no longer need them (you could put an image in one layout XML file and then include this 3 times on your row layout file).
You might be able to do it by inflating the image layout XML file as many times as you need for each row then adding it to the row view (but this gives problems with recycling if you only added 2 images the first time and the next row needs 3); but from what I remember you cannot add inflated content to an Adapter row or you get an exception (not entirely sure, so give it a try if in doubt).
In a particular row you can place the image views below the text view of that row and when you do not need to show them you can hide it by setting the visibility to GONE.

Add ImageView to a ListActivity

I would like to add an ImageView under my ListView in my ListActivity. So that when the user scrolls to the end of the list, the ImageView shows.
I can't seem to get my layout correct. Either the ImageView is not displayed at all, or if I use layout_weights, then both the ListView and ImageView get 50% of the screen. Any ideas how I can get what I want? Thanks.
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/list_no_items" />
<ImageView
android:id="#+id/instruct_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/instructions"/>
</LinearLayout>
Unfortunately I don't think what you want to accomplish is easily done with standard Android layouts. You can't have a ListView within another ScrollView.
You could use the ListView footer to position the ImageView below the ListView.
Or, you could try to add the ImageView as the last row in the ListView. To do this, override the getView method of the adapter and return the ImageView if it is the last row.

Categories

Resources