In my app i need to perform flip animation on cells of Grid View to show another image when user touches. initially i have populated grid view with images when user touches them they should flip and some other image is shown to user.Thanks in advance.
this is my adapter class:-
public class Adapter extends BaseAdapter {
private Context mycontext;
public Integer[] picids = {
R.drawable.pic_1, R.drawable.pic_2, R.drawable.pic_3,
R.drawable.pic_4, R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8, R.drawable.pic_9
};
public Adapter(Context c){
mycontext = c; }
#Override
public int getCount() {
// TODO Auto-generated method stub
return picids.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return picids[position];
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageview = new ImageView(mycontext);
imageview.setImageResource(picids[position]);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setLayoutParams(new GridView.LayoutParams(120,120));
return imageview ;
}
}
and following is my main activity class:-
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView grid = (GridView)findViewById(R.id.grid_view);
grid.setAdapter(new Adapter(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
A relatively hacky way to do this would be to use JazzyListView, and "grab" an animation out of the source and implement it in your onClick listener to trigger an animation event.
Related
I am new to android development. In my android activity I have a GridView and a Button. The GridView adapter class is displaying a set of icons in the GridView. What I want is, if the Button in the activity is clicked, the set of icons in the GridView should be replaced with another set of icons. And every Button click in the activity should switch the two sets of icons in the GridView.
So, how to do this using notifyDatasetChanged()? Please Help.
GridView Adapter class:
public class CustomAdapter extends BaseAdapter{
boolean imageSetChange = false;
// set 1
public Integer[] mThumbPics = {
R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9, R.drawable.pic10,
R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13, R.drawable.pic14,
R.drawable.pic15, R.drawable.pic16,
R.drawable.pic17, R.drawable.pic18,
R.drawable.pic19, R.drawable.pic20,
R.drawable.pic21
};
//set 2
public Integer[] mThumbEng = {
R.drawable.eng_pic1, R.drawable.eng_pic2,
R.drawable.eng_pic3, R.drawable.eng_pic4,
R.drawable.eng_pic5, R.drawable.eng_pic6,
R.drawable.eng_pic7, R.drawable.eng_pic8,
R.drawable.eng_pic9, R.drawable.eng_pic10,
R.drawable.eng_pic11, R.drawable.eng_pic12,
R.drawable.eng_pic13, R.drawable.eng_pic14,
R.drawable.eng_pic15, R.drawable.eng_pic16,
R.drawable.eng_pic17, R.drawable.eng_pic18,
R.drawable.eng_pic19, R.drawable.eng_pic20,
R.drawable.eng_pic21
};
private Context mContext;
View MyView;
ImageView imageView;
public CustomAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbEng.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbEng[position];
}
#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
if(imageSetChange==false)
{if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView=li.inflate(R.layout.menuitem, null);
}
else{
MyView=(View)convertView;
}
imageView =(ImageView)MyView.findViewById(R.id.image);
imageView.setImageResource(mThumbEng[position]);
return MyView;
}
else{
if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView=li.inflate(R.layout.menuitem, null);
}
else{
MyView=(View)convertView;
}
imageView =(ImageView)MyView.findViewById(R.id.image);
imageView.setImageResource(mThumbUrdu[position]);
return MyView;
}
}
/** public void changeImages(boolean change){
this.imageSetChange = change;
cda.notifyDataSetChanged();
}
**/
}
Button onclick listener:
Button lang_change_btn = (Button) findViewById(R.id.button1);
lang_change_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
//cda.changeImages(true);
}
});
I am having a grid view and I have to add search functionality in the action bar.So that whatever i type to search,the items should come whatever i searched.Grid view is having image and text.I stuck in this very badly.I dont know how to implement it.I gone through android guide but I unable to get it.
Please suggest some easy way to do it.I need search like in watsaap application.
Any suggestion is appreciated.Thanks.
Here is my gridview code.
public class FragmentDeals extends Fragment implements Checkable{
private boolean mChecked;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragmentdeal,
(ViewGroup)
findViewById(R.id.layoutdeal));
//View view = inflater.inflate(R.layout.griddeal,null);
final GridView mGrid = (GridView) layout.findViewById(R.id.GridDeal);
mGrid.setAdapter(new DealAdapter());
mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener() {
#Override
public boolean onActionItemClicked(ActionMode mode,
MenuItem item) {
// TODO Auto-generated method stub
return true;
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Select Items");
mode.setSubtitle("One item selected");
return true;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
// TODO Auto-generated method stub
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onItemCheckedStateChanged(ActionMode mode,
int position, long id, boolean checked) {
//listView.setLongClickable(false);
int selectCount = mGrid.getCheckedItemCount();
switch (selectCount) {
case 1:
mode.setSubtitle("One item selected");
break;
default:
mode.setSubtitle("" + selectCount +"items selected");
break;
}
}
});
return layout;
// TODO Auto-generated method stub
}
#Override
public boolean isChecked() {
// TODO Auto-generated method stub
return mChecked;
}
#SuppressWarnings("deprecation")
#Override
public void setChecked(boolean checked) {
// TODO Auto-generated method stub
mChecked = checked;
layout.setBackgroundDrawable(checked ? getResources().getDrawable(
R.drawable.bground) : null);
}
#Override
public void toggle() {
// TODO Auto-generated method stub
setChecked(!mChecked);
}
}
private class DealAdapter extends BaseAdapter {
#Override
public int getCount() {
return mThumbIds1.length;
}
#Override
public Object getItem(int position) {
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) {
//CheckableLayout l;
View myView = convertView;
LayoutInflater inflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.griddealitems, null);
// Add The Image!!!
ImageView iv = (ImageView)myView.findViewById(R.id.grid_deal_image);
iv.setImageResource(mThumbIds1[position]);
// Add The Text!!!
TextView tv = (TextView)myView.findViewById(R.id.grid_deal_text);
tv.setText(names1[position] );
return myView;
}
private Integer[] mThumbIds1= {
R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car,R.drawable.car, R.drawable.car,
R.drawable.car, R.drawable.car,
R.drawable.car,R.drawable.car,R.drawable.car
};
private String[] names1={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};
}
I have never tried for search functionality with gridview. But i have done the same for list view. Please have a look on the following link to implement search functionality for listview
Search functionality with list view
http://www.androidbegin.com/tutorial/android-search-listview-using-filter/
Im new to android im desingnig one app description is...
i have kept one edit text and one button whenevr i click on the button i have to store that value in a list... i tried like this
I didnt get where theerror is....
public class Dynamic extends Activity {
ArrayList<Getters> x=new ArrayList<Getters>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.nameok);
final EditText ed=(EditText)findViewById(R.id.name);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String name=ed.getText().toString();
Getters y=new Getters(name);
x.add(y);
ListView lv= (ListView)findViewById(R.id.listView1);
Demo demo=new Demo();
lv.setAdapter(demo);
}
});
}
public class Demo extends BaseAdapter
{
#Override
public int getCount() {
// TODO Auto-generated method stub
return x.size();
}
#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 v = convertView;
if (v == null) {
LayoutInflater vi = LayoutInflater.from(parent.getContext());
v = vi.inflate(R.layout.forlist, null);
}
//View v=LayoutInflater.from(getApplicationContext()).inflate(R.layout.forlist, null);
TextView listname=(TextView)findViewById(R.id.listname);
listname.setText(x.get(position).name);
return v;
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
getters..
public class Getters {
public Getters(String name) {
this.name = name;
}
String name;
public String getName() {
Log.v("name---",""+name);
return name;
}
}
please First see this and then implement your code so its easy to implement.
Edit :
http://www.framentos.com/en/android-tutorial/2012/07/16/listview-in-android-using-custom-listadapter-and-viewcache/
Hope you will get idea about how to manage list data dynamically.
Edit
Just write below code for add event in List view
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String name=ed.getText().toString();
your List_view_obj.add(name);
//call adapter class and pass that string
your List_view_obj.setAdapter(your adapter's obj);
your_adapter_obj.notifyDataSetChanged();
}
});
Hope you got it.
I am trying to develop a application which contain a gallery with view flipper.
Gallery shows all the images.What I want to do is, the image which is shown in the view flipper should be highlighted in the gallery.
Currently I am able to highlight it on onClick.But not able to make communication between both.
I tried using following method
viewflipper.getDisplayedChild();
but it doesn't work.
following is my code
public class SliderActivity extends Activity
{
int mFlipping = 0 ;
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
android.widget.ViewFlipper flipper;
Gallery gallery;
private int[] IMAGE_IDS =
{
R.drawable.android0, R.drawable.android1, R.drawable.android2,R.drawable.android3,R.drawable.android4
};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mygame);
flipper = (android.widget.ViewFlipper) findViewById(R.id.flipper1);
for(int i=0;i<IMAGE_IDS.length;i++)
{
// This will create dynamic image view and add them to ViewFlipper
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(IMAGE_IDS[i]);
flipper.addView(image);
}
gallery=(Gallery)findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
if(mFlipping==0){
/** Start Flipping */
flipper.startFlipping();
mFlipping=1;
//mButton.setText(R.string.str_btn_stop);
}
else{
/** Stop Flipping */
flipper.stopFlipping();
mFlipping=0;
// mButton.setText(R.string.str_btn_start);
}
}
public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Theme);
mGalleryItemBackground = a.getResourceId(
R.styleable.Theme_android_galleryItemBackground,0);
a.recycle();
}
public int getCount()
{
return IMAGE_IDS.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(IMAGE_IDS[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
public void onClick(View v) {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
If I understand your question... I think you need to use setOnItemSelectedListener...
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View gallery,
int position, long arg3) {
myPosition = position;
//tell your flipper something useful here using the current position of the gallery
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
And talk to your flipper from in the onItemSelected callback.
I have a gallery where I have implement a custom adapter extending the BaseAdapter. For some items, I want them to be disabled, and not clickable as well.
Overriding the isEnabled(int) method doesn't work. I am still able to click on the disabled items, and then, the gallery centers this item inside it.
Any ideas?
This following is the relevant source code of gallery widget
public boolean onSingleTapUp(MotionEvent e) {
if (mDownTouchPosition >= 0) {
// An item tap should make it selected, so scroll to this child.
scrollToChild(mDownTouchPosition - mFirstPosition);
// Also pass the click so the client knows, if it wants to.
if (mShouldCallbackOnUnselectedItemClick || mDownTouchPosition == mSelectedPosition) {
performItemClick(mDownTouchView, mDownTouchPosition, mAdapter
.getItemId(mDownTouchPosition));
}
return true;
}
return false;
}
As you can see, the gallery scrolls to the tapped child item before performing the click on it.
So the only way you can truly disable an item is by extending Gallery and overriding onSingleTapUp(MotionEvent e) in it.
#Override
public boolean onSingleTapUp(MotionEvent e) {
int itemPosition = pointToPosition((int) e.getX(), (int) e.getY());
if (item at itemPosition is disabled) {
// Do nothing.
return true;
}
return super.onSingleTapUp(e);
}
Try and let me know.
Try the below code where you can handle the click event for specific positions. Also you can disable the specific items.
public class SplashActivity extends Activity{
private Activity _activity;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Gallery g = new Gallery(this);
g.setAdapter(new ImageAdapter(this));
setContentView(g);
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.menu1,
R.drawable.menu2,
R.drawable.menu3,
R.drawable.menu4,
R.drawable.menu1,
R.drawable.menu2,
R.drawable.menu3,
R.drawable.menu4
};
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final 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);
if(position!=0){
i.setEnabled(false);
i.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(SplashActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
return i;
}
}
}