How to change a view when another view is pressed/clicked - android

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.

Related

Change Color of layout using user defined method Android

A user defined method to change Color of the layout on the basis of button clicked. And, also keep track of what button is clicked.
Like, if a user clicks any button, then the Id of that button should be saved in some variable and then the method will be called to change the colour of a specific button.
The problem is that I know how to change the colour of a button. It can be "user1.setBackgroundColor(Color.RED);"
But don't know how it can be implemented by using a function that takes a single variable...
If you have already decided which colors to use, I think the simplest way is to do layout.setBackgroundColor(getColor(R.color.new_color)); in the click listener of the button.
You can make your activoty implements View.OnClickListener.
So whenever the user click on a button, the method onClick will be called and the view that was clicked will be the argument of that method. You can call view.getId () to find out which button was clicked or you can use view.setBackground (Color.parseColor ("#999999")) to change the button background.

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

setfocusableInTouchMode vs onClick

I have menu button in screen,i set setfocusableInTouchMode and onclick for menu Button.If i click the menu button it getting focused and second click only getting fired.I want to do both operation at a time.
call RequestFocus on your Button followed by performClick() this will focus and also fire click event on your Button .

How to disable a button, except after checked RadioButton

How to make a button unclickable, except after checking a radio button?
If the button is called myButton try using
myButton.setEnabled(false);
Then you can add some type of listener to your radio button and in that listener add
myButton.setEnabled(true);
Also, you can check out the android:clickable attribute in xml.

which 'on' method is called when the trackball's 'Select' button is pressed?

which 'on' event is invoked when the user selects a ListView item by pressing the trackball button?
i trapped onKeyDown, onTouchEvent, onTrackballEvent
but it's none of those...
thanks
That is a "click" and triggers your OnItemClickListener (or onListItemClick() if you are implementing a ListActivity).

Categories

Resources