Im using an imageswitcher and want to know how to programatically set the initial selected item in my gallery of images.
Currently on activating the image selection activity the selection always defaults to the first in the array of available images. If a user has previously selected one I would like to recall that item as the first presented image in the switcher gallery.
On selection I am sending the selection int to a 'global variable' which I could use to call when the activity is recalled.
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher1);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(this));
int texPositionVariable = 2;
mSwitcher.setImageResource(mImageIds[texPositionVariable]); // should load position 2 but doesn't
g.setOnItemSelectedListener (new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
Log.d("textureselected",String.valueOf(position));
GlobalVariables.setTexture((float)position);
mView.requestRender();
}
#Override
public void onNothingSelected(
AdapterView<?> paramAnonymousAdapterView) {
}
});
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.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(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(200, 150));
i.setBackgroundResource(R.drawable.long1);
return i;
}
private Context mContext;
}
private Integer[] mThumbIds = {
R.drawable.textures00, R.drawable.textures01,
R.drawable.textures02, R.drawable.textures03,
R.drawable.textures04, R.drawable.textures05,
R.drawable.textures06, R.drawable.textures07};
Thanks.
You are trying set the ImageResource before Gallery's OnItemSelectedListener but By default Gallery's OnItemSelectedListener will trigger so again ImageResource to your ImageSwitcher's set to position of 0(zero). You need to set the selection to Gallery
Try this:
Replace
mSwitcher.setImageResource(mImageIds[texPositionVariable]);
with
g.setSelection(texPositionVariable);
Question Answered by using 'setSelection(int)' within the gallery.
Gallery g = (Gallery) findViewById(R.id.gallery1);
g.setAdapter(new ImageAdapter(this));
// this worked
float texSelected = GloblaVariables.getTexture();
g.setSelection((int)texSelected);
g.setOnItemSelectedListener (new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
Log.d("textureselected",String.valueOf(position));
GlobalVariables.setTexture((float)position);
mView.requestRender();
}
#Override
public void onNothingSelected(
AdapterView<?> paramAnonymousAdapterView) {
}
});
Thanks
Related
Is it possile to set some item unclickable in ListView or GridView on Android?
For example, if user click item 0, I want to set item 1 cannot be click.
But if handle in onItemClick method, it still can click and change color.
I want it cannot be click means item click but do nothing.
My code as below:
GridView gv = (GridView)findViewById(R.id.gv);
gv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(position == 0) {
//set item 1 unclickable
}
}
});
ListView lv = (ListView)findViewById(R.id.lv);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(position == 0) {
//set item 1 unclickable
}
}
});
How can I set it?
There is one problem with what you want to achieve. If - for example - the item that you want to disable is not currently visible in ListView because user have smaller screen and it doesn't fit the size of ListView, this item doesn't yet have any View assigned to it. This means that you can't really make this item not clickable, because it doesn't exist.
Solution which Avadhani Y posted is wrong, because it makes the item that you just clicked not clickable and you want to alter another item.
What you want then is to create your own Adapter for ListView (if you don't yet use your custom Adapter) and override getView() method, like this:
public class MyAdapter extends ArrayAdapter<String> {
private List<String> items;
public boolean itemAtPos0Clicked = false;
public MyAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public MyAdapter(Context context, int resource, List<String> items) {
super(context, resource, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(position == 1 && itemAtPos0Clicked) {
convertView.setClickable(false);
}
return convertView;
}
}
And your OnItemClickListener for ListView should look like this:
final ListView lv = (ListView)findViewById(R.id.listView1);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if((position == 0) && (lv.getFirstVisiblePosition() == 0)) {
((MyAdapter)lv.getAdapter()).itemAtPos0Clicked = true;
((MyAdapter)lv.getAdapter()).notifyDataSetChanged();
}
}
});
I didn't test it or anything, so you will probably have to fix few errors.
Yes, you can do that as below:
ListView lv = (ListView)findViewById(R.id.lv);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
if(position == 0) {
arg1.findViewById(R.id.lv).setClickable(false);
}
}
});
How can we switch the pictures with some information? When we switch the picture then information should also be get switched in Gallery view in android.
public class ImageAdapter extends BaseAdapter {
String[] Merchantname=null,Cardname=null,Points=null,Expirydate=null,status=null;
private Context ctx;
int imageBackground;
Bitmap[] image_data;
public ImageAdapter(Context c, Bitmap []card_image,String [] Merchantname,String [] Cardname,String []points,String[] Expirydate, String []status) {
ctx = c;
image_data = card_image;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
this.Merchantname=Merchantname;
this.Cardname=Cardname;
this.Points=points;
this.Expirydate=Expirydate;
this.status=status;
}
public int getCount() {
return image_data.length;
}
public Object getItem(int arg0) {
return arg0;
}
public long getItemId(int arg0) {
return arg0;
}
public View getView(int position, View arg1, ViewGroup arg2) {
TextView tv1,tv2,tv3,tv4,tv5;
ImageView i ;//= new ImageView(this.ctx);
if (arg1 == null) {
i = new ImageView(this.ctx);
} else {
i = (ImageView) arg1;
}
tv1=(TextView)findViewById(R.id.Merchantname);
tv2=(TextView)findViewById(R.id.Cardname);
tv3=(TextView)findViewById(R.id.Expirydate);
tv4=(TextView)findViewById(R.id.status);
tv1.setText(Merchantname[position]);
tv2.setText(Cardname[position]);
tv3.setText(Expirydate[position]);
tv4.setText(status[position]);
// ImageView iv = new ImageView(ctx);
// Drawable drawable = new BitmapDrawable(getResources(), image_data[position]);
i.setImageBitmap(image_data[position]);
i.setImageDrawable(new BitmapDrawable(getResources(), image_data[position]));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(300,200));
i.setBackgroundResource(imageBackground);
return i;
}
}
You simple need to use an ArrayList whose Index would be linked with the Gallery views individual views, and the lenth of the ArrayList would match the Length of Gallery View, on the setOnItemClickListener use the position variable to change the Content from the Array List matching the same index
// Reference the Gallery view
Gallery g = (Gallery) findViewById(R.id.gallery);
// Set the adapter to our custom adapter (below)
g.setAdapter(new ImageAdapter(this));
// Set a item click listener, and just Toast the clicked position
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
Create layout with ImageView and TextView. Then write your custom adapter for Gallery and there in getView method put to ImageView and TextView your values. Them set this adapter to your Gallery.
The Problem Lies here,
tv1=(TextView)findViewById(R.id.Merchantname);
tv2=(TextView)findViewById(R.id.Cardname);
tv3=(TextView)findViewById(R.id.Expirydate);
tv4=(TextView)findViewById(R.id.status);
You forgot to mention the Parent View before asking for the LayoutFile, add (TextView)arg1.findViewById() for all the TextViews and let us know.
Hope it helps
I know there is a lot of information out there about using the onItemClickListener and list view but I am new to android development and cannot seem to get it working.
I am not quite sure where I should add the listener so I would really appreciate some help and guidance.
I have two files, the main activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ArrayList<GroceryList> menuitems = getItems();
ListView listView = (ListView) findViewById(R.id.Menu);
listView.setAdapter(new GroceryListAdapter(this, R.layout.categorymenu, menuitems));
}
and the ListAdapter File:
public class GroceryListAdapter extends ArrayAdapter<GroceryList> {
private ArrayList<GroceryList> grocerylists;
private Activity activity;
public ImageManager imageManager;
public GroceryListAdapter(Activity a, int textViewResourceId, ArrayList<GroceryList> grocerylists) {
super(a, textViewResourceId, grocerylists);
this.grocerylists = grocerylists;
activity = a;
}
public static class ViewHolder{
public TextView name;
public TextView message;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.categorymenu, null);
holder = new ViewHolder();
holder.name = (TextView) v.findViewById(R.id.categoryname);
holder.message = (TextView) v.findViewById(R.id.message);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
final GroceryList grocerylist = grocerylists.get(position);
if (alcohollist != null) {
holder.name.setText(grocerylist.name);
holder.message.setText(grocerylist.message);
}
return v;
}
I am sorry if I am asking a question that has already been answered but I spent a lot of time trying to figure it out for myself but with no success.
I hope some one with more experience than myself will be able to tell me where and how I should add the onItemClickListen method.
Thanks!
By the looks of it you are using a regular Activity, so you should add this:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "You clicked item at position"+position,
Toast.LENGTH_SHORT).show();
}
});
BEFORE the .setAdapter in your main activity. That should work.
In your Main activity:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
#Override
protected void onListItemClick(ListView l, View v, int position, long id){
// startActivity( new Intent());
Intent i = new Intent(this,"Next_Activity_Name".class);
i.putExtra("selected",(int)selected_position);
final int resultCode = 2;
startActivityForResult(i,resultCode);
}
You have to add this piece of code into ur main activity after the onCreate() method..
Write down the following code after setting the adapter.
listView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}});
Handle the functionality that you want to do on click, under the onItemClick method.
you have use custom adapter so you can set touchListner at perticular widget which you define in categorymenu layout. it is quit easy to do this but
if you use simple listview then
Add parameter in listview :
lview.setOnItemClickListener(this);
public void onListItemClick(ListView parent, View v, int position, long id) {
// do with list-view item Position
}
For more ....
Simple Listview
http://www.androidpeople.com/android-listview-onclick
http://developer.android.com/resources/tutorials/views/hello-listview.html
http://mobile.tutsplus.com/tutorials/android/android-listview/
Paresh Mayani
Custome Listview
http://saigeethamn.blogspot.com/2010/04/custom-listview-android-developer.html
how to customize listview row android
http://www.josecgomez.com/2010/05/03/android-putting-custom-objects-in-listview/
Here so many answers, But it seem to be you didn't understood what they are telling. I will also try to give answer.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ArrayList<GroceryList> menuitems = getItems();
ListView listView = (ListView) findViewById(R.id.Menu);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
// do whatever you want on clicking any list itm
}
});
listView.setAdapter(new GroceryListAdapter(this, R.layout.categorymenu, menuitems));
}
I hope you will understand it.
I want to create a photo gallery on Android. I'm using Gallery and BaseAdapter to create a scrollable gallery. Now I want to add an action when an image of the gallery is shown (each image's width is the same as screen width), so I need to get its index in the image array. My question is how to get the index of the image which shows on the screen?
I want to get the index of the image as soon as it shows on the screen, not on click event.
I've tried to get current image position in getView(), but the result is strange:
I scroll to image02 but position=2 (should be 1). When I scroll back, image02's position=0 (should be 1).
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) this.findViewById(R.id.Gallery);
g.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mImageIds = {
//each image's size is 320x60
R.drawable.btm01,
R.drawable.btm02,
R.drawable.btm03
};
public ImageAdapter(Context c){
this.mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mImageIds.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
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(320,60));
return imageView;
}
Check this example.
// Reference the Gallery view
Gallery g = (Gallery) this.findViewById(R.id.Gallery);
// Set the adapter to our custom adapter (below)
g.setAdapter(new ImageAdapter(this));
// Set a item click listener, and just Toast the clicked position
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(Gallery1.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
Note: As of Jellybean the gallery widget is deprecated. A ViewPager should be used instead.
I'd like to programmatically move between images in the Gallery widget, with animation.
I can change the currently displaying image using the setSelection(int position) method, however that does not animate. Then there's setSelection(int position, bool animate) but the extra boolean on the end there doesn't appear to do anything.
In the source of Gallery it appears that it can handle DPAD key-presses, so a work-around I thought of was to fake the key-presses. Eg.
dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT))
However I can't get this working for some reason. Anyone tried this?
I notice three of the widget's methods I'd love to use moveNext(), movePrevious() and scrollToChild() are all private and unusable.
Does anyone know how I might be able to do this?
Just call the key press handler for the gallery directly:
public boolean onKeyDown(int keyCode, KeyEvent event)
i.e
Gallery gallery = ((Gallery) findViewById(R.id.gallery));
gallery.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(0, 0));
One important thing - this solution works only if child that is on left/right was already created, which means that it has to be 'visible'. If you have your image on fullscreen - consider setting spacing to -1 value.
You can Animate using dispatchKeyEvent or calling onFling directly.
Here is sample code for dispatchKeyEvent:
KeyEvent evtKey = new KeyEvent(0, KeyEvent.KEYCODE_DPAD_RIGHT);
dispatchKeyEvent(evtKey);
Use gallery.setSelected(int); Here is a simple example.
public class Splash extends Activity {
ArrayList objects = new ArrayList();
Gallery g;
int i = 0;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.photos);
g = (Gallery) findViewById(R.id.gallery);
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
objects.add(getResources().getDrawable(R.drawable.icon));
g.setAdapter(new CustomAdapter(this, objects));
g.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
Log.i("", "selected " + arg2);
}
#Override
public void onNothingSelected(AdapterView arg0) {}
});
}
#Override
public void onBackPressed() {
g.setSelection(i++);
}
private class CustomAdapter extends BaseAdapter {
private Context mCtx;
private List objects;
public int getCount() {
return this.objects.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public CustomAdapter(Context context, ArrayList objects) {
super();
mCtx = context;
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView row = (ImageView) convertView;
if (row == null) {
row = new ImageView(mCtx);
row.setBackgroundDrawable(objects.get(position));
}
return row;
}
}
}
In the end I wrote my own version of the Gallery widget with the help of the code at this site.
I then wrote my own method which uses mFlingRunnable.startUsingDistance(distance);
Now I can programmatically animate the gallery between images.
Try this
mGallery.onFling(null,null, velocity,0);
http://groups.google.com/group/android-developers/browse_thread/thread/9140fd6af3061cdf#