Passing checkbox reults - android

I am very new at this; I am trying to pass the result of checkbox’s to another activity. I have tried it a few different ways without success and I would be grateful for some direction. Please any help would be great. Thank you
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appliance);
final CheckBox chbxshirleys = (CheckBox)findViewById(R.id.checkboxshirleys);
final CheckBox chbxdianas = (CheckBox)findViewById(R.id.checkboxdianas);
final CheckBox chbxzoila = (CheckBox)findViewById(R.id.checkboxzoila);
final CheckBox chbxsheila = (CheckBox)findViewById(R.id.checkBoxSheila);
final CheckBox chbxrobert = (CheckBox)findViewById(R.id.checkBoxrobert);
final CheckBox chbxsam = (CheckBox)findViewById(R.id.checkBoxsam);
final CheckBox chbxcamren = (CheckBox)findViewById(R.id.checkBoxcamren);
final CheckBox chbxricks = (CheckBox)findViewById(R.id.checkBoxricks);
final Button vendorbutton = (Button)findViewById(R.id.vendorbutton);
vendorbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent (getApplicationContext(),ApplianceMessage.class);
Bundle extras = new Bundle();
extras.putString("shirleys", null);
extras.putInt("vendor", 0);
myIntent.putExtras(extras);
String vendor ="";
if (chbxshirleys.isChecked())
{
vendor += chbxshirleys.getText();
}
if (chbxdianas.isChecked())
{
vendor += chbxdianas.getText();
}
if (chbxzoila.isChecked())
{
vendor += chbxzoila.getText();
}
if (chbxsheila.isChecked())
{
vendor += chbxsheila.getText();
}
if (chbxrobert.isChecked())
{
vendor += chbxrobert.getText();
}
if (chbxsam.isChecked())
{
vendor += chbxsam.getText();
}
if (chbxcamren.isChecked())
{
vendor += chbxcamren.getText();
}
if (chbxricks.isChecked())
{
vendor += chbxricks.getText();
}
}
});
}

I think you can use
intent.putExtra("String name", chbxshirleys.isChecked());
In Activity get boolean value

First of all please learn more about checkboxes on android from here:
http://developer.android.com/guide/topics/ui/controls/checkbox.html
And for good example you can look that one:
http://www.mkyong.com/android/android-checkbox-example/
And for pass the result other activity use extras for activity.

Related

How to use onSaveInstanceState and onRestoreInstanceState to prevent losing checkbox and radiobutton selections

I have a problem in saving state of check state of radio button and checkbox. How can I save check-state of these elements by using onSaveInstanceState and onRestoreInstanceState?
It is a simple quiz app. AFter finish button is clicked, a new score sheet is opened and correct answers turn to green.but at the same time, when orientation is changed, user's selections are lost.
Here is the code:
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
answer2.setTextColor(0xAA76FF03);
answer3.setTextColor(0xAA76FF03);
answer5.setTextColor(0xAA76FF03);
answer42.setTextColor(0xAA76FF03);
answer43.setTextColor(0xAA76FF03);
answer44.setTextColor(0xAA76FF03);
answer45.setTextColor(0xAA76FF03);
}
protected void onRestoreInstanceState(Bundle savedState)
{
answer2.setTextColor(0xAA76FF03);
answer3.setTextColor(0xAA76FF03);
answer5.setTextColor(0xAA76FF03);
answer42.setTextColor(0xAA76FF03);
answer43.setTextColor(0xAA76FF03);
answer44.setTextColor(0xAA76FF03);
answer45.setTextColor(0xAA76FF03);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.html_new_page);
finishButton = (Button) findViewById(R.id.finish_button);
enterName = (EditText) findViewById(R.id.enter_name);
answer1 = (EditText) findViewById(R.id.answer_1);
answer2 = (RadioButton)findViewById(R.id.answer2) ;
answer3 = (RadioButton)findViewById(R.id.answer3);
answer5 = (RadioButton)findViewById(R.id.answer5);
answer41 = (CheckBox)findViewById(R.id.answer41);
answer42 = (CheckBox) findViewById(R.id.answer42);
answer43 = (CheckBox) findViewById(R.id.answer43);
answer44 = (CheckBox) findViewById(R.id.answer44);
answer45 = (CheckBox) findViewById(R.id.answer45);
finishButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String checkAnswer1 = answer1.getText().toString();
c2 = answer2.isChecked();
c3 = answer3.isChecked();
checkAnswer41 = answer41.isChecked();
checkAnswer42 = answer42.isChecked();
checkAnswer43 = answer43.isChecked();
checkAnswer44 = answer44.isChecked();
checkAnswer45 = answer45.isChecked();
c5 = answer5.isChecked();
if ( checkAnswer1.equals("Hypertext Markup Language") || checkAnswer1.equals("HyperText Markup Language")) {
c1 = true;
answer1.setTextColor(0xAA76FF03);
}
else {
c1 = false;
}
if (checkAnswer41 == false && checkAnswer42 == true && checkAnswer43 == true && checkAnswer44 == true && checkAnswer45 == true){
c4 = true;
}
else {
c4 = false;
}
int q1 = (c1) ? 1 : 0;
int q2 = (c2) ? 1 : 0;
int q3 = (c3) ? 1 : 0;
int q4 = (c4) ? 1 : 0;
int q5 = (c5) ? 1 : 0;
int correctNumber = q1 + q2 + q3 + q4 + q5;
correct = Integer.toString(correctNumber);
Intent i = new Intent();
i.setClass(Activity1.this, ResultActivity.class);
i.putExtra("outData", enterName.getText().toString());
i.putExtra("out", correct);
startActivity(i);
}
});
};
}
Use something like this: (adjust to your own code)
public class YourActivity extends AppCompatActivity {
private static final String CHECKBOX_CHECKED_KEY = "is_checkbox_checked";
private CheckBox myCheckBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState != null) {
myCheckBox.setChecked(savedInstanceState.getBoolean(CHECKBOX_CHECKED_KEY));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState)
outState.putBoolean(CHECKBOX_CHECKED_KEY, myCheckBox.isChecked());
}
}
You will need to define different keys for each CheckBoxe's state you want to save.
Here is the example.
static final String STATE_USER = "user";
private String mUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
mUser = savedInstanceState.getString(STATE_USER);
} else {
// Probably initialize members with default values for a new instance
mUser = "NewUser";
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString(STATE_USER, mUser);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
and Official document for Ref

Android: dynamically create matrix of editText based on user input?

I'm new to android development. I've noticed some similar questions but none of them really answered my question well enough that I could figure it out, or else they relied upon depreciated functions.
So far, I've created my main class. I want to open to a screen where the user inputs two numbers for the row and column length of a matrix they want to reduce
public class MainActivity extends AppCompatActivity {
public int numberOfRows;
public int numberOfColumns;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setRows(View v3) {
EditText editTextRow = (EditText) findViewById(R.id.editText2);
if ("".equals(editTextRow.getText())) {
}
else {
numberOfRows = Integer.parseInt(editTextRow.getText().toString());
}
}
public void setColumns(View v2) {
EditText editTextCol = (EditText) findViewById(R.id.editText3);
if ("".equals(editTextCol.getText())) {
numberOfRows = Integer.parseInt(editTextCol.getText().toString());
}
}
public void reduce(View view){
if(numberOfRows != 0 && numberOfColumns != 0) {
}
else {
Intent intent = new Intent(this, ReduceMatrix.class);
Bundle bundle = new Bundle();
bundle.putInt("rows", numberOfRows);
bundle.putInt("cols", numberOfColumns);
intent.putExtras(bundle);
startActivity(intent);
}
}
}
I'm having trouble coming up with the next activity, where I want to dynamically create a matrix of editText...
public class ReduceMatrix extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reduce_matrix);
Intent passedIntent = getIntent();
Bundle extras = passedIntent.getExtras();
int rowNum = extras.getInt("rows");
int colNum = extras.getInt("cols");
Could anyone explain how to go about doing this? I was thinking some sort of for loop, but I have a really hard time with the android layout. Should I be using tableLayout? or gridview? Whenever I have something like
layout = new LinearLayout(this);
It tells me that my qualifier must be in an expression?
You need to add it programmatically. Here's my solution :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent passedIntent = getIntent();
Bundle extras = passedIntent.getExtras();
EditText[][] editTexts = new EditText[rowNum][colNum];
GridLayout gridLayout = new GridLayout(this);
//define how many rows and columns to be used in the layout
gridLayout.setRowCount(rowNum);
gridLayout.setColumnCount(colNum);
for (int i = 0; i < rowNum; i++) {
for (int j = 0; j < colNum; j++) {
editTexts[i][j] = new EditText(this);
setPos(editTexts[i][j], i, j);
gridLayout.addView(editTexts[i][j]);
}
}
setContentView(gridLayout);
}
//putting the edit text according to row and column index
private void setPos(EditText editText, int row, int column) {
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.width = 100;
param.height = 100;
param.setGravity(Gravity.CENTER);
param.rowSpec = GridLayout.spec(row);
param.columnSpec = GridLayout.spec(column);
editText.setLayoutParams(param);
}
This will enable you to add EditText(s) dynamically.

unable to deploy data in an intent

problem: I set point breaks at the following code:
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_NAME, workoutName);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_DAYS, workoutDays);
, both showed up as null when I ran the app in debug mode. workoutName contains a simple String that is passed to a new activity, whereas workoutDays constains an array of String.
the full code is provided below:
public class CreateWorkoutActivity extends Activity {
public final String TAG = this.getClass().getSimpleName();
protected String[] workoutDays = new String[7];
protected String workoutName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_workout);
Button mNextButton = (Button) findViewById(R.id.next_button1);
CheckBox satBox = (CheckBox) findViewById(R.id.sat_checkbox);
CheckBox sunBox = (CheckBox) findViewById(R.id.sun_checkbox);
CheckBox monBox = (CheckBox) findViewById(R.id.mon_checkbox);
CheckBox tuesBox = (CheckBox) findViewById(R.id.tues_checkbox);
CheckBox wedBox = (CheckBox) findViewById(R.id.wed_checkbox);
CheckBox thursBox = (CheckBox) findViewById(R.id.thurs_checkbox);
CheckBox friBox = (CheckBox) findViewById(R.id.fri_checkbox);
final EditText mWorkoutName = (EditText) findViewById(R.id.workout_name1);
workoutName = mWorkoutName.getText().toString();
Log.i(TAG, workoutName);
if (satBox.isChecked()) {
workoutDays[0] = new String(satBox.getText().toString());
}
if (sunBox.isChecked()) {
workoutDays[1] = new String(sunBox.getText().toString());
}
if (monBox.isChecked()) {
workoutDays[2] = new String(monBox.getText().toString());
}
if (tuesBox.isChecked()) {
workoutDays[3] = new String(tuesBox.getText().toString());
}
if (wedBox.isChecked()) {
workoutDays[4] = wedBox.getText().toString();
}
if (thursBox.isChecked()) {
workoutDays[5] = satBox.getText().toString();
}
if (friBox.isChecked()) {
workoutDays[6] = friBox.getText().toString();
Log.i(TAG, workoutDays[6]);
}
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, workoutDays.toString());
Intent intent = new Intent(CreateWorkoutActivity.this, WorkoutRoutinesActivity.class);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_NAME, workoutName);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_DAYS, workoutDays);
Log.i(TAG, workoutDays.toString());
startActivity(intent);
}
});
}
The problem is not in the intent, but in the way you obtain workoutName (this is the null value). You create the activity, set up final EditText mWorkoutName = (EditText) findViewById(R.id.workout_name1); and then immediately ask for the input value through workoutName = mWorkoutName.getText().toString();, but at this time the user still hasn't entered anything. You should put that second line in the listener below (so its activated only after the user presses mNextButton. It's a good idea to put some check after it and send a message to user that they need to fill in that field (if it is indeed necessary).
Looks like the values for workoutName and workoutDays are not filled in initially when the view is created. You should move retrieving the value from the text fields to your onClickListener function.
you checking the CheckBox and EditText in onCreate, absolutely the EditText will be empty and all CheckBox it not checked

Compare data from edit text with data from if statement

I'm making a quiz app. User has to finish the phrase shown on display and write the name of the car in the edittext, after pushing on button, if the answer right, edittext become green, if doesn't, become red. If all answers right (green), intent move on next activity.
I have some difficulties with if statement edit text become red even the answer was right. Also how to make INTENT to move on next activity if all right, if not it doesn't move?
public class MainActivity extends AppCompatActivity {
EditText et_one_one, et_one_two, et_one_three;
Button buttonCheck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_one_one = (EditText) findViewById(R.id.et_one_one);
et_one_two = (EditText) findViewById(R.id.et_one_two);
et_one_three = (EditText) findViewById(R.id.et_one_three);
final String t1 = et_one_one.getText().toString();
final String t2 = et_one_two.getText().toString();
final String t3 = et_one_three.getText().toString();
buttonCheck = (Button) findViewById(R.id.buttonCheck);
buttonCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (t1.equals("maserati")){
et_one_one.setBackgroundColor(Color.GREEN);
}
else {
et_one_one.setBackgroundColor(Color.RED);
}
if (t2.equals("mercedes")){
et_one_two.setBackgroundColor(Color.GREEN);
}
else{
et_one_two.setBackgroundColor(Color.RED);
}
if (t3.equals("bmw")){
et_one_three.setBackgroundColor(Color.GREEN);
}
else{
et_one_three.setBackgroundColor(Color.RED);
}
}
});
}
}
You're changing the color of just the et_one_one each time in your if else statements. Shouldn't it be for different edittexts?
buttonCheck.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean allAnswersCorrect = true;
String t1 = et_one_one.getText().toString();
String t2 = et_one_two.getText().toString();
String t3 = et_one_three.getText().toString();
if (t1.equals("maserati")){
et_one_one.setBackgroundColor(Color.GREEN);
}
else {
allAnswersCorrect = false;
et_one_one.setBackgroundColor(Color.RED);
}
if (t2.equals("mercedes")){
et_one_two.setBackgroundColor(Color.GREEN);
}
else{
allAnswersCorrect = false;
et_one_two.setBackgroundColor(Color.RED);
}
if (t3.equals("bmw")){
et_one_three.setBackgroundColor(Color.GREEN);
}
else{
allAnswersCorrect = false;
et_one_three.setBackgroundColor(Color.RED);
}
if(allAnswersCorrect){
Intent intent = new Intent(YourActivity.this, YourSecondActivity.class);
startActivity(intent);
}
}
});
Maintain a allAnswersCorrect boolean to check whether your answers are correct or not. If all are correct the move to your next activity.
You should use t2.equals("maserati"), and it will be ok.

Passing multicheckbox values to another activity

Let me try this again with better phrasing. I am very new at this; I am trying to pass the value result of checkbox’s to another activity. I have tried it a few different ways without success and I would be grateful for some direction.
I know how to make a “Toast” with the result, that is not what I want it to do. I want the checkbox results will appear in a textview in another activity. If someone could help me out with some hints on how to accomplish that task I would be grateful, I am sorry to bother everyone with such unchallenging questions.
I have read a couple books and watched some tutorials but my skills are still lacking. The books are as repetitive and redundant as the tutorials (buttons and layouts, etc.).
final CheckBox chbxshirleys = (CheckBox)findViewById(R.id.checkboxshirleys);
final CheckBox chbxdianas = (CheckBox)findViewById(R.id.checkboxdianas);
final CheckBox chbxzoila = (CheckBox)findViewById(R.id.checkboxzoila);
final CheckBox chbxsheila = (CheckBox)findViewById(R.id.checkBoxSheila);
final CheckBox chbxrobert = (CheckBox)findViewById(R.id.checkBoxrobert);
final CheckBox chbxsam = (CheckBox)findViewById(R.id.checkBoxsam);
final CheckBox chbxcamren = (CheckBox)findViewById(R.id.checkBoxcamren);
final CheckBox chbxricks = (CheckBox)findViewById(R.id.checkBoxricks);
final Button vendorbutton = (Button)findViewById(R.id.vendorbutton);
vendorbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String vendor ="";
if (chbxshirleys.isChecked())
{
vendor += chbxshirleys.getText();
}
if (chbxdianas.isChecked())
{
vendor += chbxdianas.getText();
}
if (chbxzoila.isChecked())
{
vendor += chbxzoila.getText();
}
if (chbxsheila.isChecked())
{
vendor += chbxsheila.getText();
}
if (chbxrobert.isChecked())
{
vendor += chbxrobert.getText();
}
if (chbxsam.isChecked())
{
vendor += chbxsam.getText();
}
if (chbxcamren.isChecked())
{
vendor += chbxcamren.getText();
}
if (chbxricks.isChecked())
{
vendor += chbxricks.getText();
}
Intent myIntent=new Intent(getApplication(),ApplianceMessage.class);
myIntent.putExtra("chbxshirleys", vendor);
myIntent.putExtra("chbxdianas", vendor);
myIntent.putExtra("chbxzoila", vendor);
myIntent.putExtra("chbxsheila", vendor);
myIntent.putExtra("chbxrobert", vendor);
myIntent.putExtra("chbxsam", vendor);
myIntent.putExtra("chbxcamren", vendor);
myIntent.putExtra("chbxricks", vendor);
startActivity(myIntent);
}
});
}
new activity
public class ApplianceMessage extends Activity {
private TextView tvname = (TextView)findViewById(R.id.tvname);
private EditText etname = (EditText)findViewById(R.id.etname);
private TextView tvcity = (TextView)findViewById(R.id.tvcity);
private EditText etcity = (EditText)findViewById(R.id.etcity);
private TextView tvtime = (TextView)findViewById(R.id.tvtime);
private EditText ettime = (EditText)findViewById(R.id.ettime);
private RadioButton rbutton =(RadioButton)findViewById(R.id.rburgent);
private TextView tvproblem = (TextView)findViewById(R.id.rburgent);
private EditText etproblem = (EditText)findViewById(R.id.etproblem);
private TextView tvvendor = (TextView)findViewById(R.id.vendorlist);
private Button msendbutton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appliance_message);
final Bundle extras = getIntent().getExtras();
extras.getInt("chbxshirleys");
extras.getInt("chbxdianas");
extras.getInt("chbxzoila");
extras.getInt("chbxshiela");
extras.getInt("chbxrobert");
extras.getInt("chbxcamren");
extras.getInt("chbxsam");
extras.getInt("chbxricks");
msendbutton = (Button)findViewById(R.id.sendbutton);
msendbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tvname.setText("Insert Name");
etname.setText("");
tvcity.setText("City Name");
etcity.setText("");
tvtime.setText("Time");
ettime.setText("");
tvproblem.setText("Problem");
etproblem.setText("");
}
});
}
}
In your first activity:
Intent i=new Intent(YourClassName.this,ApplianceMessage.class);
i.putExtra("selected",vendor);
startActivity(i);
In your second Activity:
String selecteditem=getIntent().getExtras().getString("selected");
textview.setText(selecteditem);
You're putting a String Extra in the intent. Just get it by:
getIntent().getStringExtra("chbxshirleys");
....

Categories

Resources