Android GridView with ImageView and TextView don´t show well - android

Hi I have a problem displaying my Gridview. I searched many sites and here on StackOverflow but took several days without finding the solution.
I leave here my xml (listado.xml) code where is gridview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<GridView
android:id="#+id/grid_release"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clipChildren="true"
android:stretchMode="columnWidth"
android:numColumns="2"
>
<!-- android:stretchMode="columnWidth"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="1dip"-->
</GridView>
The xml file with textview and imageview (grid_release.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#DC0000"
android:orientation="vertical"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/rls_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/rls_txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titulo" />
Also leave the code of ImageAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
View grid;
if(convertView==null)
{
grid = new View(context);
grid = layoutInflater.inflate(R.layout.grid_release, null);
}else{
grid = (View)convertView; //imageView=(ImageView)convertView;
}
ImageView rlsimg = (ImageView)grid.findViewById(R.id.rls_img);
rlsimg.setImageDrawable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
TextView rsltxt = (TextView)grid.findViewById(R.id.rls_txtTitle);
rsltxt.setText("Adpt00"+position);
return grid;
Thanks for help!!!

This solve my problem:
Metodo GetView of ImageAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView;
if(convertView==null)
{
LayoutInflater li = ((Activity)context).getLayoutInflater();
myView = li.inflate(R.layout.grid_prueba1, null);
myView.setLayoutParams(new GridView.LayoutParams(100,100));
myView.setPadding(5,5,5,5);
}else{
myView = (View)convertView;
}
ImageView img = (ImageView)myView.findViewById(R.id.img2);
img.setImageDrawable(LoadImageFromURL(GridViewConfig.getResim_list().get(position)));
TextView txt = (TextView)myView.findViewById(R.id.txt2);
txt.setText("Adept0000"+position);
return myView;
}
But now I have another problem, textview displayed within the image. I want this text below the image. I'll have to open another query.

Related

Wrong View is being returned

I have a layout for my gird item which contains a linear layout and in that a textView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
android:minHeight="120dp"
android:background="#drawable/bg_all_white_rounded_gray"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/ui_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/margin_social_logo"
android:gravity="center"
android:text="Family Friends"
android:textColor="#color/colorPrimaryDark"
android:textSize="#dimen/font_text"
android:visibility="visible" />
</LinearLayout>`
When i call
inflater.inflate(R.layout.main_grid_row, parent, false); from adapter it returns view of type
android.support.design.internal.NavigationMenuView
instead of a linear layout.
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View rowView = convertView;
ViewHolderCategories holder = null;
if (convertView == null) {
rowView = mInflater.inflate(R.layout.main_grid_row, parent, false);
holder = new ViewHolderCategories(mContext, rowView);
rowView.setTag(holder);
} else {
holder = (ViewHolderCategories) rowView.getTag();
}
MainCategory data = mAllData.get(position);
holder.updateView(data.getCategoryName());
return rowView;
}
I am not able to figure why its returning some other view instead of a liner layout
Try this:
rowView = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_grid_row, null);
This is my Activity layout file in which i have added the grid view.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/colorAppTheme">
<GridView
android:id="#+id/grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" /></RelativeLayout>
just removed the android:gravity="center" form GridView and its working fine.
Don't have any idea how its related but i thought to post the answer.

Indenting ListView items Android

I am trying to indent listView row by certain factor. For this, I used view.layout() and view.setX() method but they don't work or result in forceclose. Is there a way to achieve this result. Here is my code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View newRowView = convertView;
ViewHolder viewHolder;
if(newRowView == null)
{
LayoutInflater inflator = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newRowView = inflator.inflate(R.layout.list_view_images, parent,false);
//newRowView.layout(100*(position/list.size()), 0, 0, 0); // not working
newRowView.setX(100*(position/list.size())); // causing force close
viewHolder = new ViewHolder();
viewHolder.appIcon = (ImageView) newRowView.findViewById(R.id.imageView1);
viewHolder.appName = (TextView) newRowView.findViewById(R.id.textView1);
viewHolder.appName.setText(names.get(position));
newRowView.setTag(viewHolder);
}
list_view_images.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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
I am trying to indent the list in semi circular fashion.
What can I do achieve required results?
Regards
Update your getView() method as follows:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View newRowView = convertView;
ViewHolder viewHolder;
if (newRowView == null) {
LayoutInflater inflator = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newRowView = inflator.inflate(R.layout.list_view_images, parent, false);
viewHolder = new ViewHolder();
viewHolder.innerContainer = (LinearLayout) newRowView.findViewById(R.id.innerContainer);
viewHolder.appIcon = (ImageView) newRowView.findViewById(R.id.imageView1);
viewHolder.appName = (TextView) newRowView.findViewById(R.id.textView1);
viewHolder.appName.setText(list.get(position));
newRowView.setTag(viewHolder);
}
else {
viewHolder = (ViewHolder) newRowView.getTag();
}
float shiftRight = 100 * ((position * 1.0f) / list.size());
viewHolder.innerContainer.setPadding((int) shiftRight, 0, 0, 0);
//...
//...
return newRowView;
}
Update your XML layout as follows:
<?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" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/innerContainer"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
Update your ViewHolder to contain another field as following:
public LinearLayout innerContainer;
Explanation: Basically, we put your original XML layout inside a LinearLayout that acts as an outer container. The we assign padding to the innerContainer (original layout) with respect to the outer layout

How to show Text on Image

I have written a code, in which i am showing images in GridView, but now i want to show text in bottom of every image.
And i want to show my Grid like this:
but getting like this [also want to show Text for Image]:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:numColumns="2"
/>
</FrameLayout>
You need to define a custom grid item layout which contains a textview to assign the text.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/thumbnail"
android:padding="8dp"
android:scaleType="cropCenter"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="bottom"
android:textColor="#android:color/white"
android:background="#55000000" />
</FrameLayout>
Here we have created a FrameLayout which has the imageview set to match the parent's dimensions, this will be the imageview that displays the photo. Next, there is a TextView which will be used to display the item title and that is aligned to the bottom of the FrameLayout.
Next, we need to edit your adapter to use this grid item layout and render the correct information.
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the single grid item layout
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.grid_item, parent, false);
}
// Set Image
ImageView thumbnail = convertView.findViewById(R.id.thumbnail);
if (thumbnail != null) {
thumbnail.setImageResource(mThumbIds[position]);
}
// Set Text
TextView title = convertView.findViewById(R.id.title);
if (title != null) {
title.setText("Image Number: " + position);
}
return convertView;
}
mLayoutInflater should be globally defined in your constructor using
mLayoutInflater = LayoutInflater.from(context);

Filling ListView out

I'm working on project and main layout contains ListView. I want to dynamically add subItems to ListViewItem View. My main layout is as follows :
main_layout.xml
<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="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView"/>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
</ListView>
</RelativeLayout>
list_item.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="50dip"
android:background="#android:color/transparent" >
</LinearLayout>
And I want to add TextView to returned view in getView with following properties :
Height : fill_parent
Width : wrap_content
Text : Text View Item
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.M_context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, parent, false);
return convertView;
}
Please note that, I want to dynamically add subItems to ListViewItem. You can suppose that, I have array of TextView and want to fill my listView using this Items. How can I do this scenario ? I can not find any result with searching on other threads and aslo Google. So, Could any one please help me to solve my problem ?
Thanks in Advance :)
This was my getView() method.
main.xml
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#+id/button1"
android:cacheColorHint="#android:color/transparent"
android:divider="#00ff00"
android:fastScrollEnabled="true"
android:focusable="false"
android:listSelector="#000000"
android:paddingTop="5dp"
android:saveEnabled="true" >
</ListView>
item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Adapter
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
/*convertView = mInflater.inflate(R.layout.item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
holder = (ViewHolder) convertView.getTag();
holder.getAppName().setText(str.get(position));*/
if(position==0)
{
ImageView iv = new ImageView(getApplicationContext());
iv.setBackgroundResource(R.drawable.ic_launcher);
convertView = iv;
}
else if(position==1)
{
TextView tv = new TextView(getApplicationContext());
tv.setText("text view");
convertView = tv;
}
else
{
EditText et = new EditText(getApplicationContext());
et.setText("ET go home");
convertView = et;
}
return convertView;
}
private class ViewHolder {
private View pathRow;
private TextView appNameView = null;
public ViewHolder(View row) {
pathRow = row;
}
public TextView getAppName() {
if (null == appNameView) {
appNameView = (TextView) pathRow.findViewById(R.id.tv);
}
return appNameView;
}
}
}
The commented out portion is the one used to be. The code below it is the one which I used to dynamically generate the view and return them. my listitem.xml still has a textView in it. But the adapter sets the view which we are returning from getView(). This works for me.
I really don;t see the point of the LinearLayout in your item.xml. You can do that in your own code.

Android's RelativeLayout seems broken

I'm working on a layout where I use a ListView with RelativeLayout line items. The lineitems themselves are not displaying correctly.
The issue is that the txtVideoDuration TextView is drawn at the top of the line item instead of the bottom. Because of this the txtVideoTitle gets a height of 0. As you can see in the XML the txtVideoDuration is supposed to be clamped to the bottom of the ListView.
My goal is to have the layout resemble the tutorial that google gave me. Example: http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
I'm using Android 2.0.1. I've even plugged in the example layout with out modification into the ListView and the same behavior happens.
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/imgVideoThumbnail"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:minHeight="64dip"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<TextView
android:id="#+id/txtVideoDuration"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_toRightOf="#+id/imgVideoThumbnail"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:singleLine="true"
android:ellipsize="marquee"
android:text="0:00:00" />
<TextView
android:id="#+id/txtVideoTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgVideoThumbnail"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="#+id/txtVideoDuration"
android:layout_alignWithParentIfMissing="true"
android:gravity="center_vertical"
android:text="[Title]"
android:textColor="#FFFFFF" />
</RelativeLayout>
This simple case works. This is the root XML layout for the activity. The buggy code at the top is the line items in the list view. It seems that the ListView itself is breaking it.
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/listVideoCatalog"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="#+id/txtName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Hello World" />
</RelativeLayout>
</merge>
This is the getView Code that inflates the line item into the ListView.
static class ViewHolder
{
TextView txtTitle;
ImageView imgThumbnail;
TextView txtDuration;
Uri videoLocation;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null)
{
LayoutInflater li = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = li.inflate(R.layout.listitem_videolineitem, null);
holder = new ViewHolder();
holder.txtDuration = (TextView) convertView.findViewById(R.id.txtVideoDuration);
holder.txtTitle = (TextView) convertView.findViewById(R.id.txtVideoTitle);
holder.imgThumbnail = (ImageView) convertView.findViewById(R.id.imgVideoThumbnail);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
VideoFile video = this.videos.get(position);
holder.txtDuration.setText(millisToTimestamp(video.videoDuration));
holder.txtTitle.setText(video.videoTitle);
// Some debugger visual code.
holder.txtDuration.setBackgroundColor(Color.GREEN);
holder.txtTitle.setBackgroundColor(Color.CYAN);
return convertView;
}
This was answered in the comment of the accepted answer. Just to make it clear, this is the answer:
Change
li.inflate(R.layout.listitem_videolineitem, null);
to
li.inflate(R.layout.listitem_videolineitem, parent, false);
How are you inflating your row views that are "buggy"? Can you provide a code snippet?
Also, android:orientation="vertical" is not valid for RelativeLayout.

Categories

Resources