I'm new to Java and Android, so the code i have i pasted together from the net. The goal is to create a simple web browser for one webpage. In that webpage, it's possible to edit text and such.
Main goal is to keep the browser full screen, so to keep away the android navigation buttons.
I tried to use this code but no success until know.
Detecting Soft Keyboard Hidden State
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MyActivity";
static int mAppHeight;
static int currentOrientation = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
View.SYSTEM_UI_FLAG_IMMERSIVE);
setContentView(R.layout.activity_main);
//make a webview object
WebView webview=new WebView(this);
webview.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
View.SYSTEM_UI_FLAG_IMMERSIVE);
}
});
// enable javascript
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
//show it through setcontentview()method
setContentView(webview);
webview.loadUrl("https://www.mypage.com");
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
hideSystemUI();
}
}
private void hideSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_IMMERSIVE);
}
}
Can someone help where to put the code from the link?
Thank you very much
Kind regards
EDIT:
I also tried this one, but i don't see the event get run when the soft keyboard shows or hides.: https://github.com/yshrsmz/KeyboardVisibilityEvent
Try:
View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
In your manifest use this -
<activity
android:name=".Activities. MainActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen">
</activity>
In Activity onCreate use only this -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout. activity_main);
AND REMOVE
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION|
View.SYSTEM_UI_FLAG_IMMERSIVE);
setContentView(R.layout.activity_main);
Related
I've got a promblem with Android app.
I use this code to hide navigation bar.
public class Initer {
public static void fullScreen(Window window) {
if (Build.VERSION.SDK_INT >= 19) {
View decor = window.getDecorView();
fullScreen(decor);
} else {
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
public static void fullScreen(View decor) {
if (Build.VERSION.SDK_INT >= 19) {
decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
}
But when I show PopupWindow or Dialog up, navigation bar will come up again.
I tried do this
PopupWindow popupWindow =new PopupWindow(getWindow().getContext());
View popupWindowView = LayoutInflater.from(getWindow().getContext()).
inflate(R.layout.dialog_interval_insert, null);
Initer.fullScreen(popupWindowView);
...
popupWindow.showAsDropDown(button_start);
to hide navigation bar when PopupWindow showing up. But it doesn't work. When I call the PopupWindow, the navigation bar will show up then hide again. And when I dismiss() it, navigation bar will be there.
How can I hide navigation bar permanently in Whole application?
you can use this code
public void FullScreen_Activity(Activity activity) {
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
and change StatusBar Color with
public void setStatusBarColor(Activity activity, int RecColor) {
if (isApi21()) {
Window window = activity.getWindow();
// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// finally change the color
window.setStatusBarColor(ContextCompat.getColor(activity, RecColor));
}
}
and how to use FullScreen_Activity method
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getMethod().FullScreen_Activity(this);
setContentView(R.layout.activity_splash);
}
}
I would like to explain what i am doing now. I developed an aar(sdk) and is running fine. However, i would like my sdk to occupy full screen when being called, even the caller has a toolbar. i tried the sample from this link
[How to set activity to fullscreen mode in Android?
However, if i put the code in android Manifest of the sdk, my app will crashed. If i do it when sdk activity OnCreate as below code. The toolbar still there.
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_sdk_main);
May I know what do i need to do to hide the toolbar. Thanks.
If you want full screen mode for particular activity then use this
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
and for all activities Create a BaseActivity and write its onCreate method as shown above and extend this base activity for all other activities.
public void setFullScreenView() {//Hiding status/navigation bar
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
View v = this.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
setContentView(R.layout.activity_splash_screen);
}
getSupportActionBar().hide();
works.
Additional explaination, my class is an extension of AppCompatActivity, below is the code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.d(SdkMainActivity.class, "onCreate");
getSupportActionBar().hide(); //---> this works
//requestWindowFeature(Window.FEATURE_NO_TITLE); //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); // ---> this the toolbar still appear
setContentView(R.layout.activity_sdk_main);
}
I have managed to hide status bar on start up. But whenever I touch the controls status bar shows up. To solve this I tried this code to hide it on every touches but it shows up first then it hides. This loop continues forever.
private View mContentView;
private View mControlsView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testgame);
mControlsView = findViewById(R.id.fullscreen_content_controls);
mContentView = findViewById(R.id.fullscreen_content);
mContentView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mControlsView.setVisibility(View.GONE);
mContentView.setSystemUiVisibility(
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
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
});
I have also tried to change theme android:theme="#android:style/Theme.NoTitleBar.Fullscreen" but still hides it until I touch controls. I want to hide it until I exit the application. It is possible to do it without root permissions because most of games that I played can hide it until I exit the game or swipe down on
I have just found solution on this page that hides status bar permanently.
Firstly I added this import android.view.WindowManager;.
Then here is my OnCreate function:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testgame);
//Hide status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Hide action bar
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
}
And this code helps you to show status bar again
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);// Show status bar
I've written a basic application to test the way Immersive Mode or FullScreen mode(only hide status bar) works on a Webview app.
Here's my main_activity. Note this is just a test app that follows the api guidelines as far as I can tell.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://baidu.com"); //just a test website, doesn't matter really. Just need a text box low enough on screen.
setupFullscreenMode();
}
private void setupFullscreenMode() {
View decorView = setFullscreen();
decorView
.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
setFullscreen();
}
});
}
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
setFullscreen();
}
}
private View setFullscreen() {
View decorView = getWindow().getDecorView();
//FLAG ORDERING MAKES A DIFFRENCE
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE // without the other layout flags, this results in glitched out status and navagation spaces
//unsurprisingly everything (pre-selected scenario etc) works fine with this flag, but it's not true fullscreen
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // This also breaks the pre-seclected scenario
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // This breaks the pre-selected scenario
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION //2
| View.SYSTEM_UI_FLAG_FULLSCREEN //1
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);//3 with 1,2,3 there's some glitching still going on(ghost statusbar)
return decorView;
}
In short this is my bug -
Be in immersive mode and go to a website (baidu in this case) and search for anything..
Scroll down to the text input box and click it. The application should have resized with the input box right above the top of the keyboard.
Hit the little "downward" triangle to dismiss the keyboard. Notice how the input field is left selected still.
(ISSUE) Scroll up enough to lower the input box on screen. Now try tapping on the input box again. The app doesn't resize to accommodate the keyboard.
Expected Behaviour would be to resize the app because that's what happens when I try doing this without fullscreen or immersive mode.
What is going on here that results in a failure ?
In my actual app this is very frustratingly flaky. It may or may not resize the app whether or not the text box is pre-selected.
For now, I'm only interested in understanding this test app's behavior.
I know that an app can be made full screen by tag in the manifest of the activity android:theme="#android:style/Theme.NoTitleBar.Fullscreen" Is it possible to switch to full screen mode from within the app, programmatically?
add two lines...
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
You can create new style and add
<item name="android:windowFullscreen">true</item>
Or you can do it programmatically:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
add this in Activity onCreate before setContentView:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
and in AndroidManifest.xml file:
<activity android:name=".ActivityName"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
</activity>
If you need a fullscreen immersive mode (for games, galleries, etc.), here is the solution I use and recommend.
The most efficient way is to set the SystemUIVisibility only when is needed:
In your onCreate()
With OnSystemUIChangeListener when the fullscreen mode is lost
public class BaseActivity extends Activity {
#SuppressLint("InlinedApi")
private static final int UI_OPTIONS = View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Step 1
hideSystemUI();
// Step 2
getWindow().getDecorView()
.setOnSystemUiVisibilityChangeListener(new View
.OnSystemUiVisibilityChangeListener() {
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
hideSystemUI();
}
}
});
}
private void hideSystemUI() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) actionBar.hide();
getWindow().getDecorView().setSystemUiVisibility(UI_OPTIONS);
}
}
Try this,
// remove title
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
or
<activity android:name=".ActivityName"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"/>
in onCreate() method write
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);