Reputation: 141
I want my GridView with ImageAdapter to have custom pressed buttons. I already have custom buttons but onClick it creates an big orange onClick thing around the button. I just want onClick that it shows my custom pressed button. How should I make that easily?
ImageAdapter:
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] buttonValues;
public ImageAdapter(Context context, String[] buttonValues) {
this.context = context;
this.buttonValues = buttonValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.mobile, null);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
String button = buttonValues[position];
if (button.equals("homework")) {
imageView.setImageResource(R.drawable.homework);
} else if (button.equals("schedule")) {
imageView.setImageResource(R.drawable.button_schedule);
} else if (button.equals("planner")) {
imageView.setImageResource(R.drawable.plannerbut);
} else {
imageView.setImageResource(R.drawable.settingsbut);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return buttonValues.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
And my Activity (GridView method):
private void setGridView() {
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
switch (position) {
case 0:
Toast.makeText(getApplicationContext(), "homework", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getApplicationContext(), "schedule", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getApplicationContext(), "planner",
Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(getApplicationContext(), "settings",
Toast.LENGTH_SHORT).show();
break;
}
}
});
}
Thanks in advance!
Wouter
Upvotes: 1
Views: 236
Reputation: 141
I fixed it by adding a ListSelector to my gridview and adding a selector in the ImageAdapter.
Upvotes: 0
Reputation: 706
You can have your button use a custom drawable where you define the different states yourself.
<Button
android:id="@+id/mybutton"
android:background="@drawable/custom_button_selector" />
custom_button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/bg_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/bgalt" />
<item android:drawable="@drawable/bg_normal" />
</selector>
And then in bg_pressed/normal define the different buttons.
Have a look at http://www.mkyong.com/android/android-imagebutton-selector-example/
Upvotes: 1