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.
Related
So I overlayed a view of another view. And try to position my arrow so that it points to the image underneath it:
The arrow is an ImageView and I set its top and left margins to position it in the right place. I did it by adding the width of the TextView underneath it that says "No photo taken today" to the TextView's left border position. But funnily, the arrow is pointing to the middle and not the end of that TextView.
int topMargin=(int)pxToDp(getRelativeTop(dailyPhotos)+(int)dailyPhotos.getHeight(),this);
int leftMargin=(int)pxToDp(getRelativeLeft(dailyPhotos)+dailyPhotos.getWidth(),this);
getRelativeLeft() I copied from this answer.
I show the width of that TextView and the screen's width in a Toast. They are both correct since that TextView's width is set to match_parent. I don't what is wrong.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Toast.makeText(this,"width="+dailyPhotos.getWidth()+", screen's width="+width,Toast.LENGTH_SHORT).show();
showNote.putExtra("photoCountTopMargin", topMargin);
showNote.putExtra("photoCountLeftMargin", leftMargin);
Reading/Changing the margin of a view needs to be done in the layoutparams of the view
LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();
layoutParams.topMargin = ddd (in pixels)
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 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??
I have a trouble while creating a kind of view. Let's just say that i'm trying to create something similar to a clock. This means that we have to position numbers in a distance and angle from a center.
I tried to emulate a way to position two elements. A center ImageView and a clock number. In XML I have something similar to:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lyt_rootView"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/ivCenter"
android:background="#drawable/cid"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/ivNumber"
android:background="#drawable/add_big"
android:layout_marginLeft="-50dp"
android:layout_marginTop="50dp"/>
</RelativeLayout>
And it works pretty well, the images set as they should be, being completly centered the first image (ivCenter) and taking some margin from the center for positioning the second image.
Now I have to do the same programatically so I can create lots of imageViews simulating the numbers. So I have this code:
RelativeLayout lyt = (RelativeLayout) findViewById(R.id.lyt_rootView);
int avatarSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 70, getResources().getDisplayMetrics());
int distance = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, getResources().getDisplayMetrics());
// center image
ImageView ivImageCenter = new ImageView(this);
ivImageCenter.setImageResource(R.drawable.cid);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(avatarSize,avatarSize);
lyt.addView(ivImageCenter, lp);
// first image (simulating a number in a clock)
ImageView ivNumber1 = new ImageView(this);
ivNumber1.setImageResource(R.drawable.add_big);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(avatarSize,avatarSize);
lp2.setMargins(distance, distance, 0, 0);
lyt.addView(ivNumber1, lp2);
but the result is not similar as the XML example. I don't see what I'm missing. The result of creating the views programatically is that the two imageViews are trying to position in a center space in the screen making the first image not centered to the screen.
What I'm missing?
Finally I solved it by creating a one pixel layout centered in XML that acts as a reference and programatically adding the views in that reference view.
I have a square image in an ImageView contained within a FrameLayout that is right aligned (landscape layout). The FrameLayout is set to FillParent width and height and the ImageView is set to Adjust View Bounds so that the resulting ImageView is a perfect square with each side being the length of the height of the landscape layout, right aligned. However, the original image is larger than this.
I need to get the coordinates clicked on the ImageView in relation to itself (ie. the top left hand corner of the ImageView is 0,0 and the bottom right corner would be the displayed width and height coordinates) so that I can work out if the clicked point was past the mid-point of the ImageView.
I initially get the size of the ImageView using:
int myImageViewWidth = imageView.getWidth();
int myImageViewHeight = imageView.getHeight();
I've created an OnTouchListener and within this I'm using event.getX() and event.getY(), but this seems to return the coordinates relative to the image's oiginal size, not the displayed size. I've also tried the following:
Matrix m = imageView.getImageMatrix();
float[] values = new float[9];
m.getValues(values);
float relativeX = (event.getX() - values[2]) / values[0];
float relativeY = (event.getY() - values[5]) / values[4];
But again this seems to return inaccurate coordinates. Can anyone please help?