Toolbar disappears when I add another widget to Activity

Asked

Viewed 93 times

1

I created a Toolbar for Mainactivity, as shown below:

Toolbar funcionando

However, when I add any other widget in the same Activity, Toolbar disappears when running the emulator:

Toolbar sumiu

Has anyone ever had the same problem or knows how to solve?

Toolbar.xml code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:id="@+id/toolbar"
xmlns:android="http://schemas.android.com/apk/res/android" />

Menu code.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:icon="@drawable/ic_add_circle_outline_black_24dp"
    android:title="Adicionar outro item"
    android:id="@+id/adicionarItem"
    app:showAsAction="always"
    />
<item
    android:icon="@drawable/ic_save_black_24dp"
    android:title="Salvar lista"
    android:id="@+id/salvarLista"
    app:showAsAction="always"
    />
</menu>

activity_main.xml code (without adding another widget):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="lucaspereira.listadecompras.MainActivity">

<include
    layout="@layout/toolbar"/>
</RelativeLayout>

Mainactivity.java code.:

...
public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}
public boolean onCreateOptionsMenu (Menu menu){
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem menuItem){
    switch (menuItem.getItemId()){
        case R.id.adicionarItem:

            break;
        case R.id.salvarLista:

            break;
    }
    return true;
}
}
  • Try putting a android:layout_below="@id/include" in the TextView.

  • The problem is that when you put one id in include, the id assigned to it will be passed to the view it represents. That is, Toolbar’s Id ends up being 'include'.

  • @acklay tried and happens the same problem.

  • @Iamluc when adding a new widget, this id is automatically assigned to the include.

  • Try to put Toolbar without using include...

  • @Lucas this enclosed answer answers that question. I had never come across that. Abs. Good luck.

Show 1 more comment

1 answer

0

The error occurs when the id of your <include> is different findViewById(R.id.toolbar);. In your case is android:id="@+id/include". See an example below:

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar"/>

In class:

toolbar = (Toolbar)findViewById(R.id.toolbar);

And so that any other view below, it is necessary to define the android:layout_below referencing the id of your <include>. Behold:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/include"
    android:text="Caraca funfou!" />
  • Implement your answer here: to use with different ids, when trying to create a reference for Toolbar, you first need to create a reference for Include.

  • View include = findViewById(R.id.include); and then to Toolbar: Toolbar appBar = (Toolbar) include.findViewById(R.id.toolbar);

Browser other questions tagged

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