Dynamic TextView on ActionBar

后端 未结 2 1839
一整个雨季
一整个雨季 2021-01-01 22:25

I need to implement a TextView in the ActionBar. This TextView shows the status of the bluetooth connection so it will update depending of this status.

I\'m not talk

相关标签:
2条回答
  • 2021-01-01 23:04

    Just in case anyone still needs this:

    You can dynamically set the text property of menu item by calling the setTitle("Your new string or String resource id") method on the menu item. That way, you do not have to cast the ActionView into a TextView. For example:

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
    
        String bluetoothStatus = "Connected"; //get from source
    
       menu.findItem(R.id.the_id_of_the_menu_item).setTitle(bluetoothStatus);
        return super.onPrepareOptionsMenu(menu);
    }
    
    0 讨论(0)
  • 2021-01-01 23:13

    You can Add TextView manually in

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    

    By following code.

    TextView tv = new TextView(this);
                tv.setText(getString(R.string.matchmacking)+"  ");
                tv.setTextColor(getResources().getColor(R.color.WHITE));
                tv.setOnClickListener(this);
                tv.setPadding(5, 0, 5, 0);
                tv.setTypeface(null, Typeface.BOLD);
                tv.setTextSize(14);
                menu.add(0, FILTER_ID, 1, R.string.matchmacking).setActionView(tv).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    

    and if you want to access text view then make tv as class variable.

    0 讨论(0)
提交回复
热议问题