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);
Related
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;
}});
I am working on a simple Android Activity which contain 10 images and 2 Buttons i.e. "Next" and "Previous", i changed all images by click on "next" and "Previous" Button using an array and both Buttons showing on every images but i want to don't show "Previous" button when user at first image and same as don't show "Next" button when user reached at tenth (last) image.
You have to use loop for this. When you on first activity make visibility of previous button hide. On last activity make next button invisible. You can make button invisible using-
btnName.setVisibility(View.INVISIBLE);
For making visible when you on second or second last image-
btnName.setVisibility(View.VISIBLE);
loop
{
if(i==0){
btnPrev.setVisibile(View.GONE);}
else
{
btnPrev.setVisibile(View.Visible)}
if(i==array.getSize()-1)
{
btnNext.setVisibile(View.GONE)};
else
{ btnNext.setVisibile(View.VISIBLE)}
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();
}
});
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.
If i had Button btn1 and i had another Button btn2
how to connect the two views on the same events like onPress, onFocus.
let me explain:
when i press btn1 now the btn1 is pressed and colored with orange background
while am pressing it, i wanna change btn2 state to be pressed and with background color orange.
any ideas?
You can set a touch listener for Button1 where you can call Button2.setPressed(true) after checking the action of the event . i.e if you want it to be pressed only while Button1 is pressed you would call the function when Action is ACTION_DOWN and call it again with a false parameter when the Action is ACTION_UP. If you want button2 to remain pressed you can use the onClicklistener instead
Call manually performClick() (or similar method) of btn2 in the onPress() or onFocus() listeners of btn1.
If You don't want the click event to be passed to btn2, do a btn2.callOnClick instead. It will call any onClick action listener associated with btn2. If there aren't any, it'll return false.