Set Text on a GridView Image in Android
Asked Answered
R

6

1

I have a grid view that have 8 images like this

Now on a particular image view i want to set a text value that will be changed dynamically Like This

Main XML

<?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="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/mainLinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@drawable/blue_bar"
        android:orientation="horizontal" >
    </LinearLayout>

    <GridView
        android:id="@+id/gridView1"
        android:layout_below="@+id/mainLinearLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="20dp"
        android:columnWidth="100dp"
        android:gravity="center"
        android:numColumns="2"
        android:stretchMode="columnWidth" >
    </GridView>

</RelativeLayout>

**Inner XML Layout **

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

Code Main Activity

gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, DASHBOARD_LINKS));

Adapter Class

public class ImageAdapter extends BaseAdapter {
    private Context context;
    private final String[] dashBoardValues;

    public ImageAdapter(Context context, String[] dashBoardValues) {
        this.context = context;
        this.dashBoardValues = dashBoardValues;
    }

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

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View gridView;

        if (convertView == null) {

            gridView = new View(context);

            // get layout from dashboard_inner.xml
            gridView = inflater.inflate(R.layout.dashboard_inner, null);
            // set image based on selected text
            ImageView imageView = (ImageView) gridView
                    .findViewById(R.id.grid_item_image);
            imageView.setImageResource(R.drawable.ic);


        } else {
            gridView = (View) convertView;
        }

        return gridView;
    }

    @Override
    public int getCount() {
        return dashBoardValues.length;
    }

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

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

}

So how could i do this in a grid view .Please help me .

Rescript answered 30/12, 2013 at 7:20 Comment(16)
not only your images post your code also.then only we will understand your problem.and help you..Medea
Check this..github.com/jgilfelt/android-viewbadger , I guess this would be helpfull:)Drumlin
i have posted the code alsoRescript
Modify your row layout of GridView and add a Textview there.. to get more info/ help add your code.Shockheaded
I have added all the code please help me now to get this feature ...Rescript
dynamically means text will be set on server value change?Credulous
yes as there will be any request from the server it will be changed .if the req is 1 ,2,3 it will be changed as it is @PiyushGuptaRescript
@PoojaDubey so yu have to store that value in arraylist and set accroding to it to textviewCredulous
@PiyushGupta yes u r right with respect to the size of array i have to change the valueRescript
let us continue this discussion in chatRescript
@PoojaDubey Is it Completed?Credulous
@PiyushGupta still i haven't got the solutionRescript
@PoojaDubey you have only 8 images always, that means you are trying to create a dashboard ah ?Rambling
@Rambling yes u r right and on a single image i want to update the text dynamicallyRescript
@PoojaDubey On a single image , is it okay to be the text at bottom of each image. with drawable on top.Rambling
@Rambling please see the image for the refrenceRescript
R
2

As others have already suggested you can use a Relative Layout or FrameLayout

<?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="match_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="31dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_alignTop="@+id/imageView1"
        android:layout_marginLeft="20dp"
        android:text="TextView" />

</RelativeLayout>

Snap

enter image description here

Check this answer kcoppock which will give you an idea of how to use framelayout

Placing/Overlapping(z-index) a view above another view in android

Also it is better to use a ViewHolder Pattern

http://developer.android.com/training/improving-layouts/smooth-scrolling.html

Robichaud answered 7/1, 2014 at 6:26 Comment(0)
T
1

Here is actual working code

public class MainActivity extends Activity {

    private static final String TAG = "AAA";

    private static Handler mHandler;

    private LayoutInflater layoutInflater;
    private GridView mGridView;
    private int click = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        layoutInflater = LayoutInflater.from(getApplicationContext());

        click = 0;

        setContentView(R.layout.my_grid_view);
        mGridView = (GridView)findViewById(R.id.my_grid_view);
        mGridView.setAdapter(new MyAdapter());
        mGridView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> adapterViews, View pView, int position, long id) {

                click++;
                TextView textView = (TextView)pView.findViewById(R.id.my_text_view);
                if(textView!=null){
                    textView.setText("Changed : Position = " + position + " : Click No = " + click);
                }else{
                    Log.d(TAG, "Fish : textView = " + textView);
                }
            }
        });

    }


    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return 200;
        }

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

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

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

            if(convertView == null){ //This is memory efficient, but after certain no of time u will get similar view repetition, 
                //to over come that overwrite getViewTypeCount() & provide proper way of persistent view & data management mechanism.
                convertView = layoutInflater.inflate(R.layout.child_one, null);
            }

            return convertView;
        }

    }}

And XMLs

<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:gravity="center"
    android:horizontalSpacing="10dp"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:verticalSpacing="10dp" />

And

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/my_image_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:scaleType="fitXY"
        android:src="@drawable/flower_1" />

    <TextView
        android:id="@+id/my_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top|right"
        android:background="@android:color/darker_gray"
        android:text="Default Text" />

</FrameLayout>
Twinflower answered 6/1, 2014 at 10:12 Comment(2)
which one is child ??Please post the screen shotRescript
2nd one is child which one is returned from getView(). 1st one is set as setContentView(R.layout.my_grid_view); Both are two different XML files in "res/layout"Twinflower
T
0

instead of image view pass framelayout reference. it will work. framelayout is view group which sub class of view only.

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:gravity="center"
    android:orientation="vertical">

<ImageView android:scaleType="fitXY"
    android:src="@drawable/ic_launcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/image_view"
    android:layout_gravity="center"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_gravity="top|right"
    android:background="@android:color/darker_gray"
    android:layout_height="wrap_content"
    android:id="@+id/badge_view"
    android:text="10"/>
</FrameLayout>

snap shot

enter image description here

Taker answered 30/12, 2013 at 7:37 Comment(7)
what i have to do in may grid view to get thisRescript
replace *Inner XML Layout * code with my code, and change my image view id to ur id (grid_item_image)Taker
i am getting this exception now com.android.ddmlib.SyncException: Local path doesn't exist.Rescript
Please modify u r xmlRescript
it is not working for me i need it on a particular image and it is not showing up on the top of my image .can u help me set on ca particular image not on all the imagesRescript
i have already posted the xml code that it and the changes u have i want to change a particular image not on each and every oneRescript
let us continue this discussion in chatTaker
R
0

Get this image and add to your project drawble. new http://www.flickr.com/photos/113556524@N05/11793569304/

This is the screen shot nw http://www.flickr.com/photos/113556524@N05/11793569304/

This your xml code to use :

<?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" 
    android:weightSum="4">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:weightSum="2">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:weightSum="2">

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:weightSum="2">

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:weightSum="2">

        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

        <Button
            android:id="@+id/button8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/aaa"
            android:text="" />

    </LinearLayout>



</LinearLayout>

In your activity java file use this :

package com.example.dashboard;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button b1,b2,b4,b3,b5,b6,b7,b8;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.neww);

        b1 =(Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        b5 = (Button) findViewById(R.id.button5);
        b6 = (Button) findViewById(R.id.button6);
        b5 = (Button) findViewById(R.id.button7);
        b6 = (Button) findViewById(R.id.button8);




        /* Wat ever you want validation , anything do here */

       // if you need to set text to button 

        b1.setText("Hi pooja");


        //  blah blah blah - wat ever you want to any button text



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Got it ;)

Rambling answered 6/1, 2014 at 5:23 Comment(5)
so how could i set the text on the top of the images as i have in the questionRescript
delete background="@null" and instead of drawbleontop, just use drawwble.Rambling
Please attach the screen shot it is missingRescript
see these links , uploadins scren shots r prblmsRambling
let us continue this discussion in chatRescript
T
0

Use FrameLayout as child of GridView. Within FrameLayout first put ImageView & on top of it put a TextView as mentioned by "sush". Then you can change Visibility & content of TextView on demand, through program. It is not working for you because probably for all TextViews of you have put same "android:id" put different id for different TextView. It is bound to work. I've tested.

Twinflower answered 6/1, 2014 at 6:43 Comment(2)
Sorry, for that u have to wait till evening, as in my office, copy & paste to out side office LAN is not allowed here :(Twinflower
ok fine i waiting for u r reply .as i need to put a text on a particular image .and the text will be dynamically added .Point to note on a single image i want to put the textRescript
O
0
https://i.stack.imgur.com/HUY4o.png Check the sample image...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/album_item"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/cover"
    android:layout_width="148dp"
    android:layout_height="148dp"
    android:src="@drawable/empty_photo" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/cover"
    android:background="#70000000"
    android:padding="6dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="12sp"
        android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>

---> Use this layout and inflate it in your adapter.You will get the text at bottom of image.

Obstreperous answered 6/1, 2014 at 13:39 Comment(5)
i want the text on the top of the image at right side as in the questionRescript
Instead of alignBottom in linearlayout use alignTop...it will workObstreperous
I have to show 8 images in one of them i have to put the text on the top dynamically .so can u post the complete code how could i get this pleaseRescript
In your adapter use position variable to set text at particular position and in remaining positions set text as null or dont set any text.Obstreperous
will u see the images as i have posted in the question do u feel that u r code will help me to get thatRescript

© 2022 - 2024 — McMap. All rights reserved.