Android, changing EditText based off of checkBoxPreference

237 views Asked by At

I am trying to add a basic setting to my app. I would like to change the hint of an EditText in one of my activities if the checkBox is unchecked.

preferences.xml

    <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="PREF_SHOW_UNITS"
        android:id="@+id/Check1"
        android:title="Show units in Kinematics"
        android:defaultValue="true" >
    </CheckBoxPreference>
</PreferenceScreen>

SettingsActivity.java:

package com.supermath.jacobgb24.supermath;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;

import static com.supermath.jacobgb24.supermath.R.xml.preferences;

public class SettingsActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(preferences);
    }
}

Code that I am trying to add changes to:

public class Kinematics extends ActionBarActivity {
static EditText tval, aval, vval, xval;
static double a, t, x, v;
static DecimalFormat fmt = new DecimalFormat("###,###.###");

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.klayout);
    CheckBox cb1 = (CheckBox) findViewById(R.id.Check1);
    tval = (EditText) findViewById(R.id.time);
    aval = (EditText) findViewById(R.id.accel);
    vval = (EditText) findViewById(R.id.velocity);
    xval = (EditText) findViewById(R.id.xpos);
    cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox, boolean isChecked) {
            if (isChecked) {
                tval.setHint("YAY");
            } else {
                tval.setHint("UNYAY");
            }
        }
    });
}

Solved

Since I was using preferences I needed the following code:

PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean disp = prefs.getBoolean("PREF_SHOW_UNITS", true);
    if(disp)
        tval.setHint("t(s)");
    tval.setHint("t(s)");
    else if(!disp)
        tval.setHint("UNYAY");
2

There are 2 answers

1
Pramod Yadav On

you can change the hint of the edittext on checkbox check via following:

your_checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton checkbox,
                boolean isChecked) {
            // TODO Auto-generated method stub
             if (isChecked)
             {
             your_edittext.setHint(textonChecked);
             }
             else
             { 
             your_edittext.setHint(textonUnchecked);
             }  
        }
    });
0
gegobyte On
//Get EditText
EditText editText1 = (EditText) findViewById(R.id.editText1);

//Get Checkbox    
CheckBox cb1 = (Checkbox) findViewById(R.id.checkBox1);

//Add Listener to Checkbox
cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if(isChecked) {
            //Set a hint to EditText
            editText1.setHint("This is a hint!");
        }

    }
});