Android - Hide ActionBar buttons

1

I made a code so that a button when pressed shows an EditText. However, I was only able 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>
    
asked by anonymous 11.05.2018 / 20:52

1 answer

1

To hide the ActionBar buttons you need to create a MenuItem item instance follows an example of how I do it.

   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;
            }
    
11.05.2018 / 21:24