Change the Gallery View for the button clicked in android - android

I have three buttons Image , Videos , Audios. I want to change the views in gallery with the audios , videos , images resources when corresponding button clicked so that only related image of the buttons appear in gallery.
My code is here:
XML layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audios" >
</Button>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Videos" >
</Button>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Images" >
</Button>
</LinearLayout>
<Gallery
android:id="#+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</Gallery>
</LinearLayout>
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ImageView>
Java file:
public class GalleryView extends Activity {
Integer[] pics = {
R.drawable.cloudy,
R.drawable.hazy,
R.drawable.mostlycloudyday,
R.drawable.partlycloud,
R.drawable.sunny,
R.drawable.sunrain,
R.drawable.thunderstorm,
R.drawable.weathercloudy,
};
ImageView imageView;
Button mAudio;
Button mVideo;
Button mImages;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAudio=(Button) findViewById(R.id.button1);
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));
imageView = (ImageView)findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
Toast.makeText(getBaseContext(),
"You have selected picture " + (pos+1) + "of Gallery",
Toast.LENGTH_SHORT).show();
imageView.setImageResource(pics[pos]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
#Override
public int getCount() {
return pics.length;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(pics[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}
Anybody please help me with this,
Thanks.

For the changing the image in gallery, you must change the Resources image id in array. e.g change image for videos in int[] video_pics and so on.
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class GalleryView extends Activity implements OnClickListener {
int[] image_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };
int[] video_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };
int[] audio_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };
ImageView imageView;
Button mAudio;
Button mVideo;
Button mImages;
ImageAdapter galleryAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAudio = (Button) findViewById(R.id.button1);
mVideo = (Button) findViewById(R.id.button2);
mImages = (Button) findViewById(R.id.button3);
mAudio.setOnClickListener(this);
mVideo.setOnClickListener(this);
mImages.setOnClickListener(this);
Gallery ga = (Gallery) findViewById(R.id.Gallery01);
galleryAdapter = new ImageAdapter(this);
galleryAdapter.setResourseArray(image_pics);
ga.setAdapter(galleryAdapter);
imageView = (ImageView) findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Toast.makeText(getBaseContext(), "You have selected picture " + (pos + 1) + "of Gallery", Toast.LENGTH_SHORT)
.show();
imageView.setImageResource(pics[pos]);
}
});
}
#Override
public void onClick(View view) {
if (view == mAudio) {
if (galleryAdapter != null) {
galleryAdapter.setResourseArray(audio_pics);
galleryAdapter.notifyDataSetChanged();
}
} else if (view == mVideo) {
if (galleryAdapter != null) {
galleryAdapter.setResourseArray(video_pics);
galleryAdapter.notifyDataSetChanged();
}
} else if (view == mImages) {
if (galleryAdapter != null) {
galleryAdapter.setResourseArray(image_pics);
galleryAdapter.notifyDataSetChanged();
}
}
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
private int[] resourseArray = null;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
#Override
public int getCount() {
return resourseArray.length;
}
public int[] getResourseArray() {
return resourseArray;
}
public void setResourseArray(int[] resourseArray) {
this.resourseArray = resourseArray;
}
#Override
public Object getItem(int arg0) {
return arg0;
}
#Override
public long getItemId(int arg0) {
return arg0;
}
#Override
public View getView(int position, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(resourseArray[position]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}

Button.onClickEvent -> ga.setSelection(int postion);

Related

AndroidRuntime: FATAL EXCEPTION: main id is not going

I was trying to practice to use GridView in Android Studio, I don't know why I keep getting this exception.
Here is the code of my main class.
package com.example.chetam.gokalsapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class Products extends Activity {
GridView grid;
String[] web = {
"Google",
"Github",
"Instagram",
"Facebook",
"Flickr",
"Pinterest",
"Quora",
"Twitter",
"Vimeo",
"WordPress",
"Youtube",
"Stumbleupon",
"SoundCloud",
"Reddit",
"Blogger"
} ;
int[] imageId = {
R.drawable.v,
R.drawable.img,
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.v,
R.drawable.img,
R.drawable.img1,
R.drawable.img2,
R.drawable.img3,
R.drawable.v,
R.drawable.img,
R.drawable.img1,
R.drawable.img2,
R.drawable.img3
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_products);
grid=(GridView)findViewById(R.id.grid);
CustomGrid adapter = new CustomGrid(Products.this, web, imageId);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Products.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
}
Here is the code of my main XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.chetam.gokalsapp.Products">
<GridView
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/grid"
/>
</RelativeLayout>
Here is my layout file
<?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:padding="5dp"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/grid_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="15dp">
</ImageView>
<TextView
android:id="#+id/grid_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="12sp" >
</TextView>
</LinearLayout>
here is my adapter class
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
private final int[] Imageid;
public CustomGrid(Context c,String[] web,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return web.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
}
else
{
grid = (View) convertView;
}
return grid;
}
}
Replace grid onClickListener with:
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Products.this, "You Clicked at " +web[position], Toast.LENGTH_SHORT).show();
}
});
You are using an integer variable for the position of the String array. If you had '+ position' because you wanted one number higher than the position, then the correct syntax is:
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(Products.this, "You Clicked at " +web[position + 1], Toast.LENGTH_SHORT).show();
}
});

How to separate ImageView from adapter of ListView

I have a listview with and adapter that changes the title (TextView) and an image (ImageView) of all single item. But I have a problem, I also have and inputsearch (edittext) with the function TextChangedListener to search any single item of the list view. But actualy this is mi code of the TextChangedListener:
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
Alimentacion.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
This generales a null pointer in this line: Alimentacion.this.adapter.getFilter().filter(cs); I think that the null pointer is because when I'm going to search something I only want to search by the title but in the adapter I have the title and the image so tries to search the title and the image and returs the null pointer, I think.
How can separate these thing to search only for the title?
My full code:
** OncreateView of myfragment.xml** (the list view is in a fragment)
// List view
private ListView lv;
// Listview Adapter
ArrayAdapter<String> adapter;
ArrayAdapter<String> adapter2;
// Search EditText
EditText inputSearch;
// Set the Text to try this out
lv = (ListView)myInflatedView.findViewById(R.id.list_view);
inputSearch = (EditText) myInflatedView.findViewById(R.id.inputSearch);
// Listview Data
final String categorias[] = {"1. Huevos y Lacteos", "2. Carnes y Derivados", "3. Pescados y Mariscos", "4. Aceites y grasos", "5. Verduras y hortalizas",
"6. Frutas", "7. Bebidas",
"8. Comida Rapida", "9. Pasta y Cereales", "10. Bolleria y Snacks"};
Integer ImgCategorias[] = {R.drawable.cat1,R.drawable.cat2,R.drawable.cat3,R.drawable.cat4,R.drawable.cat5,R.drawable.cat6
,R.drawable.cat7,R.drawable.cat8,R.drawable.cat9,R.drawable.cat10};
// Pass results to ListViewAdapter Class
CustomList adapter = new
CustomList(this.getActivity(), categorias, ImgCategorias);
// Binds the Adapter to the ListView
lv.setAdapter(adapter);
// OLD ADAPTER (where only change the text, it works to the TextChangeListener)
/* adapter2 = new ArrayAdapter<String>(this.getActivity(), R.layout.adapter_categorias, R.id.TVTitulo, categorias);
lv.setAdapter(adapter2); */
/**
* Enabling Search Filter
* */
inputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text (LINE OF THE NULLPOINTER)
Alimentacion.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
And the CustomList.java
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final String[] categorias;
private final Integer[] ImgCategorias;
public CustomList(Activity context,
String[] categorias, Integer[] ImgCategorias) {
super(context, R.layout.adapter_categorias, categorias);
this.context = context;
this.categorias = categorias;
this.ImgCategorias = ImgCategorias;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.adapter_categorias, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.TVTitulo);
ImageView imageView = (ImageView) rowView.findViewById(R.id.IVCategoria);
txtTitle.setText(categorias[position]);
imageView.setImageResource(ImgCategorias[position]);
return rowView;
}
}
In conclusion, I want if is posible have the images and the titles in diferents adapters and in the search function put only the adapter of the title. Or something that allows me search for the title with no error..
I have seen this question: android attaching multiple adapters to one adapter
But I don't understand very well how to implement it to my app..
Thanks.
I have modified the code as below and it's working fine now. Please check and let me know:
public class Image {
String categorias;
Integer ImgCategorias;
public String getCategorias() {
return categorias;
}
public void setCategorias(String categorias) {
this.categorias = categorias;
}
public Integer getImgCategorias() {
return ImgCategorias;
}
public void setImgCategorias(Integer imgCategorias) {
ImgCategorias = imgCategorias;
}
}
Your Adapter class:
import java.util.ArrayList;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomList extends BaseAdapter {
private Context context;
private ArrayList<Image> data;
private ArrayList<Image> listImage;
public CustomList(Context context, ArrayList<Image> listImage) {
this.context = context;
data = listImage;
this.listImage = new ArrayList<Image>();
this.listImage.addAll(listImage);
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View rowView = inflater
.inflate(R.layout.adapter_categorias, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.TVTitulo);
ImageView imageView = (ImageView) rowView
.findViewById(R.id.IVCategoria);
txtTitle.setText(data.get(position).getCategorias());
imageView.setImageResource(data.get(position).getImgCategorias());
return rowView;
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
return data.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("DefaultLocale")
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
data.clear();
if (charText.trim().length() == 0) {
data.addAll(listImage);
} else {
for (Image wp : listImage) {
if (wp.getCategorias().toLowerCase().contains(charText)) {
data.add(wp);
}
}
}
notifyDataSetChanged();
}
}
Activity Class:
import java.util.ArrayList;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class Alimentacion extends Activity {
Context context;
TextView txtTVtexto;
EditText txtInputSearch;
ListView list;
CustomList adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_alimentacion);
context = Alimentacion.this;
txtTVtexto = (TextView) findViewById(R.id.TVtexto);
txtInputSearch = (EditText) findViewById(R.id.inputSearch);
list = (ListView) findViewById(R.id.list_view);
ArrayList<Image> listImage = prepareImageList();
adapter = new CustomList(context, listImage);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos,
long id) {
Image image = (Image) list.getItemAtPosition(pos);
Toast.makeText(context, "Name: "+image.getCategorias(), Toast.LENGTH_LONG).show();
}
});
txtInputSearch.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
String text = "";
try {
text = txtInputSearch.getText().toString().toLowerCase(Locale.getDefault());
} catch ( Exception e ) {
e.printStackTrace();
}
adapter.filter(text);
}
});
}
private ArrayList<Image> prepareImageList() {
ArrayList<Image> listImage = new ArrayList<Image>();
Image image = new Image();
image.setCategorias("1. Huevos y Lacteos");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("2. Carnes y Derivados");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("3. Pescados y Mariscos");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("4. Aceites y grasos");
image.setImgCategorias(R.drawable.ic_stub);
listImage.add(image);
image = new Image();
image.setCategorias("5. Verduras y hortalizas");
image.setImgCategorias(R.drawable.share);
listImage.add(image);
image = new Image();
image.setCategorias("6. Frutas");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("7. Bebidas");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("8. Comida Rapida");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("9. Pasta y Cereales");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
image = new Image();
image.setCategorias("10. Bolleria y Snacks");
image.setImgCategorias(R.drawable.ic_launcher);
listImage.add(image);
return listImage;
}
}
Layout:
fragment_alimentacion.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="es.bawi.testdeporte.Alimentacion" >
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="#+id/TVtexto"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="center_horizontal|center_vertical"
android:text="Blank Fragment" />
<!-- Editext for Search -->
<EditText
android:id="#+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/TVtexto"
android:hint="Productos"
android:inputType="textVisiblePassword" />
<!-- List View -->
<ListView
android:id="#+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/inputSearch" />
</RelativeLayout>
adapter_categorias.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_blue_light"
android:paddingBottom="2dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:background="#android:color/holo_green_light"
android:layout_height="70dp">
<ImageView
android:id="#+id/IVCategoria"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher"
android:focusable="false" />
<TextView
android:id="#+id/TVTitulo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/IVCategoria"
android:text="Categoria"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/TVCalorias"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/TVTitulo"
android:paddingTop="0dp"
android:text="NÂș Productos"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>

How to start a new activity while always keeping the gallery?

I am making a memory game using the gallery view. I want all the cards to be shown at the bottom and when the user picks one card from the gallery the particular card is displayed above it(in the image view). When the user touches this card, the card should turn (using flip animation) and the front of the card should be displayed (which should be a new activity). During this flip and all the time I want to display the gallery at the bottom. How can i do this? I have the following code which has no errors but the app crashes before starting. On viewing the log cat it suggests an error while starting the new acidity.
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
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.Toast;
public class GalleryActivity extends Activity {
//---the images to display---
Integer[] imageIDs = {
R.drawable.you,
R.drawable.date,
R.drawable.competition,
R.drawable.contacts,
R.drawable.memory,
R.drawable.mission,
R.drawable.recognition,
R.drawable.report,
R.drawable.stallion,
R.drawable.strategy,
R.drawable.team
};
ImageView youview;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
Toast.makeText(getBaseContext(),
"card" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
//---display the images selected---
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
youview=(ImageView)findViewById(R.drawable.you);
youview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(GalleryActivity.this, You.class);
startActivity(intent);
}
});
}
public class ImageAdapter extends BaseAdapter
{
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the item---
public Object getItem(int position) {
return position;
}
//---returns the ID of an item---
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
Here is the layout that I am using:-
<?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:orientation="vertical"
android:background="#006600">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Choose the card"
android:textSize="20dp"
android:textStyle="bold"/>
<Gallery
android:id="#+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
<ImageView
android:id="#+id/image1"
android:layout_width="320dp"
android:layout_height="400dp"
android:layout_above="#+id/gallery1"
android:scaleType="fitXY" />
</RelativeLayout>
You should use a fragment for this as so:
private void addCardFragment() {
String fragmentTag = getFragmentTag();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.card_flip_in, R.animator.card_flip_out);
CardFragment fragment = new CardFragment();
//set bundle with argument for image id to show if fragment
fragmentTransaction.replace(R.id.your_relative_or_frame_layout, CardFragment, fragmentTag);
fragmentTransaction.commit();
fragmentTransaction = null;
}

Add textview with button in gallery

I'm working on a project that contains some images in gallery, I want to add text with button that changes respective to selected image. How can I add this?
This is the code:
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class GalleryView extends Activity {
Integer[] pics = {
R.drawable.antartica1,
R.drawable.antartica2,
R.drawable.antartica3,
R.drawable.antartica4,
R.drawable.antartica5,
R.drawable.antartica6,
R.drawable.antartica7,
R.drawable.antartica8,
R.drawable.antartica9,
R.drawable.antartica10
};
ImageView imageView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));
imageView = (ImageView)findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(),
"You have selected picture " + (arg2+1) + " of Antartica",
Toast.LENGTH_SHORT).show();
imageView.setImageResource(pics[arg2]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context ctx;
int imageBackground;
public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}
public int getCount() {
return pics.length;
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(pics[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}
}
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Gallery
android:id="#+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Gallery>
<ImageView
android:id="#+id/ImageView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
</LinearLayout>
well you can make your own Customised gallery in which every image can be viewed in a grid view followed by a text view below the thumbnail and a button (may be needed for deleting the image).
To view the images dynamically from the memory card you have to implement ArrayList or you can take other oferings of collection framework ..
i did the same and it worked with mine , hopefully will work with you too.

Android gallery get image name

In the below code i have created a gallery object and also have included a image view,on click of image how to get the image name and load it in image view.I have also included main.xml
package HelloGallery.com;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
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.Toast;
public class HelloGalleryActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView iv=(ImageView) findViewById(R.id.imageView1);
iv.setVisibility(View.INVISIBLE);
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(HelloGalleryActivity.this, "" + position, Toast.LENGTH_SHORT).show();
iv.setVisibility(View.VISIBLE);
iv.setsource("android:src="#drawable/"+imagename); //how to do this
}
});
}
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
};
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) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
/* public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
return null;
}*/
}
}
Main.mxml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView android:layout_width="wrap_content" android:id="#+id/imageView1" android:layout_height="wrap_content" ></ImageView>
</LinearLayout>
Try this,
Drawable d=getResources().getDrawable(R.drawable.icon);
iv.setImageDrawable(d);

Categories

Resources