I need to raise the Activity up 15% of the height of the screen (regardless of which screen element has to rise to 15%) off the screen visibility ... I'm doing this so way
i have custom view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
/.../
<com.example.android.camera2basic.AutoFitTextureView
android:id="#+id/texture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="false"
android:layout_alignParentStart="true">
</com.example.android.camera2basic.AutoFitTextureView>
/.../
</RelativeLayout>
here I set the option to do so
private void initVar() {
// Margin set in % of the screen
int marginLeft = 0;
------> int marginTop = 15; here I show percent of screen height
int marginRight = 0;
int marginBottom = 0;
// Here we get the height and width of the screen
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
// Here we set the parameters for our view
RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
param.setMargins((screenHeight * marginLeft) / 100, -((screenHeight * marginTop) / 100),
(screenHeight * marginRight) / 100, (screenHeight * marginBottom) / 100);
mTextureView = (AutoFitTextureView) findViewById(R.id.texture);
mTextureView.setLayoutParams(param);
and that's what happens when I load the project into the phone Samsung S5 1980 x 1080 it looks like I want, exactly 15% occupied (white bar below is the default color of the screen that opens when we raise a View)
But when I load the same project in the emulator 1280 x 800 screen rises to 25-30% (not exactly 15%)
And if you try the emulator with a resolution of 1440x2560, it turns out like this
In the first screenshot white bar does not reach the inscription "Front picture", the second takes over, and a third is about 50% of the screen...
Very strange because the formula is obtained for which there is a calculation implies that I take the height of the screen and get from it exactly 15% ...
Why on one device is 15%, while on the other it is more and the third is generally 50% of the screen ??
What am I doing wrong??
Related
I'm stuck trying to resize the width of a View in a ListView programmatically, because I want it to be in DP, not pixels.
My ListView generate instances of a RelativeLayout with a View that fills all the screen (match_parent), and a TextView and a button inside.
The View is set to width = match_parent to fill the whole screen, as intended
What I want, is to have different View width in my adapter. Like 25%, 50%, or 75% of the total width of the base View.
mRelativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(**[VALUE I NEED HERE]**, mRelativeLayout.getLayoutParams().height));
The ImageView is set to width = match_parent
So this are the relevant parts of code in my ViewList adapter :
RelativeLayout mRelativeLayout = (RelativeLayout) convertView.findViewById(R.id.test_layout);
mRelativeLayout.setBackgroundResource(arrayListName.myColor);
if (arrayListName.myColor == R.color.custom ) {
dayviewLayout.setLayoutParams(new RelativeLayout.LayoutParams( 25% , dayviewLayout.getLayoutParams().height));
}
if (days.mMoodColor == R.color.custom2) {
dayviewLayout.setLayoutParams(new RelativeLayout.LayoutParams( 50% , dayviewLayout.getLayoutParams().height));
}
if (days.mMoodColor == R.color.custom3) {
dayviewLayout.setLayoutParams(new RelativeLayout.LayoutParams( 75% , dayviewLayout.getLayoutParams().height));
}
The 25-50-75% are obviously wrong, as it takes only int values. I want it to fit any screen.
Thanks for your support.
dp units are just px units multiplied by the current screen density, which is available via the DisplayMetrics class:
float density = [some context].getResources().getDisplayMetrics().density;
int dp = (int) ([your px value] * density);
I want to get the actual screen height of the device that runs my app. To achieve this i try the following:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int Height = metrics.heightPixels;
TextView HeightView = (TextView) findViewById(R.id.screenHeight);
HeightView.setText("Screen Height: " + Height);
int Width = metrics.widthPixels;
TextView WidthView = (TextView) findViewById(R.id.screenWidth);
WidthView.setText("Screen Width: " + Width);
The device that I run using the emulator has a screen width of 1080 pixels and a screen height of 1920 pixels. The width is displayed correctly (1080 pixels) but the height is according to the app only 1776 pixels. What do I need to make my app display the correct screen height? Is metrics.heightPixels a bad way of getting the screen height?
see this answer
if your API level > 13 try this if you are in activity
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
getWidth and getHeight is for API level 13 or less
UPDATE
For API 17 and higher method Display.getRealSize() returns full size of screen, as mentioned in documentation:
Gets the real size of the display
without subtracting any window decor or applying any compatibility
scale factors.
The size is adjusted based on the current rotation of the display.
The real size may be smaller than the physical size of the screen when
the window manager is emulating a smaller display (using adb shell am
display-size).
int width=Resources.getSystem().getDisplayMetrics().widthPixels;
int height=Resources.getSystem().getDisplayMetrics().heightPixels;
I needed to get the actual available height, and out of all the options this is the only thing that worked for me.
Create a rectangle, and get its dimensions:
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int screenHeight = r.bottom - r.top;
where activityRootView is the root view of your layout.
If you need to extract the toolbar height:
int availableScreenHeight = screenHeight - toolbar.getHeight();
where toolbar is your toolbar view.
availableScreenHeight will now be without the statusbar, without the toolbar, and without the navigation bar (if the device is using it).
you can use this code:
val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width: Int = size.x
val height: Int = size.y
You can try this:
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
Point size = new Point();
d.getSize(size);
Log.i("LOG", "W=" + size.x + " , H=" + size.y);
I am building an android app that requires a layout to be added programmatically. This is the line that seems to be giving me a hard time:
RelativeLayout.LayoutParams rightScreenParams = new RelativeLayout.LayoutParams(
(int) (screenWidth * 0.25), (int) (screenHeight * 0.94));
The variables screenWidth and screenHeight are the width and height of the screen in pixels. The layout is supposed to be 25% of the width of the parent layout, and 94% of the height. When I add the parameters to the layout and then add the layout, it takes up the entire screen instead of the dimensions I mentioned.
So am I using this constructor correctly? Any help is appreciated. Thank you for your time.
Edit:
I get screenWidth and screenHeight in onCreate():
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
screenWidth = displayMetrics.widthPixels;
screenHeight = displayMetrics.heightPixels
The variable screenHeight is returning as 1184 even though the actual height in pixels is 1280. The variable screenWidth seems to be fine. So how can I get an actual screen height in pixels?
RelativeLayout.LayoutParams rightScreenParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.Wrap_Parent,samehere);
View.setLayoutParams(rightScreenParams);//your view
//after you add it to the layout then you call
rightScreenParams.width = (int) (screenWidth * 0.25);
rightScreenParams.height = (int) (screenHeight * 0.94);
its the same thing but it forces android.
In my application I control the size of my button proportionally with the screen size:
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
int buttonWidth = (int) (screenWidth * 0.07);
int buttonHeight = (int) (buttonWidth * 1.2);
The button takes the same space (for example 5 %) of the device's screen size. What I want now is that the button's text takes the same space of the button area on every device.
I tried this:
int textSize = (int) (buttonHeight * 0.145);
myButton.setTextSize((float) (textSize));
I don't know why but on bigger screens (such as tablets) the text uses a lot less space of the button than on smaller screens.
Thanks !
Maybe
Try specifying the unit of measurement like this:
myButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, (float) (textSize));
setTextSize uses sp units by default instead of px, so that might be causing your issue!
Solved:
As Zoltán wrote I didn't take the statusbar and titlebar into consideration (They're both 25 in height)
I used the following code in my onClickListener method inside my Activity and forwarded the results to my Service class and printed it out there:
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight = rectgle.top;
int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight = contentViewTop - StatusBarHeight;
serviceIntent.putExtra("Titlebarheight", Integer.toString(TitleBarHeight));
serviceIntent.putExtra("Statusbarheight", Integer.toString(StatusBarHeight));
-----------------------------------------------------------------------------------------
I am trying to set the layout height of my ImageView in my main.xml, but when I set the layout_height to 270dp (or px) it fills up the whole screen from top to bottom, but my screen size is actually 320x240 (HTC magic - Android 1.5) - So it SHOULDN'T fill up the screen from top to bottom!
Code inside Activity:
Display display = getWindowManager().getDefaultDisplay();
Log.i("Screen height", Integer.toString(display.getHeight())); // Returns 320
Log.i("Screen width", Integer.toString(display.getWidth())); // Returns 240
Code inside main.xml:
<ImageView
android:id="#+id/bitmapImageView"
android:layout_height="270dp" // Fills up the whole screen from top to bottom
android:layout_width="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#FFFFFF"
android:src="#drawable/ic_launcher" />
Why is this happening? How can I fix it?
Example when using 270 as layout height:
Example when using 240 as layout height:
The height of the status bar and the title bar of your application seems to be about the same as the width of your ImageView, which is 50px judging from your XML.
So then 50px + 270px = 320px, which is the total height of your screen (including the status and title bar).
See: Screen Resolution of android and
scale-independent pixel
Basically, a dp (display-independent pixel) is not the same size on all displays. At your resolution 1dp =~ .75px
To do a full screen activity see: Fullscreen Activity in Android?
Specifically, set: android:theme="#android:style/Theme.NoTitleBar.Fullscreen" in the android manifest for that activity.