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!
Related
i am trying to use an ImageView over an ImageView in a GridView. I already found this quesiton Android, ImageView over ImageView and it is really familiar with my problem, but i want to use this Layout descripted in the question inside a GridView.
So i created a .xml-file containing the <GridView></GridView> (i think this is not really useful to show, so i skip this file) and a .xml-file with the layout i want to use in every cell in the gridview. This is the following file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rl_act_question_list_alternative" >
<ImageView
android:id="#+id/iv_question_list_country"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/germany" />
<ImageView
android:id="#+id/iv_question_list_solved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/icon_question_solved" />
As the next step i am creating a subclass of a BaseAdapter overriding the method getView(int position, View convertView, ViewGroup parent) with the following code:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(ActivityLevelsAlternative.this).inflate(R.layout.act_question_list_alternative_item, null);
}
ImageView mCountry = (ImageView) convertView.findViewById(R.id.iv_question_list_country);
ImageView mIconSolved = (ImageView) convertView.findViewById(R.id.iv_question_list_solved);
final VOQuestion question = mLevel.getAllQuestions().get(position);
mCountry.setImageResource(question.getPicture());
mIconSolved.setImageResource(mPrefManager.isQuestionSolved(question.getId()) ? R.drawable.icon_question_solved : R.drawable.icon_question_unsolved);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), ActivityQuestion.class);
i.putExtra(ActivityQuestion.QUESTION_ID, question.getId());
ActivityLevelsAlternative.this.startActivity(i);
}
});
return convertView;
}
But the the result is not the expected... see: http://q63i.img-up.net/Screenshotc656.png
My question: how can i get the check mark over the country image?
Try to change your check mark image like this:
<ImageView
android:id="#+id/iv_question_list_solved"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignTop="#id/iv_question_list_country"
android:layout_alignLeft="#id/iv_question_list_country"
android:layout_alignRight="#id/iv_question_list_country"
android:layout_alignBottom="#id/iv_question_list_country"/>
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.
I want to display a custom view, composed of a TextView above an ImageView in a vertical LinearLayout, one at a time in a Gallery.
The problem is that my custom view does not fill the screen. I can see a part of the other views on the sides and I don't want this issue.
Here is the xml of my custom view : gallery_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gallery_item_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/gallery_item_cardname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp"
android:textStyle="bold"
android:text="#string/contrebandiers_lvl1"
android:textColor="#android:color/darker_gray"
/>
<ImageView
android:id="#+id/gallery_item_cardimg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:contentDescription="#string/app_name"
android:src="#drawable/contrebandiers_lvl1"
/>
</LinearLayout>
Here is the code of the method getVew of my adapter : GTRoundDeckAdapter
public View getView(int position, View convertView, ViewGroup parent)
{
GalleryItem galleryItem;
if(convertView == null)
{
galleryItem = new GalleryItem();
convertView = mInflater.inflate(R.layout.gallery_item, null);
galleryItem.cardName = (TextView) convertView.findViewById(R.id.gallery_item_cardname);
galleryItem.cardImg = (ImageView) convertView.findViewById(R.id.gallery_item_cardimg);
convertView.setTag(galleryItem);
}
else
{
galleryItem = (GalleryItem) convertView.getTag();
}
GTCard gtCard = (GTCard) mRoundDeck.get(position);
galleryItem.cardName.setText(gtCard.getNameId());
galleryItem.cardImg.setImageResource(gtCard.getImgId());
return convertView;
}
I thank you for your help.
Samuel
I would recommend that you use the ViewPager instead of Gallery. In my personal experience, I have found Gallery to slightly buggy. If your requirement is to show only one View at a time, then ViewPager (which lets you swipe left and right to go one by one) should suit your needs.
You will need to include the Android Support package to use ViewPager.
Link to support package: http://developer.android.com/sdk/compatibility-library.html
Link on how to use ViewPager: http://blog.stylingandroid.com/archives/537
you should use both the width of textview and imageview fillparent.then it will be fill the screen of the gallery...
I am not quite sure my question really has something to do with listview.There is an app named Gleeo Time Tracker ,and here has a screenview of it.When you press the symbol "+",a new item will be created,and you can delete one item by pressing the "-".More is that when I click the record button on the left of the item,the background will change.
My question is,what is it in the end,a listview? How can I achieve such thing?Thank you all!
What you want to do, is build a custom ListView.
This is done by providing a layout file, for example
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent">
<ImageView android:id="#+id/Plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
<TextView android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
</LinearLayout>
This layout is then applied to each row of your ListView by overriding its Adapter's getView() method, for example something like this:
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
ImageView plus = (ImageView) v.findViewById(R.id.plus);
icon.setImageDrawable(BrowseActivity.this.getResources().getDrawable(R.drawable.plus));
TextView title = (TextView) v.findViewById (R.id.Browse_FileName);
// add a listener to your button and you're done
return v;
}
read about list view in developer docs and here is an example
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.