1
I created a Toolbar for Mainactivity, as shown below:
However, when I add any other widget in the same Activity, Toolbar disappears when running the emulator:
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 theTextView
.– viana
The problem is that when you put one
id
ininclude
, theid
assigned to it will be passed to the view it represents. That is, Toolbar’s Id ends up being 'include'.– itscorey
@acklay tried and happens the same problem.
– Lucas
@Iamluc when adding a new widget, this
id
is automatically assigned to theinclude
.– Lucas
Try to put Toolbar without using include...
– itscorey
@Lucas this enclosed answer answers that question. I had never come across that. Abs. Good luck.
– viana