0

I am new to android development . I've made a simple demo of login screen. I used Xml to do this and have checked my output on landscape and portrait mode. It looks fine in portrait mode, but on landscape my login button is not visible and I am not able to scroll my view. I used dp in my xml file and I think it is due to dp that I have this problem.

Here are my screen shots to show what is displayed.enter image description here

This is portrait mode which is looking fine ..

enter image description here

When I rotate my device it moves to landscape, but it is not showing button and below text view? Can I add scroll view ? or can I add in % percentage instead of dp?

Here is my code

<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"
   tools:context=".MainActivity"
    android:background="#223399"
    android:orientation="vertical">

    <TextView
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"
        android:text="Login here"
        android:gravity="center"
        />


    <EditText
        android:layout_marginTop="80dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Username"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        />
    <EditText
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        />

    <Button
        android:layout_marginTop="80dp"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/loginClick"
        android:layout_gravity="center"
        />
    <TextView
        android:layout_marginTop="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:text="New User?"
        android:textColor="#00eeff"
        android:gravity="center"
        android:id="@+id/regiester_id"
        />


</LinearLayout>

java code

   public static final String MyPREFERENCES = "MyPrefs" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

How can I set the screen up so that it looks good in portrait as well as landscape mode?

Update code

<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"
   tools:context=".MainActivity"
    android:background="#223399"
    android:orientation="vertical">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="fill_parent">

    <TextView
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"
        android:text="Login here"
        android:gravity="center"
        />


    <EditText
        android:layout_marginTop="80dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Username"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        />
    <EditText
        android:layout_marginTop="20dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        />

    <Button
        android:layout_marginTop="80dp"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/loginClick"
        android:layout_gravity="center"
        />
    <TextView
        android:layout_marginTop="40dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:text="New User?"
        android:textColor="#00eeff"
        android:gravity="center"
        android:id="@+id/regiester_id"
        />
    </ScrollView>


</LinearLayout>
user944513
  • 12,247
  • 49
  • 168
  • 318
  • You can use `android:layout_weight`. Refer this: http://stackoverflow.com/questions/3995825/what-does-androidlayout-weight-mean – Amey Shirke Aug 16 '15 at 05:32
  • i know layout weight i thing in this it is not used – user944513 Aug 16 '15 at 05:34
  • 1
    Scrollview must have a single child only not multiple child so use linearlayout inside of scroll view. Then use multiple child in that linear layout. – Pankaj Aug 16 '15 at 06:32

4 Answers4

1

I gave it a quick try using layout_weight. You can more fine tune it using different weights.

<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"
    tools:context=".MainActivity"
    android:background="#223399"
    android:orientation="vertical"
    android:weightSum="5">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceLarge"
        android:text="Login here"
        android:gravity="center"
        android:layout_weight="1"
        />


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Username"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_weight="1"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Password"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_weight="1"
        />

    <Button

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/loginClick"
        android:layout_gravity="center"
        android:layout_weight="1"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:text="New User?"
        android:textColor="#00eeff"
        android:gravity="center"
        android:id="@+id/regiester_id"
        android:layout_weight="1"
        />


</LinearLayout>
Amey Shirke
  • 599
  • 1
  • 5
  • 16
0

when i rotate my device it move to landscape .it is not showing button and below text view ? can I add scroll view ? or can I add in % percentage instead of dp.

I would say use ScrollView for better screen adjustment, it will be helpful in smaller device screens too.

also you can make different types of layouts for every size of screens and for landscape and portrait.

please refer to developer's site for types of screen layouts.

I hope you will get info for different types of screen size from there.

Shvet
  • 1,159
  • 1
  • 18
  • 30
0

ScrollView must have only one child, try to change your code like that:

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#223399">

       <LinearLayout  
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:orientation="vertical">

            <TextView
                android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:textAppearanceLarge"
                android:text="Login here"
                android:gravity="center"
                />


            <EditText
                android:layout_marginTop="80dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Username"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                />
            <EditText
                android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                />

            <Button
                android:layout_marginTop="80dp"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Login"
                android:id="@+id/loginClick"
                android:layout_gravity="center"
                />
            <TextView
                android:layout_marginTop="40dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:textAppearanceMedium"
                android:text="New User?"
                android:textColor="#00eeff"
                android:gravity="center"
                android:id="@+id/regiester_id"
                />

       </LinearLayout>
</ScrollView>
Rami
  • 7,879
  • 12
  • 36
  • 66
0

Scroll View Must have one child. So update your code with below and seee its working or not.

<ScrollView 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:background="#223399"
        tools:context=".MainActivity" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:text="Login here"
                android:textAppearance="?android:textAppearanceLarge" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="80dp"
                android:hint="Username" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="20dp"
                android:hint="Password" />

            <Button
                android:id="@+id/loginClick"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="80dp"
                android:text="Login" />

            <TextView
                android:id="@+id/regiester_id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="40dp"
                android:gravity="center"
                android:text="New User?"
                android:textAppearance="?android:textAppearanceMedium"
                android:textColor="#00eeff" />
        </LinearLayout>

    </ScrollView>
Deepak Goyal
  • 4,747
  • 2
  • 21
  • 46