KaiZ KaiZ - 1 month ago 8
Android Question

Why is my toolbar not showing anything, even though it appears and is placed correctly?

I'm trying to have a

Toolbar
on my
SettingsActivity
, and it appears correctly, but for some reason I can't get it to fill it's content at all. It just appears in a blank state. Here is my code:

SettingsActivity.java:

public class SettingsActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_settings, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
}

return super.onOptionsItemSelected(item);
}

public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View rootview = inflater.inflate(R.layout.activity_settings, container, false);
return rootview;
}
}
}


activity_settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="pt.ismai.a26800.readr.activities.SettingsActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_settings" />

</LinearLayout>

</android.support.design.widget.CoordinatorLayout>


content_settings.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/settings"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>

</LinearLayout>


menu_settings.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="pt.ismai.a26800.readr.activities.SettingsActivity">
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />
</menu>


AndroidManifest.xml:

<activity
android:name=".activities.SettingsActivity"
android:label="@string/action_settings"
android:theme="@style/AppTheme.NoActionBar" />

Answer

This is due to the order of operations. You are currently setting the toolbar and then loading up the fragment with the toolbar inside. This means when you call setSupportActionBar(toolbar); toolbar will currently be null.

You could potentially solve this by setting the toolbar from within SettingsFragment by calling:

final View rootview = inflater.inflate(R.layout.activity_settings, container, false);
Toolbar toolbar = (Toolbar) rootview.findViewById(R.id.toolbar);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

This gets the fragments parent activity and sets the toolbar accordingly.