custom View Display problem on different android screens - android

i have extended View and override onDraw and onSizechanged so i can draw on the screen after i have the full height and width of the screen.But when i tried on my phone the imgs are slightly in different position than that i have defined.Also i have define actual values(in px) in my code and i think i can't use dip at least i don't know....
What can i do to make it seems the same on many screens,when i'm not using Layouts and i do it on code?

Make images feel same on different screens, you have to first identify height and width of device and according to it you should write some method that will set your image at place where you want on different screens.
To get height and width of screen you have to write this method at start of activity:
float ht,wt;
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
ht = displaymetrics.heightPixels;
wt = displaymetrics.widthPixels;

Related

Overriding of button in android, change layout

In my project, Button added to the view dynamically and how many number of button is also unknown. So to carry these button i have created linearlayout(horizonatal). Problem is when button exceeded several limit(say 5) then 6 button wont draw over the screen, i don't want scroll, i want all the button on screen, wont matter if buttons will created below the buttons that already created. How to achieve this. How to know that screen is filled with buttons?
It sounds like you will have to dynamically keep track of how much screen space is being occupied by buttons based on the height and potentially the width of the screen. To get the screen size in pixels you can do something like
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
or
DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
// displayMetrics.heightPixels will give you the height
// displayMetrics.widthPixels will give you the width
Then you just have to compare this to the total number of pixels being used by buttons. Keep a counter of buttons created. Since you know the pixel height per button, just multiply that by the number of buttons created and you will know if it exceeds the height.
If you need the size in dp you can divide the values from displayMetrics.heightPixels or displayMetrics.widthPixels by the density
float dp = displayMetrics.heightPixels/displayMetrics.density

How can i make activity to be displayed on desired part of the screen like the activity is displayed in fallowing photo?

Activity displayed on some part of the screen
This question is a little vague, but if you are referring to the image placement:
First find the width of the device screen:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screen_width = displayMetrics.widthPixels;
Then you can position the Image based off screen_width.
For example, if you wanted it centered:
Say the width of the Image is 720. Then:
my_background_image.setX( (int)(screen_width/720) /2 );
Otherwise, if you are referring to the activity itself, then you would position it in your res/layout/[your activity].xml.
Depends on your layout. Constrained, linear, etc.
Can you be more specific?

Get screen size in Android

It seems in Android there are two ways to get screen width & height:
The 1st way:
WindowManager windowManager =
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//We get width and height in pixels here
width = size.x;
height = size.y;
The 2nd way is:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
//We get width and height in pixels here
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Both ways get screen width and height in pixels. So, I get a bit confused about the following two questions:
Q1. What are the differences between these two screen sizes theoritically?
Q2. Why google provide two ways to get screen size? I think there must be a reason behind it.
You should read about those two Classes
Window Manager
http://developer.android.com/reference/android/view/WindowManager.html
Resources
http://developer.android.com/reference/android/content/res/Resources.html
Window Manager gets screen width and height by getting the width and height of display that the window manager instance is managing including secondary display.
Getting width and height from Resources return the current display metrics and the objects should be treated as Read Only.
looking at the docs about DisplayMetrics:
A structure describing general information about a display, such as
its size, density, and font scaling.
It seems that the DisplayMetrics class provides you with basic hardware display information. It does not care about any system UI components that may (or may not) be present.
While the Display class is more extended and takes the things mentioned above into account.
To answer your question:
int width = metrics.widthPixels;
int height = metrics.heightPixels;
will provide you with absolute values, i.e. you will always get 1080/1920 on a full HD display.
The description of the getSize() method is pretty much self-explanatory:
[...] The size returned by this method does not necessarily represent
the actual raw size (native resolution) of the display. The returned
size may be adjusted to exclude certain system decoration elements
that are always visible. [...]
DisplayMetrics is a data object which best describes the details of the current display, including widthPixels, heightPixels, xDpi, yDpi, density, densityDpi and scaledDensity.
Display is a more logical object which performs the logical operations behind finding these parameters. For example, Display can be used to extract DisplayMetrics like so:
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics); // metrics will now be populated with the required data.
Note: The above code is, in essence, how Android generates the DisplayMetrics instance inside the Resources object.
I most often use DisplayMetrics, but both options should work just fine.

how to set width of button with respect to the resolution available on android device

my current android project requires the width of button should be set dynamically depending on the resolution available on device
i want to arrange 9 button in a row but the size will depend on the resolution available
for the current scene we are using below code
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
//int sheight = displaymetrics.heightPixels;
int swidth = displaymetrics.widthPixels;
int px=swidth/11;
bW.setWidth(px);
bE.setWidth(px);
bR.setWidth(px);
bT.setWidth(px);
bY.setWidth(px);
bU.setWidth(px);
bI.setWidth(px);
bO.setWidth(px);
bP.setWidth(px);
somehow the width of button is not changed in any manner it remains the same
If i consider you buttons to be images, that you have set using the Selector file, then you need to keep those files in drawable-hdpi, drawable-mdpi, and drawable-ldpi, and the images will accomodate itself according to the available resolution..
i think you should use android:layout_weight property.Its going to consume space depending on the width available.
set to all the buttons as android:layout_weight="1"
Try this url
Linear Layout and weight in Android
Buttons and other objects that uses a ninepatch image won't get smaller than the ninepatch itself.
Make sure you are using an appropriate ninepatch image for you buttons that will fit the sizes you need.
i came up with this solution
ViewGroup.LayoutParams params = button.getLayoutParams();
params.width=px;
button.setLayoutParams(params);

Creating a grid of scaled ImageViews [Android]?

I need to make a grid whose width and height is the same as the screen. I guess I would somehow use
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
to do this but I need to scale the images inside so that each ImageView is 1/10th of the width of the screen (with 10 images for example). Similarly for the image heights.
I have tried
a = (ImageView) findViewById(R.id.imageID);
a.setMaxHeight(metrics.heightPixels/numberOfImages);
But as you can imagine, this is very tedious with 130 images (and not to mention, it doesn't work!) It wouldn't be a problem if I didn't need to cater for lots of screen sizes!
Any help is appreciated! (Bit of an Android noob (-: )

Categories

Resources