Android Color Context Menu

What is the Android process context?

  • It is the interface for global information about the application environment.
  • This is an abstract class provided by the Android system.
  • It allows access to application-specific resources and classes and calls to application-level operations such as launch activities, broadcast and receive intentions.

 

Zero: Demand Analysis

 

If there is such a requirement that the customer requests to modify the font "Process Context" in Android to a certain color, how do you do this?

The results are as follows:

 

One: Functional Realization

 

When a user presses a component for a long time, the menu that pops up is the context menu.

The steps to create a context menu using menu resources are as follows:

(0) Add a TextView with id tv to the XML file.

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello ZWZ!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

 

(1) Register the context menu in the onCreate method of MainActivity.

For example, here I register a context menu with a text box and use the following code to display the context menu only when I press the text box long.

    TextView textView1 = findViewById(R.id.tv);
    registerForContextMenu(textView1);

 

(2) Design Menu menu items and place them in the res / menu directory.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/m01"
        android:title="Language and Literature Division"
        />

    <item
        android:id="@+id/m02"
        android:title="Subdivision of Medicine and Health"
        />

    <item
        android:id="@+id/m03"
        android:title="Construction Engineering Subdivision"
        />

    <item
        android:id="@+id/m04"
        android:title="Information and Mechanical Engineering Subdivision"
        />

    <item
        android:id="@+id/m05"
        android:title="Tencent Cloud Internet College"
        />

    <item
        android:id="@+id/m06"
        android:title="Textile and Clothing and Art Design Division"
        />

</menu>

 

(3) Override the onCreateContextMenu method in MainActivity.

In this method, the onCreateContextMenu method of the parent class is inherited, then the inflate() method is called to parse a menu resource file, and the parsed menu is saved in the menu. The key code is as follows:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu,view,menuInfo);
        getMenuInflater().inflate(R.menu.menu, menu);
    }

 

(4) Add color to the text of the context menu, but Menu menu items do not add color directly.

We need to take the text out, save it as a SpannableString, and then set it back after adding a color attribute to the text.

The key codes are as follows:

MenuItem item1 = menu.findItem(R.id.m01);
SpannableString spannableString1 = new SpannableString(item1.getTitle());
spannableString1.setSpan(new ForegroundColorSpan(Color.RED), 0, spannableString1.length(), 0);
item1.setTitle(spannableString1);

Where R.id.m01 isMenu.xmlThe menu item ID in the file.

item1.getTitle() refers to taking out the text of the menu

Color.RED Or you can use Color.rgb(255,0,0) such RGB format to replace.

 

(5) Complete code.

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView1 = findViewById(R.id.tv);
        registerForContextMenu(textView1);

    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu,view,menuInfo);
        getMenuInflater().inflate(R.menu.menu, menu);

        MenuItem item1 = menu.findItem(R.id.m01);
        SpannableString spannableString1 = new SpannableString(item1.getTitle());
        spannableString1.setSpan(new ForegroundColorSpan(Color.RED), 0, spannableString1.length(), 0);
        item1.setTitle(spannableString1);

        MenuItem item2 = menu.findItem(R.id.m02);
        SpannableString spannableString2 = new SpannableString(item2.getTitle());
        spannableString2.setSpan(new ForegroundColorSpan(Color.MAGENTA), 0, spannableString2.length(), 0);
        item2.setTitle(spannableString2);

        MenuItem item3 = menu.findItem(R.id.m03);
        SpannableString spannableString3 = new SpannableString(item3.getTitle());
        spannableString3.setSpan(new ForegroundColorSpan(Color.YELLOW), 0, spannableString3.length(), 0);
        item3.setTitle(spannableString3);

        MenuItem item4 = menu.findItem(R.id.m04);
        SpannableString spannableString4 = new SpannableString(item4.getTitle());
        spannableString4.setSpan(new ForegroundColorSpan(Color.GREEN), 0, spannableString4.length(), 0);
        item4.setTitle(spannableString4);

        MenuItem item5 = menu.findItem(R.id.m05);
        SpannableString spannableString5 = new SpannableString(item5.getTitle());
        spannableString5.setSpan(new ForegroundColorSpan(Color.LTGRAY), 0, spannableString5.length(), 0);
        item5.setTitle(spannableString5);

        MenuItem item6 = menu.findItem(R.id.m06);
        SpannableString spannableString6 = new SpannableString(item6.getTitle());
        spannableString6.setSpan(new ForegroundColorSpan(Color.BLUE), 0, spannableString6.length(), 0);
        item6.setTitle(spannableString6);
    }
}

 

Two: Summary

 

The color context menu is one of the basic questions in my Android final exam. I think it is necessary to record and summarize that it can be used not only in the context menu, but also in the Toast pop-up message. There are many more that can be applied ~~

Keywords: Android xml encoding Attribute

Added by Zetusko on Thu, 25 Jun 2020 05:02:24 +0300