Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore default actionbar layout

I apply a custom View to the ActionBar, like this

// Inflate the "Done/Discard" custom ActionBar view.
LayoutInflater inflater = (LayoutInflater) DetailsHost.mActionBar
        .getThemedContext().getSystemService(DetailsHost.LAYOUT_INFLATER_SERVICE);
final View customActionBarView = inflater.inflate(
        R.layout.actionbar_custom_view_done_discard, null);

// Show the custom ActionBar view and hide the normal Home icon and title.
DetailsHost.mActionBar.setDisplayOptions(
        ActionBar.DISPLAY_SHOW_CUSTOM,
        ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
                | ActionBar.DISPLAY_SHOW_TITLE);
DetailsHost.mActionBar.setCustomView(customActionBarView, 
        new ActionBar.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

(based on Roman Nuriks code).

How do I restore the initial layout? Note: I use ActionBarSherlock

like image 291
Leandros Avatar asked Dec 14 '12 19:12

Leandros


People also ask

How do you display action bar back button on all versions of Android?

Show back button using actionBar. setDisplayHomeAsUpEnabled(true) this will enable the back button. Custom the back event at onOptionsItemSelected. This will enable the back function to the button on the press.

How do we remove the default ActionBar of an application?

If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme. AppCompat.

How do I replace my action bar toolbar?

You'll want to add android:fitsSystemWindows="true" to the parent layout of the Toolbar to ensure that the height of the activity is calculated correctly. When using the support library, make sure that you are importing android. support. v7.

Where is ActionBar in Android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


2 Answers

ActionBar actionbar;

actionbar = getActionBar();

Button cls = (Button)findViewById(R.id.btn_close);

cls.setOnClickListener(new OnClickListener(){           
    public void onClick(View view){
       actionbar.setDisplayShowCustomEnabled(false);                
   }            
});

Note: The button with id 'btn_close' was located in the custom actionbar layout.This function was written in mainactivity.

Hope this Helps!!

like image 112
Karthick Avatar answered Nov 12 '22 00:11

Karthick


Show/hide custom actionbar view

Since you only added a custom view to the bar without removing the title it should be sufficient to hide that custom View. You can use method setDisplayShowCustomEnabled(). Just call:

getActivity().getActionBar().setDisplayShowCustomEnabled(false);

And enable home functionality again:

getActivity().getActionBar().setDisplayShowHomeEnabled(true);

(Note in all code examples use getSupportActionBar() instead of getActionBar() if you're using actionbar compat. Also the getActivity() is only needed from fragments, in activities refer to the activity itself, in most cases this)

Restore actionbar title

If however you also removed the title when creating your custom view you'll have to enable that again also.

getActivity().getActionBar().setDisplayShowTitleEnabled(true);

Restore completely

You can also call the setDisplayOptions() method with a combination of options to reconfigure the actionbar in one call. The below example removes the custom view and shows the title.

getActivity().getActionBar().setDisplayOptions(
    ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);

See Android API docs for more details on these options.

like image 26
Tomik Avatar answered Nov 12 '22 00:11

Tomik