Toolbar button click event functionality


Question


I have created settings button in Toolbar, now I need to navigate the screen to settings screen, when ever i click the settings button.

image

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="schemas.android.com/apk/res/android"; xmlns:app="schemas.android.com/apk/res-auto">;
 <item android:id="@+id/action_settings" 
android:title="ConfigApp" 
android:icon="@drawable/ic_action_name" 
app:showAsAction="ifRoom" /> 
</menu>

Answer 1:


You can use onOptionsItemSelected function. This reference will help you do so.




Answer 2:


Try this:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.your_menu_xml, menu);
        return true;
    }

 public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_settings:
                //Strar activity here
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return super.onOptionsItemSelected(item);
    }



Answer 3:


you should override the onCreateOptionsMenu and onOptionsItemSelected methods and take that particular id from menuitem and navigate to settings screen.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.your_menu_xml, menu);
        return true;
    }

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_settings:
                Intent i = new Intent(yourActivity.this, SettingsActivity.class)
                startActivity(i);
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return super.onOptionsItemSelected(item);
    }


来源:https://stackoverflow.com/questions/63350590/how-do-i-make-my-overflow-actions-appear-with-a-materialtoolbar-that-is-set-as-t


码神部落- 版权声明 1、本主题所有言论和图片纯属会员个人意见,与码神部落立场无关。
2、本站所有主题由该帖子作者发表,该帖子作者屌丝范儿码神部落享有帖子相关版权。
3、码神部落管理员和版主有权不事先通知发贴者而删除本文。
4、其他单位或个人使用、转载或引用本文时必须同时征得该帖子作者屌丝范儿码神部落的同意。
5、帖子作者须承担一切因本文发表而直接或间接导致的民事或刑事法律责任。
6、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责。
7、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意。

最新回复 (0)
    • 码神部落
      2
        立即登录 立即注册 GitHub登录
返回
发新帖
作者最近主题: