I am using Horizontal Scroll View
containing Relative Layout
with 6 edittext
in it .All edittext
are displayed horizontally in view
MyXml.xml
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/horizontalView">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!--android:weightSum="6"-->
<android.support.v7.widget.CardView
android:id="@+id/sv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:imeOptions="actionNext"
android:ems="3"
android:inputType="number"
android:maxLength="2"
android:singleLine="true" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv1"
android:id="@+id/sv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="4"
android:imeOptions="actionNext"
android:inputType="textCapCharacters"
android:maxLength="1"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv2"
android:id="@+id/sv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="4"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="2"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv3"
android:id="@+id/sv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="3"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="4"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv4"
android:id="@+id/sv5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="5"
android:imeOptions="actionNext"
android:inputType="number"
android:maxLength="1"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_toRightOf="@+id/sv5"
android:id="@+id/sv6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/edt6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="end"
android:gravity="center"
android:hint="hintvalue"
android:ems="5"
android:inputType="number"
android:maxLength="3"
android:singleLine="true"
android:text="" />
</android.support.design.widget.TextInputLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</HorizontalScrollView>
Question : Now whenever I clicked on first Edittext
(or any edittext). horizontal scrollview
automatically scroll to the end position. I dont know about this behaviour. why horizontal scroll view automatically scrolled?
What I have tried
1) I tried to call smoothscroll
to 0,0 position on first edit text focus change.
edt1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b){
hv1.smoothScrollTo(0,0)
}
}
});
2) I also tried with creating one custom class
extending horizontal scrollview
and change postion to 0 in onLayout
overrided method
public class HorizontalCustomScrollView extends android.widget.HorizontalScrollView {
private boolean enableScrolling = true;
public boolean isEnableScrolling() {
return enableScrolling;
}
public void setEnableScrolling(boolean enableScrolling) {
this.enableScrolling = enableScrolling;
}
public HorizontalCustomScrollView(Context context) {
super(context);
}
public HorizontalCustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HorizontalCustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public HorizontalCustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onLayout (boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
this.scrollTo(0,0);
}
}
3) I also try to give direction to fullScroll
to FOCUS_LEFT
and also with FOCUS_RIGHT
in onLayout
overrided method but this is also not worked.
@Override
protected void onLayout (boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//this.scrollTo(0,0);
this.fullScroll(HorizontalScrollView.FOCUS_LEFT);
//and also try with Focus right
//this.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
Before Focus
After Focus on Edit text 1 (or 2,3 or any other edittext .. Horizontal Scroll moves to last automatically.)
But still Horizontal scroll view automatic scroll to last position . How can I make it stop?
I am able to resolve my Problem here but still dont know why this is happening.
I remove all the properties of editext that are defined and added that all properties one by one and check which property causing issue and I found out that when I add gravity="center"
property to edittext again horizontal scroll view scrolls to end.
So It is happening because of gravity="center" property of edittext.
But still I don't know why gravity that is of edittext is direct affecting to horizontal scroll view weird behavior . is gravity is somehow related to content view of parent container ?
If any one have answer about this behavior please share your views.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With