Start outSide scroll after edittext scroll finished - android

I've tried every simple examples on stackoverflow but nothing can solve my problem. Here's my question
I have ScrollView in activity and I have scrollbars="vertical" option editText inside ScrollView.
I want to scroll outside scrollview's scroll after inside edittext scroll finish, like on top of the edittext scroll or bottom of the edittext scroll
I thought that it can be done with view catchs scroll lisetener but nothing worked.

Try the below sample code hope it works for you.
In XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#aa8822"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#aa8822"
android:orientation="vertical" >
<EditText
android:id="#+id/edt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:ems="10"
android:maxLines="3"
android:scrollbars="vertical"
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold" >
<requestFocus />
</EditText>
</LinearLayout>
</ScrollView>
Put some more view so the scroll view scroll works.
Now in your activity-
EditText edt = (EditText) findViewById(R.id.edt);
edt.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.edt) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});

Related

Scrolling is not working when I set GestureListener to ScrollView

I have a ScrollView and a nested TextView. When I set setOnTouchListener to it,the gestures are recognized but the scrolling not working. And If I set setOnTouchListener to nested TextView,its working fine. I have tried by googling but could not solve the problem.
But my need is to set the setOnTouchListener to ScrollView.
Please help.
layout.xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:id="#+id/scrollView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:background="#00FF00"
android:padding="15dp"
android:textSize="18sp" />
</LinearLayout>
</ScrollView>
MainActivity.java
mGestureDetector = new GestureDetectorCompat(this,this);
findViewById(R.id.scrollView).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, final MotionEvent event) {
Log.e("Stark", "setOnTouchListener");
mGestureDetector.onTouchEvent(event);
return true;
}
});
Why you need to set onTouch. If only scroll up and down. Try below
ScrollView sv;
sv.post(new Runnable() {
#Override
public void run() {
sv.fullScroll(View.FOCUS_DOWN);
}
});
make sure you have this in scrollview xml. hope it helps.
android:fillViewport="true"

Gather Touches from MainActivity

I want to gather all the possible touches from my entire screen. I implemented a
#Override
public boolean onTouchEvent(MotionEvent e)
{
double x = e.getX();
double y = e.getY();
}
But now I added a TableView to the "scene" and when I click on it the above method is not being called because the TableView is swallowing the Touches.
Everything around the yellow area is clickable but the yellow area itself not. I want it to be clickable as well, basically I want that all the time the WHOLE screen area is clickable so I can store touch positions. How to do it?
EDIT:
The onTouchEvent method is in MainActivity class.
Here is the activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:shrinkColumns="*"
android:stretchColumns="*" >
<TableRow android:layout_height="wrap_content">
<TextView
android:id="#+id/column1"
android:text="tapcount"
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
cover whole view with your own transparent, like this:
<RelativeLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!--your content inside-->
</LinearLayout>
<View
android:id="#+id/cover"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</RelativeLayout>
then use your code for View with id=cover and return always false in your onTouchEvent as in doc:
Returns
True if the event was handled, false otherwise.
with false hardcoded MotionEvent will be delivered to views below/under
in your onCreate() method try implementing something like this
View contentView = (View) findViewById(R.id.linearLayout1); //Your layout
contentView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
Toast.makeText(getApplicationContext(), event.getX() + " " + event.getY(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});

scrollView.requestFocus() returns true, but it not works

All,I hava a scroll contains a editText.
Once I clicks the editText ,it gets focus and a SoftInput shows.
In my mind, if I clicks the blank space , the softinput hide, and editText lose the focus, and scrollview get focus.
So my code like this:
scrollView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
//call twice, once down, once up
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (activity.getCurrentFocus() != null && activity.getCurrentFocus().getWindowToken() != null) {
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
scrollView.requestFocus();
activity.getCurrentFocus();
}
}
return false;
}
});
ScrollView
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/scrollView"
android:layout_below="#+id/topBar"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
But after all, editText still focus(activity.getCurrentFocus() == editText), any advices?
Use LinearLayout instead of ScrollView. Or wrap EditText with LinearLayout below.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollView"
android:layout_gravity="center_horizontal" >
<LinearLayout
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout"
android:layout_gravity="center_horizontal" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/editText" />
</LinearLayout>
</ScrollView>

ScrollBar doesnt work in EditText

Hope all of you doing well.
My Problem is:
I have xml code like following:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_left_margin"
android:layout_marginRight="#dimen/activity_right_margin"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_bottom_padding"
android:paddingTop="#dimen/activity_top_padding" >
<EditText
android:id="#+id/custDetailsNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/name_cust_all_add"
android:inputType="textPersonName"
android:textSize="#dimen/common_fontsize" />
<EditText
android:id="#+id/custDetailsDescEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:hint="#string/description_add_all_cust"
android:inputType="textMultiLine"
android:lines="5"
android:minLines="3"
android:gravity="top|left"
android:scrollbars="vertical"
android:scrollbarStyle="insideInset"
android:overScrollMode="always"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:textSize="#dimen/common_fontsize" />
</LinearLayout>
</ScrollView>
In that i have two EditText in </ScrollView>, Second <EditText/> does not scrolling inner side. Problem is Full View is scrolled while i am scrolling but i want to also scroll vertically in second EditText when user enter more than 5 lines. In that ScrollBar is displaying but not scrolling.
How can i solve this.
Your Help would be appreciated.
This code should work.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.parentScrollView).setOnTouchListener(parentListener);
findViewById(R.id.custDetailsDescEditText).setOnTouchListener(childListener);
}
OnTouchListener parentListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
findViewById(R.id.custDetailsDescEditText).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
};
OnTouchListener childListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
};
Try something like this in your code
ScrollView scroller = new ScrollView(this);
EditText et=(EditText)findViewById(R.id.yourEdittext);
scroller.addView(et);

EditText Scrollable with keyborad active

Well friend ..
I have a design so:
The main Scroll is very necessary when appear the keyboard... If i dont make that, the button can't see...
The problem is when i try move inside the Edittext with the keyboard active...
I tried this:
android:scrollbars="vertical"
and this:
setMovementMethod(new ScrollingMovementMethod());
but not solve this yet
Anyone help me?
grax advance!
Check this without using scroll bar:
add permission in menifest file for corresponding activity.
android:windowSoftInputMode="adjustPan"
Please try this:
Your layout should be similar to example:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/edittext"
android:layout_width="200dp"
android:layout_height="500dp"
android:text="some long text"
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</ScrollView>
and in your java try this:
EditText edit = (EditText)findViewById(R.id.edittext);
edit.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v.getId() == R.id.edittext) {
v.getParent().requestDisallowInterceptTouchEvent(true);
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
});
It could be helpful.

Categories

Resources