Android Buttons have an undesired margin - android

I wrote a test application to describe my problem better. I started a new Android project with this activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jannevers.myapplication.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Now the first problem is, the button does not really fill the parent, it has a margin (see screenshot):
I can work around this, by making the button bigger than it should be.
But then the second problem is, that the margin is the same on the most devices, but not all. E.G. at a Samsung Galaxy S6 Edge, there is almost no margin (see screenshot):
This is really weird, anyone know a solution?

Use style="?android:attr/borderlessButtonStyle"
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
style="?android:attr/borderlessButtonStyle"/>

Check if into your style.xml there is a rule about layout margin like this:
<item name="android:layout_margin">10dp</item>

Related

'TextView' gets cropped on a Samsung Galaxy Tab

I have this simple 'XML' layout. I have tested in various devices and emulators and works fine. But on Galaxy Tab Samsung 9.6inch (1280x800) 157 dpi, text customer gets cropped in the end & becomes "custom".
I have read that i should declare dimens xml for various densities but i thought using DP is what makes your layout look the same in all devices. Am i wrong?
Also this tablet is a ldpi and x-large screen. I have tested it in other similar devices with similar specs and works well. How should i make this work for this device?
NOTE I want specifically 100dp to match others texts in width
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/labelSelGateName"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_paddingLeft="16dp"
android:text="customer" />
</LinearLayout>
There could be the issue of TextSize in that device. Have you tried playing with the text size?
Is this layout is in any other layout?
Its happening because u have used padding on whole side.just set padding only top and bottom as u have only fixed the width.then it should be worked
to support padding just follow below-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/labelSelGateName"
android:layout_width="132dp"
android:layout_height="wrap_content"
android:layout_paddingLeft="16dp"
android:layout_paddingRight="16dp"
android:text="customer" />
</LinearLayout>

TextView is too small in tablet

I've already created the layout-sw720dp but I don't know if I have to change all of the sizes of those Layouts,I mean I have layout-sw330dp, layout-sw480dp, and so on, I have to change the size of TextView, Button, and all of the stuff on the layout?
Is there any option to make it dynamic instead of changing it manually?
Edit
My problem is when I run my app on a Nexus5 for instance it looks good, all size is correct, but when I try to run my app on Nexus9 all of the widgets are smalls (buttons, textviews, etc...)
The recent update to TextViews in support library v26 was built for just this purpose:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="100sp"
app:autoSizeStepGranularity="2sp" />
</LinearLayout>

Android background not correctly applied on first instance

I have a very simple layout, simply two buttons below each other. On both buttons I set a drawable layer-list as background, containing the selectableItemBackground causing a ripple effect on the buttons.
A weird bug occurs: on the first button, the ripple effect doesn't happen, but on the second button it does. How can this be explained, or can it be a bug in Android/Support Libraries?
Setting clickhandlers doesn't change anything, the behaviour stays the same.
See example gif below, and XML code below that.
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/first_button"
android:text="First Button"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="12dp"
android:background="#drawable/white_selectable_button"/>
<Button
android:id="#+id/second_button"
android:text="Second Button"
android:layout_width="match_parent"
android:layout_margin="12dp"
android:layout_height="48dp"
android:background="#drawable/white_selectable_button"/>
</LinearLayout>
white_selectable_button.xml (in res/drawable):
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/white"/>
<item android:drawable="?attr/selectableItemBackground"/>
</layer-list>
It appears that this drawable is applied to the button, since when setting the color to red, the button actually appears red. Only the selectableItemBackground ripple isn't applied.
I've tested this with design support libraries 24.1.1, 23.4.0 and 23.2.0, on all versions, this doesn't change anything.
EDIT: Filed bug report to Android bug tracker: https://code.google.com/p/android/issues/detail?id=219620
Took your code and confirmed it also does not work on my device. I submitted this as a bug against the Android Support Library. So eventually it will get fixed.
In the meantime, I figured out an easy workaround to fix the bug. All you need to do is add an invisible dummy Button before the other two as it appears this bug only affects the first button. You can eventually remove this once its fixed by Google.
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/white_selectable_button"
android:visibility="gone" />

Android: Strange gap between buttons

I am just beginning with Android. I am making a layout with buttons, but a very strange thing is happening — there is a small gap between them. Setting margin and padding to 0 is also not helping.
Here is my simple layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
It is looking like this:
Make your own flat Button style with ripple effect [lollipop] and without it [pre-lollipop]:
https://gist.github.com/dmytrodanylyk/3513e42839ae4309d2b5
You have to use android:background to change the background of button. For example if you use android:background = "#FFFFFF" your visual problem is fixed. In reality , there is no space between the buttons, their background creates an "illusion"

Gap appearing in Android Layout

I've got a weird gap in my Android layout. I've set the background colors of my views and layouts so that you can see what is where. The gray box in the middle is the gap. I've got a RelativeLayout overall (white background), with a toolbar at the top (seen in pink), then a LinearLayout which is everything else (seen in yellow). In that yellow layout I've got two children: detail information (seen in blue), and a ListView (seen in red). However, the gray area in the middle... I have no idea where that is coming from.
Here is my layout file (with some info clipped for readability):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
>
<RelativeLayout
android:id="#+id/toolbar"
android:layout_width="fill_parent"
android:layout_height="44dp"
android:background="#drawable/toolbar_gradient"
android:layout_alignParentTop="true"
android:background="#f0f"
>
<Button style="#style/Button.Done" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/toolbar"
android:paddingTop="4dp"
android:weightSum="10"
android:orientation="vertical"
android:background="#ff0"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="3.5"
android:weightSum="9"
android:orientation="vertical"
android:background="#00f"
>
</LinearLayout>
<ListView
android:id="#+id/tableView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="6.5"
android:background="#f00" >
</ListView>
</LinearLayout>
</RelativeLayout>
What is happening here? What am I missing?
EDIT: It appears the layout xml may not be the problem. I've been working to reduce and reduce and reduce and find the real culprit. It's looking like my problem exists in the ListAdapter code. Will update more as I discover it.
Did you try removing the android:layout_alignParentBottom="true" line from the definition of your first LinearLayout?
Ok, so after what felt like endless iterations of removing things until it was fixed and adding things back in until it broke again to narrow down to the specific problem I have discovered my error. I narrowed the problem down to my styling on the layout of the cells inside the ListView above.
I created a script awhile back to help me generate many different style.xml copies for different layout sizes and such so that I can provide good font size or layout_height values for different devices and make them look right. However, due to an error in my styles template, the style.xml which was being generated with a line <item name="android:layout_height">dp</item>. The missing number before the dp caused whacky behavior.
sigh Well, THAT sucked. ;)

Categories

Resources