YBDevi YBDevi - 3 months ago 14
Android Question

In Scroll View Edit text has scroll both are worked but in edit text if we type no of lines user can't see edit text it is scroll to top.

In my layout i have scroll view in that scrollable edit text.Both are working by using

setOnTouchListener
.if we touch edittext it is scroll and layout scroll is interact.
But the problem is when user typing edittext it is moving top of the layout and user can't see edit text field.
At the time user typing both scrolls are working.
i think solution will be when user is clicked on edit text we need to stop layout scroll.In that way edit text will visible to user and it is not moving when user typing.But i don't know how to implement it if anyone know please help me.

Layout code:-

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Welcome To Register Page"
android:textSize="20dp"
android:textStyle="bold"
android:id="@+id/title"
android:textColor="@android:color/holo_blue_bright"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:layout_marginTop="5dp"
android:orientation="vertical"
android:id="@+id/lin1"
android:background="@drawable/shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Personal Details"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fname"
android:inputType="textCapSentences"
android:imeOptions="actionNext"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lname"
android:inputType="textCapSentences"
android:imeOptions="actionNext"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone No :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/pno"
android:inputType="number"
android:imeOptions="actionNext"
android:layout_marginLeft="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/lin1"
android:id="@+id/lin2"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:background="@drawable/shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Address"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="City/Town :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/city"
android:inputType="textCapSentences"
android:imeOptions="actionNext"
android:layout_marginLeft="5dp"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="State :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/state"
android:inputType="textCapSentences"
android:imeOptions="actionNext"
android:layout_marginLeft="5dp"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/country"
android:inputType="textCapSentences"
android:imeOptions="actionNext"
android:layout_marginLeft="5dp"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zip Code :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/zipcode"
android:inputType="number"
android:imeOptions="actionNext"
android:layout_marginLeft="5dp"/>

</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lin3"
android:layout_below="@id/lin2"
android:orientation="vertical"
android:layout_marginTop="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Full Address"
android:gravity="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="10"
android:lines="10"
android:maxEms="1000"
android:scrollbars="vertical"
android:layout_marginTop="5dp"
android:id="@+id/address"
android:gravity="top"
android:background="@drawable/shape"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lin4"
android:layout_below="@id/lin3"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:background="@drawable/shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Other Details"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-Mail :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email"
android:singleLine="true"
android:imeOptions="actionDone"
android:layout_marginLeft="5dp"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Birth :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dob"
android:focusable="false"
android:imeOptions="actionNone"
android:layout_marginLeft="5dp"/>

</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lin5"
android:layout_below="@id/lin4"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:background="@drawable/shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Other Details"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-Mail :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email_dum"
android:singleLine="true"
android:imeOptions="actionDone"
android:layout_marginLeft="5dp"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Birth :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dob_dum"
android:focusable="false"
android:imeOptions="actionNone"
android:layout_marginLeft="5dp"/>

</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lin6"
android:layout_below="@id/lin5"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:background="@drawable/shape">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Other Details"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E-Mail :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email_dum1"
android:singleLine="true"
android:imeOptions="actionDone"
android:layout_marginLeft="5dp"/>

</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date of Birth :"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dob_dum1"
android:focusable="false"
android:imeOptions="actionNone"
android:layout_marginLeft="5dp"/>

</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/lin6"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/submit"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/clear"
android:text="Clear"
android:layout_marginLeft="5dp"/>

</LinearLayout>
</RelativeLayout>
</ScrollView>


Main Activity_code:-

public class MainActivity extends AppCompatActivity {

EditText first_name,last_name,phone_no,city,state,country,zip_code,full_address,email,dob;

Button submit,clear;
private int year;
private int month;
private int day;
private Calendar cal;

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

first_name=(EditText)findViewById(R.id.fname);
last_name=(EditText)findViewById(R.id.lname);
phone_no=(EditText)findViewById(R.id.pno);
city=(EditText)findViewById(R.id.city);
state=(EditText)findViewById(R.id.state);
zip_code=(EditText)findViewById(R.id.zipcode);
country=(EditText)findViewById(R.id.country);
full_address=(EditText)findViewById(R.id.address);
email=(EditText)findViewById(R.id.email);
dob=(EditText)findViewById(R.id.dob);

submit=(Button) findViewById(R.id.submit);
clear=(Button) findViewById(R.id.clear);

cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);


submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent intent=new Intent(MainActivity.this,result.class);

Bundle bundle=new Bundle();
bundle.putString("First_Name",first_name.getText().toString());
bundle.putString("Last_Name",last_name.getText().toString());
bundle.putString("Phone_No",phone_no.getText().toString());
bundle.putString("City",city.getText().toString());
bundle.putString("State",state.getText().toString());
bundle.putString("Country",country.getText().toString());
bundle.putString("Zip_Code",zip_code.getText().toString());
bundle.putString("Full_Address",full_address.getText().toString());
bundle.putString("Email",email.getText().toString());
bundle.putString("DOb",dob.getText().toString() );

intent.putExtras(bundle);

startActivity(intent);
}
});

clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

first_name.setText("");
last_name.setText("");
phone_no.setText("");
city.setText("");
state.setText("");
country.setText("");
zip_code.setText("");
full_address.setText("");
email.setText("");
dob.setText("");

}
});

dob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DateDialog();
}
});
full_address.setOnTouchListener(new EditText.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;

case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}

// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});
}

private void DateDialog() {
DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
{

dob.setText(dayOfMonth+"/"+monthOfYear+"/"+year);

}};


DatePickerDialog dpDialog=new DatePickerDialog(this, listener, year, month, day);
dpDialog.show();
}
}


u can check this code.

Thanks in Advance.

Answer

Following is my answer i found from other SO questions and google.It will help when edittext is focused parent scroll is not work.

full_address.setOnTouchListener(new EditText.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    int action = event.getAction();
                    switch (action) {
                        case MotionEvent.ACTION_DOWN:
                            // Disallow ScrollView to intercept touch events.
                            v.getParent().requestDisallowInterceptTouchEvent(true);
                            break;

                        case MotionEvent.ACTION_UP:
                            // Allow ScrollView to intercept touch events.
                            v.getParent().requestDisallowInterceptTouchEvent(false);
                            break;
                    }

                    // Handle ListView touch events.
                    v.onTouchEvent(event);
                    return true;
                }
            });

Still answer is incomplete.

Following links will help you.

Stop Scroll view

Stop scrollview from seting focus.