In root LinearLayout android:gravity="center" shows padding - android

when I remove gravity attribute then the image will set on top corner right but when it used it leave padding at the top why gravity leaves space ????
enter image description here

android:gravity="center" ---> center_horizontal+center_vertical
The center_vertical attribute leaves the padding at the top. Changing it to center_horizontal will make the view/views just align center horizontally so use
android:gravity="center_horizontal"

I think why should you use layout_marginTop in imageview?it's not necessory to set.you can set layout_gravity at imageview and remove gravity at top linear layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:src="#drawable/android_ic"
android:layout_gravity="center"
/>
</LinearLayout>

When you have added android:gravity="center" your entire layout will be at center of the screen.Since you have added multiple views below the imageView, it has been managed to be at the top with some space.Change it to android:gravity="center_horizontal" and also if the image is small set the height as "wrap_content" instead of 200dp ,it may also have additional space.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:src="#drawable/coffee"
/></LinearLayout>

Related

ImageView out of layout bounds

i have an imageview on a relative layout.
You can see in the screenshot :
How can i move the imageview(the weel) in the bottom-right corner, but display only 25 % part of it, like in the follow picture:
Thanks in advance !
<?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:background="#E0E0E0"
>
<com.androidsources.welcomescreen.MyRecyclerView
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.anupcowkur.wheelmenu.WheelMenu
android:id="#+id/wheelMenu"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/wheel"/>
</RelativeLayout>
You can try something like this:
<com.anupcowkur.wheelmenu.WheelMenu
android:id="#+id/wheelMenu"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:paddingLeft="150dp"
android:paddingTop="150dp"
android:src="#drawable/wheel"/>
alignParentRight and alignParentBottom attributes will move your widget at the right bottom corner. Also try to change padding to achieve desired visual effect.
I know that there is already an answer, but for those who can't make the accepted answer work here is an alternative.
For some reason, I can't place part of the view outside the parent if I'm using RelativeLayout but when I tried LinearLayout it worked. So the solution I used is to enclose the view in a LinearLayout then position the LinearLayout on bottom-end of the parent.
<LinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true">
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/some_pic"
android:layout_marginStart="150dp"
android:layout_marginTop="150dp"/>
</LinearLayout>
Note the width and height of the image is not match_parent, using match_parent will shrink the image not displace it.

Android - How do I make the button taller in a linear layout?

I have a linear layout inside a relative layout, and I want to make the buttons 'taller' to fit the height of the linear layout.
I tried adding padding, as you can see in the screenshot, the layout is 'taller' but the buttons are not. I tried 'fill_parent', 'match_parent' but it didn't make a difference. How do I make the buttons expand in height?
<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"
android:id="#+id/timerLayout">
<Chronometer
android:id="#+id/chronometer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="#string/chronometer"
android:textSize="50sp" />
<LinearLayout
android:id="#+id/buttonLayout"
android:orientation="horizontal"
android:paddingLeft="4.0dp"
android:paddingTop="50.0dp"
android:paddingRight="4.0dp"
android:paddingBottom="4.0dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/btnReset"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:drawableLeft="#drawable/ic_restore_white_24dp"
android:drawableStart="#drawable/ic_restore_white_24dp"
android:text="#string/pause_btn"
android:layout_weight="1.5" />
<Button
android:id="#+id/btnStart"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:text="#string/start_btn"
android:drawableLeft="#android:drawable/ic_media_play"
android:drawableStart="#android:drawable/ic_media_play"
android:layout_weight="1.5" />
<Button
android:id="#+id/btnSave"
android:layout_width="0.0dp"
android:layout_height="fill_parent"
android:text="#string/save_btn"
android:drawableLeft="#drawable/ic_save_white_24dp"
android:drawableStart="#drawable/ic_save_white_24dp"
android:layout_weight="1.0" />
</LinearLayout>
The extra space you are seeing is from the padding values in your linear layout, and your buttons are already filling the parent linear layout. But your linear layout has specified the following.
android:layout_height="wrap_content"
If you want to see buttons with a larger height, change the height value for your linear layout to something else like:
android:layout_height="300dp"
your linearlayout says height:wrap_content
so the child elements will takeup only as much space as specified.
Try giving either minHeight for linearlayout or simply remove linearlayout and in your buttons specifiy "alignparentbottom = true".
For left-most button give alignparentStart = true.
Take a look at your xml layout file very closely
<LinearLayout
android:id="#+id/buttonLayout"
android:orientation="horizontal"
android:paddingLeft="4.0dp"
android:paddingTop="50.0dp" //this guy
android:paddingRight="4.0dp"
android:paddingBottom="4.0dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
the android:paddingTop is the space on the top of every child in your LinearLayout so if you remove it the buttons will fit.

What am I misunderstanding about aligning views at the bottom?

I tried to align my linearlayout at the bottom of my entire layout which is relative, and found the solution here that does it: How to align views at the bottom of the screen?
But why wouldn't this work also?
<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.sandbox.activities.ListActivity">
<ListView
android:id="#+id/my_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/search_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/my_listview"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<EditText
android:id="#+id/my_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter Search Term" />
<Button
android:id="#+id/find_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Find"
android:onClick="findThings"/>
</LinearLayout>
If the listview turns out to be short, like 1 row, "search_footer" is just underneath it, and there is a huge gap below it.
1) "search_footer" is told to align at the bottom of the parent, which is the relativelayout. Since the relativelayout's height is match_parent, and it's the root layout, this means the whole height of the screen, so why wouldn't "search_footer" be at the bottom of the screen no matter the size of the listview?
2) Also, if I change the search_footer's height to match_parent, I expected a really tall button, but it remained unchanged-why?
Add to ListView this attribute
android:layout_above="#id/search_footer"
and remove
android:layout_below="#id/my_listview"
attribute from LinearLayout.
You specified that search_footer align to bottom of parent but also to be below the listview, so in the case that the list is short the search footer just stretches in the space between the listview and the bottom of the screen. you want to specify the footer to be aligned to the bottom of the parent and the listview to be above it:
<ListView
android:id="#+id/my_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/search_footer"
/>
<LinearLayout
android:id="#+id/search_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
When you change the height of the search_footer the buttons doe't change because they are set to android:layout_height="wrap_content". Your layout would normally change when you do this however in this case it didn't change either, due to the same reason as above

Custom Button Class not resizing based on layout_weight android

I have a CircularMeter class which is derived from Button. The problem is that it is not resizing even if the weight given is 0.5 (ie. half the vertical screen).
<LinearLayout
android:id="#+id/WidgetDataLayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="0.5"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
<com.test.CircularMeter
android:id="#+id/cm1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Any Ideas ?
You can take help from this example to make your circularmeter fill half of the vertical screen
eg.
<LinearLayout 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"
android:weightSum="1">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_weight="0.5"
android:background="#123456"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#123456"
android:text="#string/hello_world" />
</LinearLayout>
</LinearLayout>
There's no layout_weight in CircularMeter.
It's parent LinearLayout has both android:layout_alignParentTop="true" and android:layout_weight="0.5" which is not correct since layout_alignParentTop is a layout attribute for a RelativeLayout parent while android:layout_weightis an attribute for a LinearLayout parent. The layout XML does not show what is the parent layout actually is. android:layout_height="0dp" would make the layout invisible unless there really is a vertical LinearLayout as a parent.
Also note that layout_weight="0.5" won't make the view size half the screen. After doing first pass layout for a linear layout, the weight mechanism just distributes any remaining space in proportion of element weights. By default an element's weight is 0. So if you have just one element with non-zero weight, it will get all remaining space in its linear layout parent.

centerHorizontal does not centre the image correctly

Here is the XML:
<?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:background="#drawable/bg"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal" //CENTER 01
>
<!--center button-->
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/img"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" //CENTER 02
/>
//...
</RelativeLayout>
</LinearLayout>
<include android:id="#+id/banner" layout="#layout/banner"
android:layout_weight="1"
android:layout_gravity="bottom"/>
</LinearLayout>
The center button is centered, but not in the real center of the screen, but rather approx 50px to the right of the center. If I remove centerHorizontal CENTER01, then this does not happen.
Does anyone know why is this happening?
Please try for //CENTER 02:
android:layout_centerInParent="true"
This issue may be due to using of both Relative Layout and Linear Layout inside a xml file.
Change all Linear Layout to Relative Layouts so that Image View will be centered as you expected.
I too mingled both Linear Layout and Relative Layout in single XML and faces this kind of issue. Once i replaces all Linear Layout to Relative Layout my problem Resolved.
Try it.Thanks.Venky
Its because in Relative Layout center_horizontal isn't the command. that works for views wrapped in linear Layout. for RelativeLayout you need to use
android:layout_centerInParent="true";
as the center gravity command in linear
or
android:layout_centerHorizontal="true"
as the gravity center_horizontal in linear.
When you set android:gravity="center_horizontal" on a parent, it influences the behaviour of its children. Hence, your ImageView gets effected, creating the effect you see after already being placed in the center by other properties.
You can see this effect for yourself by placing a TextView in there with android:layout_width="fill_parent".

Categories

Resources