I have created a gridview and added a checkbox in it.On clicking on checkbox grid view item which are image view with textview get selected.
But I want to check the checkbox on tap of grid view item.
This my MainActivity
public class MainActivity extends Activity {
GridView gridview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.mainGrid);
gridview.setAdapter(new ImageAdapter(MainActivity.this));
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
//
//
ImageAdapter.holder.ItemCheck.setId(position);
ImageAdapter.holder.ItemCheck.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//ImageAdapter.holder.ItemCheck.setChecked(true);
Toast.makeText(MainActivity.this, "Itemclicked" ,Toast.LENGTH_LONG).show();
}
});
Toast.makeText(
MainActivity.this, "Itemclicked", Toast.LENGTH_SHORT).show();
}
});
};
#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;
}
}
This is my AdapterClass
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public static ViewHolder holder;
public ImageAdapter(Context c) {
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
public boolean areAllItemsEnabled(){
return true;
}
#Override
public boolean isEnabled(int position) {
// TODO Auto-generated method stub
return true;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//ViewHolder holder;
View myView = convertView;
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
//Inflate the layout
holder = new ViewHolder();
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8,8, 8);
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = inflater.inflate(R.layout.grid_items_ontap, null);
holder.ItemCheck = (CheckBox) myView.findViewById(R.id.itemCheckBox);
myView.setTag(holder);
}else{
holder = (ViewHolder) myView.getTag();
}
// holder.ItemCheck.setId(position);
// holder.ItemCheck.setOnClickListener(new OnClickListener() {
//
// public void onClick(View v) {
// // TODO Auto-generated method stub
// Toast.makeText(mContext, "Itemclicked" ,Toast.LENGTH_LONG).show();
// }
// });
// Add The Image!!!
ImageView iv = (ImageView)myView.findViewById(R.id.grid_item_image_OnTap);
iv.setImageResource(mThumbIds[position]);
// Add The Text!!!
TextView tv = (TextView)myView.findViewById(R.id.grid_item_text_onTap);
tv.setText(names[position] );
holder.ItemCheck.setChecked(isEmpty());
holder.id = position;
return myView;
}
public class ViewHolder {
public CheckBox ItemCheck;
int id;
}
private String[] names={"ab","cd","ef","gh","ij","kl","mn","","","","","","",""};
private Integer[] mThumbIds = {
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
};
}
Gridview.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/gridlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:id="#+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="0dp"
android:numColumns="3"
android:clickable="true"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" >
</GridView>
</LinearLayout>
griditems.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:orientation="vertical" >
<ImageView
android:id="#+id/grid_item_image_OnTap"
android:layout_width="100dp"
android:layout_height="100dp" >
</ImageView>
<TextView
android:id="#+id/grid_item_text_onTap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:text="TextView"
android:textColor="#android:color/black" >
</TextView>
<CheckBox
android:id="#+id/itemCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:button="#drawable/custom_checkbox"
android:checked="true"
android:visibility="visible"
android:focusable="true" />
</RelativeLayout>
Please suggest me.I m new to android and I got stuck here.
Thanks
I want to check the checkbox on tap of grid view item.
you can do it by using second parameter of onItemClick method as:
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
CheckedTextView check = (CheckedTextView) view;
check.setChecked(!check.isChecked());
boolean click = !check.isChecked();
check.setChecked(click);
if (click) {
//write what you want whenever click a checkbox
}
}
Related
I am creating custom grid. Each row has 1 button and 2 textviews. I am trying to assign values/text to each programmatically. I am able to set text for both the text views but, findviewbyId for button returns null.
Grid is like:
<GridView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dip"
android:horizontalSpacing="10dip"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#FFFFFF"
android:padding="5dip"
android:id="#+id/gvMYActivitySearch" />
Row layout is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<Button
android:id="#+id/btnHistory"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:background="#drawable/button_background"
android:text="History"/>
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="35dp" />
<TextView
android:id="#+id/tvContact"
android:layout_width="wrap_content"
android:layout_height="35dp" />
</LinearLayout>
Custom grid java file is like
public class CustomGrid extends BaseAdapter {
private Context mContext;
private final String[] names;
private final String[] Contact;
private final String[] ID;
public CustomGrid(Context c,String[] names,String[] Contact, String[] ID ) {
mContext = c;
this.Contact = Contact;
this.names = names;
this.ID = ID;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return names.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;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//grid = new View(mContext);
grid = inflater.inflate(R.layout.myactivity_singlerow, null);
} else {
grid = (View) convertView;
}
TextView tvName = (TextView) grid.findViewById(R.id.tvName);
TextView tvContact = (TextView) grid.findViewById(R.id.tvContact);
Button btnHistory = (Button) grid.findViewById(R.id.btnHistory);
tvName.setText(names[position]);
tvContact.setText(Contact[position]);
if(btnHistory!=null) {
btnHistory.setId(Integer.parseInt(ID[position]));
btnHistory.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
new MainActivity().testButton(view.getId());
}
}
);
}
return grid;
}
When I try to debug, I can see for the first time when position is '0', btnHistory is not null. But after that when position is 1, btnHistory is null.
What could possibly go wrong? When both the textviews are not null, why btnHistory is null?
Comment to improve your code:
callback somewhere outside your getView
I mean this:
public class CustomGrid extends BaseAdapter implements OnClickListener {
if(btnHistory!=null) {
btnHistory.setId(Integer.parseInt(ID[position]));
btnHistory.setTag((Integer)view.getId());
btnHistory.setOnClickListener(this);
}
public void onClick(View v) {
new MainActivity().testButton(v.getTag());
}
}
I want to set images in grid view with multiple columns. Images can be vary in columns.How can i achieve it. Thanks in advance
Images in GridView
create activity_main.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"
android:orientation="vertical"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="3" >
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</RelativeLayout>][1]][1]
Create MainActivity.
public class MainActivity extends Activity {
GridView gv;
Context context;
ArrayList prgmName;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv=(GridView) findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
}
#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;
}
}
Create a folder by name drawable in res directory. Insert the images into drawable folder by name as below.
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
Create the layout as programlist.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/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
Create Customclass ie CustomAdapter.
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
You will get the Output like..
I want a Gridview with image and EditText. I want to show image in fullscreen on click and select that Gridview item to delete on long click.But the GridView events OnItemLongClickListener() and OnItemClickListener() are not responding with EditText, though they work when Edittext android:focusable="false" is set. I have tried many different solutions but none of them are working. here is my code:
activity_main.xml
<LinearLayout 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=".MainActivity" >
<GridView
android:numColumns="2"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/grid"
/>
</LinearLayout>
grid_single.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical"
android:id="#+id/lin"
>
<ImageView
android:id="#+id/grid_image"
android:layout_width="50dp"
android:layout_height="50dp">
</ImageView>
<EditText
android:id="#+id/grid_text"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="15dp" >
</EditText>
</LinearLayout>
Main_activity.java
//import not included
public class MainActivity extends Activity {
GridView grid;
String[] web = {
"Google",
"Github",
"Instagram",
"Facebook",
} ;
int[] imageId = {
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher,
R.drawable.ic_launcher
};
private LayoutParams layoutParams;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
grid=(GridView)findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
#Override
public boolean onItemLongClick(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
grid.setItemChecked(position, true);
View tv=(View) grid.getChildAt(position);
tv.setBackgroundColor(Color.LTGRAY);
return false;
}
});
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent,
View view, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
}
CustomGrid.java
//import not included
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);
EditText textView = (EditText) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
// Button b1=(Button)grid.findViewById(R.id.button1);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
Any help would be appreciated.. thanks in advance..
try adding these lines in your EditText
android:focusable="false"
android:focusableInTouchMode="false"
So finally solved the problem.
I used gesturelistener and detected motion.
Thank You.
I have a image button and a image view. I am trying to get a hover theme,like when the user clicks an item there will be a hover image over the item which means he checked the item. My xml:
layout1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/fetchedImageObj"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="#drawable/test"
android:background="#drawable/image_selector"
android:clickable="true"
android:padding="0dp"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/hoverImage"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="centerInside"
android:src="#drawable/correct"
android:visibility="gone"/>
</RelativeLayout>
And this is my adapter where i am trying to do the task. But the problem whenerver i select a item and scrolls down it disappears or switch to other object
Adapter:
public class FetchItemAdapter extends BaseAdapter {
private Activity myContext;
private LayoutInflater inflater;
public FetchModel fetchModel;
private boolean check_click=false;
public FetchItemAdapter(Activity context,FetchModel fetchModel)
{
this.myContext=context;
inflater = context.getLayoutInflater();
this.fetchModel = fetchModel;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return this.fetchModel.images.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;
}
private static class ViewHolder {
ImageButton fetchedImageObj;
ImageView selection;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
System.out.println("get View");
final ViewHolder viewHolder = new ViewHolder();
convertView = inflater.inflate(com.yolove.R.layout.fetched_images_row,null);
//viewHolder = new ViewHolder();
viewHolder.fetchedImageObj = (ImageButton) convertView.findViewById(com.yolove.R.id.fetchedImageObj);
viewHolder.selection=(ImageView)convertView.findViewById(com.yolove.R.id.hoverImage);
convertView.setTag(viewHolder);
ImageLoader.getInstance().displayImage(fetchModel.images.get(position).imageurl,viewHolder.fetchedImageObj);
viewHolder.fetchedImageObj.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(check_click==false)
{
viewHolder.selection.setVisibility(View.VISIBLE);
check_click=true;
}
else
{
viewHolder.selection.setVisibility(View.GONE);
check_click=false;
}
}
});
return convertView;
}
}
I have made my holder final to get the viewholder.selection object.
On my opinion you need to make a selector xml for your image background and set the image view visible in xml file.
In my android application how can I display different icons in the same GridView on button click event. I have to keep on interchanging between two sets of icons in my gridview on button click. What I have done is that I have made two activities each with a button and different set of icons in a GridView and keep on switching between those activities on button click. But is there any better approach for this that on same activity I can just change the gridview elements on button click? Thanks.
Edited
I am doing this somehow using the ViewSwitcher by the following code:
JavaActivity Code:
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
GridView EngGrid,UrduGrid;
final ViewSwitcher switcher;
Button Next, Previous;
EngGrid=(GridView) findViewById(R.id.gridView1);
EngGrid.setAdapter(new EngAdapter(this));
EngGrid.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
// Toast.makeText(MenuActivity.this, "" + position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), DisplayActivity.class);
i.putExtra("menu_id", position);
startActivity(i);
}
});
UrduGrid=(GridView) findViewById(R.id.gridView2);
UrduGrid.setAdapter(new UrduAdapter(this));
UrduGrid.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
// Toast.makeText(MenuActivity.this, "" + position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), DisplayActivity.class);
i.putExtra("menu_id", position);
startActivity(i);
}
});
switcher = (ViewSwitcher) findViewById(R.id.ViewSwitcher);
Next = (Button) findViewById(R.id.button2);
Previous = (Button) findViewById(R.id.button1);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new AnimationUtils();
switcher.setAnimation(AnimationUtils.makeInAnimation
(getBaseContext(), true));
switcher.showNext();
}
});
Previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new AnimationUtils();
switcher.setAnimation(AnimationUtils.makeInAnimation
(getBaseContext(), true));
switcher.showPrevious();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_test, menu);
return true;
}
public class UrduAdapter extends BaseAdapter{
public Integer[] mThumbIds = {
R.drawable.urdu_dua1, R.drawable.urdu_dua2,
R.drawable.urdu_dua3, R.drawable.urdu_dua4,
R.drawable.urdu_dua5, R.drawable.urdu_dua6,
R.drawable.urdu_dua7, R.drawable.urdu_dua8,
R.drawable.urdu_dua9, R.drawable.urdu_dua10,
R.drawable.urdu_dua11, R.drawable.urdu_dua12,
R.drawable.urdu_dua13, R.drawable.urdu_dua14,
R.drawable.urdu_dua15, R.drawable.urdu_dua16,
R.drawable.urdu_dua17, R.drawable.urdu_dua18,
R.drawable.urdu_dua19, R.drawable.urdu_dua20,
R.drawable.urdu_dua21
};
private Context mContext;
public UrduAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[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
View MyView;
if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView =li.inflate(R.layout.urdumenuitem, parent,false);
}
else{
MyView =(View)convertView;
}
ImageView iv=(ImageView)MyView.findViewById(R.id.image1);
iv.setImageResource(mThumbIds[position]);
return MyView;
}
}
public class EngAdapter extends BaseAdapter{
public Integer[] mThumbIds = {
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;
public EngAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[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
View MyView;
if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView =li.inflate(R.layout.urdumenuitem, parent,false);
}
else{
MyView =(View)convertView;
}
ImageView iv=(ImageView)MyView.findViewById(R.id.image1);
iv.setImageResource(mThumbIds[position]);
return MyView;
}
}
}
XML_layout:
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ViewSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0EEE0"
>
<GridView
android:id="#+id/gridView1"
android:layout_below="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:horizontalSpacing="5dp"
android:layout_margin="10dp"
android:stretchMode="columnWidth"
android:gravity="center_vertical"
/>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/gridView1"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="#drawable/settings"
android:focusable="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0EEE0"
>
<GridView
android:id="#+id/gridView2"
android:layout_below="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:horizontalSpacing="5dp"
android:layout_margin="10dp"
android:stretchMode="columnWidth"
android:gravity="center_vertical"
/>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/gridView2"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="#drawable/settings"
android:focusable="true" />
</RelativeLayout>
</ViewSwitcher>
Question still remains, how can we switch between the two sets of icons in the same GridView on button click.
In your GridView's adapter, you can create a method that changes the icons of the views, for example:
private boolean imageSetChange = false;
public void changeImages(boolean change){
this.imageSetChange = change;
notifyDataSetChanged();
}
Then in your getView() method, you switch the item depending on the value of imageSetChanged.