How to add an interactive icon to the leftmost corner of the action bar on Android?

I would like to know how to create a shortcut in the corner of the action bar. I figured out how to add the application icon in the corner, but it is not clickable and has a noticeable delay time after opening the application.

At this moment I want to place any icon, not just the application icon. I would also like the icon to be clickable and ultimately able to post the activity in its place, for example, refresh the page or return to the top, like the Twitter app. I will include a screenshot of both my application and the Twitter application to give you an idea of ​​what I am asking.

Button image of Twitter application when I want:

enter image description here

, , :

enter image description here

+4
2

, , , .
, XML MainActivity. , , .

Activity xml :

<android.support.v7.widget.Toolbar
    android:id="@+id/main_screen_top_toolbar"
    android:minHeight="?attr/actionBarSize"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:contentInsetStart="0dp"
    app:contentInsetStart="0dp"
    android:theme="@style/MainToolBarTheme"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageButton
        android:id="@+id/main_screen_top_toolbar_settings"
        android:layout_height="75dip"
        android:layout_width="75dip"
        android:layout_gravity="left"
        android:background="@drawable/icon_settings" />

    <ImageButton
        android:id="@+id/main_screen_top_toolbar_logo"
        android:layout_height="70dip"
        android:layout_width="120dip"
        android:layout_gravity="center"
        android:background="@drawable/icon_app" />

    <ImageButton
        android:id="@+id/main_screen_top_toolbar_social"
        android:layout_height="80dip"
        android:layout_width="70dip"
        android:layout_gravity="right"
        android:background="@drawable/icon_social" />

</android.support.v7.widget.Toolbar>

style.xml - NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
</style>

<style name="MainToolBarTheme">
    <item name="android:background">@android:color/white</item>
    <item name="android:layout_height">100dp</item>
</style>

(.java ) - :

ImageButton settingsButton = (ImageButton) v.findViewById(R.id.main_screen_top_toolbar_settings);

settingsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Do whatever...
        }
    });

/ ImageButton . , , , .

+1

. , ,

XML: 1. .

<LinearLayout> 
......
......
...  
<android.support.v7.widget.Toolbar
 android:id = "@+id/toolbar"
 xmlns:local = "http://schemas.android.com/apk/res-auto"
 android:layout_width = "match_parent"
 android:layout_height = "wrap_content"
 android:background = "@color/colorPrimary"
 android:minHeight = "?attr/actionBarSize"
 local:popupTheme = "@style/ThemeOverlay.AppCompat.Light"
 local:theme = "@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

..... 
.........
</LinearLayout>

onCreate. , AppCompat Activity.

Toolbar toolbar;
toolbar = (Toolbar)findViewById(R.id.toolbar);

setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
  getSupportActionBar().setDisplayShowHomeEnabled(true);
  toolbar.setNavigationIcon(R.drawable.ic_menu);  // Replace with your icon
}

style.xml ,

<item name = "windowNoTitle">true</item>
<item name = "windowActionBar">false</item>

. .. ..!!

+1

Source: https://habr.com/ru/post/1624892/


All Articles