Reputation: 631
I'm using ActionBarCompat that is attached to 1 underlying activity. And there are a number of fragments that can be navigated with the navigation drawer.
And each fragment has a respective title set programatically.
Apps like the official Gmail app have a clean launcher activity, i.e the very first screen that you see for a brief moment has no icon, no title in the action bar. How can I achieve the same?
After the app is loaded with the following fragments, the ActionBar has been styled appropriately with the respective titles and icons programatically, as mentioned earlier.
I've tried the following code by putting it into the style.xml and calling it from the manifest. But this disabled Action Bar throughout the app, which I don't want.:
<style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat">
<item name="android:windowNoTitle">true</item>
</style>
Upvotes: 0
Views: 1369
Reputation: 27246
I think you could create your own ActionBar Layout like this:
In your BaseActivity (you have one, right?) do something like this:
public class BaseActivity extends ActionBarActivity {
private TextView mActionBarTextView;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final android.support.v7.app.ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
LayoutInflater inflator = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View actionBarView = inflator.inflate(R.layout.ab_custom_view, null);
mActionBarTextView = (TextView) actionBarView.findViewById(R.id.title);
mActionBarTextView.requestFocus();
this.getSupportActionBar().setCustomView(actionBarView);
setTitle(Constants.EMPTY_STRING);
}
}
ab_custom_view.xml
would look like (set your own TextView preferences, this is just an untested example):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:textColor="@color/white"
android:textSize="18sp"
android:maxLines="1"
android:ellipsize="marquee"
android:focusable="true"
android:singleLine="true"
android:focusableInTouchMode="true" />
</RelativeLayout>
Back in your BaseActivity
, add the folowing convenience methods:
@Override
public void setTitle(final CharSequence title) {
if (mActionBarTextView != null) {
if (title != null) {
mActionBarTextView.setText(title);
} else {
mActionBarTextView.setText(null);
}
requestTitleFocus();
}
}
@Override
public void setTitle(final int titleId) {
if (mActionBarTextView != null) {
if (titleId > 0) {
mActionBarTextView.setText(getString(titleId));
} else {
mActionBarTextView.setText(getString(R.string.some_default_string);
}
requestTitleFocus();
}
}
public void requestTitleFocus() {
if (mActionBarTextView != null) {
mActionBarTextView.requestFocus();
}
}
Finally, in your activities (that inherit from BaseActivity), if you need to set a different than empty title:
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Your Title");
}
(I assume you store your mostly used Strings in a static final field for convenience and efficiency…)
public final class Constants {
public static final String EMPTY_STRING = "";
}
This should do the trick.
The perk you gain is that you could use a custom Font in the ActionBar (to match your App's font if you were using a custom one) because you have full control (and a reference) to your ActionBar's textview. ;)
Upvotes: 1
Reputation: 998
Try using style for your activity in manifest file. Remember, in your onCreate() method you must call setStyle() or requestWindowFeature() before setContentView() when you want your action bar or app style back.
<style name="MyApp.Fullscreen" parent="android:Theme.Black.NoTitleBar">
<item name="android:windowBackground">@color/app_background</item>
</style>
Upvotes: 0