Reputation: 1305
I have to create Android activity which looks like this:
Now I've created only such part of the activity.
Which coded this way.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ru.alexeyzhulin.elecomp.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageButton
android:id="@+id/logoButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
app:srcCompat="@drawable/main_icon" />
<SearchView
android:id="@+id/searchText"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:queryHint="@string/search_message"
android:textAlignment="center" />
<ImageButton
android:id="@+id/cartButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
app:srcCompat="@drawable/cart_icon1" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Please help me how to put last four buttons to the activity.
The particular question is:
How to put four buttons in the center of the screen.
Upvotes: 0
Views: 108
Reputation: 19
Inside your vertical linear layout you can give 2 more linear layouts with horizontal orientation and also with weightsum =2. Inside both these two linear layouts add 2 more linear layouts with weight =1.Give apropriate paddings for peoper button looks and then you can give the click action to the layouts.
Upvotes: 0
Reputation: 1260
I am trying to put a sample code for you. This code can give you idea how to place four button at center of activity.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" //I changed it to wrap_content
android:orientation="horizontal">
<ImageButton
android:id="@+id/logoButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
/>
<SearchView
android:id="@+id/searchText"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:queryHint="assss"
android:textAlignment="center" />
<ImageButton
android:id="@+id/cartButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
/>
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<Button
android:id="@+id/b1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="button" />
<Button
android:id="@+id/b2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:text="button" />
<Button
android:id="@+id/b3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/b1"
android:layout_marginTop="40dp"
android:text="button" />
<Button
android:id="@+id/b4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/b2"
android:layout_marginTop="40dp"
android:text="button" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
Upvotes: 2
Reputation: 98
You can achieve this kind of layout using Table layout , I recommend you to look into these links 1 , 2(android documentation) , 3 for table layout tutorial.
Upvotes: 0
Reputation: 76
Just use this on the top Linear Layout
gravity="center"
OR....
<ImageButton
android:layout_weight = "1"
android:id="@+id/logoButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
app:srcCompat="@drawable/main_icon" />
If this doesnt work try to do this : Nest 2 ImageViews inside 1 LinearLayout and create 2 Linear Layouts with weight = 1
Code :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/logoButton"
android:layout_width="55dp"
android:layout_weight = "1"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
app:srcCompat="@drawable/main_icon" />
<SearchView
android:layout_weight = "1"
android:id="@+id/searchText"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:queryHint="@string/search_message"
android:textAlignment="center" />
</LinearLayout>
Upvotes: 1