I have designed an UI with some 5-6 buttons and 2 galleries. On click of a button a listView is shown partially (does not cover the entire screen) from top right corner. I want to disable the onClick and scrolling of other buttons and galleries when the listView is visible.
That is I want to achieve a scenario similar to that of alertDialog. I don't want to individually disable the onClick of each button as I may need to add or remove buttons later and maintaining the code may become a tedious work. Is there any way to to disable the onClick and scrolling in general.
PS : It would also be helpful if the onClick and scrolling of a layout can be disabled. In that case I can disable the onClick of all layouts other than that of the listView that pops up
use this-
button.setEnabled(false);
or
button.setVisibility(View.INVISIBLE);
You can use viewgroup like this
and can disable the click event of that view group.
Related
I was trying to make an area so when ToggleButton is on, you are able to toggle buttons within this area and if ToggleButon is off, you won't be able to toggle the buttons within this area.
For this specific area I used a FrameLayout, so I can stack two LinearLayout on top of each other. So when I hit the Edit-Button, the one on top which is half transparent disappears with setVisibility(View.GONE) and you are now allowed to click the buttons now.
PROBLEM: The buttons in this area are always clickable
I was able to toggle them separately with button.setClickable(true/false) but is there a solution so you just can't click through the LinearLayout that is on top (like "not-through-clickable")?
Link to an image of the Layout: https://i.imgur.com/eTyhCDc.png
Desired behavior:
Hit EDIT -> half transparent Layout on top of blue Layout disappears -> TESTOFF1 and TESTOFF2 are now clickable.
-> Hit DONE -> the half transparent Layout appears on top again and the buttons below it are not clickable anymore (without using .setClickable for every single button).
The view on top gets first crack at any touch events. If the top view doesn't handle the touch then the view below get a chance on down to the bottom view. The top layer is a LinearLayout so, but default, it doesn't handle touches, so it allows the touch events to percolate down to the buttons which are happy to respond.
One way to resolve this is to place a touch listener on the semi-transparent view that just returns true. Thus, the LinearLayout will consume the events and not let the buttons see them.
When the view is gone, the buttons become the top view so they will see the clicks.
If you want to know more about how touch events are handled, read this Stack Overflow answer for an excellent explanation.
editButton.setOnClickListener {
button1.isEnabled = !button1.isEnabled
button2.isEnabled = !button2.isEnabled
editButton.setText(
if (button1.isEnabled) {
R.string.done
} else {
R.string.edit
}
)
transparentView.isInvisible = button1.isEnabled
}
This example is using kotlin and the Android KTX libraries
Java version
editButton.setOnClickListener(new OnClickListener {
button1.setEnabled(!button1.isEnabled());
button2.setEnabled(!button2.isEnabled());
editButton.setText(button1.isEnabled() ? R.string.done : R.string.edit);
transparentView.setVisibility(button1.isEnabled() ? View.INVISIBLE : View.VISIBLE);
});
I want to click a button which help me scroll any screen on the android. The button will be presented as a widget which will be overlay on the top of all the apps. Is there any API or method available in Android to do so? If not, how can it be achieved?
Try wrapping your whole layout inside a ScrollView and move that ScrollView on the button's click using the fullScroll method.
your_scroll_view.fullScroll(View.FOCUS_DOWN);
your_scroll_view.fullScroll(View.FOCUS_UP);
I have 40 ImageViews inside a GridLayout, they have different colors and I want to know if user touched the desirable image or somewhere else(for example if user touched red image). How should I do that?
Set an OnClickListener for each view and store the views. In onClick you can check the view and know what ImageView was clicked. You could also think about changing the type to ImageButtons!
If you have further problems with your Grid not being able to be clicked, check this out: http://cyrilmottier.com/2011/11/23/listview-tips-tricks-4-add-several-clickable-areas/
TLDR: Disable focusing of the views in your layout.
If you manage your images in a collection maybe think about switching to gridview and implement an adapter with an itemClickListener. So depend on which item is clicked do you action.
How GridLayout items come from the Adapter(List/Image) in Android App
I have four ImageButtons in my app.
Three smaller buttons can be either shown or "hidden" below the bigger button. I hide buttons using rotate and translate animations.
The problem is:
OnClickListener's onClick method always gets triggered no matter a smaller button shown or not.
I mean, when smaller buttons are "hidden", touching the place on screen, where one of the smaller button resides when shown, triggers onClick method.
It looks like Android OS does not take actual placement of the button into account when deciding whether it should trigger onClick method or not.
How can I overcome the issue?
I want onClick method to be called ONLY when there is a button below my finger.
EDIT:
All suggested workarounds rely on hiding the button. This doesn't help at all. The onClick method gets called for INVISIBLE and GONE buttons too. I checked this in debugger.
Looks like you are going for an Arc menu. Why not use the awesome implementations of Siyamed and DaCapricorn, instead of re-inventing the wheel?
Did you set your click interface your activity and implemented onClickListener? If so you need to separated your buttons by setting if conditions like if(arg0 == button1) for all the buttons.
The issue was caused by the fact that tween animations (the original animation framework) animate the pixels, not the touch zones of a widget.
I have replaced tween animations with property animations and now everything works like I expect it to work.
Setting
btn.setVisibility(View.INVISIBLE);
doesn't call onClick method where buttons are not visible. I have tested it. So you might be registering onClick listener in unnecessary views.
Try to set the listener using the property onClick of the button in the layout.xml (for example
android:onClick="onClickButtonCamera"
and then declare a public function with the same name like:
public void onClickButtonCamera(View view) {
//some stuff...
}
I allways use it and the function is never called when the buttons are hidden.
I hope it helps you.
i think there are three ways to solve this issue.
1) if you are setting onClick Listener then set it null when you hide buttons and set listener when you show them.
2)Add a checking in onclick for each button about their visibility simply return; the function when button's visibility is not VISIBLE(use the function button.getVisibility() != View.VISIBLE)
3)Last one not sure, usually setting gone as visibility will protect it from clicking.
I'd suggest setting the button visibility to GONE after completing the animation.
UPDATE:
Another suggestion is to have a button that is not animated (for which the problem should not happen) and an image (graphically identical) that is animated after the button is hidden. The image should not receive any clicks, under the big button or not, and the real button should not receive clicks when hidden.
I am new at android programming. I have three buttons on the right and three buttons on the left and in the center I have a listview which views items dynamically.
I am implementing the ontouch() (action:UP) to move around buttons and onlong press - corresponding performclick() is activated. everything works just fine but when I long-press on the listview to activate performclick(). nothing happens. - I am not sure why?
I also tried initializing the list view layout for onlongclick listner but it didn't work.
I want to make list view activate the onlongclick too for a button that ? Is this possible?
NOTE: I don't want to activate the performclick() onITEMselected.
Thanks!
Did you try using ListView.setOnItemLongClickListener() instead ?