How do I programmatically check an Android radio button with ripple animation? - android

tl;dr: When the user clicks an unrelated text view, I want a radio button to be selected with a ripple animation. performClick doesn't do this. What should I be doing instead?
I have a radio button with both a label and a longer description.
The general structure of my layout is this:
<RadioGroup>
<RadioButton
id="#+id/officialBusinessRadio"
text="Official business" />
<TextView
id="#+id/officialBusinessCaption"
text="This operates under a ..." />
<!-- ... -->
</RadioGroup>
I've added an OnClickListener to the text view so that tapping on the caption selects the radio button:
officialBusinessCaption.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
officialBusinessRadio.performClick();
}
});
When I tap on the radio button itself or on its own text ("Official Business"), the radio button animates to selected and a ripple ink effect occurs. When I top on the caption text (and performClick is called), the radio button still animates to selected, but no ripple occurs.
Is there a more correct way to get this effect? That is, is there a better way to forward a click on the caption text view to the radio button? If not, is there a straightforward way to trigger the ripple effect on the radio button programmatically?
I would prefer against setting all the text using a string resource for the following reasons:
I intend on styling the label and the caption differently.
I want the radio vertically center aligned with the first line of text.

I've hit this problem by population RadioButtons in a RecyclerView. All of them are set to isClickable = false because I override their selection according to another rule, but the way to trigger the selection to true is doing:
Kotlin:
radioButton.post {
radioButton.isChecked = true
}
Java:
radioButton.post(new Runnable() {
#Override
public void run() {
radioButton.setChecked(true);
}
})
before, I was doing setChecked(true) only and it wouldn't work.

Related

Why when I use setClickable(true) it's not show the action of click

Actually, I use a ListView and when I use setClickable(false) I have the animation as if I clicked on a button you see? The animation that shows you click. Which is not normal, I think, basic.
And when I use setClickable(true) I no longer have the animation, as well as if I use
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
And i would like to use the OnClickListener but I think it would be better for the user to see that he can click, so to have the animation when you click.
So, I'd like to see when the user clicks on an item in the list, it does the action I want (I'll add that later) but let's imagine a Toast but it displays the effect as if you click on a button. The effect i got if i use setClickable(false) (the default setting).
That's the ripple effect !
In the row's layout of the ListView just add:
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
This will add the Ripple effect. If you want to show it on top of the other views, use the forground attribute:
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
add this foreground:?attr/selectableItemBackground to your view attribute, it should work

android custom togglebutton border

I have a grid of 64 togglebuttons in an 8x8 form. when the app runs, it sets each togglebutton's background/drawable to a colour. this is done in the program, not the xml. my problem is that while there seems to be space between the buttons on the graphical layout of the app, when the app runs, and changes the buttons to a colour, the space disappears. this makes the buttons look like a single plate, with no definition between buttons.
What I want is to put a border on the buttons to make it clear where each button is.
the function of the buttons is to be used to indicate a musical note being pressed. when the button is pressed, the colour of the button turns from grey to light blue, and adds the note to a sequence.
So i need to be able to put a border on a togglebutton with a custom design, that changes when activated/deactivated, keeping the border in both states. i have also tried setting the max width and max height, and also setting the padding on the buttons in an attempt to seperate them.
an example of the code is as follows:
public class MainActivity extends Activity implements OnClickListener
{
ToggleButton tg1;
....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
tg1 = (ToggleButton) findViewById(R.id.toggleButton1);
tg1.setOnClickListener(this);
....
tg1.setBackgroundColor(0xffcccccc); //set button grey
}
public void onClick(View v)
{
if((tg1.isChecked()))
{
tg1.setBackgroundColor(0xff00ffff); //set button blue
}
else
{
tg1.setBackgroundColor(0xffcccccc); //set button grey
}
}
}
any help is greatly appreciated.
Right way: do not call isChecked manually to change background. Use selector. You can draw 9-patch with a border or create shape (use stroke to set border color and solid to set fill color).
Or create a custom togglebutton to support desired functionality.

How can I change the name of buttons on click of a button?

In my calculator app, I want an Inverse button, which when clicked changes the text of other buttons. Like sin to sin inverse etc. Is this possible?
You can just change the text of a button in that button onclick event.
Say For example
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(btn.getText().toString().trim().equals("sin")){
btn.setText("sin inverse");
}else if(btn.getText().toString().trim().equals("sin inverse")){
btn.setText("sin");
}
}
});
I think this will help you
While the other answers are completely right showing you to how rename the buttons, I suggest another solution with a cleaner design: Have different buttons for "sin" and "sin inverse", and just make them visible/invisible when clicking the "Inverse" button. That way you can write clean click handlers and don't have to use a lot of "if (isInverseMode()...)".
To make that work correctly, you just declare some additional buttons for the inverse operations in your XML layout file and set them to android:visibility="gone".
If you then set one the visible buttons to invisible and the next insivible button besides it to visible in the code, then the effect for the user looks like you exchanged one button by the other (so he only notices the text of the button changing).
It's possible.
Just re-set the button text in your onClick method for the button.
buttonId.setText("Your button text");
From what you're saying though it sounds like you want to change the buttons function as well as the text... in which case you need to put an if statement in your onClick method to handle the two button states.
Setting the text on a button would help anytime.
This code may help you..
final Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setText(new StringBuilder(btn.getText().toString().trim()).reverse());
}
});

ListView item selection

I have a ListView with custom list items. Each list item consists of 2 linear layouts one next to other.
LinearLayout 1 | LinearLayout 2 |
I've declared state list drawables for both LinearLayouts where in state_pressed I'm changing the background of the LinearLayout.
And here comes the issue - When the user taps on the LinearLayout2 only the background of LinearLayout2 should be changed, the background of LinearLayout1 should remain unchanged. On the other hand, when the user taps on LinearLayout1, only the background of LinearLayout1 should be changed. But now when the user taps on either of both LinearLayouts, both of them change their background.
The behaviour on tap on LinearLayout2 should be as onListItemClick() while when the user taps on LinearLayout1 a Dialog should appear (if this matters).
Any ideas how could I solve the background change issue? I've tried playing with focusable and clickable options. If i set clickable=true to both LinearLayouts, the children (TextViews) of LinearLayout2 do not change their colour (the TextViews should change their text colour).
Thank you!
This is because when using a list view you have to change some tags in XML to make the background transparent so that it will correctly work with your back ground.
Add this to your ListView XML code.
android:cacheColorHint="#00000000"
To set the ListView's background to transparent.
Well I think a single solution if you are using BaseAdapter as extends
First give unique Id to both those Layouts in you xml file and add
android:clickable="true"
In your method
public View getView(int position, View convertView, ViewGroup parent) {
when your are getting those views like
holder.layout1_name=(LinearLayout)view.findViewById(R.id.layout1);
holder.layout1_name.setOnClickListener( clicklayout1);
holder.layout2_name=(LinearLayout)view.findViewById(R.id.layout2);
holder.layout2_name.setOnClickListener( clicklayout2);
Add click listener on them
private OnClickListener clicklayout1 = new OnClickListener() {
public void onClick(View v) {
//Do what you want to do here
}
};
private OnClickListener clicklayout2 = new OnClickListener() {
public void onClick(View v) {
//Do what you want to do here
}
};
May be this may help you

Can I set something like "tooltip" for a view in android?

Can I set some message to appear like a "tooltip" for a TextView or Button?
There's no concept of "hovering" in a touch screen, but you could set a LongClickListener for your View, and have a Toast appear after a long press. Something like this:
Toast viewToast = Toast.makeText(this, "My View Tooltip", Toast.LENGTH_SHORT);
View myView = (View)findViewById(R.id.my_view);
myView.setOnLongClickListener(new OnLongClickListener() {
#Override
public void onLongClick(View v) {
viewToast.show();
}
});
EDIT: After reading your comment, you should just use the hint attribute in your EditText XML layout:
<EditText
android:hint="My tip here" />
-First set a textview with your hint and set it to invisible.
-Create an animation xml with alpha animation,specify how long you would like to display(at the end set the animation to zero alpha so that it remains invisible) and put it in res->anim folder
-Inside your onCreate and onClick methods of view that need tooltip
set the text view to visible
Hook the animation(like
R.anim.tooltip) to this text view
-Use boolean flags and allow the user to switch off the tool tips in menu.
I'll leave the code specifics to you. You find them easily in stackoverflow.

Categories

Resources