ClassCastException (Gallery + ImageView) - android

I have followed a tutorial to create a gallery with an imageview below it. All I want to do is place the gallery on the bottom and the imageview above it, but when I switch them around in the XML it gives me a ClassCastException.
Here is the XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/backrepeat"
android:orientation="vertical" >
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Gallery
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
That is the non-working version, but placing gallery above imageview works.
From log:
01-26 22:20:13.673: E/AndroidRuntime(349): Caused by: java.lang.ClassCastException: android.widget.ImageView
01-26 22:20:13.673: E/AndroidRuntime(349): at com.appinfluence.fanapp.v1.Photos.onCreate(Photos.java:36)
onCreate:
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(Photos.this));
imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(mImageIds[0]);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(mImageIds[position]);
}
});
}
Other helpful code:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}

Hey NickNelson Just now i got the solution to your problem, just clean the project it will work.
Sample screenshot

No need to use separate ImageView in the layout, Gallery will display the images. Remove the ImageView and check it..
edit
just clean the project . ( as yugandhar said.)

Kindly try this XML for the layout section:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical">
<ImageView android:layout_alignParentTop="true"
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/antartica1"/>
<Gallery
android:id="#+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentBottom="true" />
</RelativeLayout>

Related

ImageView's size can't adapt with image from net in GridView

I have a question , it make me tired whole day.
Why ImageView's background is show when load net picture into ImageView in GridView? ImageView's size can't wrap_content.
How could I fix it? Make ImageView's size adapt picture size (don't change the piacture size)
thanks advance.
A .Main XML
<ListView
android:id="#+id/gridview_listview"
android:layout_width="100dp"
android:background="#00000000"
android:scrollbars="none"
android:dividerHeight="5dp"
android:divider="#color/colorAlpha"
android:listSelector="#drawable/list_item_selected"
android:layout_height="match_parent" />
<GridView
android:id="#+id/gridview"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_width="0dp"
android:scrollbars="none"
android:listSelector="#drawable/gridview_item_selected"
android:layout_height="wrap_content">
</GridView>
B.GridView Item XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="1dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_gridview"
android:padding="0dp"
android:background="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/tv_gridview"
android:text="test"
android:textColor="#00ffff"
android:textSize="15sp"
android:gravity="center"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
C.GridView adapter extends BaseAdapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
GridViewHolder gridViewHolder = new GridViewHolder();
ImageView imageView;
TextView textView;
if (convertView == null ) {
convertView = layoutInflater.inflate(R.layout.gridview_item , null) ;
imageView = (ImageView) convertView.findViewById(R.id.iv_gridview);
textView = (TextView) convertView.findViewById(R.id.tv_gridview);
gridViewHolder.setImageView(imageView);
gridViewHolder.setTextView(textView);
convertView.setTag(gridViewHolder);
}else {
gridViewHolder = (GridViewHolder) convertView.getTag();
}
Picasso.with(context).load("http://img1.cache.netease.com/catchpic/A/A8/A86B03AE11945B98D0AE0A98480D28CE.jpg").into(gridViewHolder.getImageView());
gridViewHolder.getTextView().setText(dataList.get(position).getName());
return convertView;
}
D.Activity code
dataList = GetData.getData();
GridViewAdapter gridViewAdapter = new GridViewAdapter(GridView.this , dataList);
gridView.setAdapter(gridViewAdapter);
gridView.setNumColumns(3);
gridView.setHorizontalSpacing(5);
gridView.setVerticalSpacing(10);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String url = "this is " + position ;
startActivity(new Intent(GridView.this , ClickActivity.class).putExtra("url" ,url));
}
});
Try setting the android:adjustViewBounds attribute on your ImageView to true in your layout XML.
<ImageView
android:id="#+id/iv_gridview"
android:padding="0dp"
android:background="#ff0000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true" />
From the documentation on android:adjustViewBounds:
Set this to true if you want the ImageView to adjust its bounds to
preserve the aspect ratio of its drawable.

Gallery Image not fitting the screen in android

I am displaying a bunch of images in the Gallery View.
The layout is ,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<Gallery
android:id="#+id/gallery1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spacing="10dip" >
</Gallery>
<ImageView
android:id="#+id/imgfromWebUrl"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ImageView>
</LinearLayout>
and the custom adapter for loading images,
public View getView(int arg0, View paramView, ViewGroup arg2) {
Log.d("","custom Adapter5");
View localView;
if (paramView == null) {
localView = mInflater.inflate(R.layout.property_image_adapter, null);
} else {
localView = paramView;
}
ImageView imgfromWebUrl=(ImageView)localView.findViewById(R.id.imgfromWebUrl);
Log.d("", "pics[0]: "+pics);
imgfromWebUrl.setImageBitmap(pics[arg0]);
imgfromWebUrl.setScaleType(ImageView.ScaleType.FIT_XY);
imgfromWebUrl.setBackgroundResource(defaultItemBackground);
imgfromWebUrl.setLayoutParams(new Gallery.LayoutParams(200, 200));
return imgfromWebUrl;
}
currently my image is shown as,
My image is fit to the center part of the screen. but I want the image to fit full screen,like this image
Please help me out!!
Any help is Appreciated!!
Use this property android:scaleType="fitXY" Document for ImageView.ScaleType
Issue:
Image is fitting to the parent.but your gallery params are fixed of 200.
Solution:
kindly remove that line.
imgfromWebUrl.setLayoutParams(new Gallery.LayoutParams(200, 200));
and make this change in xml:
<Gallery
android:id="#+id/gallery1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:spacing="10dip" >
Configure the layout parameters like so:
i.setLayoutParams( new
Gallery.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT));

Design looks bigger when using minsdkversion 3

I created an app, it's finely working as what i want on minsdkversion="3". But other activities look even bigger than usual. If i change the minsdkversion="8" or something, the other activities looks as normal, but i didn't get the output i want.
Following is the code for gridview and imageview layout files:
activity_main.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<GridView
android:id="#+id/grid_View"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="0dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp"
android:gravity="center_horizontal"
android:clipChildren="true" >
</GridView>
</LinearLayout>
grid.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" >
<ImageView
android:id="#+id/image_View"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.05"/>
</LinearLayout>
MainActivity.java:
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
i = i+1;
imageView.setId(i);
Log.d("===== i value =====", String.valueOf(i));
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
What is the wrong with my code, what i have to change?
I have to show some toast when particular body part is pressed
Step #1: Display the body in an ImageView
Step #2: Override onTouchEvent() in your activity or fragment that is managing the ImageView
Step #3: Determine the body part based on the location of the touch event and the size of the ImageView, and display your Toast

Android - GridView height to max height

I've got a GridView inside a scrollview and the GridView has 4 images loaded into it (via an ImageAdapter). My issue is I can get 3 of the images to show but the 4th doesn't. After further investigation, I've found that the height of the gridview is only the height of the row, so if I set the gridview height in xml to 700dp, then it shows up.
Here's a screenshot:
As you can see, I've set the background of the GridView to HotPink to illustrate where the GridView is.
Here's the XML for the main_menu:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llLayoutContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- <LinearLayout android:id="#+id/llMiddle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"> -->
<LinearLayout
android:id="#+id/llLeft"
android:layout_width="400dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/layout_left"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<ScrollView
android:id="#+id/svMenu"
android:layout_width="400dp"
android:layout_height="match_parent"
>
</ScrollView>
</LinearLayout>
<LinearLayout
android:id="#+id/llRight"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:background="#drawable/layout_right"
android:padding="0dp">
</LinearLayout>
<!-- </LinearLayout> -->
</LinearLayout>
The LinearLayout "llLeft" is the layout where the menu items get loaded (inside the ScrollView). layout_left is only a shape which draws the dark outline around the greenish background.
Here's the main_menu_header.xml which contains the GridView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="5dp">
<TextView
android:id="#+id/tvDashboardHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Dashboard Main Menu"
android:textSize="20dp"
style="#style/TextShadow"
android:paddingTop="5dp"
android:gravity="left"
android:background="#drawable/menu_item_bg"
/>
<GridView
android:id="#+id/gvMenuItems"
android:layout_width="400dp"
android:layout_height="match_parent"
android:columnWidth="110dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:background="#color/HotPink"
android:gravity="center" >
</GridView>
</LinearLayout>
And here's how I populate the GridView:
ScrollView sv = (ScrollView)findViewById(R.id.svMenu);
LayoutInflater liInflater = getLayoutInflater();
ViewGroup vg = (ViewGroup)liInflater.inflate(R.layout.main_menu_header, sv, false);
sv.addView(vg);
//Setup the Main Menu items on the left side.
GridView gvMenuItems = (GridView) findViewById(R.id.gvMenuItems);
gvMenuItems.setAdapter(new ImageAdapter(this));
ImageAdapter class:
ImageAdapter class:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(parent.getLayoutParams().width, 125)); //new GridView.LayoutParams(400, 125));
//imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setMaxHeight(50);
imageView.setMaxWidth(50);
//imageView.setPadding(30, 0, 0, 0);
//imageView.setPadding(40, 30, 20, 30);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.home,
R.drawable.open_folder,
R.drawable.paper_pen,
R.drawable.person
};
}
I thought the gridview property of android:layout_height="match_parent" would work but it's not. Any ideas as to how I can get the GridView to take up the whole left side, inside the scrollview?
My best guess is that scrollview is giving you problem.
Why do u need scrollview for?
Grid View will handle the scrolling. It is kind of redundant.So please remove it and add inflated view directly to llLeft directly. That shall solve the problem.
In fact, it would be more elegant just to use xml to solve your UI problem. Use <include> tag in your main_menu.xml. Here is how
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llLayoutContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<!-- <LinearLayout android:id="#+id/llMiddle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"> -->
<LinearLayout
android:id="#+id/llLeft"
android:layout_width="400dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/layout_left"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/main_menu_header" />
</LinearLayout>
<LinearLayout
android:id="#+id/llRight"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:background="#drawable/layout_right"
android:padding="0dp">
</LinearLayout>
<!-- </LinearLayout> -->

A grid layout of icon/text buttons

I am attempting to create a 3 x 3 grid of items. Each Item consists of an ImageView on top of a TextView. Unfortunately, I am having issues getting everything to play nicely.
Here is my attempt to get 2 such items side by side. The text views don't even show, and the icons are squished together (instead of evenly spaced)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1" android:gravity="center"
android:paddingLeft="40px" android:paddingRight="40px" >
<TableRow>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/usertoolsimage"
android:src="#drawable/ftnicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="User Accounts"
android:gravity="right"
android:padding="3dip" android:textColor="#ffffff" />
</LinearLayout>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/queueimage"
android:src="#drawable/ftnicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="Queue Management"
android:gravity="right"
android:padding="3dip" android:textColor="#ffffff" />
</LinearLayout>
</TableRow>
<TableRow>
<TextView
android:text="test 3"
android:padding="3dip" android:textColor="#ffffff" />
<TextView
android:text="test 4"
android:gravity="right"
android:padding="3dip" android:textColor="#ffffff" />
</TableRow>
</TableLayout>
My goal in the end is to have a grid of clickable items where the item is an image and text for a main menu. Can anyone guide me on what layouts I should use to achieve this?
Your best bet in my opinion would be to use the gridView that way it supports scrolling and spacing and you can be very dynamic in what each items layout and events are. Another option is to just create a lay out the images with a combination of Relative/Linear Layouts.
GridView layout:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="3"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
and then in your activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mGame.status = GameStatus.PLAYING;
setContentView(R.layout.gridLayout);
GridView grid = (GridView) findViewById(R.id.myGrid);
grid.setAdapter(new customAdapter());
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
//do some stuff here on click
}
});
}
public class customAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
//create a basic imageview here or inflate a complex layout with
//getLayoutInflator().inflate(R.layout...)
ImageView i = new ImageView(this);
i.setImageResource(mFams.get(position).imageId);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
final int w = (int) (36 * getResources().getDisplayMetrics().density + 0.5f);
i.setLayoutParams(new GridView.LayoutParams(w * 2, w * 2));
return i;
}
public final int getCount() {
return 9;
}
public final Family getItem(int position) {
return mFams.get(position);
}
public final long getItemId(int position) {
return position;
}
}
Or the basic layout using linear layouts:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/linearLayout1"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:orientation="horizontal">
<ImageView>...</ImageView>
<ImageView>...</ImageView>
<ImageView>...</ImageView>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout2"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:orientation="horizontal">
<ImageView>...</ImageView>
<ImageView>...</ImageView>
<ImageView>...</ImageView>
</LinearLayout>
<LinearLayout android:id="#+id/linearLayout3"
android:layout_height="fill_parent""
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:orientation="horizontal">
<ImageView>...</ImageView>
<ImageView>...</ImageView>
<ImageView>...</ImageView>
</LinearLayout>
You should really use a GridView to do grids, and not TableRows. Have you seen Android's tutorial for GridView's? To be able to achieve what you want with an image overlayed with text, you would need to utilize the FrameLayout as shown in Android's FrameLayout example. The tricky part here though, is that you need to apply this layout to each item that is going to be in the Gridview.
So for example, lets say you create a layout file called image_text_view.xml which looks like this:
<FrameLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:id="#+id/gridImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="false">
</ImageView>
<CheckedTextView
android:id="#+id/imageTick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textColor="#000000"
android:layout_gravity="center_horizontal|bottom"
android:checkMark="#drawable/icon"
android:checked="false"
android:visibility="invisible"
>
</CheckedTextView>
</FrameLayout>
You need to apply this layout in each of your GridView item. To do this (editing Android's GridView example) You would need to redefine the getView in the ImageAdapter as follows:
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if(convertView==null){
v = LayoutInflater.from(mContext).inflate(R.layout.image_text_view,null);
v.setLayoutParams(new GridView.LayoutParams(100,100));
ImageView imageView = (ImageView)v.findViewById(R.id.image);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setImageResource(mThumbsIds[position]);
CheckedTextView checkedTextView = (TextView) v.findViewById(R.id.imageTick);
checkedTextView.setEnabled(true);
checkedTextView.setVisibility(View.VISIBLE);
}
else
{
v = convertView;
}
return v;
Using this, for example, you will have whatever you set (whether its text or icons) overlaying the images for each item in the grid. You may need to do minor tweaks to this example to meet your exact needs, but this is the strategy i would go with.

Categories

Resources