Creating gridview with 3 rows of imageviews that fill the whole screen - android

I have been searching everywhere for similar solutions but none seem to work for me.
On my first screen I will have a gridview which consists of 1 column and 3 rows vertically. Each row will have an imageview and a partially transparent textview on top of the text view. The imageviews span the screen width fine. My only problem is that the 3 image views do not span the full screen vertically, there is space inbetween rows, although I have tried many methods to fix this. I will post up my xml files and code:
row_layout.xml - this is the gridview items
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/item_image"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:src="#drawable/season1">
</ImageView>
<TextView
android:id="#+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/item_image"
android:layout_alignTop="#+id/item_image"
android:layout_alignRight="#+id/item_image"
android:layout_alignBottom="#+id/item_image"
android:gravity="bottom|left"
android:textSize="30sp"
android:textAlignment="center"
android:lines="1"
android:layout_marginTop="145dp"
android:background="#99000000">
</TextView>
main.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"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:padding="0dp"
android:gravity="center"
android:numColumns="1"
android:fitsSystemWindows="true"
android:stretchMode="columnWidth" >
</GridView>
My custom adapter:
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();
public CustomGridViewAdapter(Context context, int layoutResourceId, ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
row.setMinimumHeight(MainActivity.height1/3);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());// my image
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
MainActivity:
public class MainActivity extends ActionBarActivity {
GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
public static int height1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
ActionBar ab = getActionBar();
ab.setDisplayShowTitleEnabled(false);
ab.setDisplayShowHomeEnabled(false);
Resources res = this.getResources();
Bitmap bMap = BitmapFactory.decodeResource(res, R.drawable.actionbar);
BitmapDrawable actionBarBackground = new BitmapDrawable(res, bMap);
ab.setBackgroundDrawable(actionBarBackground);
//set grid view item
Bitmap season1Icon = BitmapFactory.decodeResource(res, R.drawable.season1);
Bitmap season2Icon = BitmapFactory.decodeResource(res, R.drawable.season2);
Bitmap season3Icon = BitmapFactory.decodeResource(res, R.drawable.season3);
gridArray.add(new Item(season1Icon,"Season 1"));
gridArray.add(new Item(season2Icon,"Season 2"));
gridArray.add(new Item(season3Icon,"Season 3"));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
height1 = metrics.heightPixels;
gridView = (GridView) findViewById(R.id.gridView1);
customGridAdapter = new CustomGridViewAdapter(this, R.layout.row_grid, gridArray);
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent a = new Intent(MainActivity.this, House.class);
a.putExtra("Position", position);
startActivity(a);
}
});
}
}
Any help will be greatly appreciated, I've spent hours trying to fix this!

Dynamically set the layout params of your imageview, once you know the height of the screen.
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
//row.setMinimumHeight(MainActivity.height1/3); //don't need this, since wrap content will make the row height match your image view's
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
holder.imageItem.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, MainActivity.height1/3);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}

Related

Make imageView in gridView clickable

I have this gridView with imageView items, but i can't make it clickable. I'm kind of new in android and i don't understand how to make theses images clickable. I tried some answers that I have read in other posts but it doesn't work.
Help...
Another question! If i want to pass a hard coded String to another activity, but this string changes every time I click on an imageView item. Can I declare a static string and then I say string="word" and then call it from the other activity with Activity.string? Would i have the value in the other activity?
Please find below my xml files, gridViewAdapter and activity!
services_list_activity.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="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".ServicesListActivity">
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
gris_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/atm">
</ImageView>
<TextView
android:id="#+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewAdapter.java
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();
public CustomGridViewAdapter(Context context, int layoutResourceId,
ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
ServicesListActivity.java
public class ServicesListActivity extends Activity implements AdapterView.OnItemClickListener{
protected static final Toast text = null;
//String to pass to another activity
public static String types;
GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.services_list_activity);
//set grid view item
Bitmap atmIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.atm);
Bitmap bankIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.bank);
Bitmap barIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.bar);
Bitmap cafeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.cafe);
gridArray.add(new Item(atmIcon,"ATMs"));
gridArray.add(new Item(bankIcon,"Banks"));
gridArray.add(new Item(barIcon,"Bars"));
gridArray.add(new Item(cafeIcon,"Cafés"));
gridView = (GridView) findViewById(R.id.gridView1);
customGridAdapter = new CustomGridViewAdapter(this, R.layout.grid_row, gridArray);
gridView.setAdapter(customGridAdapter);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//intent
Intent data = new Intent(getApplicationContext(), ListActivity.class);
switch (position) {
case 0:
//String to send to another activity
types= "restaurant";
startActivity(data);
break;
case 1:
startActivity(data);
break;
case 2:
startActivity(data);
break;
case 3:
startActivity(data);
break;
default:
break;
}
}
}
Add this to your ImageView:
android:clickable="true"
With feedback:
android:background="?attr/selectableItemBackground"
Then set an OnClickListener in your Activity:
yourImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

Listview OnItemClickListener with customer adapter not working

Have seen this issue many times, i am using a customer adapter to populate my Listview with my customer layout xml. I understand the click is probably never going to List view and my layout is blocking it, but after trying the possible solutions nothing seems to be trying :
Tried:
android:descendantFocusability="blocksDescendants"
for layout of my custom xml
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
for each individual elements in the layout
Any other way to solve this
EDIT
Adding the code for where listview is attached to adapter:
if(!allcontactsstring.isEmpty()){
adapter = new MyContactListAdapter(getContext(),R.layout.contact_list_view,imageArry);
listView.setAdapter(adapter);
}
AdapterView.OnItemClickListener mylistViewClicked = new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG);
}
};
listView.setOnItemClickListener(mylistViewClicked);
My Custom adapter :
public class MyContactListAdapter extends ArrayAdapter<MySQLiteContact> {
Context context;
int layoutResourceId;
ArrayList<MySQLiteContact> data=new ArrayList<MySQLiteContact>();
public MyContactListAdapter(Context context, int resource, ArrayList<MySQLiteContact> objects) {
super(context, resource, objects);
//this.layoutResourceId = layoutResourceId;
this.layoutResourceId = resource;
this.context = context;
this.data = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ImageHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
//ImageView tmp = (ImageView)row.findViewById(R.id.icon);
//tmp.clearFocus();
//tmp.isClickable();
//TextView tmp1=(TextView)row.findViewById(R.id.Itemname);
//tmp1.clearFocus();
holder = new ImageHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.Itemname);
holder.imgIcon = (ImageView)row.findViewById(R.id.icon);
row.setTag(holder);
}
else
{
holder = (ImageHolder)row.getTag();
}
MySQLiteContact picture = data.get(position);
holder.txtTitle.setText(picture.getsFirstName()+" "+picture.getsLastName());
byte[] outImage=picture.getBimage();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
theImage=Bitmap.createScaledBitmap(theImage , 100, 100, true);
theImage= ContactDetailsMain.getRoundedRectBitmap(theImage,100);
holder.imgIcon.setImageBitmap(theImage);
return row;
}
static class ImageHolder
{
ImageView imgIcon;
TextView txtTitle;
}
}
My xml with text view and imageview:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:src="#drawable/ic_contact_picture_holo_light"
/>
<TextView
android:id="#+id/Itemname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:textColor="#color/black"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"
android:paddingTop="5dp"
/>
</LinearLayout>
EDIT
A dirty workaround
As a crud and dirty workaround i have added a on click listener inside the adapter to each and every layout that gets inflated and this seems to work currently to get the positions.
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println(position);
}
});
Not clean to have so many listeners , would be nice to have only one but yes for the moment it work..
Well your click works, what it doesn't work on your code is the fact that you don't see the Toast right? You just forgot to call show() on Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG) which should be Toast.makeText(getContext(),"S"+position,Toast.LENGTH_LONG).show()
Enjoy :p
Edit

listview has empty row at the last

I have a listview in my application, but my problem is that the ListView has an empty row below the last item like this image.
link : http://blog.naver.com/wolfjunghun/220150613193
For example, listview has its list of size of 20, but listview has 21 rows.
How can I get rid of that line(not only using android:footerDividersEnabled="false", but also empty space, too) and empty space...?
Please, somebody help me!
.xml file below
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
>
<ListView
android:id="#+id/list_comment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:divider="#drawable/comment_divider"
android:overScrollMode="never"
android:footerDividersEnabled="false"
android:fadingEdgeLength="0dp"
/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="50dp"
/>
</LinearLayout>
and call xml in activity :
if(mCellEntity.getComments()!=null&&mCellEntity.getComments().size()>0){
list_comment.setVisibility(View.VISIBLE);
findViewById(R.id.frame_cell).setBackgroundColor(Color.parseColor("#ebedee"));
Collections.sort(mCellEntity.getComments(), new TimeAscCompare());
commentAdapter=new CommentAdapter(this, R.layout.cell_comment_item2, mCellEntity.getComments());
list_comment.setAdapter(commentAdapter);
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
int width = displayMetrics.widthPixels;
int margin = (int) ComUtil.convertDpToPixel(40,this);
width = width -margin;
ListViewHelper.getListViewSize(list_comment, width);
}else{
list_comment.setVisibility(View.GONE);
}
getView method in Commentadapter :
public class CommentAdapter extends BaseAdapter {
........... skip ..........
public CommentAdapter(Activity ctx, int layout , ArrayList<CellEntity> timeLineList){
inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.list = timeLineList;
this.ctx = ctx;
this.layout = layout;
this.applicationClass = (ApplicationClass)ctx.getApplicationContext();
SharedPreferences userPref = ctx.getSharedPreferences(CommonConst.PREFS_USER_INFO, Activity.MODE_PRIVATE);
mobileView = userPref.getBoolean(CommonConst.UserInfo.PREFS_MOBILE_VIEW, false);
if (this.mTypeface == null)
this.mTypeface = Typeface.createFromAsset(ctx.getAssets(), "NanumGothicBold.ttf.mp3");
if(ctx instanceof CellMapTimeLineActivity){
isMapView = false;
}else if(ctx instanceof CellMapSkipActivity){
this.layout = R.layout.timeline_skip_item;
isMapView = false;
}
options = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.bg_cellmap_line)
.showImageOnFail(R.drawable.bg_cellmap_line)
.resetViewBeforeLoading()
.cacheOnDisc()
.imageScaleType(ImageScaleType.EXACTLY)
.bitmapConfig(Bitmap.Config.RGB_565)
.displayer(new FadeInBitmapDisplayer(300))
.build();
icoOptions = new DisplayImageOptions.Builder()
.showImageForEmptyUri(R.drawable.pic)
.showImageOnFail(R.drawable.pic)
.resetViewBeforeLoading()
.cacheOnDisc()
.build();
typefaceHelveticaBold = Typeface.createFromAsset(ctx.getAssets(), "HelveticaBold.ttf");
}
........... skip ..........
static class ViewHolder {
/////holder setting/////
}
#Override
public View getView(int position, View view, ViewGroup parent) {
final ViewHolder holder;
CellEntity row = new CellEntity();
row = list.get(position);
if(view == null){
view = inflater.inflate(layout, parent , false);
holder = new ViewHolder();
holder.img_bottom_line =(ImageView)view.findViewById(R.id.img_bottom_line);
holder.user_name = (TextView)view.findViewById(R.id.user_name);
holder.likeCount = (TextView)view.findViewById(R.id.likeCount);
holder.txt_like = (TextView)view.findViewById(R.id.txt_like);
......... skip ........
view.setTag(holder);
}else{
holder = (ViewHolder) view.getTag();
}
......... skip ........
return view;
}
......... skip ........
}
It's like that.
I don't understand why that list view call the getView method 21-times even list view has 20 items in the list!

CustomListView onItemClickListener not working while using HorizontalScrollView, Infinitely called getView() method

I am using the following code.
Adapter.java class
public class Adapter extends ArrayAdapter<String> {
private java.util.List<String> List;
private Context context;
public Adapter(List<String> list, Context ctx) {
super(ctx, R.layout.row_item, list);
this.List = list;
this.context = ctx;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
NameHolder holder = new NameHolder();
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row_item, null);
TextView tv = (TextView)v.findViewById(R.id.textView1);
LinearLayout ll = (LinearLayout) v.findViewById(R.id.myLayout);
holder.topic_name = tv;
holder.myLayout = ll;
v.setTag(holder);
}
else
holder = (NameHolder) v.getTag();
String topic = List.get(position);
holder.topic_name.setText(topic);
ImageView imgUsers = new ImageView(context);
imgUsers.setImageResource(R.drawable.ic_launcher);
holder.myLayout.addView(imgUsers);
return v;
}
private static class NameHolder {
public TextView topic_name;
public LinearLayout myLayout;
}
}
MainActivity.java class :
public class MainActivity extends Activity {
ListView lv;
ArrayList<String> list;
Adapter myAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.listView1);
list = new ArrayList<String>();
for(int i=0; i<10;i++){
list.add("Name"+i);
}
myAdapter = new Adapter(list, MainActivity.this);
lv.setAdapter(myAdapter);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"lets see"+list.get(arg2), Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
row_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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:orientation="horizontal"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Now, the expected output is a single image in every listItem, which tend to go to infinite.
And the onItemClickListener is not working as well.
Also, onItemClickListener works if we don't add HorizontalScrollView.
Writing onClickListener() in Adapter itself helps little as it works unpredictably.
Also, on adding a print statement in getView() and observing in Logcat, I find it difficult to understand the pattern in which getView() is called.
These are the problems I face. I require a working code of the things I wish to apply. Thanks in advance.
// Replace this code
public View getView(int position, View convertView, ViewGroup parent) {
NameHolder holder;
if (convertView == null) {
holder = new NameHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_item, null);
holder.topic_name = (TextView)convertView.findViewById(R.id.textView1);
holder.myLayout = (LinearLayout) convertView.findViewById(R.id.myLayout);
convertView.setTag(holder);
}
else
holder = (NameHolder) convertView.getTag();
String topic = List.get(position);
holder.topic_name.setText(topic);
ImageView imgUsers = new ImageView(context);
imgUsers.setImageResource(R.drawable.ic_launcher);
holder.myLayout.removeAllViews();
holder.myLayout.addView(imgUsers);
convertView.setTag(holder);
return convertView;
}
Looks like you are adding an ImageView every single time here
ImageView imgUsers = new ImageView(context);
imgUsers.setImageResource(R.drawable.ic_launcher);
holder.myLayout.addView(imgUsers);
Why don't you include the ImageView as part of your list item layout?

gridview with image and text android

I successfully created a gridview populated with images. Now, what I want to do is put a text below the image.
This is the screenshot of my app so far.
http://i203.photobucket.com/albums/aa266/paulocarabuena_bucket/screen-2.png
Here is my getView method in my imageadapter class..
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(this.context);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(this.thumbs[position]);
return imageView;
}
Here is the onCreate method of my main activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.launchFullScreen();
this.setContentView(R.layout.main);
this.grids = (GridView) this.findViewById(R.id.elements);
ImageAdapter adapter = new ImageAdapter(this);
this.grids.setAdapter(adapter);
this.grids.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
}
Thanks in advance. :)
Instead of Directly using Image View , you should inflate a view as below :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget44"android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_x="201px"
android:layout_y="165px"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/icon_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="center_horizontal"
android:textColorHighlight="#656565">
</TextView>
</LinearLayout>
And your Adapter should be like this :
public class ImageAdapter extends BaseAdapter{
Context mContext;
public static final int ACTIVITY_CREATE = 10;
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 5;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText("Profile "+position);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
}
else
{
v = convertView;
}
return v;
}
}
You can do it using an XML, that defines the GridView cell:
image.xml
<?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:gravity="center_horizontal"
>
<ImageView
android:id="#+id/image"
android:layout_marginBottom = "10dp"
android:src="#drawable/cell_sfondo"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
Then in your getView():
if(convertView == null) {
view = inflater.inflate(R.layout.image, null);
}
else
view = convertView;
ImageView image = (ImageView)view.findViewById(R.id.image);
image.setImageResource(this.thumbs[position]);
Paresh Mayani have a really good and simple example. This helped me a lot.
http://www.technotalkative.com/android-gridview-example/
You can create a layout for your image and text in each cell of the grid layout. And I suggest inflating the layout from an xml. You can try to do something like this:
//Use a viewHolder to optimize the list
ViewHolder holder = null;
if (convertView == null) {
//Create a new view holder to optimize the loading of elements in list
holder = new ViewHolder();
convertView = LayoutInflater.from(this.context).inflate(R.layout.my_custom_xml,null) ;
holder.imageView = (ImageView)convertView.findViewByID(R.id.my_image_view);
holder.textView = (TextView)convertView.findViewByID(R.id.my_text_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
//Bind data to the row
//Set the position in holder
holder.position = position;
//Set the image for each row
holder.imageView ...
//Set the text for each row
holder.textView.setText()...
//And the viewholder looks like this
private class ViewHolder{
//The position of this row in list
private int position;
//The image view for each row
private ImageView imageView;
//The textView for each row
private TextView textView;
}
And now you can simply create my_custom_xml which contain the text and image view:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width= "fill_parent"
android:layout_height = "wrap_content"
android:orientation= "vertical">
<ImageView
android:id = "#+id/my_image_view"
android:layout_width = ....
android:layout_height = ...
/>
<TextView
android:id = "#+id/my_text_view"
android:layout_width = ....
android:layout_height = ....
/>
</LinearLayout>
Instead of taking dynamic imageview,you can inflate layout having textview under imageview in adapter as:
if(convertView==null){
inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
holder=new Holder();
convertView=inflater.inflate(R.layout.gridinflater, null);
holder.imageView=(ImageView) convertView.findViewById(R.id.img);
holder.textView=(TextView) convertView.findViewById(R.id.txt);
convertView.setTag(holder);
}
else {
holder = (Holder) convertView.getTag();
}
where gridInflater is layout having textview under imageview in Relative Layout as parent.
public static class Holder {
public ImageViewProgress imageView;
public TextView textView;
}

Categories

Resources