I'm using a GridView for a game board. Recently some users have had problems with the board scrolling vertically (on Samsung Galaxy / Vibrant phones running 2.2) -- This bug does not occur on my Nexus One.
One user produced some screenshots of the problem.
How could I lock the GridView in place? Is there a way to disable scrolling?
Try to add or override setOnTouchListener for GridView,
then in onTouch method you can use code like this to
make gridView not scrollable
Java
gridView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return event.getAction() == MotionEvent.ACTION_MOVE;
}
});
Kotlin
gridView.setOnTouchListener { v, event ->
event.action == MotionEvent.ACTION_MOVE
}
You can try setEnabled(false), although it might have other side effects. GridView is really not meant to be used the way you are using it. You should create your own custom view or layout. You could also use a TableLayout.
Related
The normal OnTouch behavior of EditText is that it gets the focus and becomes ready for typing.
How to extend the OnTouch behavior of EditText without affecting its normal OnTouch behavior?
When I try to write code in onTouch method, I found that the original normal behavior is missed, and the only behavior is the defined in the method. It is expected but how to keep the original behavior alongside the new extending behavior.
The extending behavior can be something like showing Toast or changing the background of the EditText and so on.
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
//The original behavior plus the new behavior.
}
}
You should return false; — that should be all you need to do.
I have been trying to hack a ListView into a ScrollView. As many threads have pointed out, this is a very bad practice (see How can I put a ListView into a ScrollView without it collapsing?). I am currently measuring the height of my ListView and fixing its size using the solution proposed here (https://stackoverflow.com/a/3495908/924217), which works perfectly for equal height rows, but is measured incorrectly for variable height (reports min height for all rows). For quick and dirty fix, does anyone know how to measure the height properly for variable height rows?
More appropriately, can anyone point me to a good workaround using LinearLayouts. I am trying to use a ListView because I have items which I need to dynamically add and remove to the list. I cannot think of any convenient ways of doing this without adapters and the other niceties appropriated by a ListView. Can someone help me find an example of this being handled properly? Also, are there any open source projects which cover this? I have seen many people asking similar questions with little non-hacky resolve.
I would recomend that you should use listfragment so that you can dynamically add/remove items.
here parentScroll = your main scrollview and childScroll = your listview
parentScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.v("PARENT", "PARENT TOUCH");
findViewById(R.id.child_scroll).getParent()
.requestDisallowInterceptTouchEvent(false);
return false;
}
});
childScroll.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
Log.v("CHILD", "CHILD TOUCH");
// Disallow the touch request for parent scroll on touch of
// child view
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
Can we create multi touch in Android?
I have multiple images and have to drag and drop that images together on
my device. Is it possible? if it is possible then any ideas would help a lot.
Thanks
You need to intercept touch events in your view. Implement an onTouchListener and inside the function:
onTouch (View v, MotionEvent event){
int count=getPointerCount();
if(count >1){
// it means there are more than 1 point touched on the screen.
}
}
This is available only since API 5
This question is a bit awkward. Is it possible to transfer the touch focus of one view to another? Basically, say that you have a view that picks up the first ACTION_DOWN touch event, and then immediately wants to transfer the focus for all touch events to another view to handle with it's onTouchEvent(MotionEvent event). I thought that doing the following would make it work, but it didn't:
#override
public boolean onTouchEvent(MotionEvent) {
this.clearFocus();
anotherView.setFocusableInTouchMode(true);
anotherView.requestFocus();
}
Obviously, it seems to me that it just doesn't work that way. Could someone explain to me how I can go about doing something like this?
If it's still a bit difficult to understand my question, think about a regular button. When pressed, the button is highlighted (focused) and if you move your finger off the button but still keep your finger on the screen, the button becomes unfocused but still has control of the entire touch events (no other view can become focused even if you move over them). My question asks if it is possible to transfer this touch focus to another view to handle without having you to remove your finger off the screen.
Try below code might work
button1.setOnFocusChangeListener(new OnFocusChangeListener()
{
#Override
public void onFocusChange(View arg0, boolean hasFocus)
{
if(hasFocus)
button2.requestFocus();
}
});
The way I worked around this was to have a single view "harness" whose sole purpose was to pass on the touch information to the other classes, who would then do the processing. Note that ViewA and B don't actually have to be views and extend the view class. Its not an ideal solution, but I don't think it's possible to solve this problem with the current Android framework (ICS/JB).
class ViewHarness extends View{
public boolean onTouch(MotionEvent event){
if(ViewA is selected)
ViewA.onTouch(event);
else
ViewB.onTouch(event);
}
I want to create an event to change what my image button looks like, but only when it is being pushed down. So far I have been using an onTouch listener, but that just changes it permanently. I can't find anything like an onKyeUpListener() type of thing for the button.
Does anything like this exist?
SOLVED
final ImageButton button = (ImageButton) findViewById(R.id.ImageButton01);
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == (MotionEvent.ACTION_UP)){
//Do whatever you want after press
}
else{
//Do whatever you want during press
}
return true;
}
});
It's in the docs. You can define different images for different states via XML.
Instead of doing it manually, why don't you change the drawable of your buttons? In general, drawables can have multiple states, so if you change the drawable for certain states (like focused or selected), the operating system will handle everything for you automatically.
From http://developer.android.com/guide/topics/ui/ui-events.html:
onTouch() ... This is called when the user performs an action qualified as a touch event, including a press, a release, or any movement gesture on the screen (within the bounds of the item).
In other words, onTouch is also called for release events. Look at MotionEvent.getAction().