I have the next layout
And when keyboard comes up, i wan't that green marked part of layout stays always visible. Another words i need to see both EditText and Button.
Here is my code sketch:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="16dp"
tools:context="com.mynfo.concept.auth.AuthActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/titleImageView"
android:layout_gravity="center"
android:src="#drawable/title_auth"
android:layout_weight="0"
android:layout_marginTop="48dp"
android:layout_marginBottom="48dp"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="8dp"
android:focusable="true"
android:focusableInTouchMode="true"
>
<EditText
android:id="#+id/barcode_editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:inputType="number"
android:textColorHint="#color/darker_grey"
android:hint="edittext"
android:maxLength="15"
android:ellipsize="end"
android:ems="10"
android:background="#0000"
android:layout_gravity="left|center_vertical"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_camera"
android:layout_gravity="right|center_vertical"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/grey"
android:layout_gravity="bottom"/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<com.melnykov.fab.FloatingActionButton
android:id="#+id/button_scan"
android:layout_width="64dp"
android:layout_height="64dp"
fab:fab_colorNormal= "#color/turquoise"
fab:fab_colorPressed= "#color/turquoise_black"
fab:fab_colorRipple= "#color/turquoise_light"
android:src="#android:drawable/ic_menu_camera"
android:layout_gravity="center"/>
</FrameLayout>
<Button
android:id="#+id/button_link_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Authorize"
android:background="#drawable/button_authorize_selector"
android:enabled="false"
android:layout_weight="0"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center">
<TextView
style="#style/TextViewPrimary"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:textSize="16sp"
android:text="Enter your name"
android:gravity="center" />
<com.mynfo.concept.views.FontFitTextView
style="#style/TextViewSecondary"
android:text="And then you will got the access"
android:gravity="center" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/authenticatingView"
android:background="#fff">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:indeterminateOnly="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/authenticatingProgressBar"
android:layout_gravity="center"/>
</FrameLayout>
</FrameLayout>
</LinearLayout>
What haven't worked: adjustPan, adjustSize with or without layout weights.
Maybe I'm doing something wrong?
Thanks for the further help.
P.S. I know that this code feels redundant, but there were some purposes like screen adapting.
In your AndroidMenifest.xml file inside your tag use
windowsSoftInputMode = "adjustResize"
Or take whole layout inside ScrollView
windowsSoftInputMode = "adjustPan"
Example code snippet:
<activity
android:name=".activityname"
android:label="#string/app_name"
android:windowSoftInputMode="adjustPan|adjustResize" >
</activity>
Use
android:fillViewport="true" tag in ScrollView. And to avoid layout size bugs, use margin in Top ScrollView and don't use it in ScrollView child.
And add this in OnCreate
scrollView = (ScrollView) this.findViewById(R.id.scrollView);
scrollView.setVerticalScrollBarEnabled(false);
imageView = (ImageView) findViewById(R.id.imageView);
this.findViewById(android.R.id.content).addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
scrollView.scrollTo(0, imageView.getHeight() + ((ViewGroup.MarginLayoutParams) imageView.getLayoutParams()).topMargin);
}
});
See the comments..
Related
I have a Button which is aligned at the bottom of my screen. This Button lifts up when the SoftKeyboard is opened. The page is blank in the middle. But it has some content at the top and some right above the bottom aligned Button. The problem is when the SoftKeyboard is opened, the blank area in the middle is filled with the content in the bottom. What I want instead is, the bottom content to stay where it was and be able to scroll behind to see it.
Use the below property for your activity in android manifest. So that screen component can be accessible even after after soft keyboard opened.
<activity
android:windowSoftInputMode="adjustResize" ... >
...
</activity>
Reference Link: https://developer.android.com/training/keyboard-input/visibility#Respond
For someone facing the same problem, I solved it this way
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<!--data binding declaration-->
</data>
<RelativeLayout
android:id="#+id/register_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/primary_amber_200"
tools:context=".login.view.EnterEmailActivity">
<LinearLayout
android:id="#+id/register_ll"
android:layout_width="match_parent"
android:layout_height="92dp"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:gravity="center">
<View
android:id="#+id/register_bar"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/primary_amber_100"
android:layout_marginBottom="12dp"
android:visibility="invisible"/>
<Button
android:id="#+id/register_loginBtn"
style="#style/Button_Default_Dark_Grey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_small"
android:fitsSystemWindows="true"
android:text="#string/next"
android:layout_marginLeft="#dimen/margin_medium"
android:layout_marginRight="#dimen/margin_medium"/>
</LinearLayout>
<androidx.appcompat.widget.Toolbar
style="#style/ToolBar_Basic_Light"
android:background="#color/primary_amber_200"
android:elevation="0dp"
android:paddingLeft="#dimen/margin_medium"
android:paddingRight="#dimen/margin_medium">
<TextView
android:id="#+id/toolbar_title"
style="#style/Text_Basic_Toolbar" />
</androidx.appcompat.widget.Toolbar>
<ScrollView
android:id="#+id/register_sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none"
android:layout_below="#id/toolbar"
android:paddingLeft="#dimen/margin_medium"
android:paddingRight="#dimen/margin_medium"
android:layout_marginBottom="#dimen/margin_xlarge92">
<RelativeLayout
android:id="#+id/register_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/register_txtHeader"
style="#style/Text_Title_H1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_large"
android:text="#string/enter_email" />
<EditText
android:id="#+id/register_email"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_below="#id/register_txtHeader"
android:layout_marginTop="#dimen/margin_xlarge"
android:background="#color/primary_amber_100"
android:gravity="center_vertical"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:paddingLeft="#dimen/margin_medium"
android:paddingRight="#dimen/margin_small" />
<View
android:id="#+id/register_blank"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#id/register_email"
/>
<RelativeLayout
android:id="#+id/register_bottom_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_small"
android:layout_below="#id/register_blank"
android:layout_marginTop="#dimen/margin_xxxlarge"
android:gravity="bottom"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/register_agreetxt"
style="#style/Text_Body_2"
android:textColor="#color/secondary_grey_800"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom"
android:text="#string/agree" />
<TextView
android:id="#+id/register_terms"
style="#style/Text_Button_Normal"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_below="#id/register_agreetxt"
android:text="#string/terms"
android:gravity="center"/>
<TextView
android:id="#+id/register_policy"
style="#style/Text_Button_Normal"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_below="#id/register_agreetxt"
android:layout_marginLeft="#dimen/margin_xlarge"
android:layout_toRightOf="#id/register_terms"
android:text="#string/policy"
android:gravity="center_vertical"/>
<TextView
android:id="#+id/register_have_accnt"
style="#style/Text_Body_2"
android:textColor="#color/secondary_grey_800"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_below="#id/register_terms"
android:text="#string/already_have_an_account"
android:gravity="center"/>
<TextView
android:id="#+id/register_login"
style="#style/Text_Button_Normal"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_below="#id/register_terms"
android:layout_marginLeft="#dimen/margin_xlarge"
android:layout_toRightOf="#id/register_have_accnt"
android:text="#string/login"
android:gravity="center"/>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</layout>
Dont forget to add android:windowSoftInputMode="stateVisible|adjustResize" to your activity in your manifest file.
I have 3 HorizontalScrollview, for each of them i have customView contain editText that i add dynamically depend from web service response. The problem is, when i want to change value in editText and keyboard appear, the HorizontalScrollView scroll automatically to the end, so i can't properly input value to my edit text. My Question is how to prevent autoscroll in my HorizontalScrollView so i can input value on my editText? and the HorizontalScrollView only scroll when user scroll it.
I've search and my problem similar with this thread but the sugestion didn't work. here is workaround that i already try :
Add android:windowSoftInputMode="stateHidden|adjustNothing" or
android:windowSoftInputMode="stateHidden|adjustResize" already try
adjustPan and other combination but it's impact nothing.
Add android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true" in LinearLayout inside my HorizontalScrollView and it's also impact Nothing.
Add android:descendantFocusability="blocksDescendants the
HorizontalScrollView didn't scroll automatically but i can't change value on my edit text, it similar when i add android:focusable = "false" in my editText
Add code bellow but impact nothing.
scrollLayout.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS
scrollLayout.isFocusable = true
scrollLayout.isFocusableInTouchMode = true
scrollLayout.setOnTouchListener { v, event ->
v.requestFocusFromTouch()
false}
Here is My Code
private fun setUpList(rootLayout: LinearLayout, scrollLayout: HorizontalScrollView, items: List<ProductItem>) {
//config horizontal scroll view
scrollLayout.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS
scrollLayout.isFocusable = true
scrollLayout.isFocusableInTouchMode = true
scrollLayout.setOnTouchListener { v, event ->
v.requestFocusFromTouch()
false
}
// add customView to linear layout
rootLayout.removeAllViews()
for (item in items) {
val view = EditableThumbnailProduct(context)
view.setProductInfo(item)
view.setOnProductChangeListener(onChangeListener())
rootLayout.setBackgroundColor(Color.TRANSPARENT)
rootLayout.addView(view)
}
}
Here is my XML
<HorizontalScrollView
android:id="#+id/scrollMed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none">
<LinearLayout
android:id="#+id/contentMed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#android:color/transparent"
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal" />
</HorizontalScrollView>
Here is my custom view
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#android:color/transparent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:orientation="vertical">
<ImageView
android:id="#+id/img_thumbnail"
android:layout_width="150dp"
android:layout_height="120dp"
android:background="#color/gray_border"
android:scaleType="fitXY" />
<TextView
android:id="#+id/txt_product_name"
style="#style/DefaultTextTitle"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_margin="5dp"
android:ellipsize="end"
android:gravity="center"
android:maxLines="2"
android:text="#string/product_name_label" />
<TextView
android:id="#+id/txt_product_stock"
style="#style/DefaultTextSubTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:gravity="center"
android:text="#string/available_stock_label" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/gray_border" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<Button
android:id="#+id/btn_add_to_cart"
style="#style/WhiteTitle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:background="#drawable/round_button_orange"
android:gravity="center"
android:text="#string/buy_button_label"
android:visibility="invisible" />
<RelativeLayout
android:id="#+id/layout_plus_minus"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/btn_minus"
style="#style/WhiteTitle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:background="#drawable/round_button_blue"
android:paddingLeft="5dp"
android:paddingTop="11dp"
android:paddingRight="5dp"
android:paddingBottom="11dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/ic_minus" />
<ImageView
android:id="#+id/btn_delete"
style="#style/WhiteTitle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:background="#drawable/round_button_blue"
android:padding="3dp"
android:scaleType="fitXY"
android:visibility="invisible"
app:srcCompat="#drawable/ic_delete" />
<EditText
android:id="#+id/txt_cart_amount"
style="#style/LightTextTitle"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginLeft="40dp"
android:layout_marginRight="10dp"
android:backgroundTint="#color/blue_text_color"
android:inputType="number"
android:maxLength="3"
android:paddingBottom="5dp" />
<ImageView
android:id="#+id/btn_plus"
style="#style/WhiteTitle"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:background="#drawable/round_button_blue"
android:gravity="center"
android:padding="5dp"
android:scaleType="fitXY"
android:textSize="16sp"
app:srcCompat="#drawable/ic_plus" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
All sugestion will be appreciate. Thanks in advance.
I have a LinearLayout and a FrameLayout inside with some margin. The strange thing is that even though visibility is set to GONE the inner view's margin seems to have an influence on the elemtns surrounding it. This is just the case in runtime, not during design time in the IDE.
Android documentation states:
GONE - This view is invisible, and it doesn't take any space for layout purposes
So why does margin still have an influence?
What sense does it make (if it has any)?
And most importantly: How do I accomplish a view completely vanishing and have no effect whatsoever on its neighbours?
Of course you can just set the margin to 0 in code but that's really inconvenient when you have to toggle visibility frequently and maintaining correct margins.
Here is the layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/cardLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/cardContentStack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:id="#+id/cardImageLarge"
android:layout_width="match_parent"
android:layout_height="#dimen/card_image_big_height"
android:layout_marginBottom="#dimen/card_image_big_bottom_margin"
android:layout_marginLeft="#dimen/margin_base"
android:layout_marginRight="#dimen/margin_base"
android:visibility="gone"
tools:src="#drawable/some_image"
tools:visibility="visible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout
android:id="#+id/cardImageWrapper"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/margin_base">
<ImageView
android:id="#+id/cardImage"
android:layout_width="#dimen/card_image_size"
android:layout_height="#dimen/card_image_size"
android:visibility="gone"
tools:src="#drawable/some_image2"
tools:visibility="visible" />
<ImageView
android:id="#+id/cardImageSmall"
android:layout_width="#dimen/card_image_small_size"
android:layout_height="#dimen/card_image_small_size"
android:layout_margin="#dimen/margin_base"
android:visibility="gone"
tools:src="#drawable/some_image3"
tools:visibility="visible" />
</FrameLayout>
<LinearLayout
android:id="#+id/cardBodyContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/cardTextContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingBottom="#dimen/margin_base"
android:paddingRight="#dimen/margin_base"
android:paddingTop="#dimen/card_title_top_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.company.android.text.textview.BodyMedium
android:id="#+id/cardTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#color/midnight"
tools:text="Michele Theisen" />
<FrameLayout
android:id="#+id/cardTitleRightSpacer"
android:layout_width="#dimen/card_title_right_spacer"
android:layout_height="match_parent"
android:layout_weight="1"
android:visibility="gone"
tools:visibility="visible">
</FrameLayout>
</LinearLayout>
<com.company.android.text.textview.BodyLittle
android:id="#+id/cardBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/card_title_top_margin"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="2"
android:textColor="#color/twilight"
android:visibility="gone"
tools:text="Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text"
tools:visibility="visible" />
</LinearLayout>
<LinearLayout
android:id="#+id/cardButtonContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="horizontal"
android:paddingBottom="#dimen/margin_small"
android:paddingLeft="#dimen/margin_small"
android:paddingRight="#dimen/margin_small"
android:visibility="gone"
tools:visibility="visible">
<com.company.views.materialstextviews.CardButton
android:id="#+id/cardSecondaryButton"
android:layout_width="0dp"
android:layout_height="#dimen/card_action_button_height"
android:layout_marginRight="#dimen/margin_small"
android:layout_weight="1"
android:background="?android:attr/selectableItemBackground"
android:maxLines="1"
android:paddingLeft="#dimen/margin_xs"
android:paddingRight="#dimen/margin_xs"
android:singleLine="true"
android:textColor="#color/twilight"
android:visibility="gone"
tools:text="BUTTON TEXT MIGHT BE LONG"
tools:visibility="visible" />
<com.company.views.materialstextviews.CardButton
android:id="#+id/cardPrimaryButton"
android:layout_width="wrap_content"
android:layout_height="#dimen/card_action_button_height"
android:background="?android:attr/selectableItemBackground"
android:ellipsize="end"
android:maxLines="1"
android:paddingLeft="#dimen/margin_xs"
android:paddingRight="#dimen/margin_xs"
android:singleLine="true"
android:textColor="#color/green"
android:visibility="visible"
tools:text="BUTTON TEXT"
tools:visibility="visible" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/cardCloseButton"
android:layout_width="#dimen/card_close_button_size"
android:layout_height="#dimen/card_close_button_size"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#drawable/ic_card_close"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground" />
</RelativeLayout>
cardImageWrapper is the view that causes the problem.
I want to fit an Imageview according to Height and width of my RelativeLayout without disturbing its ratio. I am using a LinearLayout with a weightSum and adding two other layouts ie RelativeLayout with weight 20 and LinearLayout with weight 80. I am adding the imageview to the RelativeLayout to take up weight 20 but currently, my image takes up the width to its content and does not follow the width of the parent, thus resulting in pushing the other layout.
My Layout is as below:
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/order_detail_container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="1dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="100"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20">
<Droid.CenterFitImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:id="#+id/imgArticleThumb"
android:src="#drawable/Icon" />
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:padding="5dp"
android:orientation="vertical">
<Droid.CustomTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/txtCategory"
card_view:customFont="Fonts/Roboto-Bold.ttf"
android:textColor="#color/CatTitle"
android:textSize="12sp"
android:text="Category" />
<Droid.CustomTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/txtTitle"
android:ellipsize="end"
android:singleLine="true"
card_view:customFont="Fonts/Roboto-Bold.ttf"
android:textColor="#000000"
android:textSize="16sp"
android:text="Title" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
CenterFitImageView.cs
protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
try
{
if (Drawable != null)
{
int w = MeasureSpec.GetSize(widthMeasureSpec);
int h = (int)Math.Ceiling((float)w * (float)Drawable.IntrinsicHeight / (float)Drawable.IntrinsicWidth);
SetMeasuredDimension(w, h);
}
else
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
}
catch (Exception e)
{
base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
Expected output:
My output:
layout_width="wrap_content" on your root LinaerLayout, is where you have gone wrong. You have to use match_parent if you need your views to be aligned properly according to their weights.
Just remember that if you are using layout_weights and weightSum, then you have to give the root layout and specific width or height depending upon the arrangement. wrap_content, means display your layout, according to whatever size the child is.
So the resulting code will be
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:orientation="horizontal">
....
....
</LinearLayout>
And this is the output that I got
The first RelativeLayout with the ImageView is taking 20, and the other one with a TextView is taking 80.
First of all why you didn't use Linearlayout instead of RelativeLayout. When you are using all LinearLayout.
I will do this in this way.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/order_detail_container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="8dp"
android:layout_marginTop="16dp"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="1dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="40"
android:gravity="center">
<Droid.CenterFitImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:id="#+id/imgArticleThumb"
android:src="#drawable/Icon" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="60"
android:orientation="vertical"
android:layout_marginLeft="5dp">
<Droid.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtCategory"
card_view:customFont="Fonts/Roboto-Bold.ttf"
android:textColor="#color/CatTitle"
android:textSize="12sp"
android:text="Category" />
<Droid.CustomTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtTitle"
android:ellipsize="end"
android:singleLine="true"
card_view:customFont="Fonts/Roboto-Bold.ttf"
android:textColor="#000000"
android:textSize="16sp"
android:text="Title" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
Just used android:layout_height="wrap_content" instead of match_parent for ImageView and also set your textview height and width wrap_content.
replace the layout_width="0" of the image with maxwidth.
I have a problem where when I click on my keyboard, the contents of my screen doesn't get pushed up--rather the keyboard simply overlaps everything (namely covers my ListView) on my screen. This is the structure of my layout:
<ScrollView>
<RelativeLayout>
</RelativeLayout>
</ScrollView>
<ListView>
</ListView>
<LinearLayout>
<EditText></EditText>
</LinearLayout>
I nested my RelativeLayout in a ScrollView because I thought that would make my entire layout scrollable, which would "push up" all the layouts so that the user can view those parts while typing his or her response. I also have in my AndroidManifest.xml: android:windowSoftInputMode="adjustResize|stateVisible|stateAlwaysHidden" Here is an image of what is happening:
And here is my code. Any help would be appreciated!
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
xmlns:fresco="http://schemas.android.com/tools"
android:id="#+id/comments_coordinator_layout">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/comments_appbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/comments_coordinator_layout"
>
<RelativeLayout
android:layout_marginTop="?attr/actionBarSize"
android:id="#+id/view_post"
android:layout_width="match_parent"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:orientation="horizontal"
android:layout_height="175dp"
android:background="#e6e6e6">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_marginTop="15dp"
android:id="#+id/poster_picture"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
fresco:placeholderImage="#mipmap/blank_prof_pic"
fresco:roundedCornerRadius="5dp"
/>
<TextView
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="#id/poster_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:id="#+id/poster_name"/>
<TextView
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:layout_toRightOf="#id/poster_name"
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="#+id/post_date"/>
<TextView
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/poster_picture"
android:layout_below="#id/poster_name"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_status" />
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_marginTop="225dp"
android:id="#+id/lv_comments_feed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/send_message">
</ListView>
<LinearLayout
android:id="#+id/send_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<EditText
android:id="#+id/write_comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="5"
android:gravity="top|left"
android:hint="Comment back!"
android:inputType="textMultiLine"
android:scrollHorizontally="false" />
<Button
android:id="#+id/send_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:text="send"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
EDIT: My attempt to add in a parent LinearLayout for ScrollView. Issue is now:
The vertically scrolling ScrollView should not contain another vertically scrolling widget when I hover over ListView
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
xmlns:fresco="http://schemas.android.com/tools"
android:id="#+id/comments_coordinator_layout">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/comments_appbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/comments_coordinator_layout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="?attr/actionBarSize"
android:id="#+id/view_post"
android:layout_width="match_parent"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:orientation="horizontal"
android:layout_height="175dp"
android:background="#e6e6e6">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_marginTop="15dp"
android:id="#+id/poster_picture"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
fresco:placeholderImage="#mipmap/blank_prof_pic"
fresco:roundedCornerRadius="5dp"
/>
<TextView
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="#id/poster_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:id="#+id/poster_name"/>
<TextView
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:layout_toRightOf="#id/poster_name"
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="#+id/post_date"/>
<TextView
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/poster_picture"
android:layout_below="#id/poster_name"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_status" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_marginTop="225dp"
android:id="#+id/lv_comments_feed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/send_message">
</ListView>
<LinearLayout
android:id="#+id/send_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<EditText
android:id="#+id/write_comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="5"
android:gravity="top|left"
android:hint="Comment back!"
android:inputType="textMultiLine"
android:scrollHorizontally="false" />
<Button
android:id="#+id/send_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:text="send"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
EDIT: I think the best solution may be to create a custom ListView... that contains the same layout that is in each ListView item. Any suggestions or ideas on how to implement this?
EDIT: Tried #VanillaBoy's answer. I know the image produced is because I have marginTop = 225 dp but even if I remove it, when I click on my EditText, it only shows my ListView and my RelativeLayout above is hidden
EDIT: As per #Fllo's answer, I am still able to reproduce the error I had in the beginning with this layout, and so here is what I have in my layout. Also if it helps my Android Manifest looks like this android:windowSoftInputMode="adjustResize|stateAlwaysHidden">.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
xmlns:fresco="http://schemas.android.com/tools"
android:id="#+id/comments_coordinator_layout">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/comments_appbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/comments_coordinator_layout"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="?attr/actionBarSize"
android:id="#+id/view_post"
android:layout_width="match_parent"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:orientation="horizontal"
android:layout_height="175dp"
android:background="#e6e6e6">
<com.facebook.drawee.view.SimpleDraweeView
android:layout_marginTop="15dp"
android:id="#+id/poster_picture"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_marginLeft="10dp"
fresco:placeholderImage="#mipmap/blank_prof_pic"
fresco:roundedCornerRadius="5dp"
/>
<TextView
android:layout_marginLeft="5dp"
android:layout_marginTop="15dp"
android:layout_toRightOf="#id/poster_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:id="#+id/poster_name"/>
<TextView
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:layout_toRightOf="#id/poster_name"
android:layout_marginLeft="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="#+id/post_date"/>
<TextView
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/poster_picture"
android:layout_below="#id/poster_name"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_status" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:id="#+id/container_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/send_message">
</LinearLayout>
<LinearLayout
android:id="#+id/send_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<EditText
android:id="#+id/write_comment"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="5"
android:gravity="top|left"
android:hint="Comment back!"
android:inputType="textMultiLine"
android:scrollHorizontally="false" />
<Button
android:id="#+id/send_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center"
android:text="send"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
Using an inner ListView with ScrollView should be avoided in Android. Indeed, these layouts have their own scroll detections and gestures. The solutions are to prevent the scroll gesture on the list or to calculate the height and expand it by default. But this will make too much work and don't respect the Android pattern and guidelines.
Instead of replace ListView by LinearLayout and keep the parent's ScrollView, as my old answer pointed out, I found a possible workaround, which has these steps:
Create your own ListView
Override onSizeChanged() which redraws the ListView when keyboard is opening
Get the lastVisiblePosition and retrieve its height in the previous method
Finally, move the list to the previous lastVisiblePosition and calculate the offset
Beside, set a headerView for the header picture container
And you need to update the child item's height before the user opens the
keyboard, so you could update the value in onClickListener of the
EditText
And it needs this kind of layouts:
layout's activity:
<CoordinatorLayout>
<AppBarLayout>
...
</AppBarLayout>
<RelativeLayout>
<CustomListView />
<LinearLayout>
...
</LinearLayout>
</RelativeLayout>
</CoordinatorLayout>
header's layout (which will be above the list):
<RelativeLayout>
<SimpleDraweeView />
<TextView />
...
</RelativeLayout>
Hum, that's all... So, good to go:
Create a Listview:
public class CustomListView extends ListView {
// last height item variable (updated from Activity)
public int lastItemHeight = 0;
public CustomListView(Context context) {
super(context, null);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
Override onSizeChanged():
#Override
protected void onSizeChanged(int w, final int h, int oldw, int oldh) {
// save last position visible before resize
final int lastPosition = super.getLastVisiblePosition();
// call super SizeChanged method
super.onSizeChanged(w, h, oldw, oldh);
// after resizing, show the last visible item at the bottom of new listview's height, above the edit text
// see : http://developer.android.com/reference/android/widget/AbsListView.html#setSelectionFromTop(int, int)
super.setSelectionFromTop(lastPosition, (h - lastItemHeight));
}
That's all for the ListView.
The lastItemHeight variable will be updated when the user clicks on the input (below the list, thanks to setting your "header" picture container as a headerView in your Activity).
Now, let see the Activity:
Setup your custom ListView and your Adapter:
// get custom listview element
final CustomListView list = (CustomListView) findViewById(R.id.container_list);
// example items child
ArrayList<String> items = new ArrayList<>();
for (int n=0; n<25; ++n) items.add("Blablabla n."+n);
// example adapter
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.item_list_text_simple, android.R.id.text1, items);
Create the HeaderView and set the Adapter:
// prepare the header content with picture
View header = getLayoutInflater().inflate(R.layout.test_header_image, null);
// set datas to the header elements (example)
Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/fresco-logo.png");
SimpleDraweeView draweeView = (SimpleDraweeView) header.findViewById(R.id.poster_picture);
draweeView.setImageURI(uri);
// add the header to listview
list.addHeaderView(header, null, false);
// set the adapter
list.setAdapter(adapter);
Handle the softkeyboard open with EditText.setOnClickListener:
// when the user clicks on EditText...
editMessage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// set a new Thread
list.post(new Runnable() {
#Override
public void run() {
// get last position of child
int lastPosition = list.getLastVisiblePosition() - 1;
// if the list can give the view's last child
if (list.getChildAt(lastPosition) != null) {
// update the height of the last child in custom listview
list.lastItemHeight = list.getChildAt(lastPosition).getHeight();
}
}
});
}
});
That's all for the code! And the last thing (Ow.. where is the end?), here's the layouts:
The main layout of Activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/comments_coordinator_layout"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<android.support.v7.widget.Toolbar ... />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
<com.package.name.CustomListView
android:id="#+id/container_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/send_message"/>
<LinearLayout
android:id="#+id/send_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<EditText ... />
<Button ... />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
The head layout of ListView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/view_post"
android:layout_width="match_parent"
android:layout_height="225dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:orientation="horizontal"
android:background="#e6e6e6">
<com.facebook.drawee.view.SimpleDraweeView ... />
<TextView ... />
<TextView ... />
<TextView ... />
</RelativeLayout>
Aw! I've finally finished. I really hope you will be able to have the right behaviour. I tested it, and it seems to work good enough.
Here's another workaround with adjustPan, but you have to keep your list starting at the bottom. Whereas this solution keeps any child at the bottom of the list when it's resize.
Hope you'll enjoy it ;)
add this in your manifest under Activity
<activity .......
....................
android:windowSoftInputMode="adjustResize"/>