How to set default Android padding to an element without padding? - android

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!!!

Related

Removing icon from BottomNavigationView

I want my menu items on the BottomNavigationBar to have text-only labels with no icon. Unfortunately, it looks like design_bottom_navigation_item.xml always has a 24x24dp space reserved for the icon, with no publicly-exposed way to set it to gone. And even after getting past that, the label layout is set to a layout_gravity of bottom|center_horizontal, and it doesn't look like there's a way to programatically set layout_gravity to centered.
What is the fastest, easiest way to achieve the goal of a text-only menu item on the bottom nav bar? I'm thinking I can do this by creating a custom version of the item layout, then subclassing BottomNavigationItemView, BottomNavigationMenuView, and BottomNavigationView to specify that custom layout... but that seems like an awful lot of work for one little change. Am I missing something simpler?
dont put iandroid:icon property to your item
Just use the code below. I tried hard to do this easy way but failed.Then I made an alternative. Just use transparent color instead of icon drawble
<item
android:icon="#android:color/transparent"
android:id="#+id/navigation_id"
android:title="title" />
Add this in your dimens file. Then you can add padding according to your navigation view size.
<dimen name="design_bottom_navigation_height"tools:override="true">30dp</dimen>

How to synchronize layout changes?

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.

android : style.xml or dimens.xml or what else? confusing

Now, am trying to be a good Android developer and put all my textsize and other size specifications, and all the styles (like, say, button color, etc) in XML files. Am trying to read up on these. In a Stackoverflow thread, I read about dimens.xml. So I created a dimens.xml under each values- directory (like values-ldpi, values-mdpi, blah blah) and put one dimens.xml in each. It works fine. Now, I want to put some style information in the XML, like, a button color. Should I put the attribute in the same file i.e. dimens.xml? I read in one thread that dimens.xml is only for dimensions, and not for style related info. In another thread, I read that the name of the XML doesn't matter. Now, what is a good practice? Keep style.xml and dimens.xml separate ? Or put everything in one xml?
And if, say, for a textview, I want to have both style (like color), and also textsize information in XML, then should I (1) put both color & textSize in styles.xml, or (2) put color in styles.xml and textSize in dimens.xml?
Keep style.xml and dimens.xml separate ?
As soon as style.xml already exists for reasons like these (button colors, text color, themes, etc.) then I believe that you should keep them separate.
And if, say, for a textview, I want to have both style (like color), and also textsize information in XML, then should I (1) put both color & textSize in styles.xml, or (2) put color in styles.xml and textSize in dimens.xml?
I would do the (2). (Although I can't blame anyone who would do the (1).). As far as I know there's no best practice guidance on that. I do it the (2) way basically because I see dimen.xml as a place to host pixel-related values. But this is my preference.

TextView with custom font isn't centered vertically

I need to use a custom font (VAG Rounded, probably not relevant) but the font changes how my TextViews react.
In the image below, you can see the two textviews with a black background. The left one use the custom font, the right one the default system font (Roboto ?). Both of them have the same xml properties and size, but the padding is not the same and more important, the left one isn't centered vartically !
How can I make the TextView draw its content well centered ?
You can try to remove your customs font padding from your text style (styles.xml):
<item name="android:includeFontPadding">false</item>
If this still doesn't work, i would set a general padding in your styles xml.
I think that you have to set android:layout_height="match_parent" and then also android:gravity="center"
then if you post also your source code we can give you more information
I could not change the font, and I wasn't going to edit each character using an editor (I don't even know what I should have done to fix it).
Si I ended up measure the difference with the default font and I added 0.15f * fontSize in the padding top...

how to give background color for button in android

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.

Categories

Resources