DonDave DonDave - 3 years ago 212
Android Question

Android: Toolbar in multiple activities

I am trying to create a toolbar in my activities. However, it doesn't work, there is nothing in the action bar. I tried to make my activities extend from BaseActivity, which has toolbar configuration.

I think that there is problem with


onCreateOptionsMenu(Menu menu)


BaseActivity.java

public abstract class BaseActivity extends AppCompatActivity {
private Toolbar toolbar;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
configureToolbar();
}
protected abstract int getLayoutResource();

private void configureToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

}
// Menu icons are inflated just as they were with actionbar
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
//Handle an option being selected on the menu
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.selPg:
SelectionPage();
return true;
case R.id.choosePg:
ChoosingPage();
return true;
case R.id.addPg:
AddingPage();
return true;
default:
return super.onOptionsItemSelected(item);
}
//....//
}


ChildActivity.java

public class form_activity extends BaseActivity {
@Override
public int getLayoutResource() {
return R.layout.add_form;
}


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


Child.xml

<!-- Load the toolbar here -->
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>


main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/selPg"
android:icon="@android:drawable/ic_menu_help"
android:title="Selecton Page"/>
<item android:id="@+id/choosePg"
android:icon="@android:drawable/ic_menu_directions"
android:title="Choosing Page"/>
<item android:id="@+id/addPg"
android:icon="@android:drawable/ic_menu_add"
android:title="Adding Page"/>
</menu>


tool_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="@android:color/white"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>


styles.xml

<style name="AppCompatTheme" parent="@style/Theme.AppCompat.NoActionBar">


manifest

android:theme="@style/AppCompatTheme">

Answer Source

You set setContentView twice (in BaseActivity and in ChildActivity).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download