button not exactly centered in it's parent - android-layout

I am surprised, i did not notice this during my 6 months of development time on Android. I have a realtivelayout whose bottom is aligned to the bottom of the screen/parent. I wanted to place a button at the center of this relativelayout. I notice that the button is not exactly centered vertically. There appears to be couple of pixels between the bottom of the button and that of the relativeLayout. Here's the piece of relevant layout code:
<RelativeLayout
android:id="#+id/some_layout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:layout_alignParentBottom="true"
android:background="#888">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ping">
</Button>
</RelativeLayout>
Any ideas? My targetSDK is 2.2 and above. Thanks in advance.

Related

When I switch to RelativeLayout, everything I put in the designs shows up in the top left corner of screen

I switched from ConstraintLayout to RelativeLayout in Android studio, now everything I put in the design shows up in the top left for some reason.
I can drag and re-position it in Design, but writing padding is very time consuming and tiresome. I even changed the default to RelativeLayout but nothing changes.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
I want to re-position them by dragging them on screen without them being stucked in the top left corner of the screen.
Yes, this is the property of RelativeLayout.
RelativeLayout needs you to tell the position relationship between widgets. If you don't specify the position relationship for all widgets, then all widgets will collapse to the (0, 0) of parent, which results in what you see,
everything I put in the design shows up in the top left.
To specify the relationship between widgets, you may use android:layout_* series property, for example:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" /> <!-- this will at top left -->
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/textView"
android:text="Button" /> <!-- this will at right of textView -->
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toBottomOf="#id/button"
android:text="TextView" /> <!-- this will below button -->
You may try to specify them in Design Layout, however I found it is a little difficult.
Click a widget, then there will be circles around this widget.
Click on one circle, hold on. All widgets will have circles around.
Drag this circle to the circles of one other widget, this will make a connection between them, if you are using ConstraintLayout.
However, it seems impossible to do so if you are using RelativeLayout, since all widgets are collapsed to the same position.
See Android Guide to RelativeLayout for more details.

How to place an imageview on the top in android Studio?

As it's shown in the android studio it looks perfectly fine but whenever i run the app on my cell phone it looks like this any idea ,please ?? the app on the phone and the android studio
You are using Relative Layout.
First thing I noticed in your code is that you used margin buttom in Image view which is top of the screen already.So I suggest to not use Margin Buttom in that because relative layout bydefault keep that image on top .So i give you some idea to fix it.
1.)Image is on top so and its Relative layout.
<ImageView
android:id="#id/image" (give any id here)
android:src=""
android:marginleft="" ("Add ur dimensions")
android:marginright=""
android:adjustviewbound="True"/>
2.)Next is Textview.
<TextView
android:id="#id/text"
android:text="Name:Lovely Hamester"
android:layout_below="#+id/image"
android:margintop=""/>
So the main thing i wanna point of is this "Id".Relative Layout directly cant put views line by line like Linear Layout,We need to give command to the view to where it need to place.So thats why we use "layout_below" which instruct Text view to stay after imageview.There are different commands also.Please check that,like "layout_rightof","layout_leftof".
Just give id to every view and command them in Relative layout.Your other code is perfect.Dont use Margin Bottom , because it kept space from bottom which looks perfect in the preview but every Mobile have different size so thats why its not shows perfect in your screen.
just move code imageview to the top
example
<RelativeLayout
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"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<ImageView
android:layout_width="200dp"
android:layout_height="600dp"
android:src="#drawable/test"/>
<ProgressBar
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/progressbar"
android:layout_centerInParent="true"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_notes"
android:layout_width="250dp"
android:layout_height="match_parent"
tools:listitem="#layout/item_note"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
android:src="#drawable/button"
app:borderWidth="1dp"
app:elevation="9dp"
android:backgroundTint="#FFFFFF"/>
</RelativeLayout>
The problem as i see it that you have a margin bottom override the layout align Parent Top so if you delete that and leave only :
android:layout_alignParentTop="true"
also it is redundant to call align right and left and bottom , align top will be enough.
i'm sure it will work.

how to center an image horizontal and vertical inside an edit text in android xml

Something like this please.
I'm new to android, and I am trying to center the image and hint both vertical and horizontal. If anyone can help, I will really appreciate it.
You can't put ImageView inside of an EditText.
You can, instead, put EditView and TextView inside RelativeLayout and center them, like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/my_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:text="This is some edit text" />
<ImageView
android:id="#+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/com_facebook_button_icon_blue" />
</RelativeLayout>
Since we put EditText width and height to match RelativeLayout, it would appear as if the ImageView is inside of an EditText.
If you want to put text under image just add this line to TextView: android:layout_below="#+id/my_image_view".

Want to mix layout_centerHorizontal with layout_toLeftOf

I am struggling with a specific layout requirement.
I want a TextView that it centred. It is single-line and uses ellipsis if the text is too long. On the same line, to the right of it, I want a small ImageView. The presence of the ImageView must not affect what the TextView thinks is the horizontal centre of the layout. So, although the text might be centred, there is actually less space available to the right of the centre line than to its left.
Within a RelativeLayout, the sort-of intuitive thing to use is:
<ImageView
android:id="#+id/image_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/text_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/image_view_id"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:ellipsize="middle" />
but this does not work because you cannot have both android:layout_toLeftOf and android:layout_centerHorizontal. It behaves as if the android:layout_centerHorizontal was not there.
I have also tried using a LinearLayout with android:layout_gravity without success. Is it possible?
It turns out that something close to what I want to achieve is very simple:
<ImageView
android:id="#+id/image_view_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
<TextView
android:id="#+id/text_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/image_view_id"
android:layout_marginLeft="#dimen/size_of_image_view"
android:singleLine="true"
android:gravity="center"
android:ellipsize="middle" />
Using android:gravity="center" with android:layout_width="match_parent" centres the text in the available space. It is not perfect as the marginLeft is there to keep the text centred within the full layout, thus robbing a little space from its left end but it is good enough for my purposes.

EditText in Layout not full width

I have problem with xml. Why my EditText doesn't show for full screen width? It showing about 80% of screen width, but not all..
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#color/colorGray"
android:layout_weight=".35" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal|vertical" >
<LinearLayout
android:id="#+id/rightCont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test" />
</LinearLayout>
</HorizontalScrollView>
</ScrollView>
Because the EditText is wrapped in a ScrollView wich takes 0.35 weight of the screen. So it will expand until it reaches the boundaries of its parent ViewGroup.
To test this, change the weight of ScrollView to 1:
android:layout_weight="1"
beside what Andy Res said, a scrollview in a scrollview is not recommended. newer android versions can handle that but older one will have trouble determining which scrollview should scroll on touch events.
Also a LinearLayout with wrap_content around the EditText with fill_parent should result in a pure wrap_content behavior of the EditText, too.
You should rethink your layout.

Resources