Android - Hide Actionbar buttons

Asked

Viewed 512 times

1

I made a code for a button to be pressed to display an Edittext. But I only managed to hide the title and icon of the application. So how do I hide the buttons too?

Mainactivity.java:

package com.mycompany.myapp;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;


public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button)findViewById(R.id.btn);
        final EditText actionBarText = (EditText)findViewById(R.id.action_bar_text);
        btn.setOnClickListener(new OnClickListener(){
            public void onClick(View p1){
            final ActionBar actionBar = getActionBar();
            final ViewGroup actionBarLayout = (ViewGroup) getLayoutInflater().inflate(R.layout.action_bar, null);
               actionBar.setDisplayShowHomeEnabled(false);
               actionBar.setDisplayShowTitleEnabled(false);
               actionBar.setDisplayShowCustomEnabled(true);
               actionBar.setCustomView(actionBarLayout);
           });
    }

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {     
      MenuInflater inflater = getMenuInflater();     
      inflater.inflate(R.menu.menu, menu);  
      return super.onCreateOptionsMenu(menu); 
    } 
}

action_bar.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="center">

    <EditText
        android:id="@+id/action_bar_text"
        android:hint="Pesquisar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"/>
</LinearLayout>

1 answer

1


To hide Actionbar buttons you need to create an instance Menuitem item follows an example of how I do.

   public boolean onCreateOptionsMenu (Menu menu){
                String permissoes = sharedPreferencesClasse.obterSharedPreferences(AlterarAutenticacoesActivity.this, "permissoesTabelas");
                if (permissoes == null) {
                    permissoes = "0";
                }
                PermissoesUsuarioUtils permissoesUsuarioUtils = new PermissoesUsuarioUtils(permissoes);

                getMenuInflater().inflate(R.menu.menu_autenticacoes, menu);
                MenuItem itemLixeira = menu.findItem(R.id.menuExcluir);
                MenuItem itemSalvar = menu.findItem(R.id.menuSalvar);

                if (permissoesUsuarioUtils.verificaSeTemPermissoesPorCodigo("12") || permissoesUsuarioUtils.verificaSeUsuarioTemTodasPermissoes()) {
                    itemLixeira.setVisible(true);
                } else {
                    itemLixeira.setVisible(false);
                }
                if (permissoesUsuarioUtils.verificaSeTemPermissoesPorCodigo("11") || permissoesUsuarioUtils.verificaSeUsuarioTemTodasPermissoes()) {
                    itemSalvar.setVisible(true);
                } else {
                    itemSalvar.setVisible(false);
                }

                return true;
            }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.