Skip to content Skip to sidebar Skip to footer

Remove Action Bar Icon With Custom View And Show Home Up Button

I am able to add custom view in actionbar. My application is supporting from version 2.3 so I had used ActionbarActivity. Now my query is to remove app icon from the actionbar and

Solution 1:

The up button is tied to home icon. That's why it's called "home as up" setDisplayHomeAsUpEnabled(true).

So you can't show up arrow with disabled "home" icon.

But you can make a View that looks like it in your custom View.

For example:

<LinearLayoutandroid:id="@android:id/home"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_vertical"android:orientation="horizontal"android:background="?attr/actionBarItemBackground"android:paddingRight="5dp"><!-- this will show tha up arrow --><ImageViewandroid:id="@+id/up"android:layout_width="wrap_content"android:layout_height="match_parent"android:src="?attr/homeAsUpIndicator"/><!-- place here any View that will be clickable along with "up" arrow (in this example, it's an icon) --><ImageViewandroid:id="@+id/home_icon"android:layout_width="wrap_content"android:layout_height="match_parent"android:layout_marginTop="@dimen/action_bar_icon_vertical_padding"android:layout_marginBottom="@dimen/action_bar_icon_vertical_padding"android:adjustViewBounds="true"android:scaleType="fitCenter"android:src="@drawable/app_icon"/></LinearLayout>

Where

android:src="?attr/homeAsUpIndicator"

Will set the up arrow based on your theme.

android:background="?attr/actionBarItemBackground"

Will set the blue selection background for pressed state

Now set an OnClickListener to a layout with arrow

View customNav = LayoutInflater.from(this).inflate(
        R.layout.custom_action_view, null);

customNav.findViewById(android.R.id.home).setOnClickListener(new View.OnClickListener() {
    @Override
    publicvoidonClick() {
        // up button clicked
    }
});


actionBar.setCustomView(customNav, lp);

Solution 2:

To remove Actonbar Icon:

Just add actionBar.setDisplayUseLogoEnabled(false); this line in oncreate() method:

To enable back button:

you need to extends SherlockActivity and insert this code in oncreate() method:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Handle back button action using this code:

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
              // do your action here when click on back button showing on action baronBackPressed();
              }
    }

Post a Comment for "Remove Action Bar Icon With Custom View And Show Home Up Button"