populate different gridview elements in android activity on button click - android

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.

Related

Image become gone when the view is reloaded in adapter

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.

Android onItemClickListener does not work (No focusables in code)

This has been driving me crazy over the past several hours... and I feel like it's something obvious...
I've gotten my code down to a simple ListView with a single TextView in it, but still can't seem to make it work (I don't see the toast or log message in the console).
Any help is greatly appreciated! :)
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SparseArray<Data> data = createData();
ListView listView = (ListView) findViewById(R.id.listView);
final CustomListAdapter adapter = new CustomListAdapter(this, data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Log.v("onItemClick", "Clicked");
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
CustomListAdapter.java
public class CustomListAdapter extends BaseAdapter {
private final SparseArray<Data> data;
private LayoutInflater inflater;
private final Activity activity;
public CustomListAdapter(Activity activity, SparseArray<Data> data) {
this.activity = activity;
this.data = data;
this.inflater = activity.getLayoutInflater();
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int position) {
Log.v("getItem", "" + position);
return data.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public int getItemViewType(int position) {
//TODO: Implement Image vs no image views
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parentView) {
if(convertView == null) {
convertView = inflater.inflate(R.layout.data, null);
}
Data data = (Data) getItem(position);
((TextView) convertView.findViewById(R.id.dataTitle)).setText(data.getTitle());
return convertView;
}
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
#Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isEnabled(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:background="#color/background" >
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background" />
</LinearLayout>
data.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="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp" >
<TextView
android:id="#+id/dataTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"
android:paddingLeft="0dp"
android:paddingBottom="8dp"
android:textSize="26sp"
android:textColor="#color/dataTitle" />
</LinearLayout>
NOTE: I do see that the touch even is at least being registered by the device:
07-15 22:00:50.734: I/InputReader(804): Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.23535 ] when=378716384422000
07-15 22:00:50.734: I/InputDispatcher(804): Delivering touch to: action: 0x4
07-15 22:00:50.734: I/InputDispatcher(804): Delivering touch to: action: 0x0
Remove android:clickable="false" from the TextView.
Also, remove android:focusable="false".
You can't catch clicks if you have these set as false.
Secondly,
#Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean isEnabled(int arg0) {
// TODO Auto-generated method stub
return false;
}
Return true in these methods. You won't be able to catch clicks if these return false for every item.
IMO it's not a good idea to override methods that you do not necessarily have to or are unsure as to what the method should return.

How to check checkbox on clicking an image?

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
}
}

How to add ImageView, TextView and Button in ListView in android

In my application I am trying to create a ListView that contains an ImageView a TextView and a Button. I have created a separate XML file and drag all the above mentioned elements in that XML and in my main java file I have created an Object of BaseAdapter and in the getView() method I have declared these elements but when I run the application I cant see the list. I haven't used the BaseAdapter, so I am missing with some code in order to view the List. I also want to apply some operations on the button so please let me know that code also.
Code for my main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Code for my list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="0dp"
android:layout_marginTop="15dp"
android:text="Medium Text"
android:textSize="15dp" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="25dp"
android:text="Button" />
</LinearLayout>
Code for my main.java file:
public class CustomListActivity extends Activity {
/** Called when the activity is first created. */
ListView lv;
LayoutInflater inflator;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.listView1);
BaseAdapter bs = new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vw = inflator.inflate(R.layout.list_items, null);
ImageView img = (ImageView)findViewById(R.id.imageView1);
TextView tv = (TextView)findViewById(R.id.textView1);
Button btn = (Button)findViewById(R.id.button1);
return vw;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
};
}
}
Thanks in advance....
ok,
public class App_Adapter extends BaseAdapter implements OnClickListener{
private Activity mActivity;
private List<App_List> mList;
private static LayoutInflater inflater=null;
private PackageManager pm;
private String appclass;
private ApplicationTask mApplicationTask;
private String link=null;
public App_Adapter (FavouriteApp favouriteApp,List<App_List> mAppList, String appclass) {
// TODO Auto-generated constructor stub
this.mActivity= favouriteApp;
this.mList= mAppList;
this.appclass = appclass;
inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pm=mActivity.getPackageManager();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View mView=convertView;
if (convertView == null)
mView = inflater.inflate(R.layout.app_adapter, parent,false);
App_List mAppList= mList.get(position);
**here i am setting two textview and one button**
((TextView) mView.findViewById(R.id.textView_appName)).setText(mAppList.getApp_Name());
((TextView) mView.findViewById(R.id.textView_appDescription)).setText(mAppList.getApp_Description());
boolean status = isAppInstalled(mAppList.getApp_Pkg());
Button btn = (Button) mView.findViewById(R.id.button_appStatus);
**// register the button for clicklistener**
btn.setOnClickListener(this);
return mView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
and call this adapter class from your activity.
You are definitely missing lv.setAdapter(bs);

How to preview the image if it is set as wallpaper programmatically in android

I have setted 1 image as mywallpaper from Gridview images,but before that i need preview of that image before setting it as wallpaper like home screen image
Note: Preview of homescreen image
Create a XML file for the preview
displayfulimage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llFullImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imgvFullImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.65" />
<TextView
android:id="#+id/txtImageName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:text="Image Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"/>
</LinearLayout>
Check the following code. It may be of some help...
Things to note ::: 1. getView() 2. onItemClick()
public class MembersGallery extends Activity implements OnItemClickListener{
GridView gvMembers;
int values[]={R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e,R.drawable.f};
ImageView im;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memberimages);
gvMembers =(GridView)findViewById(R.id.gvMembers);
MyAdapter myad = new MyAdapter();
gvMembers.setAdapter(myad);
gvMembers.setOnItemClickListener(this);
}
class MyAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
return values.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;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
im=new ImageView(MembersGallery.this);
im.setPadding(6, 6, 6, 6);
im.setLayoutParams(new GridView.LayoutParams(85,85));
im.setImageResource(values[position]);
return im;
}
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
//Toast.makeText(this," Clicked", 30).show();
ImageView rImage = new ImageView(this);
rImage.setImageResource(values[arg2]);
displayFullImage(rImage,250,250);
}
private void displayFullImage(ImageView im2, int width, int height) {
// TODO Auto-generated method stub
ImageView tempImageView = im2;
AlertDialog.Builder imageDialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.displayfulimage,
(ViewGroup) findViewById(R.id.llFullImage));
ImageView image = (ImageView) layout.findViewById(R.id.imgvFullImage);
image.setImageDrawable(tempImageView.getDrawable());
imageDialog.setView(layout);
imageDialog.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
imageDialog.create();
imageDialog.show();
}
}

Categories

Resources