Android: How to understand layouts? - android-studio

I am struggling big time with trying to make a simple layout with two TextView:s under each other on the left and corresponding two TimePicker:s under each other on the right. However, whichever view I use or attributes I choose that seem relevant I do not get the desired outcome. Here is my current try:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
>
<TextView
android:layout_column="1"
android:id="#+id/text_start_time"
android:text="#string/start_time"
android:gravity="center_horizontal"
/>
<TimePicker
android:id="#+id/time_picker_start"
android:timePickerMode="spinner"
android:layout_marginTop="-25dp"
android:layout_marginBottom="-25dp"
android:layout_gravity="center"
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
>
<TextView
android:layout_column="1"
android:id="#+id/text_end_time"
android:text="#string/end_time"
android:layout_gravity="center_horizontal"
/>
<TimePicker
android:id="#+id/time_picker_end"
android:timePickerMode="spinner"
android:layout_marginTop="-25dp"
android:layout_marginBottom="-25dp"
/>
</TableRow>
<TableRow>
<Button
android:layout_column="2"
android:layout_width="match_parent"
android:text="#string/button_calculate"
android:gravity="center"
android:onClick="calculateWorkingTime"
/>
</TableRow>
</TableLayout>
This gives the following output
Where is "Start"?
As you can see "Start" is not even shown! I would like to align "Start" with the vertical center of the TimePicker spinner. How to do that?

First, don't use a TableLayout. Use Linears, as you are a beginner and they're easier to use and understand. Here's what you should do:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/text_start_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start_time"
android:gravity="center_horizontal"
/>
<TimePicker
android:id="#+id/time_picker_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner"
android:layout_marginTop="-25dp"
android:layout_marginBottom="-25dp"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/text_end_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/end_time"
android:gravity="center_horizontal"
/>
<TimePicker
android:id="#+id/time_picker_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner"
android:layout_marginTop="-25dp"
android:layout_marginBottom="-25dp"
android:layout_gravity="center"
/>
</LinearLayout>
<Button
android:layout_column="2"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="#string/button_calculate"
android:gravity="center"
android:onClick="calculateWorkingTime"
/>
</LinearLayout>
What I've done is creating a Linear Layout and, into it, another two with orientation:horizontal, which makes the elements appear one next to each other.
Also, I've put width and height to all the elements, what is very important.
I've put wrap_content, if you want to change it, no problem.
I didn't test it, if there's any error, just coment
Hope it helps!

Related

The element type "Linear Layout" must be terminated by the matching end-tag "</Linear Layout>" help solve this error in android studio

I keep getting this error and I'm pretty sure I have closed all the tags what might be the problem? here is my xml.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<LinearLayout
android:id="#+id/buttons_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#73C8C0"
android:orientation="vertical"
android:paddingVertical="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="#+id/button_C"
style="#style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_margin="12dp"
android:backgroundTint="#DF0707"
android:text="#string/c"
android:textColor="#color/white"
android:textSize="32sp"
app:cornerRadius="36dp" />
<com.google.android.material.button.MaterialButton
android:id="#+id/open_bracket"
style="#style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_margin="12dp"
android:backgroundTint="#887E7E"
android:text="("
android:textColor="#color/white"
android:textSize="32sp"
app:cornerRadius="36dp" />
<com.google.android.material.button.MaterialButton
android:id="#+id/closed_bracket"
style="#style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_margin="12dp"
android:backgroundTint="#777171"
android:text=")"
android:textColor="#color/white"
android:textSize="32sp"
app:cornerRadius="36dp" />
<com.google.android.material.button.MaterialButton
android:id="#+id/button_divide"
style="#style/Widget.MaterialComponents.ExtendedFloatingActionButton"
android:layout_width="72dp"
android:layout_height="72dp"
android:layout_margin="12dp"
android:backgroundTint="#FF5722"
android:text="/"
android:textColor="#color/white"
android:textSize="32sp"
app:cornerRadius="36dp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Your whole code is ok but u need start a linear layout tag to end it, add this at the starting of your xml:
<LinearLayout>
Hope it helps!!

How to create a layout which is rounded on all sides?

I have created a layout which is supposed to have rounded corners on all the four sides. However, the layout shows rounded corners only in the left and right top corners. How to create a layout which has rounded corners on all the four sides (including the left and right top corners)? Here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center"
android:background="#FFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/rounded_linearlayout"
android:orientation="vertical"
android:layout_margin="#dimen/welcome_margin">
<TextView
android:text="#string/fitToWork"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="#dimen/margintop_value"
android:layout_marginLeft="#dimen/margin_left_value"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center">
<TextView
android:id="#+id/tv_yes"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/yes"
android:textColor="#FFF"
android:textSize="#dimen/yes_text_size"
android:background="#B5D625"/>
<TextView
android:id="#+id/tv_no"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/no"
android:textColor="#FFF"
android:textSize="#dimen/yes_text_size"
android:background="#2E2E2E"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Here is the rounded_linearlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFF"/>
<corners android:bottomRightRadius="8dip"
android:bottomLeftRadius="8dip"
android:topRightRadius="8dip"
android:topLeftRadius="8dip"/>
<stroke android:width="1dip" android:color="#AFAFAF"/>
</shape>
Thanks in advance!
If you want rounded corners on all the four sides then my suggestion is use a cardView instead. In cardView you can do it easily using card_view:cardCornerRadius="4dp" this attribute change the value according to your requirement.
Import CardView as a library from support library and add it in your project
Little code snippet
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:background="#android:color/darker_gray"
card_view:cardCornerRadius="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/abc_action_bar_default_height_material"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
android:layout_weight="1"
android:textColor="#android:color/black"
android:text="Button" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#android:color/black"
android:layout_weight="1"
android:textColor="#android:color/white"
android:text="Button" />
</LinearLayout>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/abc_background_cache_hint_selector_material_dark" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
First of all, if you need all the corners to be rounded, you don't need to mention all the corners one by one, add this:
<corners android:radius="8dp"/>
I pulled your code and compiled it, it looks like it is working. The only thing is that your buttons are aligned with bottom of your parent LinearLayout and doesn't let you to see the bottom corners. I added a bit of padding to the parent LinearLayout and now all the rounded corners are visible.
EDIT:
I changed your code to this and it worked:
Here is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#FFF"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_margin="10dp"
android:background="#drawable/rounded"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:text="fitToWork" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center" >
<TextView
android:id="#+id/tv_yes"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#B5D625"
android:gravity="center"
android:text="yes"
android:textColor="#FFF"
android:textSize="25sp" />
<TextView
android:id="#+id/tv_no"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#2E2E2E"
android:gravity="center"
android:text="no"
android:textColor="#FFF"
android:textSize="35sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
and here the drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFF" />
<corners
android:bottomLeftRadius="8dip"
android:bottomRightRadius="8dip"
android:topLeftRadius="8dip"
android:topRightRadius="8dip" />
<stroke
android:width="1dip"
android:color="#AFAFAF" />
</shape>
Let me know if it work for you.
Cheers
A.

Android Linear layout and percentage

I want to create layout similar to one attached. The left hand side needs to be scrolled text occupying 75% of screen horizontally. The remaining 25% will consists of few icons stacked vertically and a button that should align at the bottom.
But I can't seem to get the working.
http://i.imgur.com/TzzBy9h.png
I would appreicate any help in getting this layout stuff right, my current layout looks like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/card_details"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:maxLines="50"
android:layout_weight="0.7"
android:gravity="bottom"
android:text="" >
</TextView>
<ImageView
android:id="#+id/photo_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:minHeight="84dp"
android:minWidth="84dp"
android:scaleType="centerCrop" />
</LinearLayout>
</ScrollView>
<LinearLayout android:gravity="center" android:orientation="horizontal"
android:padding="4.0dip" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/read_id_btn"
style="#style/PageButton"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="#string/read_id" />
</LinearLayout>
</LinearLayout>
~
Try weigth "2" and "1".
Good luck with it.
Try This code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginBottom="15dp"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>

How to align with view element present in the included layout in Android

I would like to align the view element in the current layout with the view element which I included through tag in Android.
Layout1: (Layout1.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/click_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/desc"
android:padding="4dp"
android:src="#drawable/icon_click" />
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="bottom"
android:paddingLeft="8dp"
android:paddingTop="6dp"
android:text="#string/textview1"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:text="#string/textView2"
/>
</RelativeLayout>
</LinearLayout>
Layout2:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="#+id/includedLayout"
layout="#layout/Layout1" />
<View
android:id="#+id/seperator"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:alpha="0.3"
android:background="#FFFFFF" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- I want to align this layout with relative layout present in the
Layout1.xml file which is included above -->
</LinearLayout>
</LinearLayout>
I want to align the second LinearLayout in the Layout2.xml with the RelativeLayout in the Layout1.xml file which is included in the Layout2.xml file.
Please let me know if anyone knows the solution.
Thanks.
whay dont you try putting the layout part that you want to align in to another file and only include that file in to the your layout2.xml and manually coding remining part in the layout2.xml.
What i mean is to take
`
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="bottom"
android:paddingLeft="8dp"
android:paddingTop="6dp"
android:text="#string/textview1"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:text="#string/textView2"
/>
</RelativeLayout>`
this part in to another separate file and manually add imageview in to layout2.xml

Admob Layout for Android

I have the following code....and I can't seem to get the ads to run outside my scroll view. I have tried to follow other questions on this topic. please assist. All of my adds are showing on top of my content. I would prefer the ads run outside my scroll view.
my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxxxxxxxxxxxxxxxx"
ads:loadAdOnCreate="true" />
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutbottom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:padding="5dp" >
<!-- android:layout_height="456dp" -->
<TextView
android:id="#+id/txtPlaceName"
style="#style/HeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txtAddress"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txtStory"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txtURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
Try adding android:layout_alignParentTop="true" on your AdView and android:layout_below="#id/adView" on your ScrollView.

Resources