How to wrap content like image, button inside Android layouts?

I want to place a list of buttons in a layout, and I want them to appear automatically. Look at this image. This is how WrapPanel transfers its content to WPF. Is there any way to do this in Android?

This is what WrapPanel does in WPF
(source: microsoft.com )

+4
source share
3 answers

You are using linearlayout in android, which allows you to place child objects in vertical or horizontal order. You put the buttons inside the line output.

You have to go through tuorial in Android layouts. AbsoluteLayout in the link is falling apart.

My knowledge of the wrapper panel is based only on the link you linked, since I did not work with WPF. This is from what I understood. Android does not have a layout for the crawl panel, where child views go to the next line when there is less space. Your options

  • use scrollviews to scroll if the views are larger than the screen.

  • Use the layout_weight property for childs views in linearlayout mode. This will make the view resize to resize.

  • Create your own layout by expanding the ViewGroup class, where you measure child views and determine their position based on the available screen width.

The first 2 options are not quite what you want. These are alternative approaches to what the android provides. But if you need the exact thing, you need to do it yourself.

+2
source

This looks like LinearLayout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <Button android:text="Button" android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout> 

For more information on using XML layouts in Android, see this good introduction to the Android developer site: http://developer.android.com/resources/tutorials/views/index.html

0
source

I think you need to use RelativeLayout to achieve this easily.

here is the code:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#c0c0c0" > <Button android:text="Button" android:id="@+id/button3" android:layout_width="fill_parent" android:layout_height="wrap_content"> </Button> <LinearLayout android:id="@+id/linearlayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/button3" android:layout_alignParentLeft="true" android:orientation="horizontal" android:layout_marginRight="21dp" > <Button android:text="Button" android:layout_width="wrap_content" android:layout_weight="2" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_gravity="left"> </Button> <Button android:text="Button" android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_weight="2"> </Button> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/linearlayout01" android:layout_alignParentLeft="true" android:weightSum="3" > <Button android:text="Button" android:layout_width="wrap_content" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_weight="1"> </Button> </LinearLayout> 

Hope this helps !;)

0
source

All Articles