A. Hajeb
A. Hajeb

Reputation: 37

viewing gridView items in android

I create a class for a gridview it works correctly and I test it in other parts of app it is working correctly. the Custom layout only contains an imageView in those parts which I test. for an activity I need to use both imageView and textView together and here is the xml code

<?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:weightSum="1"
android:orientation="vertical">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView3"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/khattesevom" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textView5"
    android:background="@drawable/textblockbio"
    android:textColor="@color/colorText"
    android:layout_gravity="center_horizontal"
    android:padding="5dp" />
</LinearLayout>

My Class's Code:

public class GridLyrics extends BaseAdapter {
private Context mContext;
private final String[] title;
private final int image[];
public GridLyrics(Context context, String[] t, int[] img)
{
    mContext = context;
    this.title = t;
    this.image = img;
}
@Override
public int getCount() {
    return title.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    if (convertView == null)
    {
        grid = new View(mContext);
        grid = inflater.inflate(R.layout.grid_lyrics, parent, false);

        TextView textView = (TextView) grid.findViewById(R.id.textView5);
        ImageView imageView = (ImageView)grid.findViewById(R.id.imageView3);
        textView.setText(title[position]);
        imageView.setImageResource(image[position]);
    }
    else
    {
        grid = (View) convertView;
    }

    return grid;
}
}

and here is the way I use it :

    GridView gridView = (GridView) tb2.findViewById(R.id.gridView4);
    String[] t = {"Marham", "Onam Mesle Mane", "Mikham", "Door", "To Besh Migi Mariz", "Oraman", "Test"};
    int[] img = {R.drawable.ic_khattesevom, R.drawable.ic_khattesevom, R.drawable.ic_khattesevom, R.drawable.ic_khattesevom, R.drawable.ic_khattesevom, R.drawable.ic_khattesevom, R.drawable.ic_khattesevom};
    gridView.setAdapter(new GridLyrics(getContext(), t, img));

My problem is in the last row the textView dosn't appear! it shows the picture but not the textView.

the picture of app I don't know what I can do!

enter image description here

Upvotes: 0

Views: 182

Answers (2)

Learning Always
Learning Always

Reputation: 1561

set TextView height to match parent and remove padding. change your xml like below.

<?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:weightSum="1"
android:orientation="vertical">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView3"
    android:layout_gravity="center_horizontal"
    android:src="@drawable/khattesevom" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textView5"
    android:background="@drawable/textblockbio"
    android:textColor="@color/colorText"
    android:layout_gravity="center_horizontal"
     />
</LinearLayout>

I think it will work for you.

Upvotes: 1

KISHORE_ZE
KISHORE_ZE

Reputation: 1476

How about this. Change your LinearLayout to ScrollView that way by scrolling down you would be able to get his name

Upvotes: 0

Related Questions