List view updates view which are not touched - android

Sorry for the bad title.
I am adding a toggle button at run-time to the list view.
When I click on the button its state gets changed. But the problem is that when I scroll down other items get selected too.
For example, if click on first toggle button the 6th toggle button gets checked too.
This is a very weird behavior.
Here is the XML code for the acitivty
<?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"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/title_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/eros_now_title_bar">
<ImageView
android:id="#+id/back_button_image"
android:layout_width="#dimen/toolbar_imageview_width_height"
android:layout_height="#dimen/toolbar_imageview_width_height"
android:layout_centerVertical="true"
android:layout_margin="#dimen/back_image_margin"
android:src="#drawable/ic_arrow_back" />
<ImageButton
android:id="#+id/back_button"
android:layout_width="#dimen/toolbar_imagebutton_width_height"
android:layout_height="#dimen/toolbar_imagebutton_width_height"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:background="#drawable/npd_back_feedback" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/back_button_image"
android:layout_toRightOf="#+id/back_button"
android:fontFamily="sans-serif-regular"
android:text="Invite friends"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
<EditText
android:id="#+id/search_text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/title_bar"
android:layout_margin="15dp"
android:hint="Search"
android:textColorHint="#color/hint_color"
android:drawableRight="#drawable/search_blue_white"/>
<ListView
android:id="#+id/friends_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/search_text"
android:layout_above="#+id/invite_count_view"
android:layout_marginTop="15dp"
android:divider="#color/transparent">
</ListView>
<RelativeLayout
android:id="#+id/invite_count_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textSize="18sp"
android:textColor="#color/eros_now_title_bar"
android:text="10 more invites to go"
android:layout_centerInParent="true"
/>
</RelativeLayout>
</RelativeLayout>
Here is the XML code for the view I am inflating in the adapter
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<com.makeramen.roundedimageview.RoundedImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/person_pic"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="26dp"
android:layout_centerVertical="true"
app:riv_corner_radius="50dip"
android:src="#drawable/ic_person_grey600_36dp"/>
<TextView
android:id="#+id/name"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_toRightOf="#+id/person_pic"
android:layout_marginLeft="17dp"
android:layout_centerVertical="true"
android:textColor="#color/black"
android:fontFamily="sans-serif-regular"
android:textSize="16sp"
android:text="Grumpy brother"
android:ellipsize="end"/>
<!--<ToggleButton-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_alignParentRight="true"-->
<!--android:layout_marginRight="35dp"-->
<!--android:layout_centerVertical="true"-->
<!--android:background="#drawable/invite_selector"-->
<!--android:textOn=""-->
<!--android:textOff=""-->
<!--android:text="" />-->
</RelativeLayout>
here is the code for getView
public View getView(int i, View convertView, ViewGroup parent) {
Log.e("getView", "name = " + contacts.get(i).getName());
if (convertView == null) {
convertView = LayoutInflater.from(activity).inflate(R.layout.friend_view, parent, false);
}
TextView name = (TextView) convertView.findViewById(R.id.name);
RoundedImageView image = (RoundedImageView) convertView.findViewById(R.id.person_pic);
ToggleButton selector = new ToggleButton(activity);
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN)
selector.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.ic_add_black_24dp));
else
selector.setBackground(activity.getResources().getDrawable(R.drawable.ic_add_black_24dp));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.ALIGN_PARENT_END, RelativeLayout.TRUE);
params.setMargins(0, 0, getPx(35), 0);
selector.setLayoutParams(params);
selector.setTextOff("");
selector.setText("");
selector.setTextOn("");
selector.setOnCheckedChangeListener(this);
selector.setTag(String.valueOf(i));
selector.setChecked(contacts.get(i).getSelectedState());
contacts.get(i).setButton(selector);
RelativeLayout card = (RelativeLayout) convertView.findViewById(R.id.card_view);
card.addView(selector);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), contacts.get(i).getURI());
} catch (IOException e) {
contacts.get(i).setURI(Uri.parse(""));
}
name.setText(contacts.get(i).getName());
if (!contacts.get(i).getURI().toString().equals(""))
image.setImageURI(contacts.get(i).getURI());
else
image.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_person));
return convertView;
}

I saw that you used contacts.get(i).getSelectedState() to set ToggleButton state in getview.
Did you use Adapter.notifyDatasetChanged() to update the listview's state?
If not, add that code after toggle the button.

Because your item view is reused so only create ToggleButton if convertView is null. If convertView is not null, get it from convertView and set it's properties.
Use setTag and findViewWithTag to identity your ToggleButton.

Related

Custom ListView Items are not clickable

I am working on an application which contains Custom ListView on its main activity. It was working fine but the day before yesterday, list view items are suddenly become non-clickable. I've searched a lot, but no answer solved my this issue. It is strange. Normal list view is working good, but custom isn't. What may be the problem?
Edit: Custom List View is as normal as it is, nothing odd. I am giving here CustomAdapter class.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.custom_list, null);
}
ClassDetailPedia detailPedia = personDetail.get(position);
if(detailPedia !=null){
TextView txtPersonName = (TextView) convertView.findViewById(R.id.txtPersonName);
TextView txtReason = (TextView) convertView.findViewById(R.id.txtReason);
TextView txtTags = (TextView) convertView.findViewById(R.id.txtTags);
TextView txtDate = (TextView) convertView.findViewById(R.id.txtDate);
ImageView imgPerson = (ImageView) convertView.findViewById(R.id.imgPerson);
txtPersonName.setText(detailPedia.getTitle());
txtReason.setText("Description: " + detailPedia.getDescription());
txtTags.setText("Tags: " + detailPedia.getTags());
txtDate.setText(GetFormattedDate(detailPedia.getDated()));
if(detailPedia.getImage() != null) {
imgPerson.setImageURI(detailPedia.getImage());
imgPerson.setScaleType(ImageView.ScaleType.CENTER);
}
}
return convertView;
}
and it extends ArrayAdapter<ClassDetailPedia>
And here I am setting CustomAdapter
ListView lstPersons = (ListView) findViewById(R.id.lstDetail);
lstPersons.setAdapter(new CustomAdapter(getApplicationContext(), R.layout.custom_list, pediaList));
custom_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape_list"
android:gravity="center">
<ImageButton
android:layout_width="80dp"
android:layout_height="60dp"
android:src="#drawable/empty_picture"
android:id="#+id/imgPerson"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="2dp">
<TextView
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Friday, April 1, 2016 # 8:30pm"
android:id="#+id/txtDate"
android:textStyle="italic"
android:textSize="12sp"
android:layout_below="#+id/txtTags"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="3dp"
android:layout_marginRight="3dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Mr. Bill Gates"
android:id="#+id/txtPersonName"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="3dp"
android:singleLine="true"
android:textSize="17sp"
android:layout_marginBottom="3dp"
android:textStyle="bold"/>
<TextView
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reason: Marketing Strategy"
android:id="#+id/txtReason"
android:layout_below="#+id/txtPersonName"
android:layout_alignParentStart="true"
android:layout_marginStart="3dp"
android:singleLine="true"
android:layout_marginTop="-3dp"
android:textSize="13sp"/>
<TextView
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tags: business, start-up"
android:id="#+id/txtTags"
android:layout_below="#+id/txtReason"
android:layout_alignParentStart="true"
android:layout_marginStart="3dp"
android:singleLine="true"
android:textSize="13sp"/>
</RelativeLayout>
</LinearLayout>
Try add android:descendantFocusability="blocksDescendants" for the root view of your custom list item (in your case it is LinearLayout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape_list"
android:gravity="center"
android:descendantFocusability="blocksDescendants"
>
...
</LinearLayout>
I made new Application, copied and pasted all the files and code and it's working fine. I don't know what caused the problem and why it is working perfectly know.

Android: Unable to setText in Custom ListView

I have created a custom ListView with 2 ImageView and a TextView.
In the Adapter, I'm doing setText in TextView, but it is not visible in the app.
Here is my List:
<?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:background="#ffffff">
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#289FA5"/>
<RelativeLayout
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#289FA5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Popular products"
android:textSize="23sp"
android:padding="7dp"/>
</RelativeLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="120dp">
<LinearLayout
android:id="#+id/pop_pro_men"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:layout_marginTop="15dp"
android:layout_marginLeft="7dp"
android:layout_marginBottom="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#289FA5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Catagories"
android:textSize="23sp"
android:padding="7dp"/>
</RelativeLayout>
<ListView
android:id="#+id/cata_men"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
Here is my Custom List:
<LinearLayout
android:id="#+id/custom_list"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:background="#ffffff">
<ImageView
android:layout_gravity="left"
android:id="#+id/imageView2"
android:layout_height="130dp"
android:layout_marginLeft="7dp"
android:layout_width="120dp"
android:src="#drawable/clothing_men"/>
<TextView
android:id="#+id/cata_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clothing"
android:layout_marginTop="35dp"
android:textStyle="bold"
android:textSize="27sp"
android:padding="10dp"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/forward_arrow"
android:layout_marginTop="42dp"/>
And Here is the getView method of my Adapter:
public View getView(final int position, View convertView, ViewGroup parent) {
final Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.custom_subcatagory_list, null,true);
TextView sub_catagory= (TextView) rowView.findViewById(R.id.cata_name);
ImageView pic = (ImageView) rowView.findViewById(R.id.imageView2);
sub_catagory.setText(catagory[position]);
pic.setImageResource(image[position]);
Log.d("Strings========", sub_catagory.getText().toString());
rowView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked " + catagory[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
Please Help as i am unable to find what is wrong in this.
Remove your layout_weight attribute from your TextView. If you remove sub_catagory.setText(catagory[position]); does "Clothing" appear when you run your app?
I got the answer now. I don't know how my textColor was set to White, because i didn't mentioned it. So, change the color to something else and it worked.And it wasted my whole day.
Sorry to disturb you all.

How to make the listView row height change based on content?

I have the following layout which corresponds to a row in a list view. I wanted the TextView in the middle to expand based on the length of text.
With the below layout, my textview is not even visible. Can someone point out what mistake I am doing?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="3dip"
android:paddingLeft="6dip"
android:paddingRight="6dip" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/batman" />
<LinearLayout
android:id="#+id/user_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView1"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/display_name_color"
android:textSize="14dp" />
<TextView
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/imageView1"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/favourite"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="40dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/bookmark"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="60dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<TextView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/actions"
android:layout_below="#+id/user_info"
android:layout_toRightOf="#+id/imageView1"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
and this is my adapter's getView function.
#Override
public View getView(int position, View convertView, ViewGroup container) {
ImageView imageView;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_row, container, false);
imageView = (ImageView)convertView.findViewById(R.id.imageView1);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView)convertView.findViewById(R.id.imageView1);
}
imageView.setTag(position);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int position = Integer.parseInt(v.getTag().toString());
mOnProfilePicClickCallback.onItemClick(position);
}
});
//Load TextFields here
Feed feed = (Feed) getItem(position);
User user = null;
if (feed != null) {
user = (User) CommonData.users.get(feed.owner);
TextView handle = (TextView) convertView.findViewById(R.id.handle);
TextView userName = (TextView) convertView.findViewById(R.id.username);
TextView feedContent = (TextView) convertView.findViewById(R.id.feedcontent);
handle.setText(feed.owner);
if(tweeter!=null && !TextUtils.isEmpty(user.displayname))
{
userName.setText(user.displayname);
}
else
{
userName.setText(feed.owner);
}
feedContent.setText(feed.feedcontent);
}
// Finally load the image asynchronously into the ImageView, this also takes care of
// setting a placeholder image while the background thread runs
if(user!=null)
{
mImageFetcher.loadImage(user.profileimageurl, imageView);
}
return convertView;
}
Try this...
Remove android:layout_above="#+id/actions"from your "content" TextView
Add android:layout_below="#+id/content" to your "actions" LinearLayout
Remove android:layout_alignParentBottom="true"from "actions"
It may work, if it still doesn't use LinearLayouts instead of RelativeLayout.
In my ListView, every row height changes automaticly according the data.

Views in list are not at the corrects size

I have a list view and an adapter. My problem is that the rows in the list are always "wrap content" even though I specifically stated the height as "150dp".
This is my adapter:
private class LiveEventsAdapter extends BaseAdapter {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View contentView = convertView;
if (contentView == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
contentView = inflater.inflate(R.layout.live_match_row, null);
TypefaceManager.getInstance(LiveMatchActivity.this).assignTypeface(contentView);
}
if (events != null) {
Event event = events.getEventsList().get(position);
TypefaceManager typefaceManager = TypefaceManager.getInstance(LiveMatchActivity.this);
TextView eventText;
TextView minute;
ImageView eventIcon;
minute = (TextView) contentView.findViewById(R.id.live_match_minute);
if (event.getOrientation().equals("home")) {
eventText = (TextView) contentView.findViewById(R.id.home_team_event_text);
eventIcon = (ImageView) contentView.findViewById(R.id.home_team_event_img);
} else {
eventText = (TextView) contentView.findViewById(R.id.away_team_event_text);
eventIcon = (ImageView) contentView.findViewById(R.id.away_team_event_img);
}
minute.setText(event.getMinute() + "\'");
eventText.setText(event.getDescription());
minute.setTypeface(typefaceManager.getTypeface("bold"));
eventText.setTypeface(typefaceManager.getTypeface("bold"));
}
}
That's how I "setadapter":
ListView eventsListView = (ListView) findViewById(R.id.live_match_list);
eventsListView.setAdapter(new LiveEventsAdapter());
and that's my rows 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="150dp"
android:background="#drawable/background_button" >
<TextView
android:id="#+id/live_match_minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="82&apos;" />
<TextView
android:id="#+id/home_team_event_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="false"
android:layout_alignRight="#+id/home_team_event_img"
android:layout_centerVertical="true"
android:paddingLeft="4dp"
android:paddingRight="4dp" />
<ImageView
android:id="#+id/home_team_event_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginRight="4dp"
android:layout_toLeftOf="#+id/live_match_minute" />
<ImageView
android:id="#+id/away_team_event_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_centerVertical="true"
android:layout_marginLeft="4dp"
android:layout_toRightOf="#+id/live_match_minute" />
<TextView
android:id="#+id/away_team_event_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="false"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/away_team_event_img"
android:paddingLeft="4dp"
android:paddingRight="4dp" />
Why is it that the rows in my list are always set on the same size, no matter what I put in?
This will NOT work.
This is what you should do.
<?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="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/background_button" >
<TextView
android:id="#+id/live_match_minute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="82&apos;" />
</RelativeLayout>
</RelativeLayout>
Basically, add another layout and specify the height to that layout.
When working with adapter views specifying a height for the root layout doesn't work.

How to center align a textview below imageview as a title in a List view

I am not able to align a text below imageview in a listview. below is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="50dp" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/icon"
android:layout_alignParentBottom="true"
android:gravity="center"
android:singleLine="true"
android:layout_marginLeft="20dp"
android:textColor="#color/white"/>
</RelativeLayout>
and ListView is
<ListView android:id="#+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="280sp"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/imgView"
android:scrollbars="none"
android:divider="#null"
android:gravity="center" >
</ListView>
java code to dynamically add text and image from array
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.gridview_layout, null);
holder = new ViewHolder();
holder.imageName = (TextView) convertView.findViewById(R.id.name);
holder.image = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageName.setText(name[position]);
holder.image.setImageResource(mThumbIds[position]);
return convertView;
}
not getting how to center text below imageview. please help me to solve this problem.
Simply use the layout_centerHorizontal attribute in your TextView:
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/icon"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:textColor="#color/white" />
In textview change
android:layout_width="wrap_content"
to
android:layout_width="match_parent"
You should use Linear layout to set orientation attribute. Set orientation to vertical for Linear layout and set imageview attribute android:layout_gravity="center"
Here is the complete code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
>
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/home" >
</ImageView>
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cardiology"
android:layout_gravity="center"
android:layout_marginTop="5px"
android:textSize="20sp" >
</TextView>
</LinearLayout>
hope it will help

Categories

Resources