How do I add a TextView under each image in a gridlayout? - android

I need to display the image description under each image but I get an error that i can't cast GridView to TextView:
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<GridView
android:id="#+id/main_gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:padding="10dp"
android:verticalSpacing="30dp" />
<TextView
android:id="#+id/tvIconDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />
</LinearLayout>
Code:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
TextView textView;
if (convertView == null) {
textView = new TextView(mContext);
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
} else {
imageView = (ImageView) convertView;
textView = (TextView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
textView.setText(thumbDesc[position]);
return imageView;
}

convertView is going to be your parent View which in this case is a GridView. That is why you are getting the cannot convert error. You are trying to cast it to ImageView (and TextView for that matter).
You should put both the ImageView and the TextView together inside of a cell.xml layout file. Then inflate and populate that xml file during your getView() call instead of making new instancesof TextView / ImageView for each cell in your grid.
Here is a tutorial that covers these topics. He has even used your specific example (Text and Image in each cell). Once you complete this tutorial you should only have to modify it slightly to make the text go under the image if that is how you want it to appear.

Related

increase images sizes loads in carousel view android

I created app to display images from internet in carousel view. I loaded images into carousel view using imageview. just like following way.
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.showsmain, null);
ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
// RelativeLayout rlayout=(RelativeLayout)vi.findViewById(R.id.rlayouts);
image.setImageResource(R.drawable.black);
orgWidth = image.getDrawable().getIntrinsicWidth();
orgHeight = image.getDrawable().getIntrinsicHeight();
imageLoader.DisplayImage(data[position], image);
imageLoader.getDimension(widthScreen, heightScreen);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthScreen, heightScreen/3);
params.width=widthScreen;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_XY);
//rlayout.addView(image);
return vi;
}
and my carousel layout xml file is,
<com.touchmenotapps.carousel.simple.HorizontalCarouselLayout
android:id="#+id/carousel_layout_event"
android:layout_width="600dp"
android:layout_height="500dp"
android:layout_above="#+id/relativeLayout1"
android:layout_alignTop="#+id/carousel_layout" >
</com.touchmenotapps.carousel.simple.HorizontalCarouselLayout>
then image show as following way in carousel,
![enter image description here][1]
Here i want to increase the image width & height. It means one images must load fill with in the screen
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.showimage, null);
ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
//LinearLayout rlayout=(LinearLayout)vi.findViewById(R.id.layout);
image.setImageResource(R.drawable.black);
orgWidth = image.getDrawable().getIntrinsicWidth();
orgHeight = image.getDrawable().getIntrinsicHeight();
imageLoader.DisplayImage(data[position], image);
imageLoader.getDimension(widthScreen, heightScreen);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthScreen,heightScreen/3);
params.width=widthScreen/2;
image.setLayoutParams(params);
image.setScaleType(ImageView.ScaleType.FIT_XY);
//rlayout.addView(image);
return vi;
}
this way I add the imageview into carousel view.
how to do that. I asked question similer this one. But i didn't get answer. pls help me someone. pls.....
my imageview xml layout file..
<?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="fill_parent"
android:id="#+id/rlayouts">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:scaleType="fitXY"
android:layout_alignParentTop="true" />
</RelativeLayout>
I think you should use a Horizontal oriented Linear layout inside a Horizontal ScrollView then you should add ImageViews with FillParent Height and Width in this Linear layout.
It would be great if you elaborate your question a bit more.

ImageView is still null, after findbyView

Hey im trying to get a List working, in that case i have a xml defined item, which i inflate in a extended ArrayAdapter. But after i have inflated the xml, then when i try to grab the imageview, it returns null by findviewbyid...
Here is xml for the item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/product_item_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="if no image avail show this"
android:visibility="gone"/>
<ImageView
android:id="#+id/product_item_imageview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
/>
</LinearLayout>
Method that instatiates the item:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SimpleListItem item= items.get(position);
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.product_item, null);
if(item.getPicture()!=null)
{
ImageView imgVw = (ImageView) findViewById(R.id.product_item_imageview);
String picturePath = item.getPicture();
Bitmap picture = ImageManager.get(getBaseContext(), picturePath);
imgVw.setImageBitmap(picture);
} else {
TextView txtVw = (TextView) findViewById(R.id.product_item_textview);
txtVw.setText(item.getText());
txtVw.setVisibility(View.VISIBLE);
}
return v;
}
Its a,
ImageView imgVw = (ImageView) v.findViewById(R.id.product_item_imageview);
also the same for TextView,
TextView txtVw = (TextView) v.findViewById(R.id.product_item_textview);
Use View v for getting ImageView and TextView from your inflated xml file.
//you are inflating layout in a view v so you need to get them by
ImageView imgVw = (ImageView)v. findViewById(R.id.product_item_imageview);
TextView txtVw = (TextView)v. findViewById(R.id.product_item_textview);

Check condition only display and dynamically listview all items with difference height

1.Check condition only display
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.main_alllatestnewslist, parent,
false);
ImageView imageview = (ImageView) vi
.findViewById(R.id.image_alllatestnewstitle);
imageview.setVisibility(View.INVISIBLE);
TextView titletext = (TextView) vi
.findViewById(R.id.text_particularlatestnewstitle);
TextView categorytext = (TextView) vi
.findViewById(R.id.text_newscategorytitle);
TextView datetext = (TextView) vi.findViewById(R.id.text_newsdate);
if (!imagepath[position].toString().equals("no picture")) {
imageLoader.DisplayImage(imagepath[position], imageview);
imageview.setVisibility(View.VISIBLE);
titletext.setWidth(460 - imageview.getWidth() - 5); <-- this line
} else {
imageview.setVisibility(View.INVISIBLE);
imageview.setImageDrawable(null);
}
titletext.setText(title[position].toString());
categorytext.setText(category[position].toString());
datetext.setText(date[position].toString());
return vi;
}
The statement cannot be check when first view of the list and causing the textview stack with imageview. After scroll one or more times it only move to left.
How to go in this statement before display it?
2. set all items' height in listview programmatically base on condition
RelativeLayout childlayout = (RelativeLayout)vi.findViewById(R.id.layout_childlist);
This is the layout for an item.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_childlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/list_bg"
android:minHeight="?android:attr/listPreferredItemHeight" >
<LinearLayout
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="5px"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
android:layout_marginTop="5px" >
<ImageView
android:id="#+id/image_alllatestnewstitle"
android:layout_width="140px"
android:layout_height="80px"
android:scaleType="centerCrop" />
</LinearLayout>
....
</RelativeLayout>
For example, one of the item took 80px to display but the other items only took 40px. However, the other items will not wrap to 40px and wrap to 80px and make all the items has same height.
How to set the height for all items with difference height?
Try imageview.setVisibility(View.GONE);
INVISIBLE only hides the view while GONE 'removes' it

Switch between Gallery and a LinearLayout - ClassCastException

Thanks for reading!
I am building a custom Gallery app where the first thumbnail is an album cover displaying album details. Here's the flow:
getView() {
//inflate cover.xml which includes two textviews and an imageview.
if(position == 0)
//set some album-specific text
else
//set image-specific text
}
Here's the actual getView() code:
public View getView(int position, View convertView, ViewGroup parent) {
//TODO: Recycle view
convertView = mInflater.inflate(R.layout.cover, null);
TextView tvTxt1 = (TextView)convertView.findViewById(R.cover.tvCoverText1);
TextView tvTxt2 = (TextView)convertView.findViewById(R.cover.tvCoverText2);
//ImageView imgView = (ImageView)convertView.findViewById(R.cover.imgImage);
if(position == 0) {
tvTxt1.setText("AlbumText1");
tvTxt2.setText("AlbumText2");
return convertView;
}
else {
tvTxt1.setText("ImageText1");
tvTxt2.setText("ImageText2");
ImageView imgView = new ImageView(mContext);
imgView.setImageResource(mImageIds[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(mGalleryItemBackground);
return imgView;
//return convertView;
}
}
The cover.xml contains an ImageView and two TextViews.
when I return convertView in the else block, I get a ClassCastException. I am certainly doing something wrong.
I have spent almost two days on this now :(
Please help!
Here's what it looks like to me. When position == 0 you are returning convertView, which is a View. When you return "else", you are returning a ImageView. Your method is set to return a View. Try casting your ImageView to a View before returning it.
Try: return (View) imgView;
Never tried it myself though...
Add this imageview into your layout xml, and then retrieve it from convertview and at the end return the convert view.
This may solve the problem.
I have worked a lot on Gallery widget, if there is more problem do let me know.
After trying all the suggestions given by helpful people here,I still wasn't able to get across the ClassCastException.
So, as a workaround - I sort of "overlayed" the Gallery with other views that I wanted to enable/disable.
This is a workaround, so if someone comes up with a better answer - do post it here so I can accept it.
So here's what worked for me:
public View getView(int position, View convertView, ViewGroup parent) {
//TODO: Recycle view
//onvertView = mInflater.inflate(R.layout.cover, null);
//ImageView imgView = (ImageView)convertView.findViewById(R.cover.imgImage);
ImageView imgView = new ImageView(mContext);
imgView.setImageResource(mImageIds[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(mGalleryItemBackground);
if(position == 0) {
tvText1.setText("AlbumText1");
tvText2.setText("AlbumText2");
tvText3.setVisibility(View.VISIBLE);
bottomBar.setVisibility(View.VISIBLE);
}
else {
tvText1.setText("ImageText1");
tvText2.setText("ImageText2");
tvText3.setVisibility(View.GONE);
bottomBar.setVisibility(View.GONE);
}
return imgView;
}
Here's my layout main.xml file:
<?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="fill_parent">
<Gallery android:id="#+main/gallery" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<!-- <ImageView android:id="#+main/imgImage" -->
<!-- android:layout_width="fill_parent" android:layout_height="fill_parent" -->
<!-- android:adjustViewBounds="true"> -->
<!-- </ImageView> -->
<TextView android:id="#+main/tvText2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:singleLine="true"
android:maxLines="1" android:text="Text2"
android:layout_alignParentBottom="true" />
<TextView android:id="#+main/tvText1" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:maxLines="2"
android:text="Text1" android:layout_above="#main/tvText2" />
<RelativeLayout android:id="#+main/bottomBar" android:layout_alignParentBottom="true"
android:layout_width="fill_parent" android:layout_height="40dip"
android:background="#A3A1A1">
<TextView android:id="#+main/tvBottomText" android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="BottomBarText"/>
</RelativeLayout>
</RelativeLayout>
The rest of the code in Main.java (whose getView method I modified) is almost verbatim from here
Thanks again for helping out!

to much space between images in gallery android

Hello i have an activity that loads lots of images from the drawable folder, and put them in a gallery view from an Adapter. The problem is that there's too much space between this images.
Here's the getView i use:
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = new ImageView(mContext);
convertView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
ImageView iv = (ImageView) convertView;
iv.setImageResource(mThumbIds[position]);
return convertView;
}
Here is my xml layout:
<Gallery android:id="#+id/gallery"
android:background="#000000"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
/>
Is there any way i can get the images to be next to each other without having to create a new view for every image?
Best Regards.
Try using android:spacing in the layout file, to decrease space between images.

Categories

Resources