Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Android] Simple ListView-based “menu” using compound drawables.
package me.diti.tests.android.exampleimagemenu.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import me.diti.tests.android.exampleimagemenu.R;
import me.diti.tests.android.exampleimagemenu.entity.ImageMenuItem;
import java.util.ArrayList;
public class ImageMenuAdapter extends ArrayAdapter<ImageMenuItem> {
private final Context context;
private final ArrayList<ImageMenuItem> list;
public ImageMenuAdapter(Context context, ArrayList<ImageMenuItem> list) {
super(context, android.R.layout.simple_list_item_1, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final TextView textView = (TextView)super.getView(position, convertView, parent);
textView.setText(list.get(position).text);
textView.setCompoundDrawablesRelativeWithIntrinsicBounds(list.get(position).img, 0, 0, 0);
textView.setCompoundDrawablePadding((int)context.getResources().getDimension(R.dimen.imagemenu_drawable_padding)); // 12dp
textView.setIncludeFontPadding(false);
// If the adapter’s `isEnabled()` is overridden, pass the enabled status to this implementation
textView.setEnabled(isEnabled(position));
return textView;
}
}
package me.diti.tests.android.exampleimagemenu.entity;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
public class ImageMenuItem {
public final int img;
public final int text;
public ImageMenuItem(@DrawableRes int img, @StringRes int text) {
this.img = img;
this.text = text;
}
}
package me.diti.tests.android.exampleimagemenu;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import me.diti.tests.android.exampleimagemenu.entity.ImageMenuItem;
public class MainActivity extends AppCompatActivity {
static final int ITEM_MY_ACCOUNT = 0;
static final int ITEM_MY_SETTINGS = 1;
static final int ITEM_SHOW_TOAST = 2;
static final int ITEM_CLOSE_APP = 3;
static final int ITEM_DISABLED = 4;
static final int ITEM_DISABLED_NO_ICON = 5;
private final ArrayList<ImageMenuItem> menuItems = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView)findViewById(R.id.listView);
menuItems.add(ITEM_MY_ACCOUNT, new ImageMenuItem(R.drawable.ic_person_black_24dp, R.string.menuitem_my_account));
menuItems.add(ITEM_MY_SETTINGS, new ImageMenuItem(R.drawable.ic_settings_black_24dp, R.string.menuitem_my_settings));
menuItems.add(ITEM_SHOW_TOAST, new ImageMenuItem(R.drawable.ic_error_outline_black_24dp, R.string.menuitem_show_toast));
menuItems.add(ITEM_CLOSE_APP, new ImageMenuItem(R.drawable.ic_close_black_24dp, R.string.menuitem_close_app));
menuItems.add(ITEM_DISABLED, new ImageMenuItem(R.drawable.ic_remove_circle_outline_black_24dp, R.string.menuitem_disabled));
menuItems.add(ITEM_DISABLED_NO_ICON, new ImageMenuItem(0, R.string.menuitem_disabled_no_icon)); // Example without icon
// Populate listView with menuItems
listView.setAdapter(new ImageMenuAdapter(this, menuItems) {
@Override
public boolean isEnabled(int position) {
switch (position) {
case ITEM_DISABLED:
case ITEM_DISABLED_NO_ICON:
return false;
default:
return true;
}
}
});
// Handle clicks on menu items
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
switch (position) {
case ITEM_MY_ACCOUNT:
case ITEM_MY_SETTINGS:
case ITEM_SHOW_TOAST:
Toast.makeText(MainActivity.this, "Clicked: " + getString(menuItems.get(position).text), Toast.LENGTH_SHORT).show();
break;
case ITEM_CLOSE_APP:
finishAffinity();
break;
default:
Log.w("ImageMenu", "Menu item at position #" + position + " not yet implemented");
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment