How to remove Titlebar? - android

I am trying to remove title bar from my activity but it is not getting removed.
Here is my code
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash_screen);
}
}
With or without the requestWindow line the output is the same then what is the use of that line?

I would reccomend editing your theme with no title bar. For example use of of these...
<style name="Theme.NoTitle" parent="#android:style/Theme.NoTitleBar"></style>
<style name="Theme.FullScreen"
parent="#android:style/Theme.NoTitleBar.Fullscreen"></style>

Apply this theme in your activity declared in the Manifest File.
<activity
android:name=".SplashScreenActivity"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
</activity>

Can you try adding this to your manifest inside activity tag :
<activity
android:name="your_app_packagename.activity_name"
android:theme="#android:style/Theme.NoTitleBar" >
also try this in your activity :
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);

Add this in onCreate
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);

Related

Shwing Error : Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)

I want to implement the Android ActionBar back button in my StreamActivity but I'm getting this error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.schoolteacher/com.example.schoolteacher.StreamActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
I don't know why this is not working, despite I followed all instructions from the documentation! plus, I did exactly this in other projects and it works.
Here is my code :
StreamActvivty.java
public class StreamActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stream);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar= getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
styles.xml
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
manifest
<activity android:name=".StreamActivity"
android:label="#string/stream"
android:parentActivityName=".ClassActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ClassActivity" />
</activity>
Try this
Add this in onCreate
ActionBar actionBar = this.getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
Add this outside of the onCreate, this is used to navigate back to the parent activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
NavUtils.navigateUpFromSameTask(this);
}
return super.onOptionsItemSelected(item);
}
And also set Parent activity
<activity
android:name=".activity.CreateAccountActivity"
android:parentActivityName=".activity.LoginActivity" />
To use Toolbar as an ActionBar, first ensure the AndroidX support library is added to your application build.gradle (Module:app) file:
dependencies {
...
implementation 'androidx.appcompat:appcompat:1.0.0'
}
You have to disabled the theme provided for ActionBar. Change your theme from styles.xml as:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
Is there toolbar inside your activity_stream.xml?
Also check your imports.
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;

Android Window title showing after requested not to show

public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, BaseActivity.class);
startActivity(i);
}
and my next activity:
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_base);
}
I still see blue bar with application name at top.
I tried changing my application manifest to use a no-title theme and the program crashed because the activities inherit from AppCompatActivity.
How do I inherit from AppCompatActivity and get rid of the application title name up top?
It's the actionbar, you can use the theme Theme.AppCompat.NoActionBar or Theme.AppCompat.Light.NoActionBar or a theme which descents from them.
To make disappear is the action bar also this should work:
getSupportActionBar().hide();
Try this...
If you don't want the action bar in your activity then,
Create a style
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and apply this style for the activity in Manifest for which you don't need action bar,
<activity android:name=".BaseActivity"
android:theme="#style/AppTheme.NoActionBar"></activity>
Do this in your onCreate() method.
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here);
this refers to the Activity
OR
You can modify your AndroidManifest.xml:
<activity android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen">
You can use this for each activity in your project.
OR
also change from style.xml
<style name="generalnotitle" parent="general">
<item name="android:windowNoTitle">true</item>
</style>
Hope this helps you.

How to create a single activity without headers

I'm developing an application where I set up an activity for the settings.
I tried to use the android studio template to create an activity setting but the structure of the code that is being created is too complex.
Can anyone tell me how to create a settingsActivity similar to the android studio template but without headers?
Thanks in advance for the answer.
Greetings.
To create a single settingActivity you can try the below pseudo code:
public class SettingsUI extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new SettingsFragment()).commit();
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
PreferenceManager.setDefaultValues(this, R.xml.settings_preference, false);
}
public static class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_preference);
}
}
}
Then settings_preference.xml code:
PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:key="sample_key"
android:title="#string/sample_title"
android:inputType="number"
android:summary="#string/sample_summary"/>
<EditTextPreference
android:key="sample2_key"
android:title="#string/sample2_title"
android:inputType="number"
android:summary="#string/sample2_summary"/>
</PreferenceScreen>
In the styles.xml file, please make one style tag if you want to remove actionbar(you said header) in only specific activity.
<style name="AppTheme1" parent="Theme.AppCompat.Light.NoActionBar" />
And goto AndroidManifest file, please apply above style to your activity something like this.
<activity android:name=".SettingActivity" android:theme="#style/AppTheme1"></activity>
if you want to remove actionbar in all of your activities, then you can change AppTheme style in the styles.xml file like below.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/mainColor</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
... ...

Unable to hide deafault action bar [ANDROID]

I am creating an android app, i am trying to hide default action bar and us my own custom toolbar as action bar but the problem is that default navigation icon and title of action bar are not being hidden and when i open navigation drawer from right side my custom navigation icon from left side hides and android's default navigation icon is displayed instead.
i have tried using NoActionBar and NoTheme styles too but its not working.
Here is a sample of my code, where i am going wrong
Any help will be appreciated.
public class MyActivity extends AppCompatActivity
{
Toolbar main_toolbar;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
setUpToolBar();
}
public void setUpToolBar()
{
main_toolbar=(Toolbar)findViewById(R.id.toolbar)
//set up the home_main_toolbar
android.app.ActionBar actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
setSupportActionBar(main_toolbar);
if (activity.getSupportActionBar() != null)
main_toolbar.setNavigationIcon(R.drawable.img_toolbar);
main_toolbar.setNavigationOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view) {
//Open navigation drawer
}
});
}
}
<activity android:name=".MainActivity" android:screenOrientation="portrait" android:theme="#style/AppTheme.NoActionBar" />
use this code in your android menifest , surely it will work
try it in manifest where your activity is defined.
android:theme="#style/AppTheme.NoActionBar"
Go to Styles.xml
add this style there
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
and use it in manifest
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />

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>
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 :)
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();
}
Daniel Wilson's comment above does work but just a minor difference in how I did it.
I did not define style at all, all I did in AndroidManifext.xml is that I added this to my activity:
android:theme="#style/Theme.AppCompat.NoActionBar"
So, I did not have to redefine style for NoActionBar, I just added it from Theme.AppCompat
I'm using this code in Activity.onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hiding the title bar has to happen before the view is created
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// ...
}

Categories

Resources