I have an onCheckChangedListener to show a textView depending on which radio button is selected. I have 1 question and 1 problem that I was wondering if anyone could help me with.
Question: Can you set the radio groups default check value to NO radio button so that none are checked to start with?
Problem: How can I use an IF statement to determine whether a text view is already "visible" and If it is then set it to "gone", I will include my current code.
code:
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
switch(arg1){
case R.id.rfolk1:
Folk1.start();
TvFolk1.setVisibility(View.VISIBLE);
TvFolk2.setVisibility(View.GONE);
Play.setVisibility(View.VISIBLE);
Pause.setVisibility(View.VISIBLE);
Stop.setVisibility(View.VISIBLE);
Play2.setVisibility(View.GONE);
Pause2.setVisibility(View.GONE);
Stop2.setVisibility(View.GONE);
break;
case R.id.rfolk2:
Folk2.start();
TvFolk2.setVisibility(View.VISIBLE);
TvFolk1.setVisibility(View.GONE);
Play2.setVisibility(View.VISIBLE);
Pause2.setVisibility(View.VISIBLE);
Stop2.setVisibility(View.VISIBLE);
Play.setVisibility(View.GONE);
Pause.setVisibility(View.GONE);
Stop.setVisibility(View.GONE);
break;
}
The View class includes a getVisibility() method. Compare that:
Eg:
if (TvFolk1.getVisibility() == View.VISIBLE)
TvFolk2.setVisibility(View.GONE);
To shorten down code, you can also make a method:
public static void goneIfVisible (View v)
{
if (v.getVisibility() == View.VISIBLE)
v.setVisibility(View.GONE);
}
And keep in mind in Java, variables are lowercased, only use uppercase for class names.
// If TextView is already showing and you want to hide it.
if (TvFolk1.isShown()) {
TvFolk2.setVisibility(View.INVISIBLE);
}
// For uncheck all radio button from radio button groups
RadioGroup rgButton = (RadioGroup)findViewById(R.id.radiobuttongroup);
rgButton.clearCheck();
Related
I've been struggling with an issue of programmatic checking of a RadioButton, which is situated in a RadioGroup. It seems that a single check() call raises three events - once for the first RadioButton and twice for the second, which is my target. At the same time clicking on the second RadioButton in the interface causes only one event to appear, which is correct.
So, is there a way to avoid multiple event raising ?
public class RadiogroupActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup r = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton b = (RadioButton) findViewById(R.id.radio1);
r.setOnCheckedChangeListener(onPromobuttonsClick);
r.check(R.id.radio1);
}
private OnCheckedChangeListener onPromobuttonsClick = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.d("x", "x");
}
};
}
So, is there a way to avoid multiple event raising ?
Call b.setChecked(true); instead.
A quick look through the source code confirms that RadioGroup#check() really does call the OnCheckChangedListener three times...
When the current selection is unchecked,
When the new RadioButton is checked, and
When the RadioGroup saves which RadioButton is now checked.
Odd quirk.
Simply post your r.setOnCheckedChangeListener(onPromobuttonsClick); line in onResume() method. It is works for me.
So this is an old one but I also got a similar behavior. A workaround that I found was to call the toggle() method of RadioButton instead of the check() method of RadioGroup.
So in this example, you woulod just need to swap
r.check(R.id.radio1);
with
b.toggle();
since b is already the view with id R.id.radio1.
I had this problem when I wanted to log analytics for radio button presses, but the way the radio groups calls were being handled with .check() caused onCheckedChangeListener to get called multiple times (which sent too many events).
I handled it the following way to only get one event per click:
// create listeners so we don't duplicate the creation in for loop
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// whatever you want to do here
}
};
// add on click listener to all radio buttons
int buttonCount = buttonGroup.getChildCount();
for (int i = 0; i < buttonCount; i++) {
if (buttonGroup.getChildAt(i) instanceof RadioButton) {
buttonGroup.getChildAt(i).setOnClickListener(onClickListener);
}
}
This is a old question but I didn't found a complete answer yet that cover my issue. But thanks to #Hellojeffy suggestion I figured a way to handle the situation when RadioButton trigger multipletimes when using setOnCheckedChangeListener().
The idea is to add a click on each view inside the RadioGroup and trigger a event on click, instead a event on check changes.
for (child in radioButtonGroup.children) {
if (child is RadioButton) {
when (child.id) {
R.id.rb1 -> child.setOnClickListener { onRb1Click(...) }
R.id.rb2 -> child.setOnClickListener { onRb1Click(...) }
R.id.rb3 -> child.setOnClickListener { onRb1Click(...) }
...
}
}
}
My case :
- I have a RecyclerView with complex item (many ImageView, texts .. )
- I use 4 ImageView as 4 buttons (Like, Comment, Share, Dislike)
- I want to use TouchListener for each button, because I want to control animation, also click event. ( I know how to use ClickListener for each item so please focus on TouchEvent ) Example : Facebook Comment button of each Story on NewsFeed, when you touch onto the button, animation scale icon. Click event just go on after release the button.
Problem is : I just can get Parent View of each Item (ex : LinearLayout contain other components of each item) by using recyclerView.findChildViewUnder(x,y).
So how to know which button was clicked like when we use clickListener :
- button1.setClickListener(this);
- button2.setClickListener(this); (on View Holder)
And after that catch event in Activity or Fragment :
- switch(view.getId()) {
case R.id.button1: .. do something; break;
case R.id.button2: .. do something; break;
....
I google this problem all day but all sample and answer just use ClickListener like I said above.
Thank you!
inside recyclerview adapter use like this
#Override
public void onBindViewHolder(final RecyclerViewAdapterA.ViewHolder_a holder, final int position) {
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
You get parent view by by
View parent = recyclerView.findChildViewUnder(x,y);
Now you can easily get its corresponding views e.g.
Button button = (Button)parent.findViewById(R.id.button);
Hope you understand!!!
I have a problem... have been thinking about it for a while now and been looking on line and still haven't come up with a clear explanation...
I have a number of textviews and have set onClickListeners to each of them.. and when the user clicks on one of them I want them to have the ability to change the text to another set of string array options which I have created progammatically. When the user selects an option the text should change to the option they choose. (I.e. TextView was A now it is B. hope this makes sense.. anyway... )
The current solution was to set a OnClickListener to every TextView and when someone pressed it an individual dialog showed. But I found that if I do this the code would be so long it would take an eternity to code so am hoping someone has a more elegant way of coding such a long process =(
So I guess my question would be... 1) is there a way I can find out which text view was pressed and then change the text of that TextView being pressed within a single method? to save me having to code 1000 alert dialogs...
http://i.stack.imgur.com/LRJGz.png
I would advise you to use a grid view.
You can see which textview was pressed like this:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
//get id
switch (v.getId()) {
case R.id.textView1: ...
}
});
One of the ways to do what you want is to use the text view setTag() and getTag() methods.
On init of a text view use the setTag() to set some value to identify the view.
In the on click event use the getTag() on the view argument to know which view was clicked.
I would suggest holding the textviews in an array, like so:
TextView[] textViewArray = new TextView[textViewCount];
Then using a for loop assign each one a tag of integer - it's position
textViewArray.setTag(i)
And add an onClickListener to each one, again using a for loop:
textviewArray[i].setOnClickListener(etc...)
Then when one is clicked, you can use get the position of view that was clicked. This will require a custom method inside of your:
textviewArray.setOnClickListener(new customOnClickListener())
Where your customOnClickListner is like this:
private class customOnClickListener implements CompoundButton.{
public void OnClick(View view){
int position = (Integer) view.getTag()
///Do more code here - your processing
}
}
Hope that makes sense :)
For your for loops, you could use for(i = 0, i
Use set id for all text, where set the id positive integer(distinct), and then have one on view click listener(set it all) where u catch all text view clicks(downcast view with textview) and in side it put a switch case where you handle clicks on which text view is clicked.
You have to set "onClickListner" on all of of your textview.
For Saving some length of code i would suggest you create a function of your dialogbox, and give some int parameter to it, which would be directly called by the clickListener of textview,
Like ,
int i=0;
......
textView1 = (TextView)findViewById(R.id.yourtextview1);
textView2 = (TextView)findViewById(R.id.yourtextview2);
......
......
// and so on, for your all textviews
#Override
public void onClick(View view) {
if (view.equals(textView1)) {
i = 1;
CustomDialog(i);
}
//Similarly for all your textViews..
..........
Make A function CustomDialog Like
public void CustomDialog(int i){
if(i==1){
//Do something
}
}
I want my toggle button's ON text to be large and OFF text to be small. But can't do it.. any suggestions? This is what i was trying
mf=(ToggleButton)findViewById(R.id.mf);
if(mf.isEnabled()==true)
{
mf.setTextSize(13);
}
else
mf.setTextSize(8);
Your code has to be called each time you click on your button. so use :
toggleButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (toggleButton.isChecked()) {
toggleButton.setTextSize(13.0f);
} else {
toggleButton.setTextSize(8.0f);
}
}
});
You can set OnClickListner with a easy method. In your .xml put the option
android:onClick="nameOfMethod"
And then in your code:
public void nameOfMethod(View v){
}
Where v is the togglebutton in this case ( ToggleButton mf = (ToggleButton)v; )
I put the solution here:
mf=(ToggleButton)findViewById(R.id.mf);
if(mf.isChecked())
{
mf.setTextSize(13);
}
else
mf.setTextSize(8);
Use isChecked() instead of isEnabled()
You need to do some debugging.
Firstly:
if(mf.isEnabled()==true)
can be
if (mf.isEnabled())
Does mf.setTextSize(13) on it's own work as expected?
Add some logging:
if (mf.isEnabled())
{
// Add some logging, is this line reached correctly?
mf.setTextSize(13);
}
else
// Add some logging, is this line reached correctly?
mf.setTextSize(8);
Chances are you need to change isEnabled() to isChecked(). isEnabled() means exactly that, whether it's enabled or not. You want to know whether the button has been checked.
In my activity I have many buttons that launch other activities. The problem is that when I press a button I don't want to be able to press another, and so to launch 2 activities (or more). What is the best solution to block the other views (buttons) after one of them was pressed?
Just in each of your onClickListener for each button make button.setDisable(true);.
Don't forget to enable them in onResume().
Add one onClickListener() for all buttons. In listener switch the actions as per id.
public .. onClickListener(){
// Disable all buttons here..
button1.setDisabled(true);
button2.setDisabled(true);
button3.setDisabled(true);
...
switch(view.getId()){
case R.id.button1:
// Start some activiy ... or do some task...
break;
case R.id.button2:
...
break;. ...
}
Don't forget to enable all buttons in onResume()
public .. onResume(){
button1.setDisabled(true);
button2.setDisabled(true);
button3.setDisabled(true);
}
OR, Try This to enable/disable all views in a viewGroup...
private void enableDisableView(View view, boolean enabled) {
view.setEnabled(enabled);
if ( view instanceof ViewGroup ) {
ViewGroup group = (ViewGroup)view;
for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
enableDisableView(group.getChildAt(idx), enabled);
}
}
}
Pass the view parameter as the parent layout containing all buttons.
I haven't used this, but this should work. Try playing around this code.
Try this to disable the Buttons
button.setEnabled(false);