How to set custom list view item's height...? - android

I have a custom list view which contains two text views and on button.
I have customized the button style.
In the list view each row is not fit to show my button properly.
I want to change the list item's height.
How can i do that..?
This is my list view.
<ListView android:layout_width="fill_parent"
android:scrollbars="none" android:footerDividersEnabled="false"
android:minHeight="50dp"
android:listSelector="#00000000" android:dividerHeight="0px"
android:divider="#00000000" android:layout_height="fill_parent"
android:layout_marginLeft="5dp" android:layout_marginTop="5dp"
android:layout_marginRight="5dp" android:id="#+id/list_view"
android:cacheColorHint="#00000000" android:background="#00000000" />
my custom view xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="150dp"
android:layout_height="wrap_content"
android:textColor="#808080" android:text="Subtitle 2" android:id="#+id/text1"
android:layout_marginLeft="5dp"
android:singleLine="true"/>
<TextView android:layout_width="60dp"
android:layout_height="wrap_content"
android:textColor="#808080" android:text="$ 00.00" android:id="#+id/text2"
android:singleLine="true"/>
<Button android:layout_width="80dp" android:layout_height="25dp"
android:background="#drawable/type10_subbg" android:text="Add"
android:textSize="20dp" android:textStyle="bold" android:textColor="#153E7E"
android:id="#+id/add" />
</LinearLayout>
Here is my getView.
#SuppressWarnings("unchecked")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
pos = position;
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.type10_sub, null);
holder.text1 = (TextView) convertView.findViewById(R.id.text1);
holder.text2 = (TextView) convertView.findViewById(R.id.text2);
holder.add = (Button) convertView.findViewById(R.id.add);
holder.add.setTag(String.valueOf(position));
holder.add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button)v;
int tag = Integer.valueOf((String)b.getTag());
System.out.println("Added at "+tag);
System.out.println("Data = "+((HashMap<String,Object>)list.get(tag)).get("text1").toString());
System.out.println("Price = "+((HashMap<String,Object>)list.get(tag)).get("text2").toString()); }
}); holder.text1.setText(((HashMap<String,Object>)list.get(position)).get("text1").toString());
holder.text2.setText(((HashMap<String,Object>)list.get(position)).get("text2").toString());
convertView.setTag(holder);
return convertView;
}
Thanks in advance....!

convertedview.setlayoutparams(new Listview.setlayoutparams(width, height));
First put LinearLayout instead of ListView
and then edit that to ListView otherwise it will not work. This is the trick.

set minHeight in R.layout.type10_sub elements

Its very easy to hardcode in a new height for the custom view simply specify the layout_height for the highest level item, in this case the LinearLayout. Below I've shown how to set it to 100dp
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="100dp">
....
</LinearLayout>

In linear layout give android:layout_height="100dp" instead of wrap_content

use:
if(convertView == null){
convertView = inflater.inflate(R.layout.type10_sub, parent);
}

Related

create adapter for two elements in a same row

I'm trying to create an adapter for a listview, which contains two elements in a same row.
The LayoutFile got two linear LinearLayouts Layout Image with two images and name
The adapter only fill the first LinearLayout data, and don't show the second LinearLayout.
Someone could help me? Thanks in advance !
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="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageViewItem"
android:layout_width="match_parent"
android:layout_height="175dp"
android:src="#drawable/icon" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="left">
<TextView
android:id="#+id/textViewTitleItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="Title"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right">
<TextView
android:id="#+id/textViewPriceItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Price"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
It is only a View, I want to duplicate it in the same row.
Below the adapter:
public ItemAdapter(Activity activity, int layoutResourceId)
{
this.activity = activity;
this.layoutResourceId = layoutResourceId;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var row = convertView;
var currentItem = this[position];
TextView txtViewTitle;
TextView txtViewPrice;
ImageView imgViewItem;
if (row == null)
{
var inflater = activity.LayoutInflater;
row = inflater.Inflate(layoutResourceId, parent, false);
txtViewTitle = row.FindViewById<TextView>(Resource.Id.textViewTitleItem);
txtViewPrice = row.FindViewById<TextView>(Resource.Id.textViewPriceItem);
imgViewItem = row.FindViewById<ImageView>(Resource.Id.imageViewItem);
} else
txtViewTitle = row.FindViewById<TextView>(Resource.Id.textViewTitleItem);
txtViewPrice = row.FindViewById<TextView>(Resource.Id.textViewPriceItem);
imgViewItem = row.FindViewById<ImageView>(Resource.Id.imageViewItem);
txtViewTitle.Text = currentItem.Title;
txtViewPrice.Text = Convert.ToString(currentItem.Price);
return row;
}
It seems that you didn't provide orientation to your parent LinearLayout. By default it is horizontal.
That's why its showing only top layout content.
You need to set it as vertical to show the entire content in your list.
android:orientation="vertical"

Android GridView onItemClickListener doesn't fire

This is my GridView ArrayAdapter:
<?xml version="1.0" encoding="utf-8"?>
<!-- gridview adapter layout -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.coapps.pico"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#drawable/gridview_background_selector" >
<!-- profile picture layout -->
<RelativeLayout
android:id="#+id/fragment_events_adapter_relativelayout_profile_picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp" >
<!-- profile picture image view -->
<ImageView
android:id="#+id/fragment_events_adapter_imageview_profile_picture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:contentDescription="#string/app_name"
android:scaleType="fitXY"
android:src="#drawable/facebook_blank_profile" />
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/background_black_transparent" >
<!-- user name text view -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_events_adapter_textview_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/facebook_blank_profile"
android:textColor="#android:color/white"
app:isBold="true" />
</HorizontalScrollView>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fragment_events_adapter_relativelayout_profile_picture"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:background="#color/orange" >
<HorizontalScrollView
android:id="#+id/fragment_events_adapter_horizontalscrollview_event_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp" >
<!-- event name -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_events_adapter_textview_event_name"
style="#style/text_shadow_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/facebook_blank_profile"
android:textAppearance="#android:style/TextAppearance.Small"
android:textColor="#android:color/white"
app:isBold="true" />
</HorizontalScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fragment_events_adapter_horizontalscrollview_event_name"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="horizontal" >
<!-- event icon -->
<ImageView
android:layout_width="wrap_content"
android:layout_height="15dp"
android:layout_gravity="right"
android:layout_margin="2dp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_action_event" />
<!-- event start time -->
<com.coapps.pico.NillanTextView
android:id="#+id/fragment_events_adapter_textview_start_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:src="#drawable/facebook_blank_profile"
android:textColor="#android:color/black"
android:textSize="12sp"
app:isBold="true" />
</LinearLayout>
</RelativeLayout>
<!-- attending event -->
<ImageView
android:id="#+id/fragment_events_adapter_imageview_attending_mask"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:background="#color/background_black_transparent"
android:contentDescription="#string/app_name"
android:padding="30dp"
android:src="#drawable/v"
android:visibility="gone" />
</RelativeLayout>
and i'm setting an onItemClickedListener:
private void setGridView()
{
//create a new event list
picoEventsList = new ArrayList<PicoEvent>();
//create new gridview arrayadapter
arrayAdapter = new EventDetailsArrayAdapter(getActivity(), picoEventsList);
//set grid's view empty view
View emptyView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_events_empty_view, null);
gridView.setEmptyView(emptyView);
gridView.setOnItemClickListener(this);
gridView.setAdapter(arrayAdapter);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//get clicked event
PicoEvent event = arrayAdapter.getItem(arg2);
//create new event details fragment
EventDetailsFragment eventDetailsFragment = new EventDetailsFragment();
//add the event to fragment's arguments
eventDetailsFragment.setArguments(PicoEventCreator.createBundle(event));
//show the event details fragment
getMainActivity().showFragment(null, eventDetailsFragment, true, true);
}
but the onItemClick() method doesn't get fired..
I've checked and non of my views in my layout has clickable="true" setted...
Any ideas ?
Hey guyz finally got a solution...
what we were doing is directly accessing the Layout inside the GridView, so the onItemClickListener finds it confusing to access the item.
So the solution is to apply the onClickListener inside the Adapter (i.e. normally ArrayAdapter)
so what i m trying to say is:
public View getView(int position, View convertView, ViewGroup parent) {
//Here row is a view and we can set OnClickListener on this
final View row;
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
//Here we inflate the layout to view (linear in my case)
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
row = convertView;
holder = (ViewHolder) row.getTag();
}
ImageItem item = data.get(position);
holder.imageTitle.setText(item.getTitle());
holder.image.setImageBitmap(item.getImage());
//Now get the id or whatever needed
row.setId(position);
// Now set the onClickListener
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Clicked" + row.getId() + "!!",
Toast.LENGTH_SHORT).show();
}
});
return row;
}
Ok i found what the problem was
Apparently GridView and HorzonitalScrollView are not such good friends...
probably because the HorzonitalScrollView has onClick funcionality..
The solution i found is to put:
android:descendantFocusability="blocksDescendants"
in my ArrayAdapter root view.

Create custom ListView with shadow

How could I create custom ListView like shown in this picture. I know I can make this happen with help of ScrollView and Layouts, but I need to do as a ListView.
Each listview item go over each other.
This may help you
List view with custom view items with partially overlaps (Android)
I have this code like your Requirement. List may overlap by reducing the dividerheight to minus value
<?xml version="1.0" encoding="utf-8"?>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="-5dp"
android:listSelector="#drawable/list_selector" />
Then add background color to adapter according to the position.
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.list_row, null);
TextView title = (TextView)vi.findViewById(R.id.title); // title
TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
HashMap<String, String> song = new HashMap<String, String>();
if(position % 2 ==0){
vi.setBackgroundResource(R.drawable.listselector_1);
}else if(position % 3 ==0){
vi.setBackgroundResource(R.drawable.listselector_2);
}else{
vi.setBackgroundResource(R.drawable.listselector_3);
}
song = data.get(position);
// Setting all values in listview
title.setText(song.get(CustomizedListView.KEY_TITLE));
artist.setText(song.get(CustomizedListView.KEY_ARTIST));
duration.setText(song.get(CustomizedListView.KEY_DURATION));
imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
return vi;
}
Otherwise you can use listselector images also as your requirement
Thank you guys for trying help me to solve this issue. I look every answer here and finally get i want. I think it will be better I put my code here so maybe someone it will be helpful. The only difference is that my created listview has also shadow in it.
Here is the code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my_font="http://schemas.android.com/apk/res/com.Helix.android.SmartBonus"
android:orientation="vertical"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
>
<ImageView
android:id="#+id/imageview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shadow_bg"
android:visibility="visible"/>
<RelativeLayout
android:id="#+id/secondLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#drawable/drop_shadow"
android:layout_below="#id/imageview">
<ImageView
android:id="#+id/companyImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/secret_logo_small"
android:layout_marginRight="10dp"/>
<TextView
android:id="#+id/companyDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/companyImageView"
android:textColor="#android:color/white"
android:layout_marginTop="7dp"
my_font:ttf_name="300"
android:text="Салон кросаты"/>
<TextView
android:id="#+id/companyName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/companyImageView"
android:layout_below="#id/companyDescription"
android:textColor="#android:color/white"
my_font:ttf_name="300"
android:text="Secret"/>
<TextView
android:id="#+id/companyPercentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_marginTop="7dp"
android:textSize="19sp"
android:layout_marginRight="1dp"
android:layout_toLeftOf="#+id/companyPercent"
my_font:ttf_name="700"
android:text="-20"/>
<TextView
android:id="#id/companyPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#android:color/white"
android:layout_marginTop="7dp"
android:textSize="12sp"
my_font:ttf_name="300"
android:text="%"/>
<ImageView
android:id="#+id/companyPercentImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/companyPercent"
android:layout_marginTop="7dp"
android:textColor="#android:color/white"
android:src="#drawable/percentage_devider"/>
<TextView
android:id="#+id/companyDistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/companyPercentImage"
android:textSize="16dp"
android:textColor="#A57F1C"
my_font:ttf_name="300"
android:text="7m"/>
<LinearLayout
android:id="#+id/checkingButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/companyDistance"
android:layout_marginTop="10dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:focusable="false"
android:background="#drawable/green_button_bg"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="16dp"
android:textColor="#android:color/white"
my_font:ttf_name="300"
android:text="Check-in"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="16dp"
android:layout_marginLeft="10dp"
android:textColor="#color/checkin_point_color"
android:layout_weight="1"
my_font:ttf_name="300"
android:text="#string/ten_point"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#drawable/slak"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
adapter.
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder viewHolder;
final Discount discount = getItem(i);
Discount prev_discount = null;
if (i > 0){
prev_discount = getItem(i-1);
}
if (view == null){
final LayoutInflater li = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.discount_data_item, viewGroup, false);
viewHolder = new ViewHolder();
viewHolder.logo = (ImageView)view.findViewById(R.id.companyImageView);
viewHolder.name = (SmartBonus_TextView)view.findViewById(R.id.companyName);
viewHolder.description = (SmartBonus_TextView)view.findViewById(R.id.companyDescription);
viewHolder.percent = (SmartBonus_TextView)view.findViewById(R.id.companyPercentText);
viewHolder.distance = (SmartBonus_TextView)view.findViewById(R.id.companyDistance);
viewHolder.mainLayout = (RelativeLayout)view.findViewById(R.id.mainLayout);
viewHolder.secondLayaout = (RelativeLayout)view.findViewById(R.id.secondLayout);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
if (i == 0){
viewHolder.mainLayout.setBackgroundColor(android.R.color.transparent);
setRoundedBackground(viewHolder.secondLayaout, Color.parseColor(discount.getBackgroundColor()));
} else {
viewHolder.mainLayout.setBackgroundColor(Color.parseColor(prev_discount.getBackgroundColor()));
setRoundedBackground(viewHolder.secondLayaout, Color.parseColor(discount.getBackgroundColor()));
}
return view;
}
private void setRoundedBackground(View view,int color){
final GradientDrawable gradientDrawable = (GradientDrawable) view.getBackground().mutate();
gradientDrawable.setColor(color);
gradientDrawable.invalidateSelf();
}
Here is the result
You should override getView method.
Sample code:
View getView(int position, View convertView, ViewGroup parent){
Object data = getItem(positon);
//usually data object should have type property
if(data.type == TYPE1){
return getLayoutInflater().inflate(R.layout.custom_xml1, null);
}else (data.type == TYPE2){
return getLayoutInflater().inflate(R.layout.custom_xml2, null);
}else{
return getLayoutInflater().inflate(R.layout.custom_xml, null);
}
};
Note:You should reuse convertView object for performance.
Ok, As I have sort of time I manage to write few steps to achieve your task.
1. Make Custom drawable using xml in drawable folder with top corners have radius..
2. Now in your Listview attributes just define negative divider height
Like, android:dividerHeight="-20dp"
3. Set the Custom Drawable as a background of List row in getView().
Also its nice if you able to set color to drawable at dynamically for list row. (In getView() of Custom Adapter).
This what I have achieved from above steps:

Center TextViews vertically in RelativeLayout when one or more has visibility set to GONE

I am trying to create a list item layout with 4 items. It is very similar to the example here with some key differences:
the RelativeLayout layout_height may be larger than listPreferredItemHeight
the image is a CheckBox
there are three TextViews
The top and middle TextViews could have visibility set to GONE
What I would like is: If _label is gone, then _message and _definition should be evenly weighted in the layout. If _label and _message are gone, _definition should be centered. I have tried several variations of height, width, gravity, layout_gravity and I even tried to solve this with a LinearLayout (both as the whole layout and just to contain the vertical text). The only way I can get this to work is if I set layout_height to listPreferredItemHeight, but then only two of the text views fit.
<?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="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight">
<CheckBox android:id="#+id/alarm_list_item_able"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="True"
android:layout_alignParentLeft="True"
android:layout_alignParentBottom="True"
android:layout_centerVertical="True"
android:focusable="false"
android:layout_marginRight="6dp"
android:onClick="onClick"
/>
<TextView android:id="#+id/alarm_list_item_label"
android:text="Label place holder"
android:singleLine="True"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/alarm_list_item_able"
android:layout_alignParentTop="True"
android:gravity="center_vertical"
/>
<TextView android:id="#+id/alarm_list_item_message"
android:text="Message place holder"
android:singleLine="True"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/alarm_list_item_label"
android:layout_toRightOf="#id/alarm_list_item_able"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
/>
<TextView android:id="#+id/alarm_list_item_location"
android:singleLine="True"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/alarm_list_item_message"
android:layout_toRightOf="#id/alarm_list_item_able"
android:layout_marginBottom="5dp"
android:layout_alignWithParentIfMissing="true"
android:layout_alignParentRight="true"
android:paddingBottom="5dp"
android:text="(0.000, 0.000) 0m"
android:gravity="center_vertical"
/>
</RelativeLayout>
Adapter getView Code using the xml from blessenm's answer
#Override
public View getView(int position, View convertView, ViewGroup parent){
Log.d(Global.TAG,"Position "+position);
View newView;
if (convertView != null )
newView = convertView;
else {
LayoutInflater layout = LayoutInflater.from(MyActivity.this);
newView = layout.inflate(R.layout.alarm_list_item, null);
}
String tmp1 = "Place Holder Label"
TextView label = (TextView)newView.findViewById(R.id.alarm_list_item_label);
LayoutParams l = label.getLayoutParams();
if (tmp1.matches("^\\s*$")){
label.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,0f));
} else {
label.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,1f));
}
label.setText(tmp1)
String tmp2 = "Place Holder Message"
TextView message = (TextView)newView.findViewById(R.id.alarm_list_item_message);
l = message.getLayoutParams();
if (tmp2.matches("^\\s*$")){
message.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,0f));
} else {
message.setLayoutParams(new LinearLayout.LayoutParams(l.width,l.height,1f));
}
message.setText(tmp2);
TextView location = (TextView)newView.findViewById(R.id.alarm_list_item_location);
location.setText("location");
return newView;
}
Layout_gravity will only work inside a linear layout. When you want all your textviews to take even height, put them in a linearlayout with vertical orientation and give weight as 1 for all the textviews. So now all of them will take equal heights.
When you need to hide the textviews, just set their layout_weight to 0, now the remaining textview will take the full available space. And if you give the textview gravity="center", the text will be centered both vertically and horizontally.
Edit: The xml and getView method
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox android:id="#+id/alarm_list_item_able"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_marginRight="6dp"
android:onClick="onClick"
android:layout_centerVertical="true"/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/alarm_list_item_able"
android:orientation="vertical"
android:layout_centerVertical="true">
<TextView android:id="#+id/alarm_list_item_label"
android:text="Label place holder"
android:singleLine="True"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/alarm_list_item_message"
android:text="Message place holder"
android:singleLine="True"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/alarm_list_item_able"
android:layout_below="#id/alarm_list_item_label"/>
<TextView android:id="#+id/alarm_list_item_location"
android:singleLine="True"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="(0.000, 0.000) 0m"
android:layout_toRightOf="#id/alarm_list_item_able"
android:layout_below="#id/alarm_list_item_message"/>
</LinearLayout>
</RelativeLayout>
The getview method
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null){
convertView = inflater.inflate(R.layout.list, null);
holder = new ViewHolder();
holder.label = (TextView)convertView.findViewById(
R.id.alarm_list_item_label);
holder.msg = (TextView)convertView.findViewById(
R.id.alarm_list_item_message);
holder.loc = (TextView)convertView.findViewById(
R.id.alarm_list_item_location);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(holder != null){
if(data.get(position).mLabel.equals("")){
LinearLayout.LayoutParams labelLp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,0);
if(!data.get(position).mMsg.equals("")) {
holder.label.setLayoutParams(labelLp);
holder.msg.setText(data.get(position).mMsg);
holder.loc.setText(data.get(position).mLoc);
} else {
holder.label.setLayoutParams(labelLp);
holder.msg.setLayoutParams(labelLp);
holder.loc.setText(data.get(position).mLoc);
}
} else {
holder.label.setText(data.get(position).mLabel);
holder.msg.setText(data.get(position).mMsg);
holder.loc.setText(data.get(position).mLoc);
}
}
return convertView;
}

Android's RelativeLayout seems broken

I'm working on a layout where I use a ListView with RelativeLayout line items. The lineitems themselves are not displaying correctly.
The issue is that the txtVideoDuration TextView is drawn at the top of the line item instead of the bottom. Because of this the txtVideoTitle gets a height of 0. As you can see in the XML the txtVideoDuration is supposed to be clamped to the bottom of the ListView.
My goal is to have the layout resemble the tutorial that google gave me. Example: http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
I'm using Android 2.0.1. I've even plugged in the example layout with out modification into the ListView and the same behavior happens.
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/imgVideoThumbnail"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:minHeight="64dip"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<TextView
android:id="#+id/txtVideoDuration"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_toRightOf="#+id/imgVideoThumbnail"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee"
android:text="0:00:00" />
<TextView
android:id="#+id/txtVideoTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgVideoThumbnail"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="#+id/txtVideoDuration"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="[Title]"
android:textColor="#FFFFFF" />
</RelativeLayout>
This simple case works. This is the root XML layout for the activity. The buggy code at the top is the line items in the list view. It seems that the ListView itself is breaking it.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listVideoCatalog"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hello World" />
</RelativeLayout>
</merge>
This is the getView Code that inflates the line item into the ListView.
static class ViewHolder
{
TextView txtTitle;
ImageView imgThumbnail;
TextView txtDuration;
Uri videoLocation;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
LayoutInflater li = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.listitem_videolineitem, null);
holder = new ViewHolder();
holder.txtDuration = (TextView) convertView.findViewById(R.id.txtVideoDuration);
holder.txtTitle = (TextView) convertView.findViewById(R.id.txtVideoTitle);
holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.imgVideoThumbnail);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
VideoFile video = this.videos.get(position);
holder.txtDuration.setText(millisToTimestamp(video.videoDuration));
holder.txtTitle.setText(video.videoTitle);
// Some debugger visual code.
holder.txtDuration.setBackgroundColor(Color.GREEN);
holder.txtTitle.setBackgroundColor(Color.CYAN);
return convertView;
}
This was answered in the comment of the accepted answer. Just to make it clear, this is the answer:
Change
li.inflate(R.layout.listitem_videolineitem, null);
to
li.inflate(R.layout.listitem_videolineitem, parent, false);
How are you inflating your row views that are "buggy"? Can you provide a code snippet?
Also, android:orientation="vertical" is not valid for RelativeLayout.

Categories

Resources