Open button's image on Android when button is visible - android

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;
}});

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); ??

Repeatable process where image can be added when a button is click

I am trying to attempt the below :
What i am trying to do is whenever a user click on a button, it will add a image to the layout(which the user can scale and move to any position within the layout). The user can repeat this process hence if the user click 6 times on the button, there should be 6 image in the layout.
i have google around, looking for some sample but ain't sure if its the correct one to use. maybe i did not use the correct keyword to search. So far i have seen is endless adapter and RecyclerView but both seen to be mainly for "ListView" type.
Anyone can suggest or share a link/example that i can read up and learn from? It will be a great help.
If you want to add image on button click set on-click listener on it. You need to get how many time the user have clicked the button.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//get adapter and view
//get image to add
addImage(i); //to desire view
}
});

Handle button press state

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();
}
});

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.

Is it possbile to call android spinner from a click of ImageButton?

I have an image button which says Select city I want to call an android spinner if anyone clicks on that image. Is it possible??
I would recommend you check out the App/Alert Dialogs section of the API Demos application. I think what you are looking for is a dialog with a single choice list.
Or take a look at this:
http://developer.android.com/guide/topics/ui/dialogs.html#AddingAList
This is entirely possible in several ways. One way is to have a spinner whose visibility is GONE. Clicking on the image makes the spinners visibility VISIBLE.
Another choice is to create a dialog that has the spinner and use the selection to do whatever it is you want to do.
There are other options, but that should get you started.
ImageView iv = (ImageView) findViewById(R.id.iv);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mySpinner.performClick();
}
});

Categories

Resources