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.
Related
I have a RelativeLayout. In that I create several ToggleButton views. The user can set those ToggleButtons on and off.
When the user opens the Activity's OptionMenu I want all those ToggleButtons to become OFF. To do this I am setting programmatically the ToggleButtons to OFF in the onPrepareOptionsMenu code.
I have also a PopupMenu registered to a Imagebutton. I want also when the users opens the PopupMenu by clicking the Imagebutton all the ToggleButtons to become OFF. So, I am turning the togglebuttons to off in the Imagebutton's setOnClickListener code.
My issue is that the updates to the Togglebuttons' state (to Off) are shown only after the OptionsMenu or the PopupMenu is closed. Instead I want all the ToggleButtons become Off as soon the user opens the menus. I thought I have to use some OnFocusChangeListener on some view. I tried to use it on the Activity's top layout but it doesn't work.
How could I get the result I want?
I found the solution myself by overriding the onWindowFocusChanged method, like this:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus) {
//code to turn off togglebuttons goes here
}
}
How do i get a specific view inside of a RecyclerView item? For example I have a floating action button inside of a recyclerview item. The recyclerview card can swipe to trigger an event (new activity) and when the user swipes i am making the fab invisible. I want so that when the user returns back to the activity with the recyclerview, the fab is visible again.
I've tried to implement this a few ways, but for some reason it's taking the fab from the NEXT card/item and placing it on the original card/item that was swiped. This is a problem because the next card might have a different colored fab or no fab at all. What is happening is that it's taking the most recent viewholder item, even if the previous one is the one I want to deal with.
So i need a way to reference the fab in the current item. I'm currently setting it to currentFab = holder.mFab (but again, it's taking the most recent holder item even though it's not the one I pressed on). I need a way to reference the fab in a specific item.
I've tried something similar in the past, I've added the Swipe function in "onBindViewHolder ", Using a Swipe Library.
What I have made is, A CardView, Inside it there are 2 Strings, And a Button, this Button will be invisible according to specific conditions, So because it's inside onBindVewHolder, it was easy to make some edits on the button.
Here's a sample :
holder.Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.Button.setVisibility(View.GONE);
}
});
Ofcourse this isn't a "fully working code" it's just an example, Plus you didn't put any code that you have tried.
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 want to set OnClickListener on a Button. Should I cast it to the Button or let it in View? You can see from the code below, the former cast the View into Button while the later let the widget in a form of View. Is the later better because it doesn't need to do the casting or is it just do the same?
It's not that I don't understand how to set click listener on a button. I just want to know it in term of performance and best practice point of view.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(listener);
or
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
View button = findViewById(R.id.button);
button.setOnClickListener(listener);
The Same, You only save the cast (Performance).
if you dont need the View object I would prefer
findViewById(R.id.button).setOnClickListener(listener);
I believe there's no difference in performance. What changes is your code readability so, if you're setting a clickListener for a button and you also use the button instance later in the code you might want to use "button".setOnClickListener.
Since, most of the times you don't need to keep a reference of the clicked view, i'd suggest to use something like ButterKnife
Performance wise there is almost no difference. For readabillity I would suggest using Button and not View.
I am pretty sure the difference in performance is minimal or simply there is no difference.
However, there is a difference between View and Button.
To shorten the answer and be as specific as possible ,I will tell you that if you want the button to be as "Standard" (Android standard at least) as possible, you should use the Button ,but if you want to sort of create a Custom Button, than you should go ahead and use the View.
To make it a bit more clear ,the Buttom is actually a View ,so when you use a Button, you actually use a View that is customized for the purpose of being a button and acting like one.
If you need more details, ask up,I will try to help or link you some documentation.
I have application with two layouts (main and detail). Application starts with main layout. I have button on main layout. When i click on it i call setContentView(R.layout.detail);.
Now i have set new layout with button and i need set onclick event on this new layout button.
I tried set both events in onCreate() method. After that works only first button on main layout. Button on detail layout don't works.
Can you help me?
Its not a recommended way to change the layouts.
Anyways, once you change the layout by setContentView(R.layout.detail) , you need to set the onclickListener of the button again.
After setting the new view, try to findviewbyid(urbuttonid) and setonclicklistener for this.
It should work.
This question has very simple solution:
When you set another content view , Initialize again button of new layout like this:
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(this);
after that your click event goes for second view's button...
or
you can do assign different id's and names to both buttons that you can handle each button click by its id.
or
you can assign click listener for each button on the views.
Good Luck