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.
Related
i am using below xml for my ListView item layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/contact_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Feel The Light" />
<TextView
android:id="#+id/contact_item_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
</LinearLayout>
i know in this xml some views are overlapping each other but in my custom array adapter i make visibility to gone for some of them based on certain condition, but what this xml do is to disable my ListView onItemClickListener but if i exclude this from xml then onItemClickListener works fine but i want to make onItemClickListener work without removing xml
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_audio_call_dark"
android:id="#+id/btn_audio_call"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_user_video_call_dark"
android:id="#+id/btn_video_call"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="30dp"
android:text="Invite"
android:background="#drawable/selector_button_green_oval"
android:textColor="#color/white"
android:id="#+id/btn_invite"/>
</RelativeLayout>
In your adapter getView(), set onClickListener to each button.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
If you want to handle click event for entire list item then you need to set onClickListener to root LinearLayout of your listViewItem XML:
ListViewItem XML: Give an id to LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp"
android:id="#+id/container">
<ImageView
android:id="#+id/contact_item_icon"
android:layout_width="50dp"
android:layout_height="50dp"/>
.................
...................
Adapter: Set OnClickListener to LinearLayout
#Override
public View getView(int position, View convertView, ViewGroup parent) {
.............
.................
LinearLayout container = (LinearLayout) convertView.findViewById(R.id.container);
container.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do something
}
}
.................
...................
}
Hope this will help!
I make a CustomAdapter extends BaseAdapter.In it's getView() method , I use ViewHolder. And I set a clickListener with a TextView to set a view (call it A)gone and another view (call it B)visible , but when I click the TextView , the A GONE but it leave a space ,so the B cannot match the parent.
My code like
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null){
convertView = mLayoutInflater.inflate(R.layout.customlayout,parent,false);
viewHolder = new ViewHolder();
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView = (TextView) convertView.findViewById(R.id.textview);
viewHoler.a = (LinearLayout) convertView.findViewById(R.id.a);
viewHoler.b = (LinearLayout) convertView.findViewById(R.id.b);
viewHolder.textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (viewHolder.a.getVisibility() == View.GONE){
viewHolder.b.setVisibility(View.GONE);
viewHolder.a.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}else {
viewHolder.a.setVisibility(View.GONE);
viewHolder.b.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
}
});
convertView.setTag(viewHolder);
return convertView;
}
the customlayout code is
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button a"/>
</LinearLayout>
<LinearLayout
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button b"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
It should be show like this way when I click the textView
but it always like another way below , just like the A view still take the space ,that like call setVisibility(View.INVISIBLE) or not setVisibility(View.GONE)
the A view is not appear because that although B view has disappeared but it still take the space
Why will it behave like that? How should I do to solve it ?
Thank you for your help.
Try with wrap_content on your LinearLayouts.
Like this -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button a"/>
</LinearLayout>
<LinearLayout
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button b"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Hey guys I'm newbie at Android development, and I would like to build a sub item for each listview item.
Just like this:
ListView 1
sub item 1
ListView 1
sub item 1
I though that it would be esier to create a new relative layout for each listview item, so my XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrap_layout"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/first_col"
android:layout_width="fill_parent"
android:layout_height="200dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/bg_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/overlay_bg" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="9dp" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/current_event"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:letterSpacing="1.2"
android:shadowColor="#color/dark_grey"
android:shadowDx="-2"
android:shadowDy="2"
android:shadowRadius="0.01"
android:textColor="#color/white"
android:textSize="22sp" />
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/persons" />
<TextView
android:id="#+id/current_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_alignParentBottom="true"
android:text="TextView"
android:textColor="#color/grey"
android:textSize="13sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/second_col"
android:layout_width="fill_parent"
android:layout_marginTop="200dp"
android:layout_height="200dp" >
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="24dp"
android:text="TextView" />
</RelativeLayout>
</RelativeLayout>
Each item is divided by two columns, the second column should be hidden column, thats why I changed the layout height to 200dp.
And to show the full layout, I'm doing that:
public View getView(final int position, View convertView, ViewGroup parent) {
// here you let SimpleAdapter built the view normally.
final View v = super.getView(position, convertView, parent);
final RelativeLayout lebg = (RelativeLayout) v.findViewById(R.id.wrap_layout);
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
}
});
return v;
}
I just need to change the height from 200 to 400 to show the full Listview item.
The issue:
Is not working that well because when I click at the listview it only changes the height when I scroll...
Is there any better way to do what I am trying?
Thanks.
When you change LayoutParams you have to call requestLayout to see changes:
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
requestLayout();
}
});
I am facing a really strange behaviour where I set an onClickListener on checkboxes and their labels, within a custom adapter.
The problem is when I check an answer on the first question, the same item on the 3rd question is checked too. The checkboxes seems to be synchronised, but i don't understand why.
Here is some screens to understand.
1rst question
3rd question
Here is the getView() from my adapter:
public View getView(final int position, View convertView, ViewGroup parent)
{
View vi = convertView;
if(convertView == null)
{
vi = inflater.inflate(R.layout.question_item, null);
}
Qcm qcm = qcms.get(position);
List<Proposition> propositions = qcm.getPropositionsList();
TextView numQuestion = (TextView)vi.findViewById(R.id.question_number_item);
TextView question = (TextView)vi.findViewById(R.id.question_text);
LinearLayout[] labels = new LinearLayout[5];
labels[0] = (LinearLayout)vi.findViewById(R.id.question_label1);
labels[1] = (LinearLayout)vi.findViewById(R.id.question_label2);
labels[2] = (LinearLayout)vi.findViewById(R.id.question_label3);
labels[3] = (LinearLayout)vi.findViewById(R.id.question_label4);
labels[4] = (LinearLayout)vi.findViewById(R.id.question_label5);
final CheckBox[] checkBoxes = new CheckBox[5];
checkBoxes[0] = (CheckBox)vi.findViewById(R.id.question_check1);
checkBoxes[1] = (CheckBox)vi.findViewById(R.id.question_check2);
checkBoxes[2] = (CheckBox)vi.findViewById(R.id.question_check3);
checkBoxes[3] = (CheckBox)vi.findViewById(R.id.question_check4);
checkBoxes[4] = (CheckBox)vi.findViewById(R.id.question_check5);
numQuestion.setText(String.valueOf(position+1));
question.setText(Html.fromHtml(qcm.getQuestion()));
for(int i=0; i<5; i++)
{
final int cpt = i;
checkBoxes[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Checked[i] -> !Checked[i]
QcmListAdapter.this.checked[position][cpt] = !QcmListAdapter.this.checked[position][cpt];
APIHelper.log('d', "position="+position);
// TODO : Faire la methode PlaySerieFragment.saveSession();
//QcmListAdapter.this.activity.getFragmentManager().getFragment(QcmListAdapter.this.);
}
});
labels[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkBoxes[cpt].performClick();
APIHelper.log('d', "onClick:cpt="+cpt);
}
});
Proposition p = propositions.get(i);
p.setFormattedView(this.activity, labels[i], (i + 1));
}
return vi;
}
and here is the view for an item :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/question_block"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<RelativeLayout android:id="#+id/question" android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView
android:id="#+id/question_number_item"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:text="X"
android:textSize="30sp"
android:textColor="#android:color/white"
android:gravity="center"
android:background="#drawable/question_number" />
<TextView
android:id="#+id/question_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:text="Question 1 : Texte descriptif de la question tellement long"
android:textSize="16sp"
android:textColor="#color/MPQ_blue"
android:layout_toRightOf="#id/question_number_item"/>
</RelativeLayout>
<RelativeLayout android:id="#+id/question_item1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/question" android:layout_marginTop="16dp">
<CheckBox android:id="#+id/question_check1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="#drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="#+id/question_label1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="#id/question_check1"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/question_item2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/question_item1" android:layout_marginTop="8dp">
<CheckBox android:id="#+id/question_check2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="#drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="#+id/question_label2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="#id/question_check2"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/question_item3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/question_item2" android:layout_marginTop="8dp">
<CheckBox android:id="#+id/question_check3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="#drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="#+id/question_label3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="#id/question_check3"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/question_item4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/question_item3" android:layout_marginTop="8dp">
<CheckBox android:id="#+id/question_check4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="#drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="#+id/question_label4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="#id/question_check4"></LinearLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/question_item5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/question_item4" android:layout_marginTop="8dp">
<CheckBox android:id="#+id/question_check5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:button="#drawable/play_qcm_checkbox" android:text="" android:paddingTop="13dp"/>
<LinearLayout android:orientation="vertical" android:id="#+id/question_label5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="#id/question_check5"></LinearLayout>
</RelativeLayout>
</RelativeLayout>
I suspect this is because the view is getting re-used. Android re-uses views in a listview for performance, as the view is re-used when you click the same view displays the checked state.
You can either remove the if statement around convertView
if(convertView == null)
..
But this will be determental to performance if you have a lot of items.
Otherwise you should use the common viewHolder method:
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
Hope this helps.
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