I already implemented my gridview using Android Universal Image Loader, but I need to put a label below every photo.
This is my ac_image_grid.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:horizontalSpacing="4dip"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="4dip"
android:padding="4dip" />
And this is my item_grid_image.xml :
<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/image"
android:layout_width="fill_parent"
android:layout_height="120dip"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/icon_text"
android:typeface="serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textStyle="bold"
android:lines="2">
</TextView>
</LinearLayout>
I know that I have to implement it in this function, but I don't know how:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = (ImageView) getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
} else {
imageView = (ImageView) convertView;
}
imageLoader.displayImage(imageUrlsSmall.get(position), imageView, options);
return imageView;
}
UPDATE: Finally I did it with this code.
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = getLayoutInflater().inflate(R.layout.item_grid_image, parent, false);
holder = new ViewHolder();
holder.text = (TextView) view.findViewById(R.id.icon_text);
holder.image = (ImageView) view.findViewById(R.id.image);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.text.setText(album[position].getName());
imageLoader.displayImage(albumsPhoto[position], holder.image, options, animateFirstListener);
return view;
}
Perhaps something like this;
private TextView mInfoView;
OnCreate():
mInfoView = (TextView) findViewById(R.id.icon_text);
getView():
// after imageLoader.displayImage
mInfoView.setBackgroundColor(Color.TRANSPARENT);
mInfoView.setText("Some description");
Related
I am trying to build a layout as shown in the image where the Date(TextView) is shown above the images in GridView and the TextView appears multiple times in view .I am confused on how to implement it. I tried putting TextView in list item but in that case TextView is repeating for each row. Can anyone suggest how to go with this.Reference image attached.
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/lvTimeline"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rlText">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="28 Feb"
android:layout_marginLeft="16dp"
android:layout_alignParentLeft="true"
android:id="#+id/tvDate"/>
</RelativeLayout>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gvTest"
android:numColumns="3"
android:verticalSpacing="3dp">
</GridView>
gridview_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="116dp"
android:layout_height="116dp"
android:scaleType="fitXY"
android:id="#+id/ivTimeline"
android:src="#android:drawable/sym_def_app_icon"/>
List adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder();
holder.tvDate = (TextView) view.findViewById(R.id.tvDate);
holder.gvTimeline = (GridView) convertView
.findViewById(R.id.gvTest);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.gvTimeline.setAdapter(new GridAdapter(imagesList));
return view;
}
public static class ViewHolder {
TextView tvDate;
GridView gvTimeline;
}
GridView Adapter :
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.gridview_item, parent, false);
holder = new ViewHolder();
holder.ivImage = (ImageView) view.findViewById(R.id.ivTimeline);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.ivImage.setImageResource(getItem(position));
return view;
}
public static class ViewHolder {
ImageView ivImage;
}
Image which I am getting now :
I have an listView with TextView and another horizontal listView corresponding to that listView. now i need to write onclick event for list item in the horizontal listview.i can't get the parent list item position(vertical listview position).please help me. Thanks in advance.
// First Listview for Vertical
<RelativeLayout 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" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null" />
</RelativeLayout>
//Adapter Getview for first list
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.list_item, null);
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(mArrayList.get(position).getSite());
typeId = mArrayList.get(position).getTypeId();
hori_view = (TwoWayView) convertView.findViewById(R.id.hor_list);
mChildList = mArrayList.get(position).getChildArrList();
HorizontalAdapter horListAdapter = new HorizontalAdapter(mContext, R.layout.hori_list,mChildList);
hori_view.setAdapter(horListAdapter);
horListAdapter.notifyDataSetChanged();
hori_view.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
mChildList = mArrayList.get(position).getChildArrList(); // Here i need parent list position
String text = mChildList.get(position).getChannel().toString();
int cId = mChildList.get(position).getChannelId();
int pen = mChildList.get(position).getPending();
int typeId = mChildList.get(position).getSiteid();
Intent intent = new Intent(mContext,ChannelDetails.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Bundle bundle = new Bundle();
bundle.putString("channel", text);
bundle.putInt("typeId", typeId);
intent.putExtras(bundle);
mContext.startActivity(intent);
}
});
return convertView;
}
// Second list for horizontal view
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:clickable="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Title"
android:paddingTop="10dp"
android:paddingLeft="15dp"
android:textColor="#000000"
android:textStyle="bold"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="10dp">
<org.lucasr.twowayview.TwoWayView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/hor_list"
style="#style/TwoWayView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
tools:context=".MainActivity" />
</LinearLayout>
</LinearLayout>
// second adapter for second list(Horizontal list)
public View getView(int position, View convertView, ViewGroup parent)
{
View retval = null;
if(retval == null)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
retval = mInflater.inflate(this.resource, null);
holder = new Holder();
}
holder.image = (ImageView) retval.findViewById(R.id.image);
holder.pending = (TextView) retval.findViewById(R.id.textOne);
holder.channel = (TextView) retval.findViewById(R.id.text1);
holder.chan_id = (TextView) retval.findViewById(R.id.text2);
holder.text3 = (TextView) retval.findViewById(R.id.text3);
mChannel = mChildList.get(position).getChannel();
holder.channel.setText(mChannel);
holder.pending.setText(String.valueOf(mChildList.get(position).getPending()));
holder.chan_id.setText(String.valueOf(mChildList.get(position).getChannelId()));
if(mChildList.get(position).getSiteid() == 100)
{
holder.image.setBackgroundResource(R.drawable.fb_icon1);
holder.text3.setText("facebook");
}
else if(mChildList.get(position).getSiteid() == 200)
{
holder.image.setBackgroundResource(R.drawable.tr_icon1);
holder.text3.setText("twitter");
}
else if(mChildList.get(position).getSiteid() == 300)
{
holder.image.setBackgroundResource(R.drawable.in_icon1);
holder.text3.setText("linkedin");
}
retval.setTag(holder);
return retval;
}
}
I'm trying to add a custom view programmatically into a listview of images in the custom adapter. Currently I'm trying to add it as the third "image". Not that steady on my customer adapter, so not sure how to get it to work. Populating all the images works perfect, but cant get that ad space in between there. Any suggestions?
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.image_row, null);
ViewHolder viewHolder = new ViewHolder();
if(position==3) {
adSpace = new AdSpace(context, "c026dd7e-60a5-46c9-948f-38eb18a", true);
adSpace.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
Log.i("AdSpace","Adding adspace");
} else {
viewHolder.image = (ImageView) rowView.findViewById(R.id.offer_row);
}
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
imageLoader.displayImage(imageURLArray.get(position), holder.image, options);
return rowView;
}
static class ViewHolder {
public ImageView image;
public AdSpace adspace;
}
list.xml:
<?xml version="1.0" encoding="utf-8"?>
<"packagename".ZoomView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/offer_list" />
image_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/"packagename""
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/offer_row"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:contentDescription="Image" />
You are creating adSpace but never using it.
if(position==3) {
adSpace = new AdSpace(context, "c026dd7e-60a5-46c9-948f-38eb18a", true);
adSpace.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
Log.i("AdSpace","Adding adspace");
return adSpace;
}
You can remove adSpace from the viewholder.
This is the row of my custom listview. As you can see, there are three imageViews. How should I override the
public View getView(int position, View convertView, ViewGroup parent)
of my adapter class, such that, I can set imageView.setImageResource, no matter how many imageviews I have in my list. Any help is much appreciated.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/LinearLayout01"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="10px" />
<ImageView
android:id="#+id/image1"
android:layout_toRightOf= "#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10px" />
<ImageView
android:id="#+id/image2"
android:layout_toRightOf= "#+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10px" />
</RelativeLayout>
</LinearLayout>
Take on holder class inside your adapter
// View Holder for fast accessing List Row
private class ViewHolder {
public ImageView image;
public ImageView image1;
public ImageView image2;
}
And getView method
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.row_layout,null);
holder.image = (ImageView)convertView.findViewById(R.id.image);
holder.image1 = (ImageView)convertView.findViewById(R.id.image1);
holder.image2 = (ImageView)convertView.findViewById(R.id.image2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Setting Image Resource here
holder.image.setImageResource(R.drawable.icon);
holder.image1.setImageResource(R.drawable.icon);
holder.image2.setImageResource(R.drawable.icon);
// return the listview row
return convertView;
}
In the ListView row layout why you take LinearLayout and then RelativeLayout inside it, Just take RelativeLayout as a root node.
I want to display some text below each grid image.
Please see my code below.
Please help me in finding why its not working
I am getting the images properly displayed but no text
if i try to display text within image its coming
so it should be something with the layout defined. but i am not able to debug
adaptor code :
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.griditems, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.grid_item_text);
Log.i(TAG, holder.text1.toString());
holder.image = (ImageView) convertView
.findViewById(R.id.grid_item_image);
// if it's not recycled, initialize some attributes
holder.image.setImageResource(gridItemIds[position]);
holder.text1.setText(gridTitles[position]);
//holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
// holder.image.setPadding(20, 20, 20, 20);
convertView.setLayoutParams(new GridView.LayoutParams(Utils
.getLayoutParameter(), Utils.getLayoutParameter()));
holder.image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView text1;
ImageView image;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ImageView android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#000000"
>
</TextView>
</LinearLayout>
gridview :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/text1view" android:textStyle="bold"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center_horizontal" android:background="#drawable/title_bar"
/>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:verticalSpacing="50dip"
android:horizontalSpacing="10dp" android:numColumns="3"
android:stretchMode="columnWidth"
android:layout_below="#+id/text1view"
android:gravity="center"
android:layout_centerHorizontal="true"
android:background="#drawable/home_bg"
/>
</LinearLayout>
I used your xml but with a simple array adapter that I wrote and everything work perfectly, try to replace your adapter with mine and let me know, thanks.
class ItemsAdapter extends ArrayAdapter<String> {
public ItemsAdapter(Context context, List<String> list) {
super(context, R.layout.griditems, list);
}
#Override
public View getView(final int position, View row, final ViewGroup parent) {
final String item = getItem(position);
ItemWrapper wrapper = null;
if (row == null) {
row = getLayoutInflater().inflate(R.layout.griditems, parent, false);
wrapper = new ItemWrapper(row);
row.setTag(wrapper);
} else {
wrapper = (ItemWrapper) row.getTag();
}
wrapper.refreshData(item);
return row;
}
}
static class ItemWrapper {
TextView text;
ImageView img;
public ItemWrapper(View row) {
text = (TextView) row.findViewById(R.id.grid_item_text);
img = (ImageView) row.findViewById(R.id.grid_item_image);
}
public void refreshData(String item) {
img.setImageResource(R.drawable.image_1);
text.setText(item);
}
}
Also try using RelativeLayout too, placing the TextView underneath the ImageView by using
android:layout_below="#id/grid_item_image"
It'll place it underneath the image, however, if you have the option to use the Graphical Layout, dragging it where you want it in conjunction with the image will set all the parameters for you instead of typing them out.