Skip to content

Commit

Permalink
replace hardcoded menu with resource
Browse files Browse the repository at this point in the history
  • Loading branch information
TrianguloY committed Jan 28, 2024
1 parent 5bf88d6 commit 56aa53f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,20 @@ protected void onCreate(Bundle savedInstanceState) {

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// option for the open in clipboard shortcut
menu.add(R.string.shortcut_checkClipboard)
.setIcon(AndroidUtils.getColoredDrawable(R.drawable.ic_clipboard, android.R.attr.textColorPrimary, this))
.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT)
.setOnMenuItemClickListener(o -> {
PackageUtils.startActivity(
new Intent(this, ShortcutsActivity.class),
R.string.toast_noApp,
this
);
return true;
});
getMenuInflater().inflate(R.menu.activity_main, menu);
AndroidUtils.fixMenuIconColor(menu.findItem(R.id.menu_checkClipboard), this);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.menu_checkClipboard) {
// the open in clipboard shortcut
PackageUtils.startActivity(new Intent(this, ShortcutsActivity.class), R.string.toast_noApp, this);
}
return super.onOptionsItemSelected(item);
}

@Override
protected void onResume() {
super.onResume();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.util.Patterns;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
Expand Down Expand Up @@ -206,19 +206,15 @@ static void setHideableText(TextView view, CharSequence text) {
}

/**
* Returns a drawable with a different color
* Fixes the color of a MenuItem icon (colorFilter=textColorPrimary)
*/
static Drawable getColoredDrawable(int drawableId, int colorAttr, Context cntx) {
// get drawable
var drawable = cntx.getResources().getDrawable(drawableId).mutate();

static void fixMenuIconColor(MenuItem menuItem, Context cntx) {
// get color
var resolvedAttr = new TypedValue();
cntx.getTheme().resolveAttribute(colorAttr, resolvedAttr, true);
cntx.getTheme().resolveAttribute(android.R.attr.textColorPrimary, resolvedAttr, true);

// tint
drawable.setColorFilter(cntx.getResources().getColor(resolvedAttr.resourceId), PorterDuff.Mode.SRC_IN);
return drawable;
menuItem.getIcon().setColorFilter(cntx.getResources().getColor(resolvedAttr.resourceId), PorterDuff.Mode.SRC_IN);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/menu/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_checkClipboard"
android:icon="@drawable/ic_clipboard"
android:showAsAction="ifRoom|withText"
android:title="@string/shortcut_checkClipboard" />
</menu>

0 comments on commit 56aa53f

Please sign in to comment.