Android - hide status and navigation bar completely for an app with nav drawer and app bar - android-layout

How can I dynamically hide the status and the navigation bar completely?
The app contains a regular navigation drawer with a appbar / toolbar and FAB buttons.
When switching to full screen, the content of the navigation and the status bar is scrolled away. Two empty bars are left on the screen. I want those empty bars to hide.
I created a minimal demo app. On the left is the regular app. When pushing on the fab, the app should be shown fullscreen.
How can I get the bars to hide?
QUESTION: Please write which change(s) are needed in the minimal demo project?
Updated with a second solution:
The GREAT solution provided by #Roaim works. Essential was to set the android:fitsSystemWindows layout property to false.
If you still have trouble with the showing and hiding of status/navigation bars, this solutin may help you.
Hide the bars completely:
public static void hideSystemUI() {
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
And show all bars:
public static void showSystemUI() {
if (getSupportActionBar() != null) {
getSupportActionBar().show();
}
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}

Update
The issue was with your layout file. I just set android:fitsSystemWindows=false to fix the issue. I made a pull request to your repo, which I think solves your issue.
You should follow the following official documentations:
Hide the status bar
Hide the navigation bar
Hide the Status Bar on Android 4.0 and Lower
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// If the Android version is lower than Jellybean, use this call to hide
// the status bar.
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(R.layout.activity_main);
}
...
}
Hide the Status Bar on Android 4.1 and Higher
View decorView = getWindow().getDecorView();
// Hide the status bar.
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
ActionBar actionBar = getActionBar();
actionBar.hide();
Hide the Navigation Bar
View decorView = getWindow().getDecorView();
// Hide both the navigation bar and the status bar.
// SYSTEM_UI_FLAG_FULLSCREEN is only available on Android 4.1 and higher, but as
// a general rule, you should design your app to hide the status bar whenever you
// hide the navigation bar.
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

Go in values>styles
Now In App theme use
To make navigation bar transculate
<item name="android:windowTranslucentNavigation">true</item>
To remove it make its color matched with your layout
<item name="android:navigationBarColor">#color/white</item>
OR Try this
public void FullScreencall() {
if(Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if(Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
decorView.setSystemUiVisibility(uiOptions);
}
}

Try this...
<style name="MyApp" parent="Theme.MaterialComponents.Light.NoActionBar">
...
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
...
</style>

Try this to hide/show statusbar and navbar, both are parts of SystemUI.
fun setSystemUIVisibility(hide: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
val window = window.insetsController!!
val windows = WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars()
if (hide) window.hide(windows) else window.show(windows)
// needed for hide, doesn't do anything in show
window.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
} else {
val view = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_FULLSCREEN or
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
window.decorView.systemUiVisibility = if (hide) view else view.inv()
}
}

Theme
<style name="Theme.FullScreen" parent="#style/Theme.AppCompat.Light.NoActionBar">
<item name="android:windowFullscreen">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:navigationBarColor">#android:color/transparent</item>
<item name="android:windowLayoutInDisplayCutoutMode" tools:targetApi="o_mr1">shortEdges</item>
</style>
AndroidManifest
<activity android:exported="false"
android:name=".FullScreenActivity"
android:screenOrientation="fullSensor"
android:theme="#style/Theme.FullScreen"/>

Related

Create Android splash screen using SVG animation

I am developing an app using Xamarin.Forms and I am trying to insert the splash screen to my Android project.
I found a few tutorials for creating a splash screen with a background color and a static png image, but I want to use my svg animation as splash screen. I thought I could follow a tutorial for static image and just replace the png image with the svg animation, but it didn't work. Here's what I have so far:
On SplashActivity.cs:
[Activity(Label = "SplashActivity", Theme = "#style/Theme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
}
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
}
async void SimulateStartup()
{
await Task.Delay(5000);
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
On MainActivity.cs:
// I only changed the MainLauncher property to false
[Activity(Label = "MyApp", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize )]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
...
}
On styles.xml (in the Xamarin.Android project):
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">#drawable/desenhando5s</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="colorPrimaryDark">#004632</item>
</style>
When I run the application, it only shows a black screen as splash screen and then shows my login page as always.
Can anybody tell me what I have to do to set my animation as the splash screen?
(FYI: in case anyone wants to know, I created the animation using SVGator)
You could use FFImageLoading to load your svg image in your SplashActivity instead of set it in styles.xml.
Splash screen:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FFImageLoading.Views.ImageViewAsync
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Code:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.layout5);
var filePath = "check";
var imageView = FindViewById<ImageView>(Resource.Id.imageView);
ImageService.Instance.LoadCompiledResource(filePath).WithCustomDataResolver(new SvgDataResolver(64, 0, true)).Into(imageView);
}
protected override void OnResume()
{
base.OnResume();
Task startupWork = new Task(() => { SimulateStartup(); });
startupWork.Start();
}
async void SimulateStartup()
{
await Task.Delay(5000);
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
Updated:
Please check the screenshot. The .svg image is in the drawable folder. The layout5 is the splash screen in the layout folder.

How to change the font size of content when radio button clicks?

I have a WebView and I am loading the data from the backend.The data is very long paragraph and it is in Html format.It is working.I have to implement change font size settings to my application.When radio button named "small" clicks,text should appear in small font,when "large" clicks text should appear in large font.The content dispaly is done in ChapterDetailFragment.class.The dialg which shows the "change font option" is in Menu in actionbar and in drawer.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.font_change) {
dialog = new Dialog(HomeActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.font_change_dialog);
dialog.show();
dialog.setCanceledOnTouchOutside(true);
//here i have to write the code for clicks of radiobutton "small","large"
}
}
This is the line for loading paragraph to the array adapter
ParagraphAdapter.class
mViewHolder.mChapterContextTextView.loadData(getItem(position - 1).getContent(), "text/html; charset=UTF-8;", null);
I don't know how to implement this.Please anyone help me
in onClick of "large" radio button, save value LARGE for large.Similarly in onClick of small radio button(value SMALL for small )
public void onTextLarge(View view) {
if (fragment.getClass().getSimpleName().equalsIgnoreCase("HomeFragment")){
setSharedPreference("FONT","LARGE");
fragment = new HomeFragment();
dialog.dismiss();
}
In the adapter,
if (fontsize.equals("LARGE")) {
mViewHolder.settings = mViewHolder.mChapterContextTextView.getSettings();
mViewHolder.settings.setTextZoom(mViewHolder.settings.getTextZoom() + 30);
mViewHolder.mChapterContextTextView.loadData(getItem(position - 1).getContent(),
"text/html; charset=UTF-8;", null);
Similarly done for fontsize.equals("SMALL"). :)

(Animation) Hide Toolbar in Fragment

Hey i want to hide my toolbar when i click inside an editText Therefore i added these lines
mORMetWeight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!enterWeightClicked) {
enterWeightClicked = true;
Toolbar mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
mToolbar.setVisibility(View.GONE);
mContainer.removeView(cv1);
mContainer.removeView(cv2);
mContainer.getChildAt(0);
header.getLayoutParams().height = (int) getResources().getDimension(R.dimen.smallEnterWeightHeight);
In my onCreateView method i added the transition effect but the animation now of the Toolbar looks horrible..
LayoutTransition transition = mContainer.getLayoutTransition();
transition.enableTransitionType(LayoutTransition.CHANGING);
I simply like to let the Toolbar scroll out and the view to the top? Is this possible with the "simple" animation style?

sherlockActivity back button color

I want to change the back button color in the ActionBar with SherlockActivity.
The problem is that I have use DarkActionBar but if I put the following code (to show the back button):
// OnCreate
getSupportActionBar().setDisplayHomeAsUpEnabled(true)
getSupportActionBar().setHomeButtonEnabled(true);
//
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
the back button appear, but appear with dark color and I am using DarkActionBar, so I want to change to light color
I have search in internet and I found this:
<item name="android:homeAsUpIndicator">#drawable/ic_action_previous_item</item>
in values/styles, but compiler say me that it requires minimun 11 Api level and my cuestion is:
why in Api level lower than 11 I can use back button but I can't change his color?
And if I agree
<item name="android:homeAsUpIndicator">#drawable/ic_action_previous_item</item>
in values, for example, v-14 it doesn't work, color is still dark.
what can I do?

Android 3.0 Honeycomb: How to enable/disable Menu Items in Action Bar?

it's pretty easy to disable a menu item in XML:
<item android:id="#+id/men_1"
android:title="#string/men_1"
android:showAsAction="ifRoom|withText"
android:icon="#drawable/ic_menu_1"
android:enabled="false"/>
It's also pretty easy to change it via code on a <3.0 app:
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
MenuItem item = menu.findItem(R.id.men_1);
item.setEnabled(false);
return true;
}
But how would I do it on Android 3.x?
I want to disable menu options depending on the Fragment shown.
Kind regards,
jellyfish
Pretty much the same but put code into the fragment instead, note different method signature.
#Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.id.men_1);
item.setEnabled(false);
super.onPrepareOptionsMenu(menu);
}
So the fragment takes responsibility for inflating the menu etc.
Edit Note the need to call setHasOptionsMenu(true)

Resources