Overriding of button in android, change layout - android

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

Related

Android screen size inconsistency

As seen in this image:
I have a rounded rectangle that is the size of 1082 x 1796, and I used getWindowManager().getDefaultDisplay() to get and print the pixel values of the screen as seen in the circle bottom left. However, when I draw this bitmap, this happens:
Is the screen size different from the getDefaultDisplay() method? How do I fix this?
I think your shape is seen by the system as a 9patch. It use the first and last lines/columns in order to know how to resize the image in order to display it consistently.
That's why there is a 2px difference in width and height. I think you can correct it by modifying the png and removing the first and last lines/columns, or just use another way to draw the image. Maybe using another format (jpg ? bmp ?) for the image could do the job. I'm not sure.
you can do that with calculation to fit all screen devices based on width and height of the device screen:
DisplayMetrics dm = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * imageHeight / imageWidth;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);

DisplayMetrics not returning exact height and width of the screen [duplicate]

This question already has answers here:
How to get real screen height and width?
(3 answers)
Closed 7 years ago.
I have to add multiple TextViews dynamically.
Each TextView should fill the screen and the next TextView should be added beneath the first view and even it should occupy the entire screen .
I've used the following code to determine screen height and with at run time.
Code snippet
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
for(int i=0;i<name.size();i++)
{
text = new TextView(getApplicationContext());
text.setText(class.get(i));
text.setTextColor(Color.parseColor("#000000"));
text.setLayoutParams(new LinearLayout.LayoutParams(width, height));
text.setGravity(Gravity.CENTER);
text.setTextSize(40);
text.setClickable(true);
LL.addView(text);
}
Result:
1st view occupies entire screen and it is extended to next screen.
When 2nd view added to LinearLayout little gap is left over at the top.
This scenario can be understood only with screenshots.
Screenshots:
slowly these screens are moving down.their height is increased by 30dp approx.
How could I get the exact height and width of the screen using DisplayMetrics.
EDIT:
Even by using this Answer screen occupies 30dp more than its height
Answer:
After calculating the screen width and height substract the height of Action bar from it.
This point is not mentioned. neither here nor in that duplicate question
use LinearLayout.LayoutParams(LinearLayout.match_parent, LinearLayout.match_parent) instead of new LinearLayout.LayoutParams(width, height)
and also make your TextView
object local instead of global
TextView text = new TextView(getApplicationContext());

Android image 1/3rd of the screen with scrollview

In my app, I download images from my web server (each having a different resolution) but I would like to show these images on a fragment screen consistently.
Ideally, I would like the image to have width="match_parent" and height to take 1/3 of the screen EXACTLY. Furthermore, the image should be shown in a as it's part of a content layout with other controls that could possibly grow larger than the screen height (hence the need for scrolling).
I have tried putting the image and the rest of the content in a LinearLayout and then setting the weight to 1 (with max weight being 3), but since the image and the other contents are in a scrollview, it doesn't quite seem to work. the image is either too large or too small (depending on the orientation and the resolution of the image) and my 1/3rd settings don't seem to be respected.
Is there any way to do this other than fixing the height to a pre-determined (dp) value? I would like to avoid that unless there's no other choice.
Many thanks,
You could always set the height for each of the images programatically to 1/3 the height of the screen, like so
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
ImageView img = (ImageView) findViewById(R.id.image);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) img.getLayoutParams();
params.height = (int)height / 3;
img.setLayoutParams(params);

Layout stretching

Is there a layout, that would allow me to make it in absolute values, but when it would be on larger / smaller screen, it would stretch and adjusted those values, to fit onto the screen but preserve the same look?
Relative layout still keens on exact values.
For example, I have a button and I want it to have it the width of 1/3 of the screen of every device.
Setting manualy in code the width of an element to (for example) screenWidth/3 works. Yet I don't think it's clean. But this technique works.
Find device dimensions at runtime and set width of button at runtime.
Display mDisplay = getWindowManager().getDefaultDisplay();
int deviceWidth = mDisplay.getWidth();
int deviceHeight = mDisplay.getHeight();
button.getLayoutParams().width = deviceWidth / 3;
Give the values in dpi of your layout and view and they will adjust themselves on any screen

custom View Display problem on different android screens

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;

Categories

Resources