I am still somehow new to Android development especially the UI. So my question is about whether What I am doing is right or wrong. Mainly my applications have simple UI but the most important thing is that they must support multiple screen sizes. So I develop all my UIs using pure java not xml because in java I can get the screen size that the application is work on right now and then from this info I can make the buttons and all other elements size and position function of the screen size like:
AnyBtn = new Button(this);
FrameLayout.LayoutParams AnyBtnparams = new FrameLayout.LayoutParams(200 * width / SCREEN_WIDTH, 90 * height / SCREEN_HEIGHT, Gravity.BOTTOM | Gravity.LEFT);
-
Did you consider using weights? Usually it works pretty well for me and our UIs are quite complicated.m0skit0– m0skit02017年05月24日 13:52:49 +00:00Commented May 24, 2017 at 13:52
4 Answers 4
https://developer.android.com/training/multiscreen/screensizes.html
Android developer's site has some good information on this topic. Above link might help you.
1 Comment
Recently I have been through this I need to dimension with different font sizes to support all multiple devices,
But later I found a library which does all these works
just import this library in your gradle, it will allocate the sizes for multiple devices
compile 'com.intuit.sdp:sdp-android:1.0.3'
hope this helps :)
Comments
I think thats not a good way to start. Xml was made part of android development because its beats java code in performance.if you look at your apk size, Java code has 80% of the total size. So take your time but learn properly.
Comments
Although making your UI from code isn't a bad idea, code maintenance becomes very difficult.
Luckily, the Android Support Library team has provided us various API's that ensure our UI adapts to all screen densities and sizes.
You can use VectorDrawableCompat and AnimatedVectorDrawableCompat, by creating vectors that scale according to the device. Have a look here.
You could create 9 patch images in Android Studio or an external tool for simpler resources. You could also target different densities with density specific layouts and resources.
PercentRelativeLayout lets you control view dimensions with percentages in XML or Java. ConstraintLayout let's you scale according to the screen's dimensions with scaling ratio control.