Is there any way to create a view above all the others,so when this view is visible, I cannot interact with other views except this one. Sounds like a lock screen.And yeah,that's what I really want to achieve. Any suggestion?
Use a RelativeLayout which fills parent both in width and height. Let this layout have a child view which could be any view.
Put the view on top and set a onTouchlistener.
view.bringToFront();
view.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
That is, have a touch listener that takes all touch events and consumes them (return true).
Use RelativeLayout and ViewGroup.bringChildToFront for the View you want to be above all others.
When adding element to RelativeLayout set layout_height and layout_width to 'match_parent'
Related
I have created a scroll view with a EditText in the middle that is multiLine (Scrollable). When that view is edited and lines are added beyond the allowed height it scrolls as expected. However, the parent scroll view for the whole container also scrolls as if it is following the text.
<ScrollView
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:background="#F7F3DE"
p1:id="#+id/scrollview">
<RelativeLayout
p1:layout_width="match_parent"
p1:layout_height="match_parent"
p1:clickable="true"
p1:focusableInTouchMode="true"
p1:id="#+id/realtiveLayout">
<EditText
p1:id="#+id/editText"
p1:focusableInTouchMode="true"
p1:layout_width="match_parent"
p1:layout_height="150dp"
p1:hint="Comments"
p1:background="#00000000"
p1:textSize="16sp"
p1:textColor="#555555"
p1:gravity="top"
p1:minLines="5"
p1:maxLines="5"
p1:inputType="textCapSentences|textMultiLine"
p1:scrollHorizontally="false"
p1:layout_marginTop="5dp"
p1:textColorHint="#A9A9A9" />
</RelativeLayout>
</ScrollView>
Has anyone seen or know of a resolution for this issue?
(Note: This is not a question of how to scroll one instead of the other by touch as that I already understand. It is a question of the main scrollview moving while typing inside the EditText even though the text is not going lower but scrolling instead.
There is an answer here:
https://stackoverflow.com/a/28180281/3956566
Using this Managing Touch Events in a ViewGroup
Each child touch needs to provide an intercept that returns the parents touch event as false. To disable the parents touch event whilst the child element is being used. You can do this by creating an onInterceptTouchEvent(MotionEvent ev) in your java.
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
/*
* This method JUST determines whether we want to intercept the motion.
* If we return true, onTouchEvent will be called and we do the actual
* scrolling there.
*/
// ...
// In general, we don't want to intercept touch events. They should be
// handled by the child view.
return false;
}
EDIT
This answer provided by
https://stackoverflow.com/users/498468/carl-odonnell
should help:
https://stackoverflow.com/a/5090420/3956566
Where the scrollview is disabled when the text field is touched.
// Get the ScrollView
final ScrollView myScroll = (ScrollView) findViewById(R.id.display_scrollview);
// Disable Scrolling by setting up an OnTouchListener to do nothing
myScroll.setOnTouchListener( new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
// Enable Scrolling by removing the OnTouchListner
tvDisplayScroll.setOnTouchListener(null);
I have two rows of imageViews in a ScrollView. The imageViews all have onClickListener and when i want to scroll in this area it doesn't work. So i guess the Click listeners intercept the scrolling of the ScrollView. What's the best way to change this behaviour ?
My View hierarchy is like this:
<ScrollView>
<RelativeLayout>
<FrameLayout>
</FrameLayout>
<RelativeLayout>
</ScrollView>
in the FrameLayout i put a Fragment which has a LinearLayout in which i inflate other LinearLayouts like this:
productHolder.productLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
}
});
Maybe you could use an OnTouchListener instead of an OnClickListener.
In the OnTouchListener you can check if the action of the motion event was a click and consume the event (return true). Otherwise you can leave the event for other listeners (return false) to consume.
Note, that you might need to add some additional implementation for figuring out which image view was clicked, however without seeing your layout, it's hard to give you a hint how to do it.
See the picture, i have a button and it is behind an invisible view (the red line), lets say a gridview, or just simply LinearLayout. Is there any possible way that will let me touch or click the button behind this invisible view? Thank you.
NOTE : i have my reason why i need the button behind the view, i just illustrated it using this picture for you guys to know what i meant. The button has to behind the view :))
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
Declare it in your layout.
Let me know if it helps you.
Yes Its possible. Until or unless your invisible view is not clickable. so check your invisible layout structure. make android:clickable="false"
1 way you can do this is:
1. add an onTouch(View view, MotionEventevent) Listener to your layout
2. get the buttons bounds
3. Check if the touch event was done inside the bounds
The code should look something like this:
Button button;
Rect rect;
onCreate(){
rect = button.getClipBounds();
layout.setOnTouchListener(this)
}
onTouch(View view, MotionEvent event){
if(rect.contains(event.getX(), event.getY())
//insert action here
}
I am having a listview inside a scrollview, but the problem is that the scrollview is scrolling but listview is not scrolling. I think this is due to that scrollView.
Can somebody who has a working solution post it here as reference?
Generally, you cannot put scrollable things inside other scrollable things, where they scroll in the same direction, and have the results be reliable. Occasionally this works (e.g., WebViews in a ViewPager), but that is the exception, not the norm.
Either:
Move the ListView out of the ScrollView, or
Move all the rest of the contents of the ScrollView into the ListView, whether using things like addHeaderView() or my MergeAdapter
If you put your ListView/any scrollable View inside the scrollView it will not work properly because when you touch the screen ,main focus of your touch is on parent view(scrollView ) not the child View (ListView).
ListView must have fixed height as below in your XML file
<ListView android:id="#+id/lv"
android:listSelector="#0f0"
android:layout_width="fill_parent"
android:layout_height="500px" />
In Java file, write below code after setContentView()
lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(your adapter here); // you have to add your adapter here
lv.setOnTouchListener(new ListView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
lv.scrollBy(0, 1);
}
return false;
}
});
Make these changes to your code and test it. After too many experiments i written this code. It is working 100% fine.
I am using relativelayout and inside it a scrollview and inside it a linear layout and a textview which is ensuring scrolling of textview, but I do not want to scroll the text after the particular condition. So, what can I do for this?
To make the scrollbars invisible use this attribute.
android:scrollbars="none"
Edit:
if there is need of runtime changes on visibility or state of enabling, for that we can use the scrolview.setEnabled(false) or android:enabled=false.
There is no direct way to stop scrollview to scroll but u can do it in other way Like:
//global boolean variable
boolean enable = true;
scrollview.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
return !enable;
}
});
when you set enable variable to true it will start scrolling and when you set it to false it stop scrolling.
If I interpreted your question correctly, you probably want to extend ScrollView and write your own implementation of onInterceptTouchEvent. You could then make your ScrollView conditionally ignore touch events, thus disabling the scrolling behavior. See this question for more details.