Android, changing EditText based off of checkBoxPreference - java

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");

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);
}
}
});

//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!");
}
}
});

Related

Cannot Resolve my CompundButton buttonview for Switch button

I am trying to disable EditText if Switch is off I have made If-else for it.
But my CompoundButton buttonView cannot be resolved. What can be the issue?
Here's my code:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
EditText editText = (EditText) findViewById(R.id.editText);
/** Called when the user taps the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent newxml = new Intent(this, DisplayMessageActivity.class);
String message = editText.getText().toString();
newxml.putExtra(EXTRA_MESSAGE, message);
startActivity(newxml);
TextView displayText = (TextView) findViewById(R.id.displayText);
displayText.setText(message);
String someText = "Your Message";
TextView yourMessage = (TextView) findViewById(R.id.yourMessage);
yourMessage.setText(someText);
}
final Switch switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// true if the switch is in the On position
if(isChecked){
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
}else{
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
}
}
});
}
Error image
Code Image
I have rewritten the portion of code below. You may try it at your end.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Switch switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// true if the switch is in the On position
if(isChecked){
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
}else{
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
}
}
});
}
..... then other code.

Checkbox value doesn,t inserted in mysql

i am trying to insert checkbox value into my mysql database, there is no error in the program...
But i cannot get the value into the database.
In the database it show empty...
public class MainActivity extends AppCompatActivity {
CheckBox box;
Button btn_test;
private static final String REGISTER_URL="http://192.168.0.106/test/checkin.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
box = (CheckBox) findViewById(R.id.checkBox);
btn_test = (Button) findViewById(R.id.button);
btn_test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
registerUser();
}
});
}
private void registerUser() {
box.isChecked();
String date = box1.toString().trim().toLowerCase();
register(date);
}
private void register(String date){
String urlSuffix = "?date=" + date;
class RegisterUser extends AsyncTask<String, Void, String> {
how can i get the value from the check box and inserted in the database
Please try this,May be it will help you :)
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
String date = box1.toString().trim().toLowerCase();
register(date); } } } );

Progress bar for counting checkboxes

I need every click on the checkbox to increase the percentage in progressBar.
Example: I have two checkboxes, each corresponding to 50% within the progressBar.
I tried several codes, but none worked, I'm a beginner in java
Can someone help me? Please.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
final CheckBox checkBox2 = (CheckBox) findViewById(R.id.checkBox2);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
final SharedPreferences.Editor editor = preferences.edit();
if (preferences.contains("checkbox1") && preferences.getBoolean("checkbox1", false) == true) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox.isChecked()) {
editor.putBoolean("checkbox1", true);
editor.apply();
} else {
editor.putBoolean("checkbox1", false);
editor.apply();
}
}
});
if (preferences.contains("checkbox2") && preferences.getBoolean("checkbox2", false) == true) {
checkBox2.setChecked(true);
} else {
checkBox2.setChecked(false);
}
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox2.isChecked()) {
editor.putBoolean("checkbox2", true);
editor.apply();
} else {
editor.putBoolean("checkbox2", false);
editor.apply();
}
}
});
}
}

button initialization in other function instead of OnCreate() method

Can we define a function in OnCreate method which can be used to initialize buttons and all other widgets. I have tried the following but I am getting
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a
null object reference error
package com.conceptkillers.www.electromania1;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
/**
* Created by Nikesh on 11/02/2016.
*/
public class OhmsLawActivity extends AppCompatActivity {
int checkedCount = 0;
LinearLayout vlayout,ilayout,rlayout,playout;
CheckBox checkBox1,checkBox2,checkBox3,checkBox4;
Spinner spinnerV,spinnerI,spinnerR,spinnerP;
EditText edittextv,edittexti,edittextr,edittextp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("Ohms Law");
InitializeAllWidgets();
}
public void InitializeAllWidgets() {
checkBox1 = (CheckBox) findViewById(R.id.checkbox_v);
checkBox2 = (CheckBox) findViewById(R.id.checkbox_i);
checkBox3 = (CheckBox) findViewById(R.id.checkbox_r);
checkBox4 = (CheckBox) findViewById(R.id.checkbox_p);
edittextv = (EditText) findViewById(R.id.edittext_v);
edittexti = (EditText) findViewById(R.id.edittext_i);
edittextr = (EditText) findViewById(R.id.edittext_r);
edittextp = (EditText) findViewById(R.id.edittext_p);
String[] valuesVoltageSpinner = {"YV","ZV","EV","PV","TV","GV","MV","KV"
,"V","mV","uV","nV","pV","fV","aV","zV","yV"};
String[] valuesCurrentSpinner = {"YA","ZA","EA","PA","TA","GA","MA","KA"
,"A","mA","uA","nA","pA","fA","aA","zA","yA"};
String[] valuesResistanceSpinner = {"YOhm","ZOhm","EOhm","POhm","TOhm","GOhm","MOhm","KOhm"
,"Ohm","mOhm","uOhm","nOhm","pOhm","fOhm","aOhm","zOhm","yOhm"};
String[] valuesPowerSpinner = {"YW","ZW","EW","PW","TW","GW","MW","KW"
,"W","mW","uW","nW","pW","fW","aW","zW","yW"};
spinnerV = (Spinner) findViewById(R.id.spinner_v);
spinnerI = (Spinner) findViewById(R.id.spinner_i);
spinnerR = (Spinner) findViewById(R.id.spinner_r);
spinnerP = (Spinner) findViewById(R.id.spinner_p);
ArrayAdapter<String> voltageAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, valuesVoltageSpinner);
voltageAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinnerV.setAdapter(voltageAdapter);
ArrayAdapter<String> currentAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, valuesCurrentSpinner);
currentAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinnerI.setAdapter(currentAdapter);
ArrayAdapter<String> resistanceAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, valuesResistanceSpinner);
resistanceAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinnerR.setAdapter(resistanceAdapter);
ArrayAdapter<String> powerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, valuesPowerSpinner);
powerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinnerP.setAdapter(powerAdapter);
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
handleCheckBox1();
}
});
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
handleCheckBox2();
}
});
checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
handleCheckBox3();
}
});
checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
handleCheckBox4();
}
});
}
public void onButtonClicked(View v) {
switch(v.getId()) {
case R.id.simple_voltage:
checkBox1.setVisibility(v.INVISIBLE);
checkBox2.setVisibility(v.VISIBLE);
checkBox3.setVisibility(v.VISIBLE);
checkBox4.setVisibility(v.VISIBLE);
checkedCount = 0;
break;
case R.id.simple_current:
checkBox1.setVisibility(v.VISIBLE);
checkBox2.setVisibility(v.INVISIBLE);
checkBox3.setVisibility(v.VISIBLE);
checkBox4.setVisibility(v.VISIBLE);
checkedCount = 0;
break;
case R.id.simple_resistance:
checkBox1.setVisibility(v.VISIBLE);
checkBox2.setVisibility(v.VISIBLE);
checkBox3.setVisibility(v.INVISIBLE);
checkBox4.setVisibility(v.VISIBLE);
checkedCount = 0;
break;
case R.id.simple_power:
checkBox1.setVisibility(v.VISIBLE);
checkBox2.setVisibility(v.VISIBLE);
checkBox3.setVisibility(v.VISIBLE);
checkBox4.setVisibility(v.INVISIBLE);
checkedCount = 0;
break;
}
}
private void handleCheckBox1() {
if(checkedCount < 2) {
if (checkBox1.isChecked()) {
checkedCount++;
edittextv.setVisibility(View.VISIBLE);
spinnerV.setVisibility(View.VISIBLE);
} else {
checkedCount--;
edittextv.setVisibility(View.INVISIBLE);
spinnerV.setVisibility(View.INVISIBLE);
}
}
}
private void handleCheckBox2() {
if(checkedCount < 2) {
if (checkBox2.isChecked()) {
checkedCount++;
edittexti.setVisibility(View.VISIBLE);
spinnerI.setVisibility(View.VISIBLE);
} else {
checkedCount--;
edittexti.setVisibility(View.INVISIBLE);
spinnerI.setVisibility(View.INVISIBLE);
}
}
}
private void handleCheckBox3() {
if(checkedCount < 2) {
if (checkBox3.isChecked()) {
checkedCount++;
edittextr.setVisibility(View.VISIBLE);
spinnerR.setVisibility(View.VISIBLE);
} else {
checkedCount--;
edittextr.setVisibility(View.INVISIBLE);
spinnerR.setVisibility(View.INVISIBLE);
}
}
}
private void handleCheckBox4(){
if(checkedCount < 2) {
if (checkBox4.isChecked()) {
checkedCount++;
edittextp.setVisibility(View.VISIBLE);
spinnerP.setVisibility(View.VISIBLE);
} else {
checkedCount--;
edittextp.setVisibility(View.INVISIBLE);
spinnerP.setVisibility(View.INVISIBLE);
}
}
}
}
You missed the setContentView
setContentView(R.layout.main)
You have not set any layout to your activity. Use setContentView(int layoutId) in your code which will attach the layout to which these widgets (spinners) belong.
The rest of the code seems fine. And by that I mean you can have a method that initializes the widgets.
Change your code and add your layout
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContectView(R.layout.view);
this.setTitle("Ohms Law");
InitializeAllWidgets();
}

Disabling edittext by using a checkbox error

I have a problem here. I want to disable a edittext by using a checkbox. If checkbox is checked, edittext isa available, if not, it is disabled. But I'm having problems. Here is my code: Can anyone check on this. Accdng to eclipse, no error. But when I run it on my phone, the enabling/disabling is not functioning.
public class Order extends Activity {
private Button button1;
private EditText txtbox1,txtbox2;
private TextView tv;
CheckBox check1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText5);
txtbox2= (EditText) findViewById(R.id.editText2);
check1 = (CheckBox)findViewById(R.id.checkcheck);
button1.setOnClickListener(new clicker());
}
public void addListenerOncheck1() {
check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
txtbox1.setFocusable(true);
txtbox1.setFocusableInTouchMode(true);
} else {
txtbox1.setFocusable(false);
txtbox1.setFocusableInTouchMode(false);
}
}
});
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3;
tv.setText(vis.toString());
}
}
}
you are nowhere adding your Listener by calling ur method addListenerOncheck1();
so put addListenerOncheck1(); add the end of you OnCreate method.
But I highly recommend you not to use all these additional self-made Classes for just adding listeners. Use the following code instead and add it into your OnCreate Method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText5);
txtbox2= (EditText) findViewById(R.id.editText2);
check1 = (CheckBox)findViewById(R.id.checkcheck);
button1.setOnClickListener(new clicker());
check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
txtbox1.setEnabled(false);
} else {
txtbox1.setEnabled(true);
}
}
});
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3;
tv.setText(vis.toString());
}
});
// This will make sure the user can only type numbers into the EditTexts:
txtbox1.setInputType(InputType.TYPE_CLASS_NUMBER);
txtbox2.setInputType(InputType.TYPE_CLASS_NUMBER);
// Code to disable editTexts at start once:
txtbox1.setEnabled(false);
txtbox2.setEnabled(false);
}

Categories