Android Listview - first item is different - android

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.

Related

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.

ImageView and Listview - Android

Good Day!!!
I have a problem with the layout specially in the landscape orientation in android.
When I am going to change the orientation to landscape, it seems that the ImageView is in a fixed position and I can only see scrollbar in the listview...I just want to have a layout that also the imageview can be scrolled so that I can see clearly the list items.
Here's my layout xml
<?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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name"
android:src="#drawable/banner" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/imageView1"
android:drawSelectorOnTop="true" />
</RelativeLayout>
This is a bit tricky because you can't put a listview inside a scrollview but you can make it work if you create a custom adapter which can have different types of rows (e.g. A row with text only, a row with an ImageView, etc.).
Then have your first row in that ListView always be an image.
Check out this tutorial (Section: Different list items’ layouts) to learn how to make different types of rows.
I think using ListView in ScrollView is not a good thing If u want that image should also scroll than do like this:
1) For Listview you need a custome adapter in which in custom adapter class's getview() method u have to something like this
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.listview_rowlayout, parent, false);
TextView textview = (TextView) row.findViewById(R.id.tv_list);
ImageView imageview = (ImageView) row.findViewById(R.id.iv_list);
textview.setText(data_text[position]);
if(position==0){
imageview.setImageResource(R.drawable.YourimageName);
}else{
imageview.setVisibility(View.GONE);
}
return (row);
}
where your listview_rowlayout.xml is something like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/iv_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParenttop="true"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/tv_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#000000"
android:layout_alignBottom="#+id/imageView1"
android:text="" />
</RelativeLayout>
and your main.xml in which u have added listview should be something like this
<?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" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="true" />
</RelativeLayout>

It is possible to create a GridVIew of LinearLayouts?

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;
}

LinearLayout problem

I'm trying to create a layout containing, among other things, a LinearLayout. The XML for the whole screen is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/fileSelView"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner android:id="#+id/dirListSpinner"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<Spinner android:id="#+id/fileTypeSpinner"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<EditText android:id="#+id/fileNameTF"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/fileTypeSpinner"/>
<LinearLayout android:id="#+id/centerBox"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/dirListSpinner"
android:layout_above="#+id/fileTypeSpinner"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
<ListView android:id="#+id/dirView" android:background="#f00"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1"/>
<RelativeLayout android:id="#+id/buttonBox" android:background="#0f0"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_weight="0">
<Button android:id="#+id/upButton"
android:text="Up"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button android:id="#+id/mkdirButton"
android:text="MkDir"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/upButton"
android:layout_centerHorizontal="true"/>
<Button android:id="#+id/okButton"
android:text="OK"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/mkdirButton"
android:layout_centerHorizontal="true"/>
<Button android:id="#+id/cancelButton"
android:text="Cancel"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="#+id/okButton"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
The result of this layout looks like this:
The LinearLayout itself is laid out the way I want, within its parent, but its contents come out all wrong. It has two children: a ListView on the left and a RelativeLayout on the right. The ListView should take up all the available height, and as much width as possible, while the RelativeLayout should be a small as possible and vertically centered within its parent. Instead, the ListView ends up being way too narrow, and the RelativeLayout grows to fill the space, despite the ListView having android:layout_weight="1" and the RelativeLayout having android:layout_weight="0". Also, the RelativeLayout is aligned with the top of its parent, despite having android:gravity="center_vertical".
What am I doing wrong?
UPDATE
OK, I changed android:gravity="center_vertical" to android:layout_gravity="center" on the RelativeLayout, and now it is vertically centered within its parent, as desired.
Regarding the layout weight issue, I tried changing android:layout_width="fill_parent" to android:layout_width="0px" on the ListView, but that didn't work; I'm getting the same result as before, with the ListView way too narrow and the RelativeLayout expanding to take up the available space.
The layout now looks like this: http://thomasokken.com/layout-problem2.png
Note that the buttons in the RelativeLayout are not correctly centered horizontally. It's as if the RelativeLayout got sized and laid out correctly at first, and then grew towards the left later, without re-laying out its children.
I haven't been able to get the ListView to get sized properly using a RelativeLayout parent, either. Could it be resizing itself in response to a setAdapter() call? I'm using a custom ListAdapter class whose getView() method returns RelativeLayout objects:
public View getView(int position, View convertView, ViewGroup parent) {
File item = items[position];
if (convertView == null) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.file_selection_dialog_row, null);
ImageView icon = (ImageView) convertView.findViewById(R.id.fdrowimage);
icon.setImageResource(item.isDirectory() ? R.drawable.folder : R.drawable.document);
}
TextView text = (TextView) convertView.findViewById(R.id.fdrowtext);
text.setText(item.getName());
return convertView;
}
The layout for the list rows looks like this:
<?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">
<ImageView android:id="#+id/fdrowimage"
android:layout_height="35dp" android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:paddingRight="5dp" android:paddingLeft="3dp"/>
<TextView android:id="#+id/fdrowtext"
android:layout_width="wrap_content" android:layout_height="35dp"
android:layout_toRightOf="#+id/fdrowimage"
android:layout_alignTop="#+id/fdrowimage"
android:layout_alignBottom="#+id/fdrowimage"
android:gravity="center_vertical"
android:textSize="23dp"/>
</RelativeLayout>
There are a few things going on here.
First as to the vertical centering of the RelativeLayout. android:gravity="center_vertical" indicates that the children of this view should have center_vertical applied. And it is actually working. As you can see by the size of the green background, your RelativeLayout is only as big as it needs to be to fit the buttons. You have two solutions. If you want the height of the view to stay the same and be centered inside its parent, you would use android:layout_gravity="center". If you want the RelativeLayout to fill the column then you need to set the layout_height of the RelativeLayout to be "fill_parent". android:layout_gravity applies to the view itself inside its parent. android:gravity applies to the view's children.
Second is the layout weight issue. The LinearLayout will first layout any wrap_content items (ie, your RelativeLayout), then it will apply children that have a layout_weight AND a size of 0. If you want your layout_weight to work properly, you need to set the layout_width of the ListView to "0px".

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>

Categories

Resources