I have been following the gallery example here: http://developer.android.com/resources/tutorials/views/hello-gallery.html
and adding a text to show up under the image from here: http://mytelcoit.com/2010/02/programming-android-create-icon-with-text-using-gridview-and-layout-inflater/
However, I am getting this error from LogCat
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): FATAL EXCEPTION: main
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): java.lang.ClassCastException: android.widget.Gallery$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.LinearLayout.measureVertical(LinearLayout.java:587)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.ViewGroup.measureChild(ViewGroup.java:4322)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:206)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.LinearLayout.measureVertical(LinearLayout.java:764)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.LinearLayout.onMeasure(LinearLayout.java:519)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4351)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.widget.FrameLayout.onMeasure(FrameLayout.java:267)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:1890)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.View.measure(View.java:10828)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.ViewRoot.performTraversals(ViewRoot.java:909)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.view.ViewRoot.handleMessage(ViewRoot.java:2003)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.os.Looper.loop(Looper.java:132)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at android.app.ActivityThread.main(ActivityThread.java:4025)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at java.lang.reflect.Method.invokeNative(Native Method)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at java.lang.reflect.Method.invoke(Method.java:491)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
06-22 17:21:02.193: ERROR/AndroidRuntime(1444): at dalvik.system.NativeStart.main(Native Method)
I am assuming the gallery view wont work with linear layout? I believe im getting the above error from getView(). Anyone know of a way to fix this?
GalleryActivity.java
package android.gallery;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class GalleryActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(GalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.caption, null);
TextView tv = (TextView) v.findViewById(R.id.caption_text);
tv.setText("test");
ImageView i = (ImageView)v.findViewById(R.id.image_icon);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return v;
}
}
}
caption.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:text="TextView"
android:id="@+id/caption_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Gallery>
I have tried this one and found one way to solve this. Please try this also:
main.xml
image_gallery_items.xml
GalleryExample.java
ImageAdapter.java
And in the AndroidMAnifest.xml specify GalleryExample only.