error event onclick of RadioButton in .xml - android

When I add an on click event in .xml I don't have and error like this
Here is my code
<RadioButton
android:id="#+id/dientich"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickHandler"
android:text="dien tich" />
<RadioButton
android:id="#+id/thetich"
android:onClick=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="the tich" />
</RadioGroup>
Thank you!

I think that the better way is to set a listener in your code
radioGroup.setOnCheckedChangeListener { radioGroup, optionId ->
when (optionId) {
R.id.dientich -> {
// do something when radio button dientich is selected
}
R.id.thetich -> {
// do something when radio button thetich is selected
}
}
}

Can you please refer to this link
<RadioButton android:id="#+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User 1"
android:onClick="onRadioButtonClicked"/>
public void onRadioButtonClicked(View view) {
//your code
}
https://developer.android.com/guide/topics/ui/controls/radiobutton

Related

Hides the view when the button is clicked

I have a radio group used to set data class properties. Here's how I've gone so far: I manually set the "bankDestination" view to invisible if proxy_bi_fast is checked. However the "bankDestination" view is still visible.
fun onRadioButtonClicked(view: View) {
if (view is RadioButton) {
val checked = view.isChecked
when (view.getId()){
R.id.proxy_BI_Fast ->
if (checked) {
binding.bankDestination.visibility = View.GONE
}
}
}
}
the related xml code:
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="#+id/account_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:text="#string/account_number"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="#+id/proxy_BI_Fast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="90dp"
android:layout_marginTop="20dp"
android:text="#string/proxy_bi_fast" />
</RadioGroup>
You haven't added onClick method in your proxy_BI_Fast radio button. Try adding that and then check.
It wont' work android:onClick="onRadioButtonClicked" in the RadioButton instead of this you have to use setOnCheckedChangeListener on the RadioGroup as describe below.
binding.rbGroup.setOnCheckedChangeListener { group, checkedId ->
when (checkedId) {
R.id.proxy_BI_Fast -> {
binding.bankDestination.visibility = View.GONE
}
}
}

How to set only one RadioButton Can be selected at the time in RadioGroup

I've created a radio button in radiogroup, but when I try running the apps all radio button can be selected all the time, and how to set only one radiobutton can be selected at one time?
I'm using Fragment
RadioGroup radioGroup = (RadioGroup) rootView.findViewById(R.id.RGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// find which radio button is selected
if(checkedId == R.id.Abdominal) {
Toast.makeText(getActivity().getApplicationContext(), "choice: A",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Arm) {
Toast.makeText(getActivity().getApplicationContext(), "choice: B",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Back){
Toast.makeText(getActivity().getApplicationContext(), "choice: C",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Chest){
Toast.makeText(getActivity().getApplicationContext(), "choice: D",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Leg){
Toast.makeText(getActivity().getApplicationContext(), "choice: E",
Toast.LENGTH_SHORT).show();
} else if(checkedId == R.id.Shoulder){
Toast.makeText(getActivity().getApplicationContext(), "choice: F",
Toast.LENGTH_SHORT).show();
}
}
});
here my xml code for RG and RB
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/RGroup">
<TableRow android:weightSum="1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abdominal"
android:id="#+id/Abdominal"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arm"
android:id="#+id/Arm"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/Back" />
</TableRow>
<TableRow>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chest"
android:id="#+id/Chest"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leg"
android:id="#+id/Leg"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shoulder"
android:id="#+id/Shoulder"/>
</TableRow>
</RadioGroup>
EDITED 1 : Answer :
If you dont want radio button can be selected in one time, so dont use Tablerow
It's not working because of TableRow inside RadioGroup. All RadioButtons are not grouped together because of TableRow between them.
RadioButton should be the direct child of RadioGroup, Otherwise grouping does not work.
Just change your code like this it will work :
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/RGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Abdominal"
android:id="#+id/Abdominal"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Arm"
android:id="#+id/Arm"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/Back" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chest"
android:id="#+id/Chest"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Leg"
android:id="#+id/Leg"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shoulder"
android:id="#+id/Shoulder"/>
</RadioGroup>
Hope this helps. :)
I have noticed that single selection does not work without setting id to radio buttons.
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/expenseRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="#string/expense" />
<RadioButton
android:id="#+id/incomeRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/income" />
</RadioGroup>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:id="#+id/radioGroup">
<RadioButton
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:text="Debit"
android:id="#+id/rDebit"
android:checked="false"
/>
<RadioButton
android:layout_width="0dp"
android:layout_weight="50"
android:layout_height="wrap_content"
android:text="Credit"
android:id="#+id/rCredit"
android:checked="false" />
</RadioGroup>
And in java file
RadioGroup radioGroup;
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
And when to do something
if (radioGroup.getCheckedRadioButtonId() == R.id.rCredit)
{
// do something
}
I encountered the same issue and found where I was making mistakes, i'm describing it below:
If you are using RadioButtons in RadioGroup, then RadioButtons should be Direct Child of RadioGroup.
Check first condition, if it is correct then give id to RatioButton and run project, It will solve the problem automatically.
Simple way. onclick of radio button. do code as per below.
public void clearRadioChecked() {
rdopa.setChecked(false);
rdopb.setChecked(false);
rdopc.setChecked(false);
rdopd.setChecked(false);
}
if you wann select rdopa then on click of rdopa do as below.
clearRadioChecked()
rdopa.setChecked(true);
You can use android:checkedButton attribute on RadioGroup, providing the id of the RadioButton you want to be checked initially and selecting another RadioButton will clear the previous selection.
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="#+id/rbNo"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:text="Yes" />
<RadioButton
android:id="#+id/rbNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</RadioGroup>
You can create your own RadioGroup, that is able to find all RadioButtons that are nested in your group regardless of direct child or not. Below the code of my NestedRadioGroup. Using this instead of your RadioGroup in your xml file should do the trick.
public class NestedRadioGroup extends LinearLayout {
private SparseArray<RadioButton> radioButtons;
private int checkedId;
public NestedRadioGroup(Context context) {
this(context, null);
}
public NestedRadioGroup(Context context, AttributeSet attrs) {
super(context, attrs);
radioButtons = new SparseArray<>();
checkedId = -1;
}
#Override
public void addView(View child, int index, ViewGroup.LayoutParams params) {
super.addView(child, index, params);
findRadioButtons(child);
}
private void findRadioButtons(View child) {
if (child instanceof RadioButton) {
RadioButton newButton = (RadioButton) child;
newButton.setId(radioButtons.size());
newButton.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
if (checkedId != -1) {
radioButtons.get(checkedId).setChecked(false);
}
radioButtons.get(buttonView.getId()).setChecked(true);
checkedId = buttonView.getId();
}
});
radioButtons.put(newButton.getId(), newButton);
} else if (child instanceof ViewGroup) {
ViewGroup group = (ViewGroup) child;
for (int i = 0; i < group.getChildCount(); i++) {
this.findRadioButtons(group.getChildAt(i));
}
}
}
}
Any ideas to make the code cleaner?
I hope it works out for you :)
We can use any layout like LinearLayout, RelativeLayout, ConstraintLayout or GridLayout as RadioGroup using the following method in Kotlin.
For the following example I used ConstraintLayout to get fixed width and height for RadioButton.
layout.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/rbYellow"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space_2x"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Yellow"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbOrange"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RadioButton
android:id="#+id/rbOrange"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Orange"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbRed"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbYellow"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
<RadioButton
android:id="#+id/rbRed"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space"
android:gravity="center"
android:text="Red"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toStartOf="#+id/rbGreen"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbOrange"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
<RadioButton
android:id="#+id/rbGreen"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="#dimen/space"
android:layout_marginEnd="#dimen/space_2x"
android:gravity="center"
android:text="Green"
android:textColor="#android:color/white"
app:layout_constraintBottom_toBottomOf="#id/rbYellow"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/rbRed"
app:layout_constraintTop_toTopOf="#id/rbYellow" />
</androidx.constraintlayout.widget.ConstraintLayout>
Kotlin code
radioGroup.children.forEach { aBtn ->
(aBtn as RadioButton).setOnCheckedChangeListener { button, isChecked ->
if(isChecked) {
radioGroup.children.forEach { bBtn ->
if(bBtn.id != button.id) {
(bBtn as RadioButton).isChecked = false
}
}
}
}
}

Problems with setvisibility for button on RelativeLayout Eclipse

Hy.... I'm making a simple project on eclipse. I'm using relative layout as shown below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".ProfileActivity"
>
<Button
android:id="#+id/Prof_edit_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
/>
<LinearLayout
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
>
<Button
android:id="#+id/Prof_save_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
android:visibility="invisible"
android:clickable="false"
/>
<Button
android:id="#+id/Prof_batal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Batal"
android:layout_marginTop="17dp"
android:visibility="invisible"
android:clickable="false"
/>
</LinearLayout>
</RelativeLayout>
In the main activity, I've made a simple onClicklistener for Edit and Save button as shown below :
mEdit = (Button)findViewById(R.id.Prof_edit_btn);
mEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mBatal.setVisibility(View.VISIBLE);
mBatal.setClickable(true);
mSave.setVisibility(View.VISIBLE);
mSave.setClickable(true);
mEdit.setVisibility(View.INVISIBLE);
}
});
mSave = (Button)findViewById(R.id.Prof_save_btn);
mSave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mEdit.setVisibility(View.VISIBLE);
mBatal.setVisibility(View.INVISIBLE);
mSave.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Data Anda berhasil disimpan", Toast.LENGTH_SHORT).show();
}
});
Well... As we can see from my layout xml, only EDIT button will be visible on the start, then SAVE and BATAL button will be visible when EDIT is clicked by user according to the code program within setonclick listener of EDIT button. And as soon as EDIT button is clicked it will be invisible.... The problems here are WHen I clicked EDIT button, only SAVE button will be visible and BATAL button will be visible when SAVE button is clicked, another problem that I'm facing now is EDIT button is not invisible at all when I click it.... So.. anyone can help me with this problem please...??? :'(
Thanks in advance....
Naaah... I found my own solution... Just set the visibility of SAVE btn and BATAL btn to GONE... And it's all done.... :-)
<Button
android:id="#+id/Prof_save_btn"
android:layout_below="#+id/Prof_LL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="17dp"
android:layout_centerHorizontal="true"
android:visibility="gone"<-----------=
android:clickable="false"
/>
<Button
android:id="#+id/Prof_batal_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Batal"
android:layout_marginTop="17dp"
android:visibility="gone"<-----------=
android:clickable="false"
/>

TextView onclick event not working

In my android code, I have implemented onclick event on 3 textviews using following code. But nothing happens when they are clicked. What is wrong ?
<TextView
android:id="#+id/tv1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/menucircle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="selectit"
android:textColor="#ffffff"
/>
public void selectit(View v)
{
Log.d("tv0","ok");
if(v.getId()==tv1.getId())
{Log.d("tv1","ok");
selectoption(1);
Log.d("tv1","ok");
}
if(v.getId()==tv2.getId())
{Log.d("tv2","ok");
selectoption(2);
}
if(v.getId()==tv3.getId())
{Log.d("tv3","ok");
selectoption(3);
}
}
Add this into your xml
android:clickable="true"
You probably need to have this code in your xml
android:clickable="true"
Or Set the Clicklistenner to TextView via code by
TextView btn=(TextView) findViewById(R.id.tv1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
selectit(v);
}
});
It will automatically make it clickable so no need of android:clickable="true"
You can set the click handler in xml with these attribute:
android:clickable="true"
Don't forget the clickable attribute, without it, the click handler isn't called.
main.xml
<TextView
android:id="#+id/tv1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/menucircle"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="selectit"
android:clickable="true"
android:textColor="#ffffff"
/>
i hope it will help u all the best
Try like below:
android:onClick="onClick"
android:clickable="true"
My textview:
<TextView
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:textSize="55sp"
android:onClick="onClick"
android:clickable="true"/>
Main Activity:
public class MyActivity extends Activity {
public void onClick(View v) {
...
}
}

Ensure one of the RadioButton in a RadioGroup is selected in Android

If I have the following radiogroup:
<RadioGroup
android:id="#+id/rgType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="6" >
<RadioButton
android:id="#+id/rbBrides"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Bridge" />
<RadioButton
android:id="#+id/gbTunnel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tunnel" />
<RadioButton
android:id="#+id/rbHighway"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Highway" />
</RadioGroup>
And I want to do the following check:
If (rgType is selected) {
system.out.println("ok");
}
if (rgType is void/null/not selected) {
system.out.println("choose at least one selection");
}
Can I use isChecked() which is used for individual radio buttons, like the following?
if (rgType.isChecked()) {
}
else {
}
You can use getCheckedRadioButtonId and see if one of your RadioButtons is checked. I'm not exactly sure what it returns if none are but probably either null or more likely -1. Either way, run that and see what it returns.
I believe it does return -1 if nothing checked so try
if (rgType == -1)
{
system.out.println("choose at least one selection");
}
else
{
system.out.println("ok");
}
RadioGroup

Categories

Resources