Create custom ListView with shadow - android

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:

Related

Multiple Clickable Areas in ListItem

I have a list that is organized as follows:
CheckBox,
ImageButton,
LinearLayout{ TextView title, TextView Subtitle}
What I am trying to do is to have three separate clickable areas of the ListItem: the checkbox should be checkable, ImageButton, and the actual list item itself. The checkbox is hidden by default, but can be made visible when a certain button on the list's header is pressed (ie SELECT). I just want a way to select multiple items from the list, and I am not irreversibly set on using hidden checkboxes- if there is a way to do this that would be easier, I would be happy to hear the alternative.
Here is the relevant snippet of my adapter:
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder;
final T t = list.get(position);
if(convertView == null){
convertView = vi.inflate(R.layout.folder_list_item,parent,false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.text_view_folder_item);
holder.subtitle = (TextView) convertView.findViewById(R.id.text_view2);
holder.eye = (ImageButton) convertView.findViewById(R.id.folder_vis_button);
holder.check = (CheckBox) convertView.findViewById(R.id.phone);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(t.getName());
holder.subtitle.setText(t.getDescription());// = (TextView) convertView.findViewById(R.id.text_view2);
return convertView;
}
And the XML is as follows. As you can see, I have made the individual list items clickable but unfocassable, as recommended in posts I have read about this very problem.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:clickable="true"
android:focusable="true">
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:visibility="invisible"
android:focusable="false"/>
<ImageButton
android:id="#+id/folder_vis_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:scaleType="fitXY"
android:layout_toRightOf="#+id/checkbox"
android:contentDescription="#string/contentDescription"
android:focusable="false"
android:src="#drawable/open_eyeball" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_toRightOf="#id/folder_vis_button"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:focusable="false"
android:clickable="true">
<TextView
android:id="#+id/text_view_folder_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/text_view2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Clicking on the list item is handled, if I convert the listitem to actually use on of the built in listitems such as android.R.layout.simple_list_item_1.
However, when I use my own layout, I cannot get click events on the list item itself, just on the image button.
I have tried inflating the view in a different way (int resource, ViewGroup root) ... I have tried working just with the imagebutton, or just with the checkbox, using an imageview instead of an imagebutton, with little in terms of success. Thank you for taking the time to look at this.

Android ListView is slow to create and bind

I (as many other) have problems getting smooth performance in my listview. I believe I have done everything by the book, but it still lags significantly (~300-500 ms)every time a new item is being drawn on the screen.
I'm using a viewHolder and reusing as many UI elements as possible (I think). There are two clear bottle necks.
The first is the line inflating the XML code in the newView() method. That is for some reason really slow (~300 ms) and I don't know if this is expected or not.
Secondly the bindView takes a long time to execute. This is only because of the TextView.setText() method calls which takes up all the time. Other people have had the same problem and I do understand that it can be a slow thing if Android needs to remeasure the entire UI for every listitem. However, after changing the XML file TextViews to a fixed size I still get really slow performance from .setText(). I know I could put the .setText in a Handler but that seems like a hack in this case. I would prevent the list from being laggy, but it would not add the text any faster. I believe I should be able to set the text faster than ~400 ms and if it takes this long something is wrong with my UI/logic.
But without further ado I present you with the source code: Hope to get some clever insight into what I'm doing wrong.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (ApplicationConfiguration.DEBUGGING)
Debug.startMethodTracing("getview");
View listViewItem;
EpisodeViewHolder holder;
Cursor itemCursor = (Cursor) getItem(position);
if (!itemCursor.moveToPosition(position)) {
throw new IllegalStateException("couldn't move cursor to position "
+ position);
}
mCurrentFeedItem = MyListItem.getByCursor(itemCursor);
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case TYPE_EXPAND:
listViewItem = newView(mContext, itemCursor, parent);
break;
default:
listViewItem = newView(mContext, itemCursor, parent);
break;
}
} else {
listViewItem = convertView;
}
bindView(listViewItem, mContext, itemCursor);
switch (type) {
case TYPE_EXPAND:
getPlayerViewHolder(position, listViewItem, itemCursor,
convertView, parent);
}
if (ApplicationConfiguration.DEBUGGING)
Debug.stopMethodTracing();
return listViewItem;
}
This is my newView
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = mInflater.inflate(R.layout.episode_list, null); // here is the bottleneck
EpisodeViewHolder holder = new EpisodeViewHolder();
holder.icon = (ImageView) view.findViewById(R.id.list_image);
holder.mainTitle = (TextView) view.findViewById(R.id.title);
holder.subTitle = (TextView) view.findViewById(R.id.podcast);
holder.timeDuration = (TextView) view.findViewById(R.id.duration);
holder.currentPosition = (TextView) view.getTag(R.id.current_position);
holder.slash = (TextView) view.findViewById(R.id.time_slash);
holder.fileSize = (TextView) view.findViewById(R.id.filesize);
holder.stub = (ViewStub) view.findViewById(R.id.stub);
holder.playerView = (View) view.findViewById(R.id.stub_player);
view.setTag(holder);
return view;
}
and my bindView method is really long but I'll shorten it down a bit
#Override
public void bindView(View view, Context context, Cursor cursor) {
.....
final EpisodeViewHolder holder = (EpisodeViewHolder) view.getTag();
.....
// not a bottleneck
// Loads the image Async. The synchronous part of the call takes around 10%
// of the entire method
PicassoWrapper.load(mContext, i, holder.icon);
// This is the bottleneck
holder.mainTitle.setText(title);
}
XML UI
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="7dip"
android:padding="0dip" >
<ImageView
android:id="#+id/list_image"
style="#style/PodcastImage" />
</LinearLayout>
<TextView
android:id="#+id/title"
style="#style/EpisodeHeaderFont"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Podcast Title" />
<TextView
android:id="#+id/podcast"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_above="#+id/current_position"
android:layout_below="#id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:ellipsize="end"
android:text="Podcast Name"
android:textSize="10dip" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/current_position"
style="#style/EpisodeMinorFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail" />
<TextView
android:id="#+id/time_slash"
style="#style/EpisodeMinorFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/current_position"
android:layout_alignBottom="#+id/current_position"
android:layout_toRightOf="#+id/current_position"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:visibility="gone" />
<TextView
android:id="#+id/duration"
style="#style/EpisodeMinorFont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/time_slash"
android:layout_alignBottom="#+id/time_slash"
android:layout_toRightOf="#+id/time_slash" />
<TextView
android:id="#+id/filesize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/duration"
android:layout_toRightOf="#+id/duration"
android:gravity="bottom|right"
android:text="0"
android:textSize="10dip"
android:textStyle="bold" />
</RelativeLayout>
<FrameLayout
android:id="#id/drag_handle"
android:layout_width="25dp"
android:layout_height="match_parent"
android:paddingBottom="30dp"
android:paddingTop="30dp" >
<LinearLayout
android:layout_width="7dp"
android:layout_height="fill_parent"
android:background="#drawable/drag_handle"
android:orientation="vertical"
android:tileMode="repeat" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
<ViewStub
android:id="#+id/stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="#+id/stub_player"
android:layout="#layout/list_item_expanded" />

Can't change Image source in my listview

I'm working on an e-commerce application, i need to display a list of products -a product = one ImageView and some textViews- from the database, but when i extract the data from the database, everting works fine except the imageView, it shows the same image that is in the source Layout.
this is the getView() Method in my adapter.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView==null)
{
holder=new ViewHolder();
convertView = inflater.inflate(R.layout.lesproduits, null);
holder.nomduProduit = (TextView)convertView.findViewById(R.id.nomProduit);
holder.prixDuProduit = (TextView)convertView.findViewById(R.id.prixProduit);
holder.imageDuProduit = (ImageView)convertView.findViewById(R.id.imageProduit);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
Bitmap bitmapImage = BitmapFactory.decodeFile(path+File.separator+lesProduits.get(position).getImage());
Drawable drawableImage = new BitmapDrawable(bitmapImage);
System.out.println(path+File.separator+lesProduits.get(position).getImage());
holder.imageDuProduit.setBackgroundDrawable(drawableImage);
holder.nomduProduit.setText(lesProduits.get(position).getNomDuProduit());
holder.prixDuProduit.setText(lesProduits.get(position).getPrixDuProduit());
return convertView;
}
and below is the source Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageProduit"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/nomProduit"
android:layout_marginTop="5dp"
android:layout_width="170dp"
android:layout_height="30dp"
android:layout_toRightOf="#+id/imageProduit"
android:text="Smart phone"
android:textSize="25sp" />
<TextView
android:id="#+id/prixProduit"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/nomProduit"
android:layout_below="#+id/nomProduit"
android:layout_marginTop="5dp"
android:text="Large Text"
android:textSize="15sp" />
</RelativeLayout>
You are changing the background, not the foreground.
ImageView has both foregrond and background images.
instead of this
holder.imageDuProduit.setBackgroundDrawable(drawableImage);
use this
holder.imageDuProduit.setImageDrawable(drawableImage);

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

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

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

Categories

Resources