I start more or less the development of android. I needed to create different layouts celon the android versions but now I would like to move a button. How to move this button on all the corresponding layouts whatever version of android at one time?
If there is already a post on it, sorry but I have not found it.
It's for the same screen size of course
Example: I have a linear layout of which I want to modify the right margin and I want this to do on the linear layout for versions <21 AND >21 by only doing it once and not two
Thanks !
If you have two layout files:
res/layout-v11/layout.xml AND
res/layout/layout.xml
You'll need to change the margin values in both files if they are hard coded.
Otherwise, you can have:
<LinearLayout
...
android:layout_marginEnd="#dimen/left_margin" >
and in res/values/dimens.xml have
<dimen name="left_margin">16dp</dimen>
and just change the value once in the dimens.xml file.
Related
The old Android Studio used to create the layout files using RelativeLayout, but the new one use ConstraintLayout, and this let me very pissed off >:[ , cause it have to import and sync... I really do not like.
I do not used to use both (RelativeLayout or ConstraintLayout), I prefer LinearLayout vertical, but I did not find how I can change the layout template in order to Android Studio create a layout using LinearLayout vertical, Anyone knows?
I looked for in Preferences -> File and Code Templates however I didn't find where I can change the xml layout template...
Thx
I'm not sure if you can do that, so just replace relative with linear in the xml & don't forget to add to add the orientation
Just delete the content in the layout file, and use the linear layout in that file
I know that we can have different layout files for supporting different screen sizes in Android.
Does anyone know if there is an option to change all other layout files when I make changes to the original layout file? For example, say I have a layout file - main.xml under layout, layout-large, layout-sw600dp and layout-sw720dp directories. If I make some changes to the main.xml in the layout directory, is there any setting which would automatically make that change in the other layout directories as well?
For the formatting capabilities I use an answer for this. This refers to the comment above and elaborates on Aleksey's answer.
<include
android:id="#+id/some_id_if_you_nee_one"
layout="#layout/some_other_xml_file"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
You may add any additional (orientation or resolution specific) formatting like below the layout= statment.
Everything that is not specific to the current resolution/orientation and common for all resolutions/oriantations should go into some_other_xml_file.xml
This works for all full views and subclasses of view. If you want something similar only for groups of style attributes then you can start a styles.xml and refer to the styles with style="..." statements.
The answer is no. And i'm not sure there is such tool. Exceptions: naming of params (strings, drawables, etc.).
Some hint: compose layouts from small parts, that inserted using
<include />.
So when you change small parts – all layout changes (not sure with different sw-*** layouts, but in one folder it works).
To support different resolutions, we need to make variations of layout files as described in Supporting Multiple Screens very well. Assuming you don't plan to show different arrangement of your UI but just want to stretch appropriately, your variations would be mostly about different weights. At least that is the case with my app so far.
Now, how do you manage changing the application with this structure? Since it repeats the layout many times, one layout change in your application causes multiple files change.
I thought of two options:
Changing the values dynamically in your code
Downside is your layout related work is spilled over to the code. I really don't like this.
Making child layout to extract common layout elements
Downside is your layout's hierarchy will be deeper and cluttered so it would be harder to figure out what's going on. Still, this is better than option #1 thanks to Hierarchy Viewer. I am not sure if this approach will be always achievable.
If you could share your tricks to get through this, it would be much appreciated.
I think I found a solution. I will accept it as an answer if others give thumbs up.
I found Configuration qualifiers described in Supporting Multiple Screens works not only for res/drawable and res/layout but also for res/values. So on my layout/some_layout.xml, I say like this:
<ImageButton
android:id="#+id/imagePlay"
android:layout_width="#dimen/button_size"
android:layout_height="#dimen/button_size"
android:scaleType="fitCenter"
android:src="#drawable/play" />
Then on values/layout.xml file you define the default button_size:
<resources>
<dimen name="button_size">44dp</dimen>
</resources>
And on values-xlarge/layout.xml file you define the xlarge mode button_size:
<resources>
<dimen name="button_size">66dp</dimen>
</resources>
I didn't try other values resources, but I assume it also works for Styles and Themes so in case your layout customization is a little bit more than a size or weight, you could define a style in the values and use it.
Let's say I just added two standard widgets -- CheckBox and CheckedTextView. The first one has nice, clear padding despite that fact I didn't set any, and there is no padding set in .xml file. The other one comes without any padding.
Now, I could get the value (fixed) of the padding from CheckBox by trial&error. But my question is how to set it in kind of dynamic fashion -- i.e. if in Android 7.0 padding for CheckBox will be "20sp" and I set "10sp" (because it is now 10sp -- I am making this up) then my two widgets would be with different paddings.
And I would like to have a consistent padding. So how to set something like "?android/default_padding" for padding?
Clarification: I am interested in using the system default padding, not hardcoding the same value made up by me over all widgets.
I looked it up for you, in API Level 10 (and also on every other Android platform) Android uses 9-Patch images with prefdefined paddings (there is no padding declared in the Selector), e.g. I mesured the checkbox and it as this pasddings: left, right: 6dp; top, bottom: 12dp. And the default button has a padding of 10dp; so there is no default padding as far as I can tell. But 10dp is good in most cases. Also, it just really depends on the screensize of your device. You will have to declare your own prefered padding like Daniel suggested. E.g: In your Values file 10dp and in values-large maybe 15.
Edit:
Here is the default checkbox for mdpi on Android 2.3.3:
You could create in your resources an xml file called dimens.xml, and the add something like this:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<dimen name="default_padding">10dp</dimen>
</resources>
Then you call from your layout something like this:
android:padding="#dimen/default_padding"
I think this is a consistent way of working :)
Good Luck!!!
i have three button which is arranged in table row .I gave background for the three button but it wraps the button .
For starters read my blog :-)
Now my suggestion is to use background definitions with gradients. They look nice and are simpler to create then background images. As I said in the blog you need three of them for the button to work as expected. I have a demo for you here: button_type_0.xml
You will also need to define the colours: colors.xml
and dimensions: dimens.xml
you might also want to consider different dimensions for various dpi values. for example I use half size corners and border for ldpi: ldpi/dimens.xml
Looks all very complicated at the beginning but it is worth it. In the end it will look like this:
declare new a new xml in the drawable folder with the image/color you can specify image for each event state.
and you can you can set this xml as you background
if your xml is 'res/drawable/abc.xml' then set background as
android:background="#drawable/abc"
You can also use ImageButton
<ImageButton
----------------------
android:src="#drawable/youimage"
/>
The benefit for ImageButton is that, no need of different image/color for highlight.