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.
Related
I am using ListFragment and ArrayAdapter to show a Image view list using following list_view.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"/>
In my Adapter.getview I am using picasso to load the image:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (null == convertView) {
convertView = inflater.inflate(R.layout.list_view, parent, false);
}
Picasso
.with(context)
.load("http://i.imgur.com/rFLNqWI.jpg")
.fit()
.into((ImageView) convertView);
return convertView;
}
This is onActivityCreated method in Fragment class extended by ListFragment:
TestAdapter adapt = new TestAdapter(getActivity().getApplicationContext(), R.layout.list_view, pictures);
setListAdapter(adapt);
Above code is giving me a listview where I can see the images loading from picasso library. How can I add text and image on the image loaded by picasso in listview? Inshort like this:
You can see one text "Type text here" in left bottom corner and one icon on right top corner (not exactly in the corner)
You could implement a Picasso Transformation where you take the bitmap and then use the Canvas primitives to draw/render the text on top of the Bitmap and return the final bitmap.
Another approach would be to use a RelativeLayout with 3 sub-views: an ImageView which stretches to its parent (the RelativeLayout) so it becomes the overall background; and then a TextView for your text and finally another ImageView for your icon.
Create list_view.xml like this
<Relative layout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:text="Type your text"
android:layout_alignParentBottom="true"
android:layout_margin="20dp"
android:layout_height="200dp"/>
</Relative>
you can use ViewHolder to set TextView and image both, and then use it with picasso.
Here's the code snippet.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.titleTextView = (TextView) row.findViewById(R.id.grid_item_title);
holder.imageView = (ImageView) row.findViewById(R.id.grid_item_image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
GridItem item = mGridData.get(position);
holder.titleTextView.setText(Html.fromHtml(item.getTitle()));
Picasso.with(mContext).load(item.getImage()).into(holder.imageView);
return row;
}
For more details refer
http://javatechig.com/android/download-and-display-image-in-android-gridview
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 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.
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.
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!