views get hide on orientation change from portrait to landscape in android mobile application

789 views Asked by At

there is an activity in my application. In this i am taking few values from user using some Editboxes. the problem is that if the phone's orientation is portrait then i can see all the EditBoxes and buttons but when the orientation changes to landscape the buttons get hide and i can see only the first 3 Editboxes and nothing else. All i want is that if such a case appears and the size of the layout gets more than a height of phone, then a scroll should appear so that i can scroll down to see the below fields and buttons. Is there a better way to do it ???? (using a different layout for landscape mode will cause recreating the activity which i do not want)

Below is the code for my 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.name.package.service">


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/title_text_size"
    android:textStyle="bold"
    android:gravity="center_horizontal"
    android:id="@+id/tv_lable"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_serivce_description"
    android:gravity="left"
    android:text="@string/plumber_service_description"
    android:textSize="@dimen/description_text_size"
    android:layout_marginTop="@dimen/large_margin"
    android:layout_below="@id/tv_book_service_title_lable"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name :"
    android:id="@+id/tv_customer_name_label"
    android:layout_below="@id/tv_serivce_description"
    android:layout_marginTop="@dimen/huge_spacing"
    android:textSize="@dimen/label_text_size"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et_customer_name"
    android:layout_alignBottom="@id/tv_customer_name_label"
    android:layout_toRightOf="@id/tv_customer_name_label"
    android:layout_marginLeft="@dimen/normal_margin"
    android:hint="fiil your name"
    android:gravity="center_horizontal"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Email :"
    android:id="@+id/tv_customer_email_label"
    android:layout_below="@id/tv_customer_name_label"
    android:layout_marginTop="@dimen/large_spacing"
    android:textSize="@dimen/label_text_size"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et_customer_email"
    android:layout_alignBottom="@id/tv_customer_email_label"
    android:layout_toRightOf="@id/tv_customer_email_label"
    android:layout_marginLeft="@dimen/normal_margin"
    android:hint="fiil your email"
    android:gravity="center_horizontal"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Contact :"
    android:id="@+id/tv_customer_contact_label"
    android:layout_below="@id/tv_customer_email_label"
    android:layout_marginTop="@dimen/large_spacing"
    android:textSize="@dimen/label_text_size"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et_customer_contact"
    android:layout_alignBottom="@id/tv_customer_contact_label"
    android:layout_toRightOf="@id/tv_customer_contact_label"
    android:layout_marginLeft="@dimen/normal_margin"
    android:hint="fiil your email"
    android:gravity="center_horizontal"
    android:marqueeRepeatLimit="marquee_forever"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tv_service_description_label"
    android:text="Description"
    android:textSize="@dimen/label_text_size"
    android:layout_below="@id/tv_customer_contact_label"
    android:layout_marginTop="@dimen/large_spacing"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/et_service_description"
    android:layout_below="@id/tv_service_description_label"
    android:layout_marginTop="@dimen/large_margin"
    android:hint="What service you need ?"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Book Service"
    android:gravity="center_horizontal"
    android:layout_below="@id/et_service_description"
    android:layout_marginTop="@dimen/large_margin"
    android:layout_centerHorizontal="true"
    />

i have also used below settings for the activity in manifest file

android:configChanges="orientation|keyboardHidden|screenSize"
        android:launchMode="singleTop"

Thanks in advance for sharing the knowledge ....:D

2

There are 2 answers

0
Anand Savjani On

Please use LinearLayout instead of RelativeLayout it will solve your problem

0
Psypher On

Wrap your layout which is RelativeLayout within a Scrollview. If the layout does not fit within the window then scrolls are enabled for you to scroll and view all your views else they will not be visible on screen.

Usage

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/ScrollViewid"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 -------add your RelativeLayout ------
</ScrollView>

A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.