How to get USABLE screen width and height in Android - java

I'm creating an app where I really need to know the correct screen dimensions. Actually I know how to do that... but a problem appeared, because even if you dorequestWindowFeature(Window.FEATURE_NO_TITLE);, this:
still stays on the screen and it seems, it's also counted as a part of the screen so the "screenWidth" is actually bigger, than the usable part.
Isn't there any method how to get the size of the usable part and not whole screen? Or at least get the sizes of the 'scrolling thing'(but there would be a problem, that there are devices that don't show them)?

I had the same question a while back and here is the answer that i found. Shows the activity dimensions.
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.graphics.Point;
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main)
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
//Set two textViews to display the width and height
//ex: txtWidth.setText("X: " + width);
}
}

I know this question is really old, but I have fought with this so many times until I found this stupidly simple solution:
public final void updateUsableScreenSize() {
final View vContent = findViewById(android.R.id.content);
vContent.post(new Runnable() {
#Override
public void run() {
nMaxScreenWidth = vContent.getWidth();
nMaxScreenHeight = vContent.getHeight();
}
});
}
Run this code after setContentView() in your Activity's onCreate(), and delay any code that depends on those values until onCreate() is finished, for example by using post() again after this one.
Explanation: This code obtains your activity's root view, which can always be accessed by generic resource id android.R.id.content, and asks the UI handler to run that code as the next message after initial layout is done. Your root view will be of the exact size you have available. You cannot run this directly on the onCreate() callback because layout did not happen yet.

you can use also the full screen
developer.android

Related

Why is this WindowManager working until I ask to recall it's value?

I have some code that I am using to find the dimensions for my QR code image. However, I am trying to do so within a fragment. What I've done is within the main activity, declare a public int for the dimen, and then run the dimension code within the MainActivity and do a getter for it. However, whenever I try to use the dimen value, either from a getter or within MainActivity itself, the app crashes. It will run, but it crashes the second you try to do anything with dimen, be it putting it into a toast, or for a method call.
public class MainActivity extends AppCompatActivity {
public int dimen;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
//initializing a variable for default display.
Display display = manager.getDefaultDisplay();
//creating a variable for point which is to be displayed in QR Code.
Point point = new Point();
display.getSize(point);
//getting width and height of a point
int width = point.x;
int height = point.y;
//generating dimension from width and height.
dimen = Math.min(width, height);
dimen = dimen * 3 / 4;
Toast.makeText(MainActivity.this,dimen,Toast.LENGTH_SHORT).show(); //If you remove this line, the code runs. However, using any other use of dimen will also crash the app. Toast.makeText is not the problem.
}
}
I have tried doing it within the fragment, and within the MainActivity. I can't find the solution.
I have found my problem. Within this line,
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
It should be
WindowManager manager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
This is because WindowManager requires to be called from an Activity or Service, but it is not being called from any.
Secondly, there was an error within the toast message. It should be
Toast.makeText(MainActivity.this,String.valueOf(dimen),Toast.LENGTH_SHORT).show();
Very simple, but ints must be string within Toast, so String.valueOf fixes this. Just posting so anyone in the future has a solution!

Android 12 Splash Screen API - Increasing SplashScreen Duration

I am learning Android's new SplashScreen API introduced with Android 12. I have so far gotten it to work on my Emulator and Google Pixel 4A, but I want to increase its duration. In my Splash Screen I do not want a fancy animation, I just want a static drawable.
I know, I know (sigh) some of you might be thinking, that I should not increase the duration and I know there are several good arguments in favor of not doing so. However, for me the duration of a splash screen with a non animated drawable is so brief (less than a second), I think it raises an accessibility concern, especially so since it cannot be disabled (ironically). Simply, the organization behind the product or its brand/product identity cannot be properly absorbed or recognized by a new user at that size and in that time, rendering the new splash screen redundant.
I see the property windowSplashScreenAnimationDuration in the theme for the splash screen (shown below), but this has no effect on the duration presumably because I am not animating.
<style name="Theme.App.starting" parent="Theme.SplashScreen">
<!--Set the splash screen background, animated icon, and animation duration.-->
<item name="windowSplashScreenBackground">#color/gold</item>
<!-- Use windowSplashScreenAnimatedIcon to add either a drawable or an
animated drawable. One of these is required-->
<item name="windowSplashScreenAnimatedIcon">#drawable/accessibility_today</item>
<item name="windowSplashScreenAnimationDuration">300</item> <!--# Required for-->
<!--# animated icons-->
<!--Set the theme of the activity that directly follows your splash screen-->
<item name="postSplashScreenTheme">#style/Theme.MyActivity</item>
<item name="android:windowSplashScreenBrandingImage">#drawable/wculogo</item>
</style>
Is there a straightforward way to extend the duration of a non animated splash screen?
As I was writing this question and almost ready to post it, I stumbled on the method setKeepOnScreenCondition (below) that belongs to the splashScreen that we must install on the onCreate of our main activity. I thought it seemed wasteful not to post this, given there are no other posts on this topic and no such similar answers to other related questions (as of Jan 2022).
SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
plashScreen.setKeepOnScreenCondition(....);
Upon inspecting it I found this method receives an instance of the splashScreen.KeepOnScreenCondition() interface for which the implementation must supply the following method signature implementation:
public boolean shouldKeepOnScreen()
It seems this method will be called by the splash screen and retain the splash screen visibly until it returns false. This is where the light bulb moment I so love about programming occurred.
What if I use a boolean initialized as true, and set it to false after a delay? That hunch turned out to work. Here is my solution. It seems to work and I thought it would be useful to others. Presumably instead of using a Handler for a delay, one could also use this to set the boolean after some process had completed.
package com.example.mystuff.myactivity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.splashscreen.SplashScreen;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity extends AppCompatActivity {
private boolean keep = true;
private final int DELAY = 1250;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Handle the splash screen transition.
SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
super.onCreate(savedInstanceState);
//Keep returning false to Should Keep On Screen until ready to begin.
splashScreen.setKeepOnScreenCondition(new SplashScreen.KeepOnScreenCondition() {
#Override
public boolean shouldKeepOnScreen() {
return keep;
}
});
Handler handler = new Handler();
handler.postDelayed(runner, DELAY);
}
/**Will cause a second process to run on the main thread**/
private final Runnable runner = new Runnable() {
#Override
public void run() {
keep = false;
}
};
}
If you are into Java Lambdas an even nicer and more compact solution is as follows:
package com.example.mystuff.myactivity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.splashscreen.SplashScreen;
import android.os.Bundle;
import android.os.Handler;
public class MainActivity extends AppCompatActivity {
private boolean keep = true;
private final int DELAY = 1250;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Handle the splash screen transition.
SplashScreen splashScreen = SplashScreen.installSplashScreen(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Keep returning false to Should Keep On Screen until ready to begin.
splashScreen.setKeepOnScreenCondition(() -> keep);
Handler handler = new Handler();
handler.postDelayed(() -> keep = false, DELAY);;
}
}
If you have comments or feedback (besides telling me I should not increase the duration of the splash screen), or a better way please do comment or respond with additional answers.
in Kotlin:
var keepSplashOnScreen = true
val delay = 2000L
installSplashScreen().setKeepOnScreenCondition { keepSplashOnScreen }
Handler(Looper.getMainLooper()).postDelayed({ keepSplashOnScreen = false }, delay)
you can put this into onCreate fun before super.onCreate calling (in activity with LAUNCHER intent filter in Manifest)
One proxy way could be to use
runBlocking { delay(1200) } in onCreate method, to keep on main thread for some specific time.

How to get a reference to the layout from within a custom TextView

This is probably an easy question to answer, but searches were just turning up things like inflating and xml layouts, which isn't what I want.
I have a custom TextView that I want to draw a cursor on. (I can't use an EditText.) In order to draw the line, I need to calculate various coordinates from the character offset that the cursor should be at. I tried the following as a method of the custom TextView:
public void setCursorLocation(int characterOffset) {
Layout layout = this.getLayout();
int line = layout.getLineForOffset(characterOffset);
mCursorX = layout.getPrimaryHorizontal(characterOffset);
mCursorBaseY = layout.getLineBaseline(line);
mCursorBottomY = layout.getLineBottom(line);
mCursorAscentY = layout.getLineAscent(line);
}
However, the layout is null. I even tried passing in the TextView as a parameter but the layout was still null. What am I doing wrong?
public void setCursorLocation(View view, int characterOffset) {
Layout layout = ((TextView) view).getLayout();
...
}
Update
I changed it to the following
public void setCursorLocation(Layout layout, int characterOffset) {
but even that layout was null. Then I looked at where I was calling it from in the main activity:
inputWindow.setText(displayText);
inputWindow.setCursorLocation(inputWindow.getLayout(), glyphCurosrPosition);
Watching the expression inputWindow.getLayout() was fine until setText was called and then all of a sudden it became null. I guess that is because it invalidated itself. How/when do I draw the cursor then? My guess is to post a runnable maybe. Update 2 Yes, see below.
This solution finally worked for me.
From the main activity:
inputWindow.setText(displayText);
inputWindow.post(new Runnable() {
#Override
public void run() {
inputWindow.setCursorLocation(glyphCursorPosition);
}
});
In the custom TextView:
public void setCursorLocation(int characterOffset) {
Layout layout = this.getLayout();
int line = layout.getLineForOffset(characterOffset);
mCursorX = layout.getPrimaryHorizontal(characterOffset);
mCursorBaseY = layout.getLineBaseline(line);
mCursorBottomY = layout.getLineBottom(line);
mCursorAscentY = layout.getLineAscent(line);
this.invalidate();
}
The downside is that there is a slight time lag before the cursor updates itself.

Change game from portrait to landscape dynamically

I have multiple classes that implement Screen.
I want to, say, have the Main Menu screen portrait, then the Play Screen landscape. Is there anyway of changing the orientation dynamically?
I thought I could just start another Activity and have the Play Screen extend Game. And the new Activity would intialize the Play Screen. But it appears that you can't call startActivity or the like, obviously, as LibGDX is platform independent.
I use Gdx.graphics.getWidth() and Gdx.graphics.getHeight() to align Sprites and Actors, so it looks "right" on different devices.
I've also tried swapping the Gdx.graphics.getWidth() and Gdx.graphics.getHeight(), respectively, but can't seem to get the position of sprites and actors correct.
Also, in my manifest I have orientation="landscape".
Here are some images that should make more sense.
So this is my Main Menu in landscape:
And this is my Main Menu in portrait:
The position and size of Actors and Sprites are all based on Gdx.graphics.getWidth() and Gdx.graphics.getHeight(). The only thing that changes between the two images is orientation="landscape" and orientation="portrait" in my manifest.
Now I want my Main Menu in portrait but the Play Screen, where the game is on, should be in landscape.
I initially started working on the Play Screen and the sizes and positions where set based on orientation="landscape", and now that I am working on the Main Menu it is based on orientation="portrait". Hence why I want to change the orientation dynamically.
This is a good question :) I develop noone's answer (which is correct)
How to set lanscape screen with Android sdk specific classes ?
And how to import android classes in a libgdx project ?
First of all note that Android classes can only be imported in the android project and not in the core. So there we save the Activity in a platform dependant object. Here is my AndroidLauncher:
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.mygdx.game.MyGdxGame;
import com.mygdx.game.util.Platform;
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
PlatformAndroid platform = new PlatformAndroid();
platform.setActivity(this);
initialize(new MyGdxGame((Platform) platform ), cfg);
}
}
PlatformAndroid implements Platform. These two classes are made to access platform specific objects. Let's look at them :
public interface Platform {
public void SetOrientation(String string);
}
and
package com.mygdx.game.android;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.mygdx.game.util.Platform;
public class PlatformAndroid extends AndroidApplication implements Platform {
private Activity activity;
#Override
public void SetOrientation(String string) {
if (string == "landscape"){
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
public void setActivity(Activity activity){ this.activity = activity; }
}
You can also create a PLatformDesktop called by the desktop launcher. Anyway, keep the platform variable in your classes and when you want to set the screen to landscape just call:
platform.SetOrientation("landscape");
You should not have multiple Activities. To learn how to switch the orientation programmatically you can have a look at this link.
As you said, LibGDX is platform-independent. That's why you cannot access your Android-Activity from your "core" code. To learn how to workaround that, have a look at Interfacing with platform specific code.
When you call your interface to change the orientation from your game when doing a Screen transition, LibGDX will automatically call resize(width, height) with the new values and you can align your Stage and so on according to the new values.
I had the same problem and this solved it:
#Override
public void resize(int width, int height) {
stage.getViewport().setScreenSize(width, height);
}
For some reason, even though the screen resolution changed, the stage didn't know that. So I've told it to the stage.
Just create a interface in core project and and declare a method with any name than implement that interface in your AndroidLauncher class. Give definition of your method.
For me GameOrien.java is interface having method declaration
public interface GameOrien {
public static final int PORTRAIT=1;
public static final int LANDSCAPE=2;
void setOrientation(int orientation);
}
In AndroidLauncher.java class which is in Android project.
public class AndroidLauncher extends AndroidApplication implements GameOrien {
**...**
#Override
public void setOrientation(int orientation) {
if(orientation==PORTRAIT)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
else if(orientation==LANDSCAPE)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
When you want to change orientation for GamePlay just call setOrientaion() method of interface. Your orientation changed.

Why does LinearLayout child getWidth method return 0?

I have a LinearLayout, and this LinearLayout will hold dynamically placed views. I need to find out what the width of the children of LinearLayout, however this has to be done in onCreate method. From researching I've found out that you can't use getWidth from this method. So instead I'm using onWindowFocusChanged, which works for the parent LinearLayout (returning me the actual size), but it doesn't work with its children.
Another thing I noticed is that when the screen is fading away and the screen is locked, I can see at the logs the actual width of the children being returned (I think the activity is being paused).
I'm really stuck and this is needed because I need to dynamically place those views depending on the children width.
You might be able to get with the below. But as others pointed out, this probably isn't a great idea.
LinearLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
LinearLayout.getMeasuredWidth();
inside the onCreate , views still can't know the state of the nearby views and the children ,etc... so only after all is prepared and the layout process is done , you can get the size of views .
here's a quick code for getting the size of the view just before it's being drawn:
private static void runJustBeforeBeingDrawn(final View view, final Runnable runnable)
{
final ViewTreeObserver vto = view.getViewTreeObserver();
final OnPreDrawListener preDrawListener = new OnPreDrawListener()
{
#Override
public boolean onPreDraw()
{
Log.d(App.APPLICATION_TAG, CLASS_TAG + "onpredraw");
runnable.run();
final ViewTreeObserver vto = view.getViewTreeObserver();
vto.removeOnPreDrawListener(this);
return true;
}
};
vto.addOnPreDrawListener(preDrawListener);
}
alternatively , you can use addOnGlobalLayoutListener instead of addOnPreDrawListener if you wish.
example of usage :
runJustBeforeBeingDrawn(view,new Runnable()
{
#Override
public void run()
{
int width=view.getWidth();
int height=view.getHeight();
}
});
another approach is to use onWindowFocusChanged (and check that hasFocus==true) , but that's not always the best way ( only use for simple views-creation, not for dynamic creations)
EDIT: Alternative to runJustBeforeBeingDrawn: https://stackoverflow.com/a/28136027/878126
https://stackoverflow.com/a/3594216/1397218
So you should somehow change your logic.

Categories