Я хочу переключить активность по щелчку значка панели инструментов.Как реализовать событие click на значке панели инструментов?
мой код:
Toolbar toolbar = (Toolbar) findViewById(R.id.mytoolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setIcon(R.mipmap.ic_launcher);
и обрабатываются по щелчку, как это но не работает:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent i = new Intent(MainActivity.this,SecondActivity.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
и XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rel_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.project.www.simpleproject.activity.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/mytoolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/AppToolbar">
</android.support.v7.widget.Toolbar>
Можно ли и если да, то дайте мне знать ThankYou
попробовать решение, прежде чем спрашивать –
я попробовал, но его не работает. –
что не работает –