I've got a 3 column GridView
and 4 items.
So it happens that three of them appear on the first row, and the last one in the second row, but this one aligned to the left. I just wanted to know if there is possible to make it appear centered in the GridView
. Like a 'T', I don't know if I explain... xD
Thank you :-)
Edit:
This is the layout that contains the GridView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/section_title_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_gd_home_grid">
<ImageView
android:src="@drawable/home_grid_txori"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:layout_centerHorizontal="true"/>
<GridView
android:id="@+id/home_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:numColumns="3"
android:drawSelectorOnTop="true"
android:layout_marginBottom="30dip"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:padding="5dip"
android:listSelector="@android:color/transparent"
android:background="@drawable/bg_home_grid_item"
/>
</RelativeLayout>
By the way, android:layout_centerHorizontal="true"
doesn't work in GridView
Solution Edit:
This issue is from years past, but I've been asked to post the solution and I had to find my old repo to get it :-)
As I said bellow in the comments, I couldn't achieve it only usin XML, so I had to trick a fake non-clickable + non-focusable item in 3rd position.
It was my first App ever, so I didn't implement the recommended "ViewHolder" solution. I strongly recommend its usage
This is how my getView() method looks like in the Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (RelativeLayout) mInflater.inflate(
R.layout.home_grid_item, null);
}
/* In order to have 4th item centered in the 2nd row, we create an empty 3rd item */
if (position == 3) {
convertView.setFocusable(false);
convertView.setClickable(false);
} else {
ImageView imageView = (ImageView) convertView
.findViewById(R.id.image);
imageView.setImageResource(mThumbs[position]);
TextView textView = (TextView) convertView.findViewById(R.id.title);
textView.setText(mTitles[position]);
}
convertView.setTag(mTabTags[position]);
return convertView;
}
You can try the following code:
android:gravity="center_horizontal"
in the xml.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With