Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Action Bar in a Fragment Activity

I am creating the framework for an app and wish to hide the action bar for the login activity. The app is going to be entirely composed of fragments because they are awesome.

I can hide the action bar fine for my activity, but every time I run the app I see my inflated blank fragment and default action bar for about 2 seconds before it manages to inflate the login fragment.

Any idea on how / where I should hide the action bar, (whilst keeping my framework) to stop this from happening?

LoginActivity.java

public class LoginActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment() {
    return new LoginFragment();
}

}

SingleFragmentActivity.java

public abstract class SingleFragmentActivity extends ActionBarActivity {
protected abstract Fragment createFragment();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if(this instanceof LoginActivity){
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }
    setContentView(R.layout.activity_fragment);

    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

    if (fragment == null) {
        fragment = createFragment();
        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragment)
                .commit();
    }
}

}

LoginFragment.java

public class LoginFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_login, parent, false);

    return v;
}

}

I originally had SingleFragmentActivity extend FragmentActivity and did this in the if statement:

    requestWindowFeature(Window.FEATURE_NO_TITLE);

But I've got a feeling that's just a different way of doing the same thing

Edit:

I have also tried setting the fragment_activity to use a different style but it doesn't seem to work either:

    <style name="NoActionBar" parent="Widget.AppCompat.ActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
</style>
like image 398
Daniel Wilson Avatar asked Nov 22 '13 01:11

Daniel Wilson


People also ask

How do I hide action bar in one activity?

If we want to remove the ActionBar only from specific activities, we can create a child theme with the AppTheme as it's parent, set windowActionBar to false and windowNoTitle to true and then apply this theme on an activity level by using the android:theme attribute in the AndroidManifest. xml file.

How to Hide Action Bar in fragment android studio?

If you are using AppCompatActivity (you should) then this is the solution that worked for me: ((AppCompatActivity) getActivity()). getSupportActionBar(). hide();

How to Hide Action Bar?

If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme.

How do we hide the menu on the toolbar in one fragment?

How do we hide the menu on toolbar in one fragment? When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment.

How to hide Actionbar from any particular activity using Java code?

If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles.xml and change the base application to “ Theme.AppCompat.Light.NoActionBar “. 2. Hide ActionBar from any particular activity using Java code

How do fragments interact with the app bar?

When the app bar is owned by an activity, fragments can interact with the app bar by overriding framework methods that are called during fragment creation. Note: This guidance applies only when the app bar is owned by the activity. If your app bar is a toolbar that's included in a fragment layout, see Fragment-owned app bar.

Can I add a toolbar to a fragment layout?

If most screens in your app don't need an app bar, or if perhaps one screen needs a dramatically-different app bar, you can add a Toolbar to your fragment layout. Though you can add a Toolbar anywhere within your fragment's view hierarchy, you should generally keep it at the top of the screen.

How to hide or show menu items depending on showing fragments?

When your app consists of many modules and each module contains several fragments, you will need to hide or show menu items depending on the showing fragments. Each module’s menu items should be put in a group for easy access. All groups’ visible states are set to false by default.


2 Answers

Okay after a few hours I think I have found the solution to keeping fragments in place for everything and removing the action bar (for all devices) for the launch activity only:

Android Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.app"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">
    <activity
        android:name="com.company.app.LoginActivity"
        android:theme="@style/NoActionBar"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Where the style is defined as like so:

   <style name="NoActionBar" parent="Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
</style>

That took a while to figure out because there is no Theme.AppCompat.Light.NoActionBar , the action bar is basically known as the title bar in older APIs, and when I applied that style to the activity itself (which I thought was the only way of using dynamic themes) it doesn't seem to work at all.

So hopefully this simple solution will keep the action bar for any other activities and it may help someone in the future :)

like image 135
Daniel Wilson Avatar answered Oct 09 '22 11:10

Daniel Wilson


try this:

if(this instanceof LoginActivity) {
    if (getActionBar().isShowing()) getActionBar().hide();
} else {
    if (getActionBar().isShowing()) getActionBar().hide();
}

or

if(this instanceof LoginActivity){
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
} else {
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
}
like image 21
The Badak Avatar answered Oct 09 '22 11:10

The Badak