It is possible to create a GridVIew of LinearLayouts? - android

i need to have a gridview of linearlayouts. Each linearLayout must have a imageview and a relativelayout children with more childrens on it.
I'm searching for tutorials/examples of creating gridviews of LinearLayouts but i can't find nothing.
Someone haves a tutorial or can give me some examples or help to do this?
thanks

Yes, it's possible and is quite simple indeed. When you use your GridView, provide an Adapter to it. In the adapter's getview method, you can create any view you like and return it. For example, you can inflate a view from XML - and that xml may contain a LinearLayout. Alternatively, you can create a linear layout on the fly in that method and add other components to it.
Have a look at this article on Google: http://developer.android.com/resources/tutorials/views/hello-gridview.html
Update: a small example
In your res/layout/item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="0dip"
android:paddingBottom="0dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/TxtName"
android:scrollHorizontally="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:layout_weight="0.2"
android:padding="2dp"/>
<TextView android:id="#+id/TxtPackage"
android:scrollHorizontally="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:textColor="#android:color/black"
android:padding="2dp"/>
</LinearLayout>
Then in your adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//get the item corresponding to your position
LinearLayout row = (LinearLayout) (convertView == null
? LayoutInflater.from(context).inflate(R.layout.item, parent, false)
: convertView);
((TextView)row.findViewById(R.id.TxtName)).setText("first text");
((TextView)row.findViewById(R.id.TxtPackage)).setText("second text");
return row;
}

Related

size of rows in ListView in android

Here is my custom list adapter's getView() method
public override View GetView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
row = LayoutInflater.From(mContext).Inflate(Resource.Layout.listview_row, null, false);
}
TextView textName = row.FindViewById<TextView>(Resource.Id.textName);
textName.Text = mItems[position];
return row;
}
And here is my listview_row.xml file which is 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="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textName"
android:padding="10dp"/>
</LinearLayout>
I am curious about how list view sizes each row.
When I inflate the "listview_row" into a view and put it in the ListView, I am expecting that the first inflated view will take up whole space of ListView because this view has attributes of layout_width="match_parent" and layout_height="match_parent".
However, when i run this code, each row of the listView size is based on the row's content in the textView. How is it possible? is it something to do with the parameters of inflate() method?
Could you guys explain how ListView sizes its row?
Follow this way to program row list for adapter.
<?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="ADD_ROW_HEIGHT"
>
<TextView
android:text="Name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textName"
android:padding="10dp"/>
</LinearLayout>
I think you can consider ListView as an area that allows other views to be displayed within it, it's a container. In your case having the view (the TextView) as wrap_content is saying only take up as much space (vertically) as needed. As such, the ListView can possibly hold more than one.
If it's the actual calculations that you want to know, then although I recall seeing something, I can't recall when or where I saw that info.

Some ListViews rows unclickable

I have a ListView, each row of which has three TextViews in it. For some reason, as I scroll through the ListView, I'm not able to click on some of the rows. There doesn't seem to be any particular pattern to it. I initially thought this was a focus issue, so I added some statements to remove clickability and focus from the TextViews, but that has not resolved it. Here is the relevant portion of my code:
questionsArrayAdapter = new ArrayAdapter<String>(this, R.id.number, questionsArrayList) {
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (row == null) {
// ROW INFLATION
row = inflater.inflate(R.layout.questionlistitem, parent, false);
}
TextView number = (TextView) row.findViewById(R.id.number);
TextView answer = (TextView) row.findViewById(R.id.answer);
TextView section = (TextView) row.findViewById(R.id.section);
number.setFocusable(false);
number.setClickable(false);
answer.setFocusable(false);
answer.setFocusable(false);
section.setFocusable(false);
section.setClickable(false);
Here is questionlistitem.xml:
<?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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:visible="false" />
<TextView
android:id="#+id/answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginRight="5dip"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingEnd="10dip"
android:paddingStart="6dip"
android:textAppearance="?android:attr/textAppearanceListItemSmall" />
<TextView
android:id="#+id/section"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:visible="true" />
</LinearLayout>
Here is my standard listener:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("got clicked");
}
Does anyone know what the problem could be?
I think you're on the right track in thinking that this is an issue where your TextViews are drawing focus away from your click events. I found this discussion of what may be the same problem filed as a bug report for the source, however this was years ago and it seems that the official word was "working as intended." At the link, the two proposed solutions seems to be using the android:focusable="false" attribute on your TextViews in the xml (not sure if this is equivalent to doing it programmatically, as you've tried) or calling setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS); on convertView before it gets returned.
Something else that may be contributing to the problem is how your TextViews are being arranged in your xml file. The children of a horizontal LinearLayout should usually not have a layout_width of match_parent or fill_parent. This causes these views to expand to fill the length of the parent view, possibly overlapping their siblings and potentially causing weirdness both visually and when trying to interact with/click the children. You may want to consider using the layout_weight attribute, or making the parent a RelativeLayout, where you'll have more control over the position of the views.

Single row to fill_parent ListView height

There's one row in the listView which I want to be with the same height as the listView (let's say that is full-screen).
The row layout looks like
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/error" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:minHeight="30dip"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</RelativeLayout>
And the adapter's getView is
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.myrow, parent, false);
return row;
}
Bu the row is displayed the same as it would be with android:layout_height="wrap_content".
Layout preview shows the row filling it's parent and I'm using inflate(R.layout.myrow, parent, false);, the listView is certainly displayed full-screen and the row is only as tall as the image + textView.
Am i missing something important ?
I had similar problem I think I have solved so reporting here.
I have more rows in my ListView, but I want that the row height be the same of the listview, which in turn shall occupy all the remaining space of the layout. My row is made of just one ImageView. I had problems with the following:
-I want that smaller images expand to occupy all the space
-Bigger images shall not exapnd more than a row height
If I have made any error please leave a note here.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9"
android:gravity="center"
android:orientation="vertical">
<ListView
android:id="#id/lay_main_firma_objekti"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
</ListView>
</LinearLayout>
Row layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="#+id/xx"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:id="#id/imageView_obj"
android:contentDescription="#string/objektDesc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="#id/xx"
android:layout_alignTop="#id/xx"
android:layout_alignRight="#id/xx"
android:layout_alignBottom="#id/xx"
android:scaleType="fitXY"
android:src="#drawable/custom_prazno" />
</RelativeLayout>
Then in the Adapter getView it is important to
convertView.setMinimumHeight(parent.getMeasuredHeight());
ImageView slika=(ImageView)v.findViewById(R.id.imageView_obj);
slika.setMinimumHeight(parent.getMeasuredHeight());
TextView xx=(TextView)v.findViewById(R.id.xx);
xx.setHeight(parent.getMeasuredHeight());
I think that is.
hth
just wondering if there's a particular reason why you want to have a ListView containing only one row? As opposed to just using the RelativeLayout directly instead of ListView?
I've ended up with hardcoding the row height. There is a problem with relative layout after setting it's minimumHeight so I've also replaced it with the LinearLayout with centered gravity.
Please let me know if there's a better solution letting Android to do it's job and minimizing hardcode.

android listview item height

Why when i use SimpleCursorAdapter for ListView i have items height in ListView like this -
(My code based on this)
But when using arrays Listview items have big height
(I learn listview based on this)
Row layout for item listview is
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="#+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
So My question is why there is a difference in row heights when using ArrayAdapter and SimpleCursorAdapter?
android:textAppearance="?android:attr/textAppearanceLarge"
seemed no effect.
android:minHeight="?android:attr/listPreferredItemHeight"
changed the height for me
The trick for me was not setting the height -- but instead setting the minHeight. This must be applied to the root view of whatever layout your custom adapter is using to render each row.
You need to use padding on the list item layout so space is added on the edges of the item (just increasing the font size won't do that).
<?xml version="1.0" encoding="utf-8"?>
<TextView android:id="#+id/text1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp" />
I did something like that :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
textView.setHeight(30);
textView.setMinimumHeight(30);
/*YOUR CHOICE OF COLOR*/
textView.setTextColor(Color.BLACK);
return view;
}
You must put the both fields textView.setHeight(30);
textView.setMinimumHeight(30); or it won't change anything. For me it worked & i had the same problem.
The height of list view items are adjusted based on its contents. In first image, no content. so height is very minimum. In second image, height is increased based on the size of the text. Because, you specified android:layout_height="wrap_content".
Here is my solutions;
It is my getView() in BaseAdapter subclass:
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView==null)
{
convertView=inflater.inflate(R.layout.list_view, parent,false);
System.out.println("In");
}
convertView.setMinimumHeight(100);
return convertView;
}
Here i have set ListItem's minimum height to 100;
This is my solution(There is a nested LinearLayout):
<?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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/item"
android:layout_width="match_parent"
android:layout_height="47dp"
android:background="#drawable/box_arrow_top_bg"
android:gravity="center"
android:text="全部收支"
android:textColor="#666"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>

Android Listview - first item is different

For some reason, the text has 0 width in the second row.
Code:
public LinearLayout getView(int position, View convertView, ViewGroup parent){
LinearLayout rowView;
if(convertView==null){
rowView=(LinearLayout) mActivity.getLayoutInflater().inflate(R.layout.icon_list, null);
}else{!
rowView=(LinearLayout) convertView;
}
//Image view
ImageView imageView=(ImageView) rowView.findViewById(R.id.list_icon);
Pair<String,Bitmap> p=getItem(position);
imageView.setImageBitmap(p.second);
//Text view
TextView textView=(TextView) rowView.findViewById(R.id.list_text);
textView.setText("AAAA");
return rowView;
}
XML:
<?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="20dip"
android:id="#+id/icon_list"
android:padding="6dip">
<ImageView android:id="#+id/list_icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:layout_gravity="left"/>
<TextView android:id="#+id/list_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"/>
</LinearLayout>
EDIT:
Before I had accidentally left in the line.
android:src="#drawable/icon"
I removed it, but the problem remains
This isn't a very good answer, but it worked well enough. I set the layout_width and layout_height of Android 45dip. This then caused everything to work properly. I still don't understand how it was allowed to ignore the android:layout_height="20dip" for the section.

Categories

Resources