How to make Percent dynamic in PercentRelativeLayout? - android

I am using PercentRelativeLayout from Design Support Library and i want to set different Percentage for 7 inch and 10 inch tablet.
For example if i have ImageView like below.
<ImageView
android:id="#+id/contactDoc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_widthPercent="70%"
app:layout_heightPercent="70%"
android:layout_centerHorizontal="true"
android:clickable="true"
android:src="#drawable/dashboard_contactdoctor" />
now if i want to set 70% for 7 inch tablet and 60% for 10inch tablet without making different layout folder like sw720dp . Can i do it?
Help is appreciated.

Those percentages are fraction resources. You should be able to set up res/values/fractions.xml and res/values-sw720dp/fractions.xml, where you define the values for the fractions. Then, use #fraction/whatever_you_called_it in the layout.

You can use different layouts for different screen sizes. You can read more about it in the documentation: https://developer.android.com/training/multiscreen/screensizes.html
A possibility wihtout providing multiple layouts would be to place the ImageView inside a LinearLayout with a android:weightSum of 10 and then set the weight of the ImageView programmatically:
LinearLayout.LayoutParams layoutParams = yourView.getLayoutParams();
layoutParams.weight = WHATEVER;
yourView.setLayoutParams(layoutParams);

Try using constraint layout, it is available on android studio 2.2 and after .
By using this, you can add both vertical and horizontal guideline according to screen percentage and then you set the height and width of your imageview relative to those guideline

First of all, you are going to need to detect 7" or 10" tablet. I assume that you already know how to do it based on your question. If not, check out this great answer.
After you know what device are you dealing with, use the following code to put inside an if condition (or somewhere else) to define the percentage of your view in code:
View view = findViewById(R.id.yourView);
PercentLayoutHelper.PercentLayoutParams params =
(PercentRelativeLayout.LayoutParams) view.getLayoutParams();
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.heightPercent = 0.60f;
view.requestLayout();
Based on another great answer.

Related

control dimensions of custom alert dialog in different screen orientations

I want to set dimensions for my custom alert dialog, based on screen orientation. My intention is to swap height and width values to keep the box look like being the same size, yet handled various screen sizes of various devices, thanks to Android fragmentation it seems difficult to achieve the same effect on all devices. Android's auto-resizing seems weird to me.
Portrait:
alertDialog.width=screen.width*0.8
alertDialog.height=screen.height=0.5
Landscape:
alertDialog.width=screen.width*0.5;
alertDialog.height=screen.height*0.8
Please note that the Custom Alert Dialog must use the same code and support Android versions from JellyBean (at least 4.2) to Nougat (7).
i am using android.support.v7.AlertDialog (the latest available thing)
i assume android.app.Dialog should be avoided now (being old)
Also, i need a black border and white background for the same. i am unable to achieve same effect on all devices, (i need transparency for rounded corners)
i have used android:windowMinWidthMajor and android:windowMinWidthMinor but they affect both layouts (portrait and landscape) and seem to be ignored if content does not fit within the specified constraints.
I wish there was android:windowMaxWidthMinor & android:windowMaxWidthMajor
This is something that I've used for resizing specific views on a page:
myView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
item.height = myView.getHeight();
});
Applying it to what you want to do:
myView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
LayoutParams params = (get the layout params according to the layout);
params.width = myView.getWidth() * 0.5;
params.height = myView.getHeight() * 0.8;
myView.setLayoutParams(params);
});
You have to use the listener because the object will be added to the view tree, will be measured and prepared for display, but won't yet be displayed, so you can change its dimensions before it is visible.
Update:
I could achieve it by setting android:layout_centerInParent="true" for my Layout. and layout values for each component in the style to
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
to achieve the center alignment.
XML Code available at https://github.com/computingfreak/CFTest/blob/master/app/src/main/res/layout/alert_popup.xml
Java Code available at
https://github.com/computingfreak/CFTest/blob/master/app/src/main/java/net/cf/sms/cftest/MainActivity.java
I wonder this line of code is redundant
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
and thus left it to Android System to decide dimensions and resizing based on content. It works for my case, but will fail in case of long text, maybe some kind of Scroll Layout will help. Cheers!

Setting views width and height programmatically or through xml

I notice that in online tutorials people use specific dp values for width and height of any view
For example, android:layout width ="20dp"
I was wondering since we have so many devices and densities would it be better to determine this value programmatically?
For example I want a specific image to occupy 20% of the screen width then I would get the screen width and multiply by 20% and set width accordingly
U know dp is supposed to make it equal size on any screen no matter what density is but this not the case for many devices and example is galaxy s2 and galaxy note
Can you please enlighten me of my ways are correct?
the better way to do it is to use linear layout in your xmls and set layout_weight in it children with the value you want. You can use weight_sum in the linear layout to set the max weight too.
e.g
linear weight_sum = 100 and a textview inside with layout_weight = 20. it means your textview has 20% of the value of the linear.
p.s: for horizontal orientarion, weight = width and width = 0dp
for vertical, weight = height and height = 0
I hope to help you ^^
For anything that dp doesn't adequately compensate for, you can insert images of different resolutions into your alternate draw able folders. They're broadly named for the different screen sizes your app will come in contact with and android will adjust accordingly by itself. In my experience, I try to do as much graphics as I can by xml as I find it far less cumbersome.

how to create android UI so that it does not change with screen resolution?

I have an android app with a background image,which I would deploy on emulator and see its behavior.
Initially I was using emulator with skin WVGA800.In the UI all the textView declared in the layout were properly placed.
But when I change the emulator skin to 500x600 then in UI few of the textView are misplaced.
So could anyone please suggest how to handle this.
The good, but difficult thing about Android is that you don't know what size or aspect ratio the device your code runs on will be. If you want full-screen images, you need to be willing to have them scaled (proportionally or disproportionally) or cropped.
If you're trying to position text in a particular place on a background image, you're going to have a bad time. I've tried this before and quickly changed my mind.
Here's my suggestion: Read the screens support docs (as #abhilasha said) and use adaptive UI elements that scale nicely (like 9-patch images, relative layouts, etc).
Then if you want to place text in an image, instead of trying to place a TextView over an ImageView, set the image you want as the background of the TextView. Then you know where the text will be relative to the image.
My options:
1)for simple ui,i think u can use many attars with view,the following;
android:layout_centerHrizontal
android:layout_centerVertical
android:layout_centerInparent
android:layout_alignParentBottom
android:layout_alignParentLeft
android:layout_alignParentRight
android:layout_alignParentTop
android:layout_alignWithParentIfMissing
android:layout_below
android:layout_above
android:layout_toLeftOf
android:layout_toRightOf
android:layout_alignTop
android:layout_alignLeft
android:layout_alignBottom
android:layout_alignRight
android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop
that is helpful to use layout or view attars to set its position.
2)u can use some layouts xml to fit it.like that
if(screen width < 480)
setcontentview(r.layout.for480lay)
else if(screen width > 480&&screen width <640)
setcontentview(r.layout.for640lay)
..
..

What is the alternative to AbsoluteLayout in Android?

I see posts saying that FrameLayout is the alternative, and that I should use margins to position things (this strikes me as wildly counter intuitive, but ok... if it works, I'll take it). However, I can't get it to work, so, I'm looking for assistance.
here's my code
FrameLayout layout = new FrameLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
Button btn = new Button(this);
btn.setBackgroundResource(R.drawable.btn);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(100,100,100,100);
btn.setLayoutParams(lp); //i've tried with and without this line, no change
layout.addView(btn , lp);
The button is drawn at 0,0 no matter what I do. When I change the lp's LayoutParam to FILL_PARENT the button is then stretched to take up the entire screen (which makes sense).
HOW do you get it to draw somewhere else on the screen, irrespective of what else is there?
As always, super grateful in advance.
[EDIT]
It seems my question isn't entirely clear (given the answers) so...
In the code above, the intent is to create a button, pass it to a layout and have it draw at 100,100 on the screen.
I'm aware of the fact that this may mean different things on different devices. I'm ok with that. I simply need a way to, programatically, and at run time, place an item at a SPECIFIC location. I don't want to rely on gravity (or the laws of thermodynamics). I just want to specify a location and have the element appear there :)
As many have pointed out, setting a button at an absolute pixel position on the screen is a really bad idea, and will never work across all of the available android phones. I'm sure there is a better way to achieve the layout you want.
However, to answer the question as asked: to position it at runtime you can use the AbsoluteLayout.LayoutParms.
AbsoluteLayout absoluteLayout = //get absolute layout
Button button = new Button();
AbsoluteLayout.LayoutParms params = absoluteLayout.generateDefaultLayoutParams();
params.x = 100;
params.y = 100;
absoluteLayout.addView(button, params);
A good explanation of the different available layouts is available at
http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts
As I do not completely understand from your description what you're trying to accomplish (do you want to overlay stuff?) maybe the visual examples there can help you in understanding how the layouts work and how to position stuff on them.
What exactly is your final objective with the layout? If all you need is a button -- say, in the center of the screen, you can just make a layout with:
<RelativeLayout
android:xmlns="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<Button
android:id="#+id/the_button"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:text="#string/the_button_text"
/>
</RelativeLayout>
AbsoluteLayout is a bad idea because of the large number of screen resolutions you need to support. 100 pixels over on one screen may be halfway, while it may be less than a quarter across the screen on a higher dpi device.

Alternative to AbsoluteLayout in Android?

If AbsoluteLayout is deprecated what can I use instead of it?
I've done an app that uses AbsoluteLayout but it doesn't work well with the different screen resolutions. I use because I can set the X and Y position of a button. Can I set the position of a button using another layout?
you can use RelativeLayouts as described here: Set the absolute position of a view
Use RelativeLayout and set left and right margins like
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.leftMargin = x;
lp.topMargin = y;
Consider using FrameLayout. If you set your child gravity to TOP|LEFT then you can use leftMargin and topMargin as the positions. As a bonus you get a few other useful positioning mechanisms by changing the gravity.
I will suggest you guys if you want to have full control over the positions of your views on the screen just to extend ViewGroup and make your own implementation of AbsoluteLayout. The easiest solution will be to use one of the existing Layouts and play with margins, paddings, gravity and so on, but the control is not gonna be so powerful and can cost some problems on the diff device screens.
I would just use a relative layout and set it based off of the other items/edges of the screen. Otherwise your layout appears different on different devices.

Categories

Resources