Greetings, it’s not possible to dynamically display a button in the toolbar. It is necessary that it appears if the EditText is not empty.

MainActivity.java

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_auth); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); Toolbar toolbar = (Toolbar)findViewById(R.id.tootbar); Button getCodeBtn = (Button)findViewById(R.id.getCodeBtn); EditText phoneNumber = (EditText)findViewById(R.id.phoneNumber); setSupportActionBar(toolbar); getSupportActionBar().setTitle("Ваш номер телефона"); toolbar.setTitleTextColor(Color.WHITE); phoneNumber.setHint("+ Номер телефона"); phoneNumber.addTextChangedListener(new PhoneNumberFormattingTextWatcher()); String number = phoneNumber.getText().toString(); if(!TextUtils.isEmpty(number)){ ТУТ ВЫВОДИМ } else { ТУТ СКРЫВАЕМ } 

menu.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/getCodeBtn" app:showAsAction="always" android:title="" android:icon="@drawable/ic_done_white_24px" /> </menu> 

    1 answer 1

    You can memorize the menuItem during the initialization of the menu and control its visibility with a text change subscription:

     // элемент меню, чьей видимостью будем управлять private MenuItem _codeMenuItem; @Override protected void onCreate(Bundle savedInstanceState) { // ... // добавим подписку с переопределённым методом для управления видимостью text.addTextChangedListener(new PhoneNumberFormattingTextWatcher(){ @Override public synchronized void afterTextChanged(Editable s) { super.afterTextChanged(s); String number = s.toString(); _codeMenuItem.setVisible(!TextUtils.isEmpty(number)); } }); // ... } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); // запомним кнопку меню _codeMenuItem = menu.findItem(R.id.getCodeBtn); } 
    • thank you very much - Andrew
    • @ Andrey, tick off the answer if it was useful :) - XIII-th