Android Padding not affecting layout - java

OK, Im starting to get the hang of how XML operates with the Android interface, but I've ran into a problem...
I just have a HorizontalScrollView with a RelativeLayout inside to which I will place other views later. The HorizontalScrollView as you can see is sitting above a button, and below another button. I like where its placed, but I dont want it exactly right above and below the buttons. I would like some spacing (padding). Ive tried adding padding but it seems to have no effect...
Heres my attempted XML:
<HorizontalScrollView
android:id="#+id/scrollview_xyzInfo"
android:layout_width="match_parent"
android:layout_height="130dp"
android:layout_above="#+id/button_refresh"
android:layout_alignParentRight="true"
android:layout_below="#+id/button_mySettings"
android:paddingTop="20dp"
android:paddingBottom="20dp"
android:background="#drawable/refresh_selector" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</RelativeLayout>
</HorizontalScrollView>
Any help would be much appreciated :-)

Nevermind! Use margins when seperating two views like so:
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
Answer Found at:
Padding not working Android

Padding and margin works only with LinearLayout. Which layout are you using?

Related

Android Studio - Layout Elements differ in size on the physical device, compared to the preview and emulator

I'm relativly new to android. While designing my layout I stumbled over an problem I can't solve and which doesn't seem to be answered anywhere. Or maybe I don't really know what I have to search for. Anyways the problem lies in the way the layout is previewed and shown in the emulator in contrast to the actual device. As seen in the pictures below the layout elements and the spacing are off. Everything is much bigger, closer together and further down aw I would expect. For the preview and the emulator im using the specs that are suitable for the pyhsical test device (One Plus 8 Pro).
In other questions I've seen answers say, that it might be a problem that I hardcode some dimensions using dp since the display has a high dpi (xhdpi to be exact) but even using only constraints or using the px the preview and emulator always differ from the app on the actual device.
Following are the pictures and the xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".EntryActivity">
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity=""
android:layout_marginTop="416px"
android:gravity="center_horizontal"
android:orientation="vertical"
android:text="#string/header_text"
android:textAlignment="center"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/buttonPlay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="336dp"
android:adjustViewBounds="true"
android:backgroundTint="#00FFFFFF"
android:onClick="changeActivity"
android:scaleType="fitCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:srcCompat="#drawable/button_play_02" />
<ImageButton
android:id="#+id/buttonCreate"
android:layout_width="156dp"
android:layout_height="wrap_content"
android:layout_marginTop="340dp"
android:layout_marginEnd="20dp"
android:adjustViewBounds="true"
android:backgroundTint="#00FFFFFF"
android:onClick="changeActivity"
android:scaleType="fitCenter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.993"
app:layout_constraintStart_toEndOf="#+id/buttonPlay"
app:layout_constraintTop_toBottomOf="#+id/textView"
app:srcCompat="#drawable/button_create" />
</androidx.constraintlayout.widget.ConstraintLayout>
Preview in Android Studio
Emulator
Physical Device
I think you are doing a lot of rookie mistakes regarding Constraint layout.
As you said you are a beginner, I'd suggest you try to use relative layout.
If you want to stick to constraint layout, here's a quick tutorial which would make your basics strong for constraint layout:
Youtube:Code in Flow playlist for Constraint Layout
Quick tip:
If you are focusing on getting constraints right, try to keep the
layout(height and width) of buttons identical.
for one of the buttons, you've used the code chunk:
android:layout_width="0dp" android:layout_height="wrap_content"
and for other button you've used:
android:layout_width="156dp" android:layout_height="wrap_content"
very important;
consider chaining the items hard. Don't worry! it's very minor thing. you'll get things alright once you go through the link.

Positioning icon half above layout

Recently, designers in my company liked creating half-above elements in layouts for Android apps. I've been struggling for a while trying to make these elements behave as good as possible, but I'm already tired of it. Is there any way of positioning views like on this image
with an assumption that if user touch row/card it became "checked" and this black dot icon became visible (second click makes it invisible of course).
You can do something like this:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toEndOf="#+id/circle_view"
android:background="#android:color/holo_blue_bright">
<android.support.v7.widget.CardView
android:id="#+id/second_view"
android:layout_width="100dp"
android:layout_height="match_parent"/>
</FrameLayout
<View
android:id="#+id/circle_view"
android:layout_width="16dp"
android:layout_height="16dp"
android:background="#android:color/holo_red_dark"
android:layout_marginEnd="-8dp"
android:layout_centerVertical="true"/>
</RelativeLayout>
second_view is endOf circle_view so if circle_view is gone second_view will align to startOf parent.
circle_view should have marginEnd set to negative value of its width divided by two

Android Studio: Graphics are cut

I have the Views attached to each other in the following order:
Activity->RelativeLayout->RelativeLayout->Text
When I change the Y of one of the Text attached to the inner RelativeLayout, parts of it are cut.
Here's a screenshot:
http://s28.postimg.org/ypmvrjz25/image.png
I'm coding purely in Java, no XML.
What's going on?
It looks like one layout is overlapping on another because the menu bar is pushing it down. If you just want the text centered I would suggest setting that in the parameters like so.
android:gravity="center"
This places the text in the center if that is what you want.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/textView12"
android:layout_gravity="center"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
As #Jonathan Whalen suggested you probably have some overlap, I suggest you to change your relative layouts background color to see wich one is overlapping the text. Also try to not use fixed height for the layouts but use wrap_content instead, as fixed dims are frequently the cause of layouts overlap.

Android placing multiple bitmaps in imageview

I have multiple ImageViews which are transparent and are meant to over lap one another. I am aligning then to one another but one problem presist. How can I set the order in which they are layered? Ive tried revising the code in both xml and how the images are set in the actual java code...
I have tried FrameLayout but maybe Im not using it right...here is my implementation:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".RadarActivity" >
<ImageView
android:id="#+id/imageview_topo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:scaleType="center"
android:visibility="invisible" />
<ImageView
android:id="#+id/imageview_counties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:scaleType="center"
android:visibility="invisible" />
No matter what I do...imageview_topo is always placed on top. Even in the code i have instantated topo first and retrieved its image first!
Thank you!
I referred a site here that says:
If multiple child views exist, then they are drawn, in order, one atop
the other. This means that the first view added to the frame layout
will display on the bottom of the stack, and the last view added will
display on top.
You can check this site too: http://blog.neteril.org/blog/2013/10/10/framelayout-your-best-ui-friend/
May be i can help you more if you show the screenshot of your UI.

How can I get a ListView and TextView to scroll as one unit when content is taller than the screen height?

I have a main menu screen with a simple ListView that contains "links" to further screens in my app (Browse, Bookmarks, Settings, About, etc.). Underneath the ListView there is a TextView (more accurately, a TextSwitcher that rotates TextViews) that changes every 10 seconds to display a new "tip".
In portrait mode, this works fine. There are my five list items in the ListView , and my tip label underneath. However, when I switch to landscape mode, the ListView is taller than the screen. The ListView scrolls normally, but I cannot scroll past the end of the ListView to see the TextView underneath.
I have tried every possible combination of Layouts, wrappers, ScrollViews, and layout_height parameters and I simply cannot get it to behave.
Here is the simplest code I can use to get the result pictured above:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout android:id="#+id/ListLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<ListView android:id="#id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1">
</ListView>
</LinearLayout>
<LinearLayout android:id="#+id/TipLayout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_below="#+id/ListLayout">
<TextSwitcher android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/TipSwitcher">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="7pt"
android:id="#+id/Tip1TextView" android:text="Tip: Hello, Android!"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tip: This is the second TextView in the TipSwitcher!"
android:id="#+id/Tip2TextView" android:textSize="7pt"></TextView>
</TextSwitcher>
</LinearLayout>
</RelativeLayout>
Like I've said, I've already tried so many different combinations that I can't list them, and for the most part I was randomly inserting XML in an attempt to get something to work the way I wanted. So I'd greatly appreciate suggestions as to how I would go about doing this the right way.
Thanks.
EDIT: Something I forgot to mention, this may or may not be relevant. My MainMenuActivity is extending ListActivity. According to the docs, "ListActivity has a default layout that consists of a single, full-screen list in the center of the screen." But, "If you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate()." So I don't think the ListActivity is interfering.
Put the TextSwitcher in the ListView itself. You can use addFooterView() for this.

Categories