I am relatively new to android development and don't have a very strong understanding of design guidelines and right layout techniques, yet. I'm working with a screen that needs to look like the following:
The code I am using currently is a mix of several linear layout blocks and I understand that this is not the right way of going about achieving such a layout.
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.culami.WelcomePageActivity"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/welcome_msg"
android:layout_gravity="center"
android:textSize="50sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal"
android:layout_marginTop="15dp" >
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/userIcon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:src="@drawable/user1"
android:contentDescription="@string/userProfileMSG"
android:layout_gravity="center"
android:onClick="" />
<TextView
android:id="@+id/nameUser1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/nameUser1"
android:layout_gravity="center"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/userIcon2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:src="@drawable/user2"
android:contentDescription="@string/userProfileMSG"
android:layout_gravity="center"
android:onClick="" />
<TextView
android:id="@+id/nameUser2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/nameUser2"
android:layout_gravity="center"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/userIcon3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:src="@drawable/user3"
android:contentDescription="@string/userProfileMSG"
android:layout_gravity="center"
android:onClick="" />
<TextView
android:id="@+id/nameUser3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/nameUser3"
android:layout_gravity="center"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/userIcon4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:src="@drawable/user4"
android:contentDescription="@string/userProfileMSG"
android:layout_gravity="center"
android:onClick="" />
<TextView
android:id="@+id/nameUser4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/nameUser4"
android:layout_gravity="center"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/addUserIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:src="@drawable/adduser"
android:contentDescription="@string/userProfileMSG"
android:layout_gravity="center"
android:onClick="" />
<TextView
android:id="@+id/addUserText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/addUser"
android:layout_gravity="center"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="35dp"
android:layout_marginBottom="10dp"
android:text="@string/buttonSubmit"
android:textSize="30sp"
android:textColor="#ffffff"
android:textStyle="bold"
android:padding="5dp"
android:background="@drawable/login_button_selector"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="0.3"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Can someone please guide me with the right approach for dealing with such layouts and also, provide me with some design guidelines I may have violated here. Thanks!
Explanation - Relative layout as parent view (for flexicbility in arranging widgets).
TextView with id "welcome" for the top text. (u can customize it u way with gravity and textsize and all that).
LinearLayout -
1) it has horizontal orientation. That is, widgets will be arranged horizantally. 2) it is below first text view :
3) it has 3 imageviews (horizontally placed as stated above). Loo at their widths, its 0dps. Because I have used layout_weight instead. Weight for each is 0.33 i.e. it will occuoy 33% of whatever is available for it horizontally. If u want 4 images, u can use weight as 0.25 for each.
Then we have another linear layout, again to have images as in the above one. it is placed below ll1.
Finally, we have a button (u can either align it below ll2 or at the parent bottom
I suggest you to use Grid View with adapter. Here are some advantages of Adapter mentioned blow.
Advantages of an adapter
1-Dynamic – Can expand to any number of elements rather than statically coding each individual view. 2-Elegant – Makes your code petite and quite clear to understand once you get over the initial difficulty 3-Beautiful – Now you don’t have to control how many items there are in rows or columns, android will automatically fill up the screen in the best way possible. This also means you don’t need to redesign your application for horizontal and vertical orientations.
These are some example of GridView using adapters for beginners. Example 1 Example 2 Example 3
I would suggest you use GridView with an adapter if you want the capability to manipulate (add and remove) users. The major advantages are:
The automatic scrolling makes it so you don't have to put the container inside a ScrollView.
For other uses that don't require the dynamic capabilities of GridView you can just use GridLayout, which is much easier as you can add all it's children from in the xml file and don't have to worry about adding them in your java code.
While you can achieve this with some LinearLayouts and the alike, my suggestion is to use a
RecyclerView
. Its very scalable and performs superb even if you happen to have a ton of elements in it.You can read up on the RecyclerView here: https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html
It may be much to take in at first, it was for me anyway, but once you get the hang of it you will start to see new solutions to many of your problems.
I take it you want the Welcome to scroll with the content, as well as the Get started button, you can achieve this with another 2 ViewTypes in the
RecyclerView.Adapter
. Or you can use anonScrollListener
on theRecyclerView
directly and scroll them offscreen when the user has scrolled a set distance.Last but not least you can adapt the sizing between items with
ItemDecoration
which is set on theRecyclerView
, and you can set how the items are displayed with theLayoutManager
(currently theresLinearLayoutManager
(think List),GridLayoutManager
andStaggeredGridLayoutManager
available. They provide all you need I believe, but if you want more you can even check outTwoWayView
from Lucas Rocha, which allows you to further customize how the items will be displayed (SpannableGridLayoutManger is what I have in mind).Direct link to his github: https://github.com/lucasr/twoway-view
Good luck, and remember that even though there may be a cheaper quicker solution, you may need to modify it in the future and then you will think back to this moment and wonder why you didnt take the elegant way :-) Or not.. Anyways, good luck.
Alright, here is my suggestion:
This solution has no inner weights for the best performance.
It's also dynamic, meaning you can manipulate the list of users, and the view hierarchy is very simple.
main_layout.xml
As you can see it now has a gridview where you can add items as you wish.
user_cell_layout.xml
Each cell is a normal textview. The icon will be a property of the textView called
drawableTop
.You will need an adapter for the gridview, to adapt each item into a grid view item.
UserAdapter.java
Then in the
onContent
of your activity just do:Hope that helps! You can now change the contents of the list and just call
adapter.notifyDataSetChanged();
and the gridview should adjust to accommodate the changes.If you want you can extend the UserAdapter to hold items of type User instead of simple Strings, where you can add an icon, a username and an id. This example is for a listview, but the adapter part is container agnostic.
If you want to read more about layout designing guideline please refer below link:
http://developer.android.com/training/improving-layouts/index.html