I am trying to make a checkbox in android studio/java that if checked will result in a subset of other checkboxes checkable and checked.
The developer site says:
abstract void setChecked(boolean checked)
Change the checked state of the view
But this hasn't worked for me.
Current code:
Java:
package crc.cybereye;
public class CreateNew extends Activity {
LinearLayout background;
Button btnBack;
CheckBox checkbox_structural_info;
CheckBox checkbox_years_of;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
background = (LinearLayout) findViewById(R.id.background);
setContentView(R.layout.activity_create_new);
btnBack = (Button) findViewById(R.id.btnBack);
checkbox_years_of = (CheckBox) findViewById(R.id.checkbox_years_of);
checkbox_floors_above = (CheckBox) findViewById(R.id.checkbox_floors_above);
checkbox_structural_info = (CheckBox) findViewById(R.id.checkbox_structural_info);
// setUpIntroCheckBoxes(); // Add change listeners to check boxes
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), NewFormActivity.class);
startActivityForResult(intent, 0);
}
});
}
More Java:
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_structural_info:
if (checked){
checkbox_years_of checked.setChecked(true) //ERROR HERE
}
break;
case R.id.checkbox_years_of:
if (checked){
}
break;
XML:
<TableRow android:layout_marginTop="10dp">
<TextView
android:text="#string/structural_info"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
/>
<CheckBox android:id="#+id/checkbox_structural_info"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"/>
</TableRow>
<TableRow>
<TextView
android:text="#string/years_of"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
/>
<CheckBox android:id="#+id/checkbox_years_of"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"/>
<TableRow>
There is an error saying that it cannot resolve method setChecked(boolean).
My code currently is just trying to get the structural_info checkbox to check the years_of checkbox if it is checked, but I also want to make years_or only checkable if structural_info is checked, I haven't got there yet because I was still stuck on this issue.
Thanks so much for any help in advance.
As requested by the creator of the post, i'm adding the comment as an answer:
You're calling setChecked to checked wich is a boolean variable.
You shoud write:
checkbox_years_of.setChecked(true)
Instead of
checkbox_years_of checked.setChecked(true)
Related
I created a simple activity that is intended to send two pieces of information to another activity. However, my button isn't registering any click events. Does anyone have any idea why this would be? I expected that by adding the onClick = "onClickWrite" to the XML file that they would be linked but there is no response when clicked. If anyone could help me out, I would greatly appreciate it. I have tried implementing the onClickListener, however, with that implementation, it throws an error on my Toasts.
Activity
public class InitializeClass extends Activity {
private Service service;
private Button button;
EditText infoOne;
EditText infoTwo;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.button_control);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(clicked);
infoOne= (EditText) findViewById(R.id.editText);
infoTwo= (EditText) findViewById(R.id.editText1);
}
private View.OnClickListener clicked = new View.OnClickListener(){
#Override
public void onClick(View view) {
if (service != null) {
int size = 100;
byte[] byteArray = new byte[size];
byte[] byteArrayTwo = new byte[size];
byteArray = infoOne.getText().toString().getBytes(Charset.defaultCharset());
byteArrayTwo= infoTwo.getText().toString().getBytes(Charset.defaultCharset());
if ((!(infoOne.getText().toString().isEmpty())) && (!(infoTwo.getText().toString().isEmpty()))) {
service.setInfo(byteArray);
service.setInfoTwo(byteArrayTwo);
intentMethod();
}
}
}
};
public void intentMethod() {
Intent intent = new Intent(this, DeviceScanActivity.class);
startActivity(intent);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".InitializeClass">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="#string/send_info"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColor="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText1" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="150dp"
android:ems="10"
android:inputType="textPersonName"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColorHint="#android:color/white"
android:textCursorDrawable="#drawable/color_cursor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="info"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColorHint="#android:color/white"
android:textCursorDrawable="#drawable/color_cursor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
</android.support.constraint.ConstraintLayout>
You need to initialize your button first.
Button button = (Button) findViewById(R.id.button);
Hope this helps!
In your activity class assign the button to the id of the button in the xml:
private Button btn = findViewById(R.id.button);
You will then create an OnClickListener:
private View.OnClickListener clicked = new View.OnClickListener(){
#Override
public void onClick(View view) {
//Insert Code here
}
};
In your onCreate method in the same activity class you will instantiate the button and assign the button to the onClickListener:
btn = new Button(this);
btn.setOnClickListener(clicked);
There are couple of things you need to do for that.
Create Field.
private Button button;
Initialize button.
Button button = (Button) findViewById(R.id.button);
Set Listener on button.
button.setOnClickListener();
I found the answer. Service in if (service != null) was never being initialized. Stupid mistake on my part. Thanks, everyone for the help and directing me in a way to implement onClickListener!
You need to cast the button (name) with you Java code .
for example on xml you have id/button (name)
declare it on your Java file, and do the casting.
and the onClickListener will look like this :
Cast the button : Button button ;
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
I am trying to make a custom dialog box which gives radio buttons to select from n then diplay text of radio button in the textfield. This is what i tried..
My Layout for dialog box is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="#+id/rg_carType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical">
<RadioButton
android:id="#+id/rb_hatchD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hatchback(Diesel)" />
<RadioButton
android:id="#+id/rb_hatchP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hatchback(Petrol)" />
<RadioButton
android:id="#+id/rb_sedanD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sedan(Diesel)" />
<RadioButton
android:id="#+id/rb_sedanP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sedan(Petrol)" />
<RadioButton
android:id="#+id/rb_suv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUV" />
</RadioGroup>
<ImageView
android:id="#+id/iv_carType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_toRightOf="#+id/rg_carType"
/>
<Button
android:id="#+id/btn_confirmCar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/rg_carType"
android:layout_centerHorizontal="true"
android:background="#drawable/button_2"
android:text="Confirm"
android:textColor="#fff" />
And the function for custom dialog box is
private void diplayCartype() {
final Dialog dialog_car = new Dialog(FinalActivity.this);
final RadioButton rb_select;
RadioGroup rg_carType;
ImageView iv_carType;
Button btn_confirmCar;
String sCar="?";
dialog_car.setTitle("Select Car Type");
dialog_car.setContentView(R.layout.dialog_cartype);
iv_carType=(ImageView)dialog_car.findViewById(R.id.iv_carType);
btn_confirmCar=(Button)dialog_car.findViewById(R.id.btn_confirmCar);
rg_carType=(RadioGroup)dialog_car.findViewById(R.id.rg_carType);
int selected=rg_carType.getCheckedRadioButtonId();
rb_select=(RadioButton)dialog_car.findViewById(selected);
switch(selected)
{
case R.id.rb_hatchD:
iv_carType.setImageResource(R.drawable.hatch_d);
sCar=rb_select.getText().toString();
break;
case R.id.rb_hatchP:
iv_carType.setImageResource(R.drawable.hatch_p);
sCar=rb_select.getText().toString();
break;
case R.id.rb_sedanD:
iv_carType.setImageResource(R.drawable.sedan_d);
sCar=rb_select.getText().toString();
break;
case R.id.rb_sedanP:
iv_carType.setImageResource(R.drawable.sedan_p);
sCar=rb_select.getText().toString();
break;
case R.id.rb_suv:
iv_carType.setImageResource(R.drawable.suv);
sCar=rb_select.getText().toString();
break;
default:
iv_carType.setImageResource(R.drawable.suv);
sCar="default";
}
final String finalSCar = sCar;
btn_confirmCar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tv_carType.setText(finalSCar);
dialog_car.dismiss();
}
});
dialog_car.show();
}
The problem is that the switch case executes only default condition, that means its not getting id to check. Thanks in advance.. :)
this code work for me
public class RadioButtonFragment extends DialogFragment {
String singleitem;
private int witch;
private MySharedPreferences mySharedPreferences = new MySharedPreferences();
private String[] item = {"Naskh asiatype", "Fajer noori Nastaleeq", "Pak nastaleeq (default)"};
#Override
public void onResume() {
super.onResume();
witch= mySharedPreferences.loadIntPrefs(Constants.RADIO_BUTTON_INDEX_KEY,2,getActivity());
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
witch= mySharedPreferences.loadIntPrefs(Constants.RADIO_BUTTON_INDEX_KEY,2,getActivity());
builder.setTitle("please selet any fount").setSingleChoiceItems(item, witch, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
singleitem= item[which];
mySharedPreferences.saveStringPrefs(Constants.FONT_KEY,singleitem,getActivity());
mySharedPreferences.saveintPrefs(Constants.RADIO_BUTTON_INDEX_KEY,which,getActivity());
Toast.makeText(getContext(),"Font is selected"+which,Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
and call to build this fragment
RadioButtonFragment radioButtonFragment = new RadioButtonFragment();
radioButtonFragment.show(getSupportFragmentManager(), RadioButtonFragment_TAG);
You need read the value at the right time (button click). Do this:
btn_confirmCar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
rg_carType=(RadioGroup)dialog_car.findViewById(R.id.rg_carType);
int selected=rg_carType.getCheckedRadioButtonId();
rb_select=(RadioButton)dialog_car.findViewById(selected);
switch(selected)
{
case R.id.rb_hatchD:
iv_carType.setImageResource(R.drawable.hatch_d);
break;
case R.id.rb_hatchP:
iv_carType.setImageResource(R.drawable.hatch_p);
break;
case R.id.rb_sedanD:
iv_carType.setImageResource(R.drawable.sedan_d);
break;
case R.id.rb_sedanP:
iv_carType.setImageResource(R.drawable.sedan_p);
break;
case R.id.rb_suv:
iv_carType.setImageResource(R.drawable.suv);
break;
default:
iv_carType.setImageResource(R.drawable.suv);
sCar="default";
}
if (!sCar.equals("default"))
sCar = rb_select.getText().toString();
tv_carType.setText(sCar);
dialog_car.dismiss();
}
});
dialog_car.show();
}
About getCheckedRadioButtonId()
Returns the identifier of the selected radio button in this group.
Upon empty selection, the returned value is -1.
There is no selected RadioButton at the moment when you are trying to check for selected one.
dialog_car.setContentView(R.layout.dialog_cartype);
// at this point you have newly created view hierarchy and
// RadioGroup has no selected items
iv_carType=(ImageView)dialog_car.findViewById(R.id.iv_carType);
btn_confirmCar=(Button)dialog_car.findViewById(R.id.btn_confirmCar);
rg_carType=(RadioGroup)dialog_car.findViewById(R.id.rg_carType);
// still has no selected items, next call returns -1 as well
int selected=rg_carType.getCheckedRadioButtonId();
You should use getCheckedRadioButtonId() from handler of some extra button (e.g. "OK") to be sure that user has seen your radio buttons and has enough time to select something.
You must use an interface (Listener) for listening to User Inputs. For example you can use RadioGroup.setOnCheckedChangeListener and then place your switch statement in it.
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radioButtonId:
// Your Code Here
break;
---
}
}
});
Your current code would statically pick the default values of initially assigned states. Above enhancements would help you handle user inputs dynamically.
I have problem.
I made method which creates Dialog with my own layout. And I have no idea how to pass values (Strings) from my EdiText and asing to any variable in my Activity.
In comments you can see how I was trying to solve this.
Java method
public void makeDialog(){
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_ip);
dialog.setTitle("IP connection");
// Todo passing value from dialog to activity
// final EditText ipValueConnection = (EditText)findViewById(R.id.ipValueConnection);
// ipValueConnection.setOnClickListener(this);
// EditText portValueConnection = (EditText)findViewById(R.id.portValueConnection);
// Toast.makeText(context, ipValueConnection.getText().toString(), Toast.LENGTH_LONG).show();
Button dialogButtonLogin = (Button) dialog.findViewById(R.id.dialogButtonLogin);
// if button is clicked, close the custom dialog
dialogButtonLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tryToConnect();
dialog.dismiss();
}
});
// set the custom dialog components - text, image and button
// TextView text = (TextView) dialog.findViewById(R.id.IP);
dialog.show();
}
XML layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/antena"
android:layout_width="220dp"
android:layout_height="120dp"
android:scaleType="centerInside"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name"
android:adjustViewBounds="true"
/>
<EditText
android:id="#+id/ipValueConnection"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="IP" />
<EditText
android:id="#+id/portValueConnection"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="PORT"/>
<Button
android:id="#+id/dialogButtonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="5dp"
/>
</LinearLayout>
Create a interface
public interface OnClickInterface {
public void onClick();
}
call it instantiate it in your activity onCreate()
OnClickInterface onClickInterface = new OnClickInterface() {
#Override
public void onClick() {
//Call Method from here
requiredMethod();
}
};
//And in your dialog classs or method
public void makeDialog(OnClickInterface onClickInterface){
//Your code
dialogButtonLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickInterface.onClick();
dialog.dismiss();
}
});
}
The error you are getting means that a reference to the editText cannot be found in the current layout file. You have find the EditText in the custom dialog view instead of the activity view.
So instead of:
final EditText ipValueConnection =(EditText)findViewById(R.id.ipValueConnection);
use:
final EditText ipValueConnection =(EditText)dialog.findViewById(R.id.ipValueConnection);
If i have understood your question correctly,
your edit text is in dialog_ip so you need to use
final EditText ipValueConnection = (EditText) dialog.findViewById(R.id.ipValueConnection);
Then you can get text from edit text as
String text= ipValueConnection.getText().toString;
And use that variable in your activity.
I am trying to make a radio button toggle using setChecked().
setChecked(false) works fine but setChecked(true) doesn't work.
Also tried toggle(), even this doesn't uncheck the radio button.
I need a solution without using radio groups. Just a single radio button and on tapping should toggle its state.
MainActivity.java
public void radioCheck(View v) {
System.out.println("radioCheck");
RadioButton rb = (RadioButton) findViewById(R.id.radioButton1);
//rb.toggle();
if (rb.isChecked() == true) {
rb.setChecked(false);
}
else {
rb.setChecked(true);
}
}
activity_main.xml
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:onClick="radioCheck"
android:text="Set me" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/radioButton1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="39dp"
android:onClick="radioCheck"
android:text="On/ Off" />
Surprisingly, setChecked works when a button is used to uncheck the radio button but the same doesn't work when set on radio button.
You're causing a infinite loop and stack overflow probably. The setChecked is all unnecessary, you don't need to toggle it yourself. The onClick is for reacting to a new selection, not checking/unchecking it yourself.
From http://developer.android.com/guide/topics/ui/controls/radiobutton.html:
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="#+id/radio_pirates"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pirates"
android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="#+id/radio_ninjas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ninjas"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
Try this way,hope this will help you to solve your problem.
if condition work true or false case so when you get isChecked() is already return true or false so no required to check with (== true) this things you have done wrong in your code.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Set me" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="radioCheck"
android:layout_marginTop="10dp"
android:text="On/ Off" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
private RadioButton radioButton1;
private Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
button2 = (RadioButton) findViewById(R.id.button2);
}
public void radioCheck(View v) {
if (radioButton1.isChecked()) {
radioButton1.setChecked(false);
}
else {
radioButton1.setChecked(true);
}
}
}
I have a relative layout. In that relative layout i have a textview at left end and check box at right end. Now
what i want is, when i clicked the entire layout( above relative layout) the check box need to check. any please
help me.
<RelativeLayout
android:id="#+id/layoutavailable"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/layouttaxexempt"
android:layout_marginTop="25dp"
>
<TextView
android:id="#+id/txt_avl"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:hint="IsAlwaysAvailable"
android:textSize="20sp"
android:textColorHint="#color/white"
android:layout_marginTop="5dp"
android:layout_marginLeft="13dp"
/>
<CheckBox
android:id="#+id/c_avl"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"/>
</RelativeLayout>
Code:
public void onClick(View v) {
if (checkbox.isChecked()){
checkbox.setChecked(false);
}
else {
checkbox.setChecked(true);
}
}
Set the listener to the layout
CheckBox cBox = (CheckBox) findViewById(R.id.c_avl);
findViewById(R.id.layoutavailable).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cBox.setChecked(!cBox.isChecked());
}
});
Use CheckedTextView instead of normal TextView.
And make CheckedTextView width and height to match_parent"
Just try this:
public class MainActivity extends Activity {
RelativeLayout layout;
CheckBox cb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb=(CheckBox)findViewById(R.id.checkBox1);
layout = (RelativeLayout) findViewById(R.id.layoutavailable);
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
cb.setChecked(true);
return false;
}
});
}
you can do by following way
yourRelativeLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
youCheckbox.setChecked(!(test.isChecked()));
}
});
First make your Relative Layout clickable by writing the given code in your Relative Layout Tag body, and also assign the id to your layout.:-
android:clickable="true"
android:id="#+id/rl"
Then in your Activity Java file, write the code in your on create instance:-
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
Relative Layout rl = (LinearLayout)findViewById(R.id.rl);
final CheckBox c_avl = (CheckBox)findViewById(R.id.checkBox1);
rl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
c_avl.setChecked(true);
}
});
}
This will certainly solve your problem, indeed.
You can also achieve this by using the toggle() method of the CheckBox class. Please note that when using the toggle method, there won't be any animation. The way that I fixed that issue was by overriding the toggle method, and defining my own toggle.
private RelativeLayout layoutAvailable = findViewById(R.id.layoutavailable);
private CheckBox cAvl = findViewById(R.id.c_avl);
layoutavailable.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
cAvl.toggle();
}
})
Just pass the state of the parent RelativeLayout to it's child CheckBox
set RelativeLayout clickable, and add an attribute android:duplicateParentState to CheckBox
<RelativeLayout
android:id="#+id/layoutavailable"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/layouttaxexempt"
android:layout_marginTop="25dp"
android:clickable="true"
>
<TextView
android:id="#+id/txt_avl"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:hint="IsAlwaysAvailable"
android:textSize="20sp"
android:textColorHint="#color/white"
android:layout_marginTop="5dp"
android:layout_marginLeft="13dp"
/>
<CheckBox
android:id="#+id/c_avl"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:duplicateParentState='true'
android:layout_marginTop="5dp"/>
</RelativeLayout>