I don't see why I only get 1 column in my grid view no matter what I do.. frustrating.
activity's xml
<?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="match_parent"
android:orientation="vertical">
<GridView
android:id="#+id/gridView_album"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="2"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
for each item in the grid view (this is inflated in my adapter)
<?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" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView_albumTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="album title" />
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="105dp"
android:layout_height="100dp"
android:src="#drawable/abs__ab_share_pack_holo_light" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/abs__ab_solid_light_holo" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/abs__ab_solid_light_holo" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/abs__ab_solid_light_holo" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:src="#drawable/abs__ab_solid_light_holo" />
</LinearLayout>
</LinearLayout>
and my adapter's getView
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
JsonAlbum jsonAlbum = (JsonAlbum) getItem(position);
ArrayList<JsonAlbumImage> jsonAlbumImageArray = jsonAlbum.album_image_list;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.album_list_row, null);
}
TextView textView_albumTitle = (TextView) v.findViewById(R.id.textView_albumTitle);
textView_albumTitle.setText(jsonAlbum.title);
ImageView imageView1 = (ImageView)v.findViewById(R.id.imageView1);
ImageView imageView2 = (ImageView)v.findViewById(R.id.imageView2);
ImageView imageView3 = (ImageView)v.findViewById(R.id.imageView3);
ImageView imageView4 = (ImageView)v.findViewById(R.id.imageView4);
ImageView imageView5 = (ImageView)v.findViewById(R.id.imageView5);
ArrayList<ImageView> imageViewList = new ArrayList<ImageView>();
imageViewList.add(imageView1);
imageViewList.add(imageView2);
imageViewList.add(imageView3);
imageViewList.add(imageView4);
imageViewList.add(imageView5);
int imageCount = Math.min( imageViewList.size(), jsonAlbumImageArray.size());
for(int i=0; i < imageCount; ++i)
{
ImageView imageView = imageViewList.get(i);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
JsonAlbumImage jsonAlbumImage = jsonAlbumImageArray.get(i);
imageLoader.displayImage(jsonAlbumImage.url_image, imageView, options);
}
Log.i("debug", "width: "+ Integer.toString(v.getWidth()));
return v;
}
Hi This is my Gridview code
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView1"
android:numColumns="2"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
My imageview code:
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="150px"
android:layout_height="150px"
android:layout_marginRight="50px"
android:src="#drawable/ic_launcher" >
</ImageView>
I am getting 2 columns..Lets u try for this attributes..
Maybe your images are too wide to take place in two columns on your screen?
BTW it's very expensive (memory) to create views each time - take a look on ViewHolder pattern.
Related
I'm very new in android and need a little help with this task.
I have gridview witch pull images and text from database and display them. Currently is displayed like image and on the right of the image is text. I want to make text under the image. Can you help me with this task. This is the gridview.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:numColumns="2" >
</GridView>
</LinearLayout>
And this is how I display image and text in it table_column.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bckimageviw"
>
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Name"
/>
</LinearLayout>
</LinearLayout>
And if need getView()
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.table_column, null);
}
// ColPhoto
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColPhoto);
imageView.getLayoutParams().height = 80;
imageView.getLayoutParams().width = 80;
imageView.setPadding(10, 10, 10, 10);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
try
{
imageView.setImageBitmap((Bitmap)MyArr.get(position).get("ImageThumBitmap"));
} catch (Exception e) {
// When Error
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}
// ColName
TextView txtName = (TextView) convertView.findViewById(R.id.ColName);
txtName.setPadding(5, 0, 0, 0);
txtName.setText("" + MyArr.get(position).get("name").toString());
return convertView;
}
u can use relative layout and set align textview to imageview.
try this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:gravity="center" >
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ColName"
android:layout_alignRight="#+id/ColName"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ColPhoto"
android:gravity="center"
android:text="Name name" />
</RelativeLayout>
I don't know why add a single element LinearLayout in another LinearLayout.
if grid item need to be put in correct position, I'd like use RelativeLayout instead.
`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<TextView android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ColPhoto"
android:text="Name"
/>
</RelativeLayout>
`
Try to change your orientation to "vertical".
EDIT:
Like, Tr4X said you don't need second LinearLayout. You can put both in one.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bckimageviw" >
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"/>
<TextView
android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
</LinearLayout>
I have a problem with my GridView when it receives incomplete row item number, it doesn't scroll anymore. My items are dynamic which are given by the server, I have 3 columns and if the number of GridView items are more than enough to enable scrolling and if the row item is 3, it works fine. For example (9 items):
But if the third row has only 1 or 2 item/s, it doesn't scroll anymore (7 or 8 items):
This has caused me headache more than excitement, my GridView in layout is:
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/white"
android:listSelector="#android:color/transparent"
android:numColumns="3"
android:stretchMode="columnWidth" >
</GridView>
In my activity nothing much, just declaration and setting its adapter. Did I miss anything? I tried adding verticalSpacing and padding separately but nothing's changed.
EDIT:
This is the setup in the whole layout:
Black is a RelativeLayout, FrameLayout and the last ImageView are aligned top and bottom respectively. Top TextView is below FrameLayout, bottom TextView is below top TextView. Now GridView is below bottom TextView and above ImageView, it should expand its height basing to the space left consumed between the first 3 elements and the bottom element. This is how it's setup in my layout.
I believe it's a layout problem more than in my code since I've done dozens of ListView and this has similar implementation.
EDIT2:
The layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SixthActivity" >
<FrameLayout
android:id="#+id/cover_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/cover_image"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#color/gray"
android:scaleType="centerCrop" />
<ProgressBar
android:id="#+id/cover_image_progressbar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="180dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_margin="10dp" />
<TextView
android:id="#+id/school_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/white"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/container_sns"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:gravity="right"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btn_telephone"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_email"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_facebook"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_twitter"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
<ImageView
android:id="#+id/btn_line"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:visibility="gone" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
<TextView
android:id="#+id/student_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/cover_frame"
android:background="#f8f7f3"
android:padding="10dp"
android:textColor="#color/brown"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/container_news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/student_name"
android:background="#drawable/bg_news"
android:clickable="true"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="#+id/news"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="85dp"
android:layout_marginRight="10dp"
android:clickable="true"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/brown"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/container_next_plan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/container_news"
android:background="#drawable/bg_next"
android:clickable="true"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="#+id/next_plan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="85dp"
android:layout_marginRight="10dp"
android:clickable="true"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/brown"
android:textStyle="bold" />
</RelativeLayout>
<GridView
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/mypage_footer"
android:layout_below="#id/container_next_plan"
android:background="#ACA899"
android:gravity="center"
android:numColumns="3" >
</GridView>
<ImageView
android:id="#+id/mypage_footer"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:scaleType="fitXY" />
</RelativeLayout>
Don't mind the ImageViews, I just took out their source. I needed to retain the setup from my actual layout. The code:
public class SixthActivity extends Activity {
private Integer[] gridViewItem = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8 };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sixth);
GridView gridView = (GridView) findViewById(R.id.grid_view);
CustomAdapter adapter = new CustomAdapter(this,
R.layout.item_module, gridViewItem);
gridView.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sixth, menu);
return true;
}
}
The adapter:
public class CustomAdapter extends ArrayAdapter<Integer> {
private Context context;
private int resource;
private Integer[] moduleList;
public CustomAdapter(Context context, int resource,
Integer[] moduleList) {
super(context, resource, moduleList);
// TODO Auto-generated constructor stub
this.context = context;
this.resource = resource;
this.moduleList = moduleList;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resource, parent, false);
holder = new ViewHolder();
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.tvModuleName = (TextView) row.findViewById(R.id.module_name);
holder.tvModuleImage = (ImageView) row.findViewById(R.id.module_image);
holder.tvModuleName.setText("SAMPLESAMPLE");
return row;
}
public class ViewHolder {
public ImageView tvModuleImage;
public TextView tvModuleName;
}
}
The item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_module"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/module_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/module_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ellipsize="end"
android:singleLine="true" />
</LinearLayout>
Try to add numbers to gridViewItem to add items enough to trigger the scrolling just like in my screenshots, and try taking out 1 or 2 items at the bottom such that that row would be incomplete to replicate my problem. Thanks.
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'" />
<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'" />
</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.
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
The gridview of my application has an image and a text below it for each grid.
I need to make sure that the complete list of grid fit in to the screen (no overflow at bottom).
Here is how i have defined my gridview
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:stretchMode="columnWidth"
android:layout_below="#+id/text1view"
android:gravity="center"
android:background="#drawable/home_bg"
/>
and each grid item is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ImageView android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dip"
android:layout_margin="0dip"
>
</ImageView>
<TextView android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:padding="0dip"
android:gravity="center_horizontal"
android:layout_below="#+id/grid_item_image"
android:layout_alignParentBottom="true"
>
</TextView>
</LinearLayout>
I have tried several ways to set the grid view layout param, but i am not getting the right output. Please help me here. the gridview adapter code is
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.griditems, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.grid_item_text);
Log.i(TAG, holder.text1.toString());
holder.image = (ImageView) convertView
.findViewById(R.id.grid_item_image);
// if it's not recycled, initialize some attributes
holder.image.setImageResource(gridItemIds[position]);
holder.text1.setText(gridTitles[position]);
int h = mContext.getResources().getDisplayMetrics().densityDpi;
convertView.setLayoutParams(new GridView.LayoutParams(h-50, h-20));
//holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
//holder.image.setScaleType(ImageView.ScaleType.FIT_XY); ......
Try this:-
grid_item.xml
<?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">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center">
<ImageView android:id="#+id/imgIcon" android:src="#drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TextView android:text="TextView" android:id="#+id/txtDesc" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/mobGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
try
android:layout_width="fill_parent"
android:layout_height="fill_parent"
for your GridView instead of wrap_content!