I have a grid view that have 8 images
Now on a particular image view i want to set a text value
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 ;)
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.
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>
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
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
http://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.
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