Handle button press state - android

I have a button in android application that has an accompanying red-circle TextView that represents a counter (iOS-style):
Button is a StateListDrawable so that when pressed is (visually) goes slightly down. I want accompanying red circle to go down as well.
I would be happy if I could found a callback that tells me when the button goes to pressed or unpressed (normal) state. I tried btn.setOnTouchListener, but it does not work. For instance, when I press button, it fires MotionEvent.ACTION_DOWN and that's good. But if I drag my finger out of button, it jumps back to normal state, but MotionEvent.ACTION_UP is not fired until I release my finger.

You have two ways to do this
Include Button and TextView in one layout and set click listener to that layout.
Perform TextView click inside Button click listener. see below code.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.performClick();
}
});

Related

Android Have View Block/Swallow Click Event

I have a view on top of another view. The bottom view is supposed to fire a callback when clicked. The top view is not supposed to fire any callback when clicked.
The problem I am facing is that when there is a click on the top view, the bottom view fires its callback. I want to prevent this.
I've read a number of posts on disabling clicks and they all suggest the same thing:
view.setEnabled(false);
and
view.setClickable(false);
Neither of these prevent the top view from swallowing/blocking the click event.
Do you know how I can have a view prevent passing clicks through?
Its a little hacky but the best solution I came up with was adding an empty onClickListener to swallow the event.
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// HACK disables click through events by swallowing click
}
});
I couldn't understand what you are trying to do without an example.
But if you want to disable click for a particular view & you don't have any click functionality for this view, then try
view.setOnClickListener(null);
Also, did you try view.setFocusable(false); ??

Open button's image on Android when button is visible

I have a large canvas where I placed multiple buttons. Each button has an image which is opened based on the button click event. I want to change it such a way so that when a button enters to the screen area, it will automatically open the button image.
I guess I need to find the current button view (that is visible on the screen) and then use functions to simulate the button click event (View.performClick();). As I am not entirely sure, any suggestion would be highly appreciated.
Can you try to take both view button view and image at the same position, when click on button view then this view hide and show image.
I think the simplest way is to add ImageButton and change the image/background by click
Something like that:
boolean isShown;
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(isShown){
//set empty bg
}else{
//set right content
}
isShown = !isShown;
}});

Populating hidden items on button click in android

I have 2 Button one below the other and I have other 4 hidden Button besides them . I want to unhide 2 of those Button when my first Button is clicked and organize them exactly below that Button while the other 2 Button should be unhide when 2nd Button is clicked exactly below the 2nd Button. Now if I place these hidden Button they will occupy space in between my visible Button, which I dont want .Is there any way by which i can push my 2nd visible Button down and show my 2 of the hidden Button onclick of 1st visible Button.
you can use setvisibility to achieve that.
button= (Button) findViewById(R.id.play);
button.setVisibility(1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//when button is clicked show/hide buttons
button2.setVisibility(View.GONE);
button3.setVisibility(View.VISIBLE);
}
});
you can also use button.setVisibility(View.INVISIBLE); instead of button.setVisibility(View.GONE);

Android: Share sibling Views pressed state

I have a layout that has two children in it. My goal is to have the whole layout function as a button. Each of the children has a different color to show pressed state. When I press them individually they show their pressed state fine. However when one is pressed, I would like them both to show pressed state.
My first thought was to use duplicateParentState, but the parent never seems to be in the pressed state because the Views fill the layout. My next thought was to use AddStatesFromChildren, but all this did was make the parent show pressed, but not the children. Finally I tried using both, but that caused an error.
How can I get sibling Views to share a pressed state?
I'm only 4 years late to this, but you were close:
set them both inside a parent layout
set your click listener on the parent
then set android:duplicateParentState="true" on each of the children.
Works like a charm!
You could possibly do something like:
Button button = new Button(this);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Button otherButton = (Button) findViewById(R.id.other_button);
otherButton.setPressed(true);
}
})
But, of course, have the buttons called as you need to.

Redirect OnItemClick on a ListView to the real touched Item view

I have a listview that shows a list of views for every row/Item (A list item can have for example 2 or 3 buttons). All Items are set to do not be focusable and not be clickable. This way clicks on the items call to my ListView OnClick. What I want to do now is call performClick() on the real clicked view (that did nothing because it was set as not focusasble/cickable). Is this possible? I have tried to find a method to get a view based on a click location and did not find anything and on the other hand onItemClick click handler for a list view seems not to get the x/y of the touch either. I know that for this I could use OnTouchEvent but then I won't be able to know what item was clicked, etc...
okay try this, might help you... set a common onClick for buttons as well as the ListItem.
#override
public void onClick(View v) {
Sting position=v.getTag().toString(); //<--- like this
//task to do when clicks on the view...
switch (v.getId()) {
case R.id.<id_of_the_button1>:
//task to do when clicks on the button 1
break;
case R.id.<id_of_the_button2>:
//task to do when clicks on the button 2
break;
}
implement OnClickListener in the custom adapter and over ride it with the above onClick.

Categories

Resources