Cannot hide Action bar in Android - java

I know several people have asked this, but the solutions did not seem to work for me. I am trying to hide the action bar. My code:
values/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
</resources>
Here is my v21/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
</resources>
And my MainActivity.java
package practice.interactivestory;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().hide();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#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_main, 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) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Using getsupport.actionbar.hide() has not worked, and AppCompat.Light.NoActionBar has not worked as well.

You can use Activity instead of AppCompatActivity for your MainActivity. Then in values/styles.xml set the AppTheme's parent to "#android:style/Theme.Holo.Light.DarkActionBar". Leave the AppTheme.NoActionBar as you described above.
Then make sure in your AndroidManifest.xml, under <application> you set android:theme="#style/AppTheme.NoActionBar".

Related

Remove Toolbar title margin in ActionMode

I'm looking to remove the space between the navigation icon and the title of my actionMode. I managed to remove it for the toolbar thanks to:
<style name="AppTheme" parent="Theme.MaterialComponents.Light">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="toolbarStyle">#style/cusToolbarStyle</item>
</style>
<style name="cusToolbarStyle" parent="#style/Widget.MaterialComponents.Toolbar">
<item name="titleMargin">0dp</item>
<item name="contentInsetStartWithNavigation">0dp</item>
<item name="contentInsetStart">0dp</item>
<item name="android:background">#color/white</item>
</style>
However I can not change the mode action to remove the space on the photo:
Ok, it was complex and difficult but I used LayoutInspector such as suggested to me #cgb_pandey. So I found the system identifier of the close ImageView and add the below code to the onPrepareActionMode method:
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
AppCompatImageView imageView = findViewById(com.google.android.material.R.id.action_mode_close_button);
if (imageView != null) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
imageView.setLayoutParams(params);
}
return false;
}

Hide system navigation bar when show DialogFragment

My app is in sticky immersive mode that the system navigation bar (I call it SNB) is hidden in running time of my app. It works well but exists one problem, it is each time I show a DialogFragment, the SNB will be appear. How can I hide it.
I have written a few line of code in my acivity class to make my app become fullscreen and sticky immersive mode.
MyActivity.java
#Override
protected void onResume() {
super.onResume();
hideSystemUI();
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
hideSystemUI();
}
public void hideSystemUI() {
// Enables regular immersive sticky mode.
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
);
}
MyDialog.java
public class MyDialog extends DialogFragment{
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog alertD = builder.create();
alertD.setView(view);
return alertD;
}
public void show() {
Activity activity = getActivity();
show(activity.getSupportFragmentManager(), "dialog");
}
#Override
public void onResume() {
super.onResume();
ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
getDialog().getWindow().setAttributes((WindowManager.LayoutParams) params);
}
}
I followed the answer to show dialog without show SNB: https://stackoverflow.com/a/33589793/10467639 but it does not work as desired
MyDialog dialog = new MyDialog();
// Set the dialog to not focusable.
getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
dialog.show();
// Set the dialog to focusable again.
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
style.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/themeColor</item>
<item name="colorPrimaryDark">#color/themeColor</item>
<item name="colorAccent">#color/themeColor</item>
<item name="colorControlNormal">#color/black</item>
<item name="colorControlActivated">#color/black</item>
<item name="colorControlHighlight">#color/white</item>
</style>
<style name="BoldGrayHeaderTextView">
<item name="android:textSize">12dp</item>
<item name="android:fontFamily">#font/nunitosans_bold</item>
<item name="android:textColor">#color/darkColor</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<!-- Splash Screen theme. -->
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/splash_background</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<!-- For CarPinItem on Map -->
<style name="carPinItemUnSelected">
<item name="android:textSize">12dp</item>
<item name="android:fontFamily">#font/nunitosans_regular</item>
<item name="android:textColor">#color/generalTextColor</item>
</style>
<style name="carPinItemSelected">
<item name="android:textSize">12dp</item>
<item name="android:fontFamily">#font/nunitosans_regular</item>
<item name="android:textColor">#color/white</item>
</style>
<!-- Style for drawer navigation item -->
<style name="NavigationDrawerStyle">
<item name="android:textSize">15dp</item><!-- text size in menu-->
<item name="android:listPreferredItemHeightSmall">40dp</item><!-- item size in menu-->
<item name="listPreferredItemHeightSmall">40dp</item><!-- item size in menu-->
</style>
<!--Style for textInputEditText -->
<style name="TextInputLayoutStyle">
<item name="android:fontFamily">#font/nunitosans_regular</item>
<item name="android:textSize">13dp</item>
<item name="android:textColor">#color/white</item>
</style>
<!--Style for profile bottom menu -->
<style name="BottomTextLayoutStyle">
<item name="android:fontFamily">#font/nunitosans_regular</item>
<item name="android:textColor">#color/black</item>
</style>
<style name="MyDatePickerStyle" parent="#android:style/Widget.Material.Light.DatePicker">
<item name="android:textColorPrimaryInverse">#color/black</item>
<item name="android:colorControlActivated">#color/successColor</item>
</style>
<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#color/white</item>
<item name="colorControlActivated">#color/white</item>
<item name="colorControlHighlight">#color/white</item>
</style>
<!--BottomNavivagionView textsize-->
<style name="Widget.BottomNavigationView"
parent="Widget.Design.BottomNavigationView">
<item name="android:fontFamily">#font/nunitosans_regular</item>
</style>
<style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowNoTitle">true</item>
</style>
</resources>
How can I solve this problem? Thank in advanced!
Followed this answer https://stackoverflow.com/a/23207365/8531215 ,I have tested and it works fine! modify onCreateDialog() method a little bit
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog alertD = builder.create();
alertD.setView(view);
Dialog dialog = alertD.create();
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
//Show the dialog!
dialog.show();
//Set the dialog to immersive sticky mode
dialog.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
//Clear the not focusable flag from the window
dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
return alertD;
}
According to the docs here:
Implement onWindowFocusChanged(). If you gain window focus, you may want to re-hide the system bars. If you lose window focus, for example due to a dialog or pop up menu showing above your app, you'll probably want to cancel any pending "hide" operations you previously scheduled with Handler.postDelayed() or something similar.
So What I suggest you do is, change your code in MyActivity.java from:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
hideSystemUI();
}
to:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus){
//we lost focus due to DialogFragment
hideSystemUI();
}
}

Contextual Action Bar (CAB) does not overlay Toolbar

I am really stuck with this.
I want to use a CAB in a fragment, so I'm using
actionMode = ((MainActivity)getActivity()).startActionMode(actionModeCallbacks);
to call startActionMode
I've been changing themes and adding and testing several lines of code (together and separated) to xml styles like:
<item name="android:windowActionModeOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
Relevant information:
I have my toolbar defined inside and <include/>in my main activity.
Toolbar is included just before the frame of MainActivity, between that frame and the DrawerLayout, so I can use the toolbar in the fragments.
I have tested the CAB inside Main Activity, but it also gets above
the Toolbar.
I also tried a dirty workoaround toggling Toolbar visibility when CAB
is created/destroyed, but it also doesn't work!
I'm using android.view.ActionMode instead of
android.support.v7.view.ActionMode because I can't call
startActionMode if I use Support Library (?).
My toolbar (*android.support.v7.widget.Toolbar):
toolbar = findViewById(R.id.toolbar);
mDrawerLayout = findViewById(R.id.drawer_layout);
toolbar.setTitle(R.string.addWordLabel_Act);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
XML (toolbar)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FAFAFA"
android:theme="#style/AppTheme.NoActionBar"
app:collapseIcon="#drawable/ic_arrow_back_white_24dp"
app:contentInsetStartWithNavigation="0dp"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:title="#string/addWordLabel_Act"
app:titleTextColor="#color/colorAccent"
/>
</android.support.constraint.ConstraintLayout>
ActionMode.Callback
private ActionMode actionMode;
private ActionMode.Callback actionModeCallbacks = new ActionMode.Callback()
{
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.action_mode_menu, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.share_actionMode:
shareWord();
mode.finish();
return true;
case R.id.deleteWords_actionMode:
deleteWords();
mode.finish();
return true;
default:
return false;
}
}
#Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
}
};
XML (CAB)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Share"
android:icon="#drawable/ic_share_white_24dp"
android:id="#+id/share_actionMode"/>
<item android:id="#+id/deleteWords_actionMode"
android:title="Delete"
android:icon="#drawable/ic_round_delete_outline_white_24px"/>
</menu>
And styles...
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/lightAccent</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="android:fontFamily">#font/sans_serif_roboto</item>
<item name="alertDialogTheme">#style/AlertDialogStyle</item>
<item name="android:windowActionModeOverlay">true</item>
</style>
<style name="AppTheme.NoActionBar"
parent="Theme.MaterialComponents.NoActionBar">
</style>
I don't know what else I can do.
Solved
I completely forgot to check Manifest, and that was the problem!
The AppTheme was set to "Theme.AppCompat.Light.NoActionBar" instead of "AppTheme". Thus, any change I made in AppTheme didn't take effect on the App.
I completely forgot to check Manifest, and that was the problem! The AppTheme was set to "Theme.AppCompat.Light.NoActionBar" instead of "AppTheme". Thus, any change I made in AppTheme didn't take effect on the App.

Change style programmatically for api less than 23

I want to change the button's style using setTextAppearance but, throw "depreciated".how to use setTextAppearance correctly?
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="button_custom" parent="Theme.AppCompat.Light">
<item name="colorControlHighlight">#FFF59D</item>
<item name="colorButtonNormal">#FFF59D</item>
</style>
</resources>
And the activity
public class MainActivity extends AppCompatActivity {
Button btn;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
context = this;
btn.setTextAppearance(context,R.style.button_custom);
}
public void setTextAppearance(Context context, int resId) {
if (Build.VERSION.SDK_INT < 23) {
super.setTextAppearance(context, resId);
}
else { super.setTextAppearance(resId);
}
}
}
You can try making your button of AppCompatButton type and use
btn.setTextAppearance(resid)
It will also helps you avoid the Build.VERSION.SDK_INT branching
You can follow this documentation https://developer.android.com/reference/android/support/v7/widget/AppCompatButton.html#setTextAppearance(android.content.Context, int)
User AppCompatButton
<android.support.v7.widget.AppCompatButton
android:id="#+id/some_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

Changing theme causes double action bar glitch

So in my app I created a way for the user to set the theme as dark or light. I implemented it into each activity, but now if the theme is dark, the activity has 2 action bars. If I leave it as the light theme, only one action bar is show. This does not make any sense to me. All help is much appreciated. Thanks.
Here is the pic of the activity:
Here is the code for the MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
settings = getSharedPreferences(PREFS_NAME, 0);
checkDark(settings.getBoolean("darkMode", false));
super.onCreate(savedInstanceState);
managerHelper = new ManagerDatabaseAdapter(this);
setContentView(R.layout.activity_main);
adapter = new RVAdapter(this, getData());
RV = (RecyclerView) findViewById(R.id.mainV);
layoutManager = new LinearLayoutManager(this);
RV.setLayoutManager(layoutManager);
RV.setAdapter(adapter);
adapter.setClickListener(this);
}
#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_main, menu);
// getMenuInflater().inflate(R.menu.menu_main, li)
return true;
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && exitD) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Confirm exit")
.setPositiveButton("EXIT", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
return super.onKeyDown(keyCode, event);
}
#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
switch (id) {
case R.id.action_settings:
Intent intent = new Intent(this, SettingsActivity.class);
intent.putExtra(EXTRA_MESSAGE, true);
startActivity(intent);
overridePendingTransition(R.anim.abc_popup_enter, R.anim.abc_fade_out);
return true;
case R.id.action_surveyList:
Intent survey = new Intent(this, GetSurvey.class);
survey.putExtra(EXTRA_MESSAGE, true);
startActivity(survey);
overridePendingTransition(R.anim.abc_popup_enter, R.anim.abc_fade_out);
return true;
}
return super.onOptionsItemSelected(item);
}
private void checkDark(boolean isChecked) {
if (isChecked)
setTheme(R.style.Dark);
else
setTheme(R.style.AppTheme);
}
}
Here is the styles.xml:
<resources>>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#E53935</item>
<item name="colorPrimaryDark">#C62828</item>
<item name="android:navigationBarColor">#E53935</item>
<item name="android:colorAccent">#1565C0</item>
<item name="android:colorForeground">#color/foreground_material_light</item>
<item name="android:colorBackground">#color/background_material_light</item>
<!-- Other attributes -->
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:navigationBarColor">#E53935</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#color/primary_dark_material_light</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
</style>
<style name="Dark" parent="#android:style/Theme.Material" >
<item name="android:colorForeground">#color/foreground_material_dark</item>
<item name="android:colorPrimary">#color/primary_material_dark</item>
<item name="android:colorPrimaryDark">#color/primary_dark_material_dark</item>
<item name="android:colorAccent">#color/accent_material_dark</item>
<item name="android:navigationBarColor">#color/primary_material_dark</item>
</style>
<style name="AppTheme.NoActionBar.Dark" parent="#style/AppTheme.NoActionBar" >
<item name="colorPrimary">#E53935</item>
<item name="colorPrimaryDark">#C62828</item>
<item name="android:navigationBarColor">#E53935</item>
<item name="android:colorAccent">#1565C0</item>
<item name="android:colorBackground">#color/background_material_dark</item>
<item name="android:textColorPrimary">#android:color/primary_text_dark</item>
<item name="android:textColorPrimaryInverse">#android:color/primary_text_light</item>
<item name="android:textColorSecondary">#android:color/secondary_text_dark</item>
<item name="android:textColorSecondaryInverse">#android:color/secondary_text_light</item>
<item name="android:statusBarColor">#color/primary_dark_material_dark</item>
</style>
</resources>
Not sure if this is the cause of the problem, but just something to note. Your AppTheme's parent is from AppCompat but your dark theme is using the built-in material theme.
Change the parent style to Theme.Material.NoActionBar:
Your custom style:
<style name="Dark" parent="#android:style/Theme.Material.NoActionBar" >
...
</style>

Categories