Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to offset a view from center in Android?
    primarykey
    data
    text
    <p>im trying to possition my button not exactly in center, but lets say in the 2/5s of the screen height, i was looking for attribute unsuccessfully, so i tried this approach</p> <pre><code>&lt;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:background="@drawable/background" android:padding="20dp" &gt; &lt;ImageButton android:id="@+id/flashlight_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_above="@+id/fakeView" android:background="@null" android:contentDescription="@string/flashlight_button_description" android:src="@drawable/freeml_bright" /&gt; &lt;View android:id="@+id/fakeView" android:layout_width="10dp" android:layout_height="10dp" android:layout_centerInParent="true" android:background="#FFAABB" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>however it does not work, even if i set margin on the fake view.</p> <p>Any ideas?</p> <p>//EDIT//</p> <p>thanks for you answers guys, padding attribute works, however as it is a big image and if i want it to start at 2/5ths of screen height, it covers the center point of screen, so if i use padding attribute it works but it pushes it away from center and does not allow it to cover it. Sorry my bad</p> <p>Though, i made it work using linear layout, which i wanted to avoid because there are more view on top and bottom next to each other so it would lead to nested views using linear layout. Unfortunately i think its the only option.</p> <p>It basically it uses another linear layout that fills the remaining space left unused by top and bottom views with height=0dp and weight=1 and sets its gravity to center</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="20dp"&gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center" &gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/application_logo_description" android:src="@drawable/mylight" /&gt; &lt;ImageButton android:id="@+id/settings_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:background="@null" android:contentDescription="@string/settings_button_description" android:src="@drawable/settings_button" /&gt; &lt;/LinearLayout&gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:orientation="vertical" &gt; &lt;ImageButton android:id="@+id/flashlight_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@null" android:contentDescription="@string/flashlight_button_description" android:src="@drawable/flashlight_button_selector" /&gt; &lt;View android:id="@+id/fakeView" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginTop="60dp" android:background="#FFAABB" /&gt; &lt;/LinearLayout&gt; &lt;ImageView android:id="@+id/imageView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:contentDescription="@string/powered_by_description" android:src="@drawable/powered_by" /&gt; &lt;ImageButton android:id="@+id/ad_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:background="@null" android:contentDescription="@string/ad_button_description" android:src="@drawable/freeml" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Thanks for your input.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload