diff options
author | cyBerta <cyberta@riseup.net> | 2019-07-10 03:24:43 +0200 |
---|---|---|
committer | cyBerta <cyberta@riseup.net> | 2019-07-10 03:24:43 +0200 |
commit | ef3a05a11724f0a8e33895e1ea8bd792063bd1fc (patch) | |
tree | 1c265a54afc48dde8be92d3bdfb2b4c6553e1e0a /app/src/main/java/se | |
parent | 9bb5d109ddd73b8e31e5f5d862372758290227aa (diff) |
add material design icons to the navigation drawer
Diffstat (limited to 'app/src/main/java/se')
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/DrawerSettingsAdapter.java | 44 | ||||
-rw-r--r-- | app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java | 21 |
2 files changed, 43 insertions, 22 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/DrawerSettingsAdapter.java b/app/src/main/java/se/leap/bitmaskclient/DrawerSettingsAdapter.java index 01b10575..89aeb4be 100644 --- a/app/src/main/java/se/leap/bitmaskclient/DrawerSettingsAdapter.java +++ b/app/src/main/java/se/leap/bitmaskclient/DrawerSettingsAdapter.java @@ -16,6 +16,9 @@ */ package se.leap.bitmaskclient; +import android.content.Context; +import android.graphics.drawable.Drawable; +import android.support.annotation.DrawableRes; import android.support.annotation.NonNull; import android.support.v7.widget.SwitchCompat; import android.view.LayoutInflater; @@ -23,6 +26,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CompoundButton; +import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; @@ -52,21 +56,27 @@ public class DrawerSettingsAdapter extends BaseAdapter { private boolean isChecked = false; private int itemType = NONE; private CompoundButton.OnCheckedChangeListener callback; + private Drawable iconResource; - private DrawerSettingsItem(String description, int viewType, boolean isChecked, int itemType, CompoundButton.OnCheckedChangeListener callback) { + private DrawerSettingsItem(Context context, String description, @DrawableRes int iconResource, int viewType, boolean isChecked, int itemType, CompoundButton.OnCheckedChangeListener callback) { this.description = description; this.viewType = viewType; this.isChecked = isChecked; this.itemType = itemType; this.callback = callback; + try { + this.iconResource = context.getResources().getDrawable(iconResource); + } catch (RuntimeException e) { + e.printStackTrace(); + } } - public static DrawerSettingsItem getSimpleTextInstance(String description, int itemType) { - return new DrawerSettingsItem(description, VIEW_SIMPLE_TEXT, false, itemType, null); + public static DrawerSettingsItem getSimpleTextInstance(Context context, String description, @DrawableRes int iconResource, int itemType) { + return new DrawerSettingsItem(context, description, iconResource, VIEW_SIMPLE_TEXT, false, itemType, null); } - public static DrawerSettingsItem getSwitchInstance(String description, boolean isChecked, int itemType, CompoundButton.OnCheckedChangeListener callback) { - return new DrawerSettingsItem(description, VIEW_SWITCH, isChecked, itemType, callback); + public static DrawerSettingsItem getSwitchInstance(Context context, String description, @DrawableRes int iconResource, boolean isChecked, int itemType, CompoundButton.OnCheckedChangeListener callback) { + return new DrawerSettingsItem(context, description, iconResource, VIEW_SWITCH, isChecked, itemType, callback); } public int getItemType() { @@ -138,7 +148,7 @@ public class DrawerSettingsAdapter extends BaseAdapter { switch(type) { case VIEW_SIMPLE_TEXT: convertView = initTextViewBinding(holder); - holder.textView.setText(drawerSettingsItem.description); + bindSimpleText(drawerSettingsItem, holder); break; case VIEW_SWITCH: convertView = initSwitchBinding(holder); @@ -154,7 +164,7 @@ public class DrawerSettingsAdapter extends BaseAdapter { holder.resetSwitchView(); convertView = initTextViewBinding(holder); } - holder.textView.setText(drawerSettingsItem.description); + bindSimpleText(drawerSettingsItem, holder); break; case VIEW_SWITCH: if (!holder.isSwitchViewHolder()) { @@ -169,23 +179,36 @@ public class DrawerSettingsAdapter extends BaseAdapter { return convertView; } + private void bindSimpleText(DrawerSettingsItem drawerSettingsItem, ViewHolder holder) { + holder.textView.setText(drawerSettingsItem.description); + if (drawerSettingsItem.iconResource != null) { + holder.iconView.setImageDrawable(drawerSettingsItem.iconResource); + } + } + private void bindSwitch(DrawerSettingsItem drawerSettingsItem, ViewHolder holder) { holder.switchView.setChecked(drawerSettingsItem.isChecked); - holder.switchView.setText(drawerSettingsItem.description); + holder.textView.setText(drawerSettingsItem.description); holder.switchView.setOnCheckedChangeListener(drawerSettingsItem.callback); + if (drawerSettingsItem.iconResource != null) { + holder.iconView.setImageDrawable(drawerSettingsItem.iconResource); + } } @NonNull private View initSwitchBinding(ViewHolder holder) { View convertView = mInflater.inflate(R.layout.v_switch_list_item, null); - holder.switchView = convertView.findViewById(android.R.id.text1); + holder.switchView = convertView.findViewById(R.id.option_switch); + holder.textView = convertView.findViewById(android.R.id.text1); + holder.iconView = convertView.findViewById(R.id.material_icon); return convertView; } @NonNull private View initTextViewBinding(ViewHolder holder) { - View convertView = mInflater.inflate(R.layout.v_single_list_item, null); + View convertView = mInflater.inflate(R.layout.v_icon_text_list_item, null); holder.textView = convertView.findViewById(android.R.id.text1); + holder.iconView = convertView.findViewById(R.id.material_icon); return convertView; } @@ -200,6 +223,7 @@ public class DrawerSettingsAdapter extends BaseAdapter { static class ViewHolder { TextView textView; + ImageView iconView; SwitchCompat switchView; boolean isSwitchViewHolder() { diff --git a/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java b/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java index 21d061d1..635238f2 100644 --- a/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java +++ b/app/src/main/java/se/leap/bitmaskclient/drawer/NavigationDrawerFragment.java @@ -244,12 +244,7 @@ public class NavigationDrawerFragment extends Fragment { private void setupAccountsListView() { drawerAccountsListView = drawerView.findViewById(R.id.accountList); drawerAccountsListView.setAdapter(accountListAdapter); - drawerAccountsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { - @Override - public void onItemClick(AdapterView<?> parent, View view, int position, long id) { - selectItem(parent, position); - } - }); + drawerAccountsListView.setOnItemClickListener((parent, view, position, id) -> selectItem(parent, position)); } private void setupSettingsListView() { @@ -261,22 +256,24 @@ public class NavigationDrawerFragment extends Fragment { private void setupSettingsListAdapter() { settingsListAdapter = new DrawerSettingsAdapter(getLayoutInflater()); if (getContext() != null) { - settingsListAdapter.addItem(getSwitchInstance(getString(R.string.save_battery), + settingsListAdapter.addItem(getSwitchInstance(getContext(), + getString(R.string.save_battery), + R.drawable.ic_battery_36, getSaveBattery(getContext()), BATTERY_SAVER, (buttonView, newStateIsChecked) -> onSwitchItemSelected(BATTERY_SAVER, newStateIsChecked))); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - settingsListAdapter.addItem(getSimpleTextInstance(getString(R.string.always_on_vpn), ALWAYS_ON)); + settingsListAdapter.addItem(getSimpleTextInstance(getContext(), getString(R.string.always_on_vpn), R.drawable.ic_always_on_36, ALWAYS_ON)); } if (isDefaultBitmask()) { - settingsListAdapter.addItem(getSimpleTextInstance(getString(switch_provider_menu_option), SWITCH_PROVIDER)); + settingsListAdapter.addItem(getSimpleTextInstance(getContext(), getString(switch_provider_menu_option), R.drawable.ic_switch_provider_36, SWITCH_PROVIDER)); } - settingsListAdapter.addItem(getSimpleTextInstance(getString(log_fragment_title), LOG)); + settingsListAdapter.addItem(getSimpleTextInstance(getContext(), getString(log_fragment_title), R.drawable.ic_log_36, LOG)); if (ENABLE_DONATION) { - settingsListAdapter.addItem(getSimpleTextInstance(getString(donate_title), DONATE)); + settingsListAdapter.addItem(getSimpleTextInstance(getContext(), getString(donate_title), R.drawable.ic_donate_36, DONATE)); } - settingsListAdapter.addItem(getSimpleTextInstance(getString(about_fragment_title), ABOUT)); + settingsListAdapter.addItem(getSimpleTextInstance(getContext(), getString(about_fragment_title), R.drawable.ic_about_36, ABOUT)); } private ActionBar setupActionBar() { |