0

I am trying to remove ActionBar on my LoginActivity below. I have implemented code from this example code but I do not want the ActionBar to show. I have not altered the code given but I really cannot find how to remove ActionBar(If possible, if not just hide it).

public class LoginActivity extends AppCompatActivity {

// Declaring View and Variables
ViewPager pager;
JoinLoginAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[]={"Sign In","Register"};
int Numboftabs = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getSupportActionBar().hide();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.join_login);

    // Creating The ViewPagerAdapter and Passing Fragment Manager, Titles fot the Tabs and Number Of Tabs.
    adapter =  new JoinLoginAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    // Assigning ViewPager View and setting the adapter
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    // Assiging the Sliding Tab Layout View
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); // To make the Tabs Fixed set this true, This makes the tabs Space Evenly in Available width

    // Setting Custom Color for the Scroll bar indicator of the Tab View
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.tabsScrollColor);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);

}

}

I have tried appying NoTheme style to activity with but this gives me the following Exception:

06-28 01:43:45.615  11537-11537/com.nauv.jambomall E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nauv.jambomall/com.nauv.jambomall.ui.activity.JoinLoginActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
        at android.app.ActivityThread.access$700(ActivityThread.java:159)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5414)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
        at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:320)
        at com.nauv.jambomall.ui.activity.JoinLoginActivity.onCreate(JoinLoginActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:5369)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5414) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:525) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Zack
  • 1,527
  • 2
  • 20
  • 32
  • 2
    I feel a duplicate coming. – Jared Burrows Jun 27 '15 at 18:09
  • possible duplicate of [Remove android default action bar](http://stackoverflow.com/questions/13370719/remove-android-default-action-bar) – Jared Burrows Jun 27 '15 at 18:09
  • 2
    This is not a duplicate, the question is the same but the problem is not? I have already SAID I implemented NoAction both Dynamically and on my xml but I get an error. – Zack Jun 27 '15 at 18:12
  • @JaredBurrows As I have said, The problem is not that I do not know how to hide actionbar, the problem is that when I implement this methods as I have stated, I get an exception. Help me solve that instead of just reading the question title and thinking its a duplicate. – Zack Jun 27 '15 at 18:34

5 Answers5

2

put this in your styles.xml

<item name="android:windowActionBar">false</item>

and Extend the Activity class instead of AppCompatActivity

Tchinmai
  • 620
  • 1
  • 6
  • 20
1

The reason why you are getting AndroidUtilRunTime Exception is you shouldn't be calling requestFeature() after super.onCreate(savedInstanceState). The proper way to do is

protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
}

Other ways to remove ActionBar

ActionBar actionBar = getActionBar();
actionBar.hide();

If you are using android.support.v7.app.ActionBar, then

ActionBar actionBar = getSupportActionBar();
actionBar.hide();

Another way of doing this is, in your styles.xml

<item name="android:windowActionBar">false</item>
capt.swag
  • 10,335
  • 2
  • 41
  • 41
1

Don't extend the AppCompatActivity. Also go in your xml layout file and remove the Actionbar/Toolbar

adao7000
  • 3,632
  • 2
  • 28
  • 37
  • I cannot declare my adapter `adapter = new JoinLoginAdapter(getSupportFragmentManager(),Titles,Numboftabs);` whenever I tried this before by extending `Activity` – Zack Jun 27 '15 at 18:14
  • I ended up extending `FragmentActivity` Although I knew about this solution , this is the only answer that solved my problem. Before I was making a mistake of extending Activity hence I was receiving more errors. Thanks. – Zack Jun 27 '15 at 18:55
1

Removing the Action Bar

You can hide the action bar at runtime by calling hide().
For example:

  ActionBar actionBar = getActionBar();  
  actionBar.hide();

On API level 11 or higher
Get the ActionBar with the getActionBar() method.

  • I already implemented this before. The problem is not that I do not know how to hide actionbar, the problem is that when I implement this methods as I have stated, I get an exception. – Zack Jun 27 '15 at 18:33
1

Instead of:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    getSupportActionBar().hide();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.join_login);

Try something like this:

getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.join_login);
Anand Singh
  • 5,672
  • 2
  • 23
  • 33