ImageView can not be resized using the layout weight - android

I want to divide my screen into 3 vertically equal parts and add an ImageView to the top part. When I add the image, the parts won't be equal.
It can not be changed using layout weight.
<LinearLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:orientation="vertical"
tools:showIn="#layout/app_bar_main"
android:paddingLeft="30dp"
android:paddingRight="30dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/logo1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</LinearLayout>

You need to set android:layout_height="0dp" in all LinearLayout like :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>

try this
<LinearLayout 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"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ff0000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00ff00">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0000ff">
</LinearLayout>
OUTPUT

Related

layout_weight is not working inside ScrollView

I can't figure out , why layout_weight is not working inside the ScrollView , when i am running this xml , i am getting imageview capturing the whole screen and below that i can see my tabs and the viewpager got hidden .
When i am giving the height in dp to all the linear layout below the ScrollView , i am getting my view perfectly ,why it's happening . i want my imageview to take 30% screen and tab/viewpager to take 70% of screen.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.emilsjolander.components.StickyScrollViewItems.StickyScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/gift_vouchr_header_img"
android:layout_weight="3"
android:adjustViewBounds="true"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageview_gift"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash"/>
</LinearLayout>
<LinearLayout
android:id="#+id/viewpager_ll"
android:layout_width="match_parent"
android:background="#color/lvb_divider_color"
android:layout_height="0dp"
android:orientation="vertical"
android:tag="sticky"
android:layout_weight="7"
android:adjustViewBounds="true"
android:layout_gravity="center"
>
<android.support.design.widget.TabLayout
android:id="#+id/common_tablayout"
style="#style/VoucherTabLayoutStyle"
android:layout_width="match_parent"
app:tabGravity="fill"
android:layout_height="#dimen/margin_48">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/common_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
</com.emilsjolander.components.StickyScrollViewItems.StickyScrollView>
</LinearLayout>
When i remove weightsum and hardcoding the height then the view is working perfectly : below is my code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.emilsjolander.components.StickyScrollViewItems.StickyScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="#color/gift_vouchr_header_img"
android:adjustViewBounds="true"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageview_gift"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/splash"/>
</LinearLayout>
<LinearLayout
android:id="#+id/viewpager_ll"
android:layout_width="match_parent"
android:background="#color/lvb_divider_color"
android:layout_height="500dp"
android:orientation="vertical"
android:tag="sticky"
android:adjustViewBounds="true"
android:layout_gravity="center">
<android.support.design.widget.TabLayout
android:id="#+id/common_tablayout"
style="#style/VoucherTabLayoutStyle"
android:layout_width="match_parent"
app:tabGravity="fill"
android:layout_height="#dimen/margin_48">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/common_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
</com.emilsjolander.components.StickyScrollViewItems.StickyScrollView>
</LinearLayout>
MyTab1 xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/margin_15"
android:layout_marginRight="#dimen/margin_15"
android:layout_marginTop="#dimen/margin_15"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/gift_amount_footer_txt"
android:text="#string/validity"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/voucher_common_layout"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/gift_title_txt"
android:layout_marginTop="#dimen/margin_26"
android:text="#string/receiver_detail"/>
<EditText
android:id="#+id/receiver_name"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_48"
android:backgroundTint="#color/QuaternaryTextColor"
android:hint="#string/receiver_name"
android:paddingLeft="#dimen/margin_2"
android:layout_marginTop="#dimen/margin_23"/>
<EditText
android:id="#+id/receiver_email"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_48"
android:hint="#string/receiver_email"
android:inputType="textEmailAddress"
android:backgroundTint="#color/QuaternaryTextColor"
style="#style/giftEditetxtStyle"
android:paddingLeft="#dimen/margin_2"
android:layout_marginTop="#dimen/margin_30"/>
<EditText
android:id="#+id/receiver_mobile"
android:layout_width="match_parent"
android:layout_height="#dimen/margin_48"
android:backgroundTint="#color/QuaternaryTextColor"
android:inputType="phone"
android:hint="#string/receiver_mobile"
android:paddingLeft="#dimen/margin_2"
android:layout_marginTop="#dimen/margin_30"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_40"
android:layout_marginBottom="#dimen/margin_30"
android:layout_marginRight="#dimen/margin_12"
android:gravity="center"
android:orientation="horizontal">
<CheckBox
android:id="#+id/send_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/gift_term_condition"
android:layout_marginLeft="#dimen/margin_5"
android:text="#string/gift_terms_condition"/>
</LinearLayout>
<Button
android:id="#+id/btn_send_gift"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/gift_submit_btn"
android:layout_marginBottom="#dimen/margin_10"
android:layout_marginTop="#dimen/margin_20"/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
MyTab2 xml :
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="#dimen/margin_15"
android:layout_marginLeft="#dimen/margin_15"
android:layout_marginRight="#dimen/margin_15">
<include layout="#layout/voucher_common_layout"/>
<LinearLayout
android:id="#+id/terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_40"
android:layout_marginRight="#dimen/margin_12"
android:orientation="horizontal"
android:gravity="center">
<CheckBox
android:id="#+id/buy_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/gift_term_condition"
android:layout_marginLeft="#dimen/margin_5"
android:text="#string/gift_terms_condition"/>
</LinearLayout>
<Button
android:id="#+id/gift_buy_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/gift_submit_btn"
android:layout_marginTop="#dimen/margin_30"/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
None of the above solution worked for me , i found the solution myself like this : hope it will help someone : No changes in the tab code . In the main xml , i have done like this :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.emilsjolander.components.StickyScrollViewItems.StickyScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/childImg_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/gift_vouchr_header_img"
android:adjustViewBounds="true"
android:layout_gravity="center">
<ImageView
android:id="#+id/imageview_gift"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/splash"/>
</LinearLayout>
<LinearLayout
android:id="#+id/viewpager_ll"
android:layout_width="match_parent"
android:background="#color/lvb_divider_color"
android:layout_height="wrap_content"
android:orientation="vertical"
android:tag="sticky"
android:adjustViewBounds="true"
android:layout_gravity="center">
<android.support.design.widget.TabLayout
android:id="#+id/common_tablayout"
style="#style/VoucherTabLayoutStyle"
android:layout_width="match_parent"
app:tabGravity="fill"
app:tabMode="fixed"
android:layout_height="#dimen/margin_48">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/common_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
</com.emilsjolander.components.StickyScrollViewItems.StickyScrollView>
</LinearLayout>
Here comes the important part : Pragmatically set the height of the child layout :D
private void setViewPagerandImageViewHeight(){
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int heightOfScreen = displayMetrics.heightPixels;
child_img_ll.getLayoutParams().height= (int) (heightOfScreen*.30);// set imageview linearlayout to capture 30% of screen
viewpager_ll.getLayoutParams().height=heightOfScreen;//while scrolling set the viewpager height to maximum ,intially it capture 70% of screen
}

Android Layout Weight Exactly Twice Of Others

I would like to simply divide a Linear Layout to 3 piece. I did something as in the code below. If I change the weight of second inner layout anything else than 2, it works. But if I change it to 2(two), the second inner layout doesn't appear in the design ?
<LinearLayout
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:gravity="center"
>
<ImageView
android:id="#+id/HomePageIcon"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingHorizontal="33dp"
android:paddingBottom="30dp"
>
</LinearLayout>
Use it like below height set to 0dp when orientation is vertical.
<LinearLayout
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorAccent"
android:layout_weight="1"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#color/background_dark"
android:gravity="center"
>
<ImageView
android:id="#+id/HomePageIcon"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorPrimaryDark"
android:orientation="vertical"
android:paddingHorizontal="33dp"
android:paddingBottom="30dp"
>
</LinearLayout>
Use the Constraint Layout and forget the Linear and Relative layout.
Pls try this,
<LinearLayout
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
>
<ImageView
android:id="#+id/HomePageIcon"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:paddingHorizontal="33dp"
android:paddingBottom="30dp"
>
</LinearLayout>
As you are using layout_weight that means your inner layouts will take the height as much as is available to it. So, in that case there is no need of using layout_height = "wrap_content" just change it to android:layout_height="0dp"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center">
<ImageView
android:id="#+id/HomePageIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="30dp"
android:paddingHorizontal="33dp"></LinearLayout>
</LinearLayout>
When you are using weight than use 0dp to android:layout_width or android:layout_height which you want for affect using weight. And its good to use android:weightSum in parent layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="center"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="center">
<ImageView
android:id="#+id/HomePageIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="30dp"
android:paddingHorizontal="33dp"></LinearLayout>

how can I put 4 equal rectangle in one layout?

i want to make layout like picture but just use relative and linear layout.i want to mak 4 equal parts.I do not want to just put 4 button ot other objects.iwant just 4 equal space.
This is my solution I state in the comment, as you can see is a nest of LinearLayouts
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="net.whatsgift.mitro.weightlayout.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:backgroundTint="#color/colorAccent"></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:backgroundTint="#color/colorPrimary"></LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:backgroundTint=""></LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="50"
android:backgroundTint="#color/colorAccent"></LinearLayout>
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:weightSum="2"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2"
>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#color/red"
/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#color/black"
/>
<LinearLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="2"
>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#color/green"
/>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#color/yellow"
/>
</LinearLayout>
try this.
Use below Layout XML file just like your Image.
<?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:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#F3FF33">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#FF333F">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#D5DA87">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#1BE114">
</RelativeLayout>
</LinearLayout>
</LinearLayout>

LinearLayout inside ScrollView is autoscrolled to bottom

I've got a ScrollView which occupies the bottom half of the screen.
Inside this ScrollView a put a LinearLayout (vertical) with a lot of content.
When I start the activity, the content somewhy automatically scrolls itself down so that it starts at the top of the window. But I need it to start at the top of the ScrollView (i.e. at the center of the window).
What am I doing wrong?
<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="ru.intrigue.activities.EditActivity"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:visibility="visible">
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView2"
android:background="#color/colorDarkBack">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<!-- content -->
</LinearLayout>
</ScrollView>
</LinearLayout>
you can do like below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.45"
android:orientation="vertical">
<ImageView
android:id="#+id/frag_home_iv_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/demo" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.55"
android:fadeScrollbars="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.26"
android:gravity="center"
android:orientation="horizontal"
android:padding="#dimen/padding_5dp"
android:weightSum="1">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/demo" />
<TextView
android:id="#+id/fragment_home_tv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:paddingLeft="#dimen/padding_5dp"
android:text="#string/demo"
android:textColor="#color/BlackColor" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Your problem solve by below code :
When open your activity call below code:
your_scroll_view.post(new Runnable() {
#Override
public void run() {
your_scroll_view.fullScroll(View.FOCUS_DOWN);
}
});
Best of luck
You can achieve the desired results by using layout_weight property:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- your content here-->
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- your content here-->
</LinearLayout>
</ScrollView>
</LinearLayout>
Let me know if it helped you.

Linearlayout not displaying anything

I using android:layout_weight to limit the size of my view but now I'm not seeing anything on the screen. Here are the codes:
<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"
android:orientation="vertical">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical"
>
// try this way hope this will help you...
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.25 % Area"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.75"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.75 % Area"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</LinearLayout>
Try this:
<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"
android:orientation="vertical">
<LinearLayout
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical"
>
This will help you to see something. Change the width according to your requirement.
try this, first u need to give the weight sum in main linear layout.
Also change 0dp to 0 dip
<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"
android:orientation="vertical"
android:weightSum="5">
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical"
>
You should use weightSum property in your parent Linear layout, like android:weightSum="number of child"
This should be helpful
<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"
android:orientation="vertical"
android:weightSum="5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical"/>
<ScrollView
android:layout_width="match_parent"
android:layout_weight="3"
android:layout_height="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
Setting android:layout_width="0dp" will make this layout invisible. Change it to wrap_content or something else.

Categories

Resources