i have button in that i want to put same space between all button so if i run app in tablet the space between button will equal divide, i am using linearlayout, i know there is layout_weight option but i don't want to stretch icon
for example
my xml code is below
<LinearLayout
android:id="@+id/shareLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btnRequestAPrescriptionRefill"
android:layout_marginTop="10dp" >
<Button
android:id="@+id/btnFacebook"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/btn_facebook" />
<Button
android:id="@+id/btnYoutube"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/btn_youtube" />
<Button
android:id="@+id/btnTwitter"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/btn_twitter" />
<Button
android:id="@+id/btnPintrest"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/btn_pintrest" />
</LinearLayout>
Expanding on fedj's answer, if you set layout_width to 0dp and set the layout_weight for each of the buttons to 1, the available width will be shared equally between the buttons.
You can add at the end of each text "\n" to split a line and make space between elements.
2 answers. In addition to the buttons you will need to use an view "empty" (1) to represent the spaces before, between and after each button. layout_weight attribute is used to scale the spaces equally.
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
You can use layout_weight. But like you said Buttons will be stretched
, so instead of using weight on Buttons, use on spaces(Views).
<Button
android:id="@+id/btnFacebook"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/btn_facebook" />
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
<Button
android:id="@+id/btnYoutube"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/btn_youtube" />
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
<Button
android:id="@+id/btnTwitter"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/btn_twitter" />
<View
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1" />
<Button
android:id="@+id/btnPintrest"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/btn_pintrest" />
</LinearLayout>
To make your intent clearer in your XML use a <Space>
instead of a <View>
as follows:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/Blue"
android:orientation="horizontal" >
<Space
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</Space>
<Button
android:id="@+id/btnFacebook"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/btn_facebook" />
<Space
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1" >
</Space>
...and repeat with the rest of your buttons...
</LinearLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With