So I am having problems with some XML layout coding... I am supposed to replicate this image
but so far I seem to be only able to get this far and the checkout button refuses to stay to the right even when I do use android:gravity="right" to float right within its container... :/
This is the code I have so far:
<RelativeLayout 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=".CartActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="Shopping Cart"
android:background="#android:color/holo_blue_dark" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/holo_blue_light"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtotal:" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#800000"
android:text="£???" />
<Button
android:id="#+id/checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Checkout" >
</Button>
</LinearLayout>
</LinearLayout>
use a android:weightSum="3" for your inner
Layout with the three elements, and after that,
android:layout_weight="1" to each view
this will make the views to hold 1/3 of the available space
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/holo_blue_light"
android:weightSum="3"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subtotal:"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#800000"
android:text="£???"
android:layout_weight="1" />
<Button
android:id="#+id/checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Checkout"
android:layout_weight="1">
</Button>
</LinearLayout>
EDIT fill_parent is deprecated use match_parent
Try to use a Relativelayout
<RelativeLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Subtotal:" />
<TextView
android:id="#+id/textView2"
android:layout_toRightOf="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#800000"
android:text="£???" />
<Button
android:id="#+id/checkout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Checkout" >
</Button>
</RelativeLayout>
Related
I am using Android Studio. My project involves receiving some data through BLE and displaying it on the screen on a Linear Layout.
My main activity code for adding View:
dataLayout.addView(view, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
Function is defined as:
addView(dataEdit, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1));
In my xml file, the required portion looks like:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="#+id/dataLayout"
android:fillViewport="true">
</LinearLayout>
EDIT:
The whole xml file looks like:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:focusable="true"
android:focusableInTouchMode="true"
android:baselineAligned="true">
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:text="Find Device"
android:layout_marginTop="5dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scanStatus" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Scan"
android:id="#+id/scan"
android:layout_gravity="center_horizontal"
android:enabled="false" />
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_marginTop="5dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/deviceInfo" />
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:text="Connect Device"
android:layout_marginTop="5dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/connectionStatus" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Connect"
android:id="#+id/connect"
android:layout_gravity="center_horizontal"
android:enabled="false" />
</LinearLayout>
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView7"
android:text="Receive"
android:textColor="#0B0101"
android:layout_marginTop="5dip" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Clear Data"
android:textColor="#FFFCFC"
android:id="#+id/clearData"
android:layout_gravity="center_horizontal"
android:singleLine="false" />
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView9"
android:text="Graphs"
android:layout_marginTop="5dip" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Plot"
android:id="#+id/Plot"
android:layout_weight="1"
android:layout_marginTop="2dip"
android:singleLine="false"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Distance:"
android:textColor="#0B0101"
android:id="#+id/textView8" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="#+id/dataLayout"
android:fillViewport="true">
</LinearLayout>
</LinearLayout>
</ScrollView>
The problem is that only first 9 values of received data are being displayed on the screen. After that, even when I scroll up the screen, I can't see any values. I want to scroll up the screen and see all the values being received.
Any help would be appreciated.
Wrapping your LinearLayout in a ScrollView should solve it. However, use a ListView or RecyclerView instead of adding views yourself. These recycle the views that are not visible to save memory.
Just wrap your root layout with a ScrollView (how to).
UPDATE
Try to update your layout in the following way:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:focusable="true"
android:focusableInTouchMode="true"
android:baselineAligned="true">
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:text="Find Device"
android:layout_marginTop="5dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scanStatus" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Scan"
android:id="#+id/scan"
android:layout_gravity="center_horizontal"
android:enabled="false" />
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_marginTop="5dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/deviceInfo" />
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:text="Connect Device"
android:layout_marginTop="5dip" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/connectionStatus" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Connect"
android:id="#+id/connect"
android:layout_gravity="center_horizontal"
android:enabled="false" />
</LinearLayout>
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView7"
android:text="Receive"
android:textColor="#0B0101"
android:layout_marginTop="5dip" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Clear Data"
android:textColor="#FFFCFC"
android:id="#+id/clearData"
android:layout_gravity="center_horizontal"
android:singleLine="false" />
<TextView
style="?android:attr/listSeparatorTextViewStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/textView9"
android:text="Graphs"
android:layout_marginTop="5dip" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Plot"
android:id="#+id/Plot"
android:layout_weight="1"
android:layout_marginTop="2dip"
android:singleLine="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Distance:"
android:textColor="#0B0101"
android:id="#+id/textView8" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:id="#+id/dataLayout">
</LinearLayout>
</LinearLayout>
</ScrollView>
i'm trying to do a school day and i use multiple linearLayout with days and hours.
With the layout_weight i can set the space for every elements inside the single LinearLayout.
But my problem is how can i set the layout_weightfor the LinearLayout because they are set with orientation horizontal and they don't take the entire screen.
It's like a table but i use LinearLayout because i need to create only rows.
This is the xml of the Activity
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.ddz.diarioscolastico.OrarioActivityProva">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Lun"
android:id="#+id/textView2"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Mar"
android:id="#+id/textView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Mer"
android:id="#+id/textView3" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Gio"
android:id="#+id/textView4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Ven"
android:id="#+id/textView5" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Sab"
android:id="#+id/textView6" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_below="#+id/linearLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="1"
android:id="#+id/textView7" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="2"
android:id="#+id/textView8" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="3"
android:id="#+id/textView9" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="4"
android:id="#+id/textView10" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="5"
android:id="#+id/textView11" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout7">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="6"
android:id="#+id/textView12" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/linearLayout8">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="7"
android:id="#+id/textView13" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout8"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="8"
android:id="#+id/textView14" />
</LinearLayout>
As you can see if i set the android:layout_height="match_parent" yes all LinearLayout take full screen but only the first is visible, i wanna divide the screen for all.
Using Layout Weight is a little bit tricky task.
Suppose you want to show three buttons horizontally which share equal width in a linear layout. Then the xml code should be like this.
<?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:weightSum="3"
android:orientation="horizontal" >
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 1"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 1"
/>
<Button
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:text="Button 1"
/>
</LinearLayout>
As you can see,In the parent LinearLayout i used weightSum as 3 and in each child button i set the width to 0dp and weight to 1 and there are three buttons so it will share equal width.
By the way the root parent (RelativeLayout) is not closed in your given layout.
EDIT:2
As you said here is the code divide each LinearLayout equally with layout_weight
<?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:weightSum="3"
android:orientation="vertical" >
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#00FF00"
>
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#FF0000"
>
</LinearLayout>
<LinearLayout
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="#0000FF"
>
</LinearLayout>
</LinearLayout>
OUTPUT :
Have you tried to replace your LinearLayout with a TableRow or use a GridLayout ? GridLayout requires API 14 but lower API can be use with the support library.
I have this layout xml file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout 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=".MainActivity">
<TextView
android:layout_marginTop="40dp"
android:id="#+id/emalLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/test" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/testTxtLay"
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/emalLbl"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="1">
<EditText
android:id="#+id/testTxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="#drawable/edit_text_design" />
</LinearLayout>
<TextView
android:layout_marginTop="60dp"
android:id="#+id/reminderLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/testTxtLay"
android:layout_centerHorizontal="true"
android:text="#string/reminder" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="10dp"
android:id="#+id/reminderTxtLay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/reminderLbl"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="1">
<EditText
android:id="#+id/reminderTxt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:lines="2"
android:background="#drawable/edit_text_design" />
</LinearLayout>
<View
android:id="#+id/spacer"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="#id/reminderTxtLay"
android:layout_centerHorizontal="true" />
<Button
android:layout_marginTop="200dp"
android:id="#+id/pickDateBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/reminderTxtLay"
android:layout_toLeftOf="#+id/spacer"
android:onClick="showDatePickerDialog"
android:text="#string/datePickerBtnTxt" />
<Button
android:id="#+id/pickTimeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/pickDateBtn"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/pickDateBtn"
android:onClick="showTimePickerDialog"
android:text="#string/pickTimeBtn" />
<View
android:id="#+id/spacerTxt"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="#id/reminderTxtLay"
android:layout_centerHorizontal="true" />
<EditText
android:layout_marginTop="15dp"
android:id="#+id/selectedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/spacerTxt"
android:layout_below="#id/pickDateBtn"
android:layout_toLeftOf="#+id/spacerTxt" />
<EditText
android:id="#+id/selectedTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/selectedDate"
android:layout_below="#id/pickTimeBtn"
android:layout_toRightOf="#+id/selectedDate" />
<Button
android:id="#+id/submitBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:onClick="submitData"
android:text="#string/submitBtn" />
</RelativeLayout>
</ScrollView>
This is the custom design for 7 inch tablets, placed in res\layout-sw600dp
Anyways in the android studio landscape preview it seems just fine:
But in the emulator something is going wrong and here is how it looks strange. What I mean is that the submit button is not on the bottom and the pickDate and pickTime buttons are at the bottom of the layout.
I know that I'm missing a basic point here, but as an android developer, I'm not able to spot it.
Can you give me a push?
Here is a complete way to do what you seem to try accomplish. You don't need to create any Views for "spaces" etc. You only need to add margin or padding to either side of your views to make it move away from another view.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
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">
<LinearLayout
android:id="#+id/linear_wrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<TextView
android:layout_marginTop="40dp"
android:id="#+id/emalLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/test" />
<EditText
android:id="#+id/testTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text_design" />
<TextView
android:layout_marginTop="60dp"
android:id="#+id/reminderLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/reminder" />
<EditText
android:id="#+id/reminderTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="2"
android:background="#drawable/edit_text_design" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="200dp"
>
<Button
android:id="#+id/pickDateBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="showDatePickerDialog"
android:text="#string/datePickerBtnTxt" />
<Button
android:id="#+id/pickTimeBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="showTimePickerDialog"
android:text="#string/pickTimeBtn" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/selectedDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<EditText
android:id="#+id/selectedTime"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/submitBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/linear_wrapper"
android:onClick="submitData"
android:text="#string/submitBtn" />
</RelativeLayout>
you can place 2 buttons next to each other!
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="1">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:gravity="left"
android:layout_height="wrap_content"
android:lines="2" />
<Button
android:id="#+id/btn2"
android:layout_width="fill_parent"
android:gravity="right"
android:layout_height="wrap_content" />
</LinearLayout>
EDITED to improve specifics:
Hi, my layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:padding="10dp"
tools:context="${packageName}.${activityClass}" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="Select Character"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_below="#+id/textView1"
android:orientation="vertical"
android:paddingTop="10dp" >
<TextView
android:id="#+id/SelectedCharacterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<TextView
android:id="#+id/TraitName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trait 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff" />
etc...
</LinearLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0"
android:fillViewport="false"
android:paddingTop="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/Character1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#4a4a4a"
android:clickable="true"
android:gravity="center"
android:text="Character 1"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
etc...
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/buttonConfirmCharacter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:text="#string/button_Confirm" />
</LinearLayout>
I am looking to make the scrollview fit the remaining space between the other elements however with this setup the scrollview doesn't seem to render at all. These settings are the result of looking for solutions and implementing weight.
An answer without java code would be preferential.
Thanks.
Here, this would works just fine
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text1" />
<TextView
android:id="#+id/SelectedCharacterName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/TraitName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trait 1" />
etc...
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true"
android:paddingTop="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/Character1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:text="Character 1" />
etc...
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/buttonConfirmCharacter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="#string/button_Confirm" /></LinearLayout>
Hi I have some problems with aligning the EditText in Android.
my code:
<RelativeLayout 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=".MainActivity" >
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title goes here" />
</LinearLayout>
<TableLayout
android:id="#+id/middleSection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header" >
<TableRow
android:id="#+id/userRow"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/userText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username" />
<EditText
android:id="#+id/userEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/passwordRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/passwordText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="#+id/passordEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
android:id="#+id/checkBoxRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/checkBoxBlank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "/>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/checkBoxText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Password"/>
</TableRow>
</TableLayout>
</RelativeLayout>
my output looks like this:
Username i__i
Password i__I
chBox Show Password
actually, I want to have:
Username i__________________i
Password i__________________i
chBox Show Password
I could not figure it out how to align it as I am new to Android(and XML)
thanks
add android:ems="10" to the EditText properties in the XML. The number 10 is an arbitrary number, and you can increase or decrease it depending on how long you want the EditText.
That, or you could just drag it longer to a relative point in the Graphical Layout tab of the XML.
use android:stretchColumns="*" in TableLayout tag that stretch column that you want
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity" >
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title goes here" />
</LinearLayout>
<TableLayout
android:id="#+id/middleSection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:stretchColumns="1"
>
<TableRow
android:id="#+id/userRow"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/userText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username" />
<EditText
android:id="#+id/userEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/passwordRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/passwordText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="#+id/passordEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</TableRow>
<TableRow
android:id="#+id/checkBoxRow"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/checkBoxBlank"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" "/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/checkBoxText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Password"/>
</LinearLayout>
</TableRow>
</TableLayout>
</RelativeLayout>
Try this. It will work.
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="55dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#android:drawable/sym_def_app_icon" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title goes here"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<!--ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#android:drawable/sym_def_app_icon" /-->
</RelativeLayout>
<LinearLayout
android:id="#+id/middleSection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/userRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/userText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username" />
<EditText
android:id="#+id/userEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/passwordRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/passwordText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="#+id/passordEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="#+id/checkBoxRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"/>
<TextView
android:id="#+id/checkBoxText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Password"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/checkBoxText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Password"/>
put your above code in one parent layout says Linearlayout like this:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/checkBoxText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Password"/>
</LinearLayout>
what happening is you have 2 controls in each table row except last one which hase 3 controls in it so your EditText has its width according to the second control width that is your check box. so putting CheckBox and TextView in one Linear Layout consider it as a one control (or Layout) and your EditText adjusted according to that.