Android: pass checkbox status to another activity and use in if statement - android

In my main activity, I have 6 checkboxes for selecting which parts of a test the person will want to take. I've looked at a bunch of pages trying to figure out what to do, but I couldn't gain a clear understanding of what I was supposed to do.
Let's say, in my main activity, I have the checkboxes:
CheckBox grade1 = (CheckBox)findViewById(R.id.grade1);
CheckBox grade2 = (CheckBox)findViewById(R.id.grade2);
CheckBox grade3 = (CheckBox)findViewById(R.id.grade3);
CheckBox grade4 = (CheckBox)findViewById(R.id.grade4);
CheckBox grade5 = (CheckBox)findViewById(R.id.grade5);
CheckBox grade6 = (CheckBox)findViewById(R.id.grade6);
And if they're checked, I want to pass them to my next activity using this onClick (with whatever code needs to be added to make it work):
private void setupMessageButton() {
Button messageButton = (Button) findViewById(R.id.main_button);
messageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Test.class));
}
});
}
Then in my new activity, I have if statements which are currently written this way:
if (grade1.isChecked()){
// Do stuff
}
I'm looking for the simplest way to do this. TL:DR - If a checkbox is checked in one activity, how can I use that status to do something in another activity with an if statement?

Using the answer from poss, I was able to figure this out. I have 6 checkboxes in my main activity in the onClick.
CheckBox grade1 = (CheckBox)findViewById(R.id.grade1);
CheckBox grade2 = (CheckBox)findViewById(R.id.grade2);
CheckBox grade3 = (CheckBox)findViewById(R.id.grade3);
CheckBox grade4 = (CheckBox)findViewById(R.id.grade4);
CheckBox grade5 = (CheckBox)findViewById(R.id.grade5);
CheckBox grade6 = (CheckBox)findViewById(R.id.grade6);
So I just need to pass them using the following code (also in the onClick):
Intent intent = new Intent(MainActivity.this, Test.class);
intent.putExtra("grade1", grade1.isChecked());
intent.putExtra("grade2", grade2.isChecked());
intent.putExtra("grade3", grade3.isChecked());
intent.putExtra("grade4", grade4.isChecked());
intent.putExtra("grade5", grade5.isChecked());
intent.putExtra("grade6", grade6.isChecked());
startActivity(intent);
Then in my second activity, just like poss stated, these need to be at the top:
Boolean check1 = getIntent().getExtras().getBoolean("grade1");
Boolean check2 = getIntent().getExtras().getBoolean("grade2");
Boolean check3 = getIntent().getExtras().getBoolean("grade3");
Boolean check4 = getIntent().getExtras().getBoolean("grade4");
Boolean check5 = getIntent().getExtras().getBoolean("grade5");
Boolean check6 = getIntent().getExtras().getBoolean("grade6");
And for my if statements, I just do:
if (check1){
// Do stuff
}
if (check2){
// Do more stuff
}
if (check2){
// Etc...
}
Thanks for the help in solving this!!

You can use intent.
In your MainActivity.java:
private void setupMessageButton() {
Button messageButton = (Button) findViewById(R.id.main_button);
messageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Test.class);
intent.putExtra("selectedGrade", v.getId());
startActivity(intent);
}
}
In your Test.java:
int selectedGrade = getIntent().getExtras().getint("selectedGrade");
switch (selectedGrade) {
case R.id.grade1: //do something
break;
case R.id.grade2: //do something
break;
....
}

You can use Intent extras for this.
For example, if checkBox 1 is checked, you can put extra into intent like this.
Intent intent = new Intent(MainActivity.this, Test.class);
intent.putExtra("check1", true);
startActivity(intent);
In your Test.class you just retrieve the value.
Boolean check1 = getIntent().getExtras().getBoolean("check1");

Related

How can I make a switch buttons change the background of an Android Studio TextView?

I would like to know how I can change the background-color of a TextView, through a switch button. It is necessary to clarify that my switch buttons are in MainActivy and my TextView are in other Activities, to which I access like this:
tvBoton = (TextView) findViewViewById(R.id.Schedule);
tvBoton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, central.class);
startActivity(i);
}
});
I don't know if it influences anything, but my switch buttons have SharePreferences:
pol = (Switch) findViewById(R.id.switch18);
sharpol = getSharedPreferences("pol", MODE_PRIVATE);
final SharedPreferences.Editor editorpol = sharpol.edit();
pol.setChecked(sharpol.getBoolean(ex, false));
pol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean ispolChecked) {
if (ispolChecked) {
editorpol.putBoolean(ex, true);
politica=(TextView) findViewViewById(R.id.polit);
politica.setText("Not working");
} else {
editorpol.putBoolean(ex, false);
}
editorpol.commit();
}
});
As it is in the previous code, I tried inside the ispolChecked, in the true option, to reference the TetView, and then put setText, but this TextView, being inside another layout does not work. I also put setContentView(R.layout.main_activity) several times, but changing it to the respective layouts, but it didn't work either.
Honestly, I don't know what to do anymore. Thanks for your answers.
Use SharedPreference to store the state of the Switch and when you have stored it go to the next Activity and change background color of TextView based on the state of the SharePreference.

Multiple Image Buttons

I created an activity on android studio and I have put there something like 20 ImageButtons. I want to use it as on each click on an image it will move to a new activity. All of the Image Buttons are working on the same principle, my app is a game, and each image represents a level. I want to build one function that will be used on all buttons and will move the user to a new activity according to the data(the properties of the image button) and use that data on the new activity. Every level has its own activity and the main activity is the menu of the game.
Below is my code:
public ImageButton beatsCall; public void Beats(){ beatsCall=(ImageButton)findViewById(R.id.beats); beatsCall.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) { Intent toy = new Intent(Levels.this,Beats.class); startActivity(toy); } }); }
You need to provide more information and code. However, you may want to try set a distinct onClickListener and then set all the imageButtons to that listener that will perform an action depending on the button clicked. For example, say you have 4 imageButtons and you want to perform a different action (in your case, start a new activity) for each different button click.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
//Start activity 1 here, for example
Intent intent = new Intent(this, YourNewActivity1.class);
String message = v.getId().toString;
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
break;
case R.id.textView2:
//Start activity 2 here
break;
case R.id.textView3:
//Start activity 3 here
break;
case R.id.textView4:
//Start activity 4 here
}
}
};
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
button3.setOnClickListener(listener);
button4.setOnClickListener(listener);
This is assuming you have the imageButtons set up in your layout file and you have them initialized in your activity.
In your new activity, you can get the message as such:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
if (some condition with message){
do something
}
You may also check out this documentation for further information regarding intents.
Something like this? In your xml make your images clickable and give them ID's like this...
<ImageView
android:id="#+id/level_1_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
/>
Then call a function like this in your Activity's onCreate
private void setupButtons() {
findViewById(R.id.level_1_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), LevelOne.class));
}
});
findViewById(R.id.level_2_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getApplication(), LevelTwo.class));
}
});
}
You could assign a tag via android:tag to each of your views and then use your single listener to switch on the view's tag to branch the behavior you want.

Button not responding to click event

Below is what i have tried. "Forward" class extends Activity. It contains two buttons. In this class , we create two objects of "Collector" class using for loop. In the first iteration of the loop we create first button. In the second, we create one more button. we set "OnClickListener" for both the buttons. but only the second button responds for the click. First button doesnt respond to a click. I'm trying to use same variable name (b1) - I want to stick to OO Principle by not creating separate object for achieving my goal. - Also I'm expecting properties of 2 Objects as separate entity.Please help me. regards.
Forward.java (is an Activity, contains two buttons):
below is a for loop in which i create 2 objects of collector class.
for (int i = 0; i < 2; i++) {
new Collector(this, i);
}
Collector.java :
public class Collector {
Forwarder f;
int n;
Button b1;
public Collector(Forwarder caller, int i) {
f = caller;
n = i; // 0 or 1
f.setContentView(R.layout.forwarder);
switch(n)
{
case 0:
b1 = (Button) f.findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// get a new Contact
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
f.startActivityForResult(i, 1);
// onActivityResult has to be implemented in f because
// f extends Activity class
}
});
break;
case 1:
b1 = (Button) f.findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// get a new Contact
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
f.startActivityForResult(i, 1);
}
});
break;
default:
}
}
}
You gave Your button a new id.
first, You wrote:
b1 = (Button) f.findViewById(R.id.button1);
at the second switch You wrote:
b1 = (Button) f.findViewById(R.id.button2);
public class Collector {
Forwarder f;
int n;
Button b1;
public Collector(Forwarder caller, int i) {
f = caller;
n = i; // 0 or 1
f.setContentView(R.layout.forwarder);
switch(n)
{
case 0:
b1 = (Button) f.findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// get a new Contact
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
f.startActivityForResult(i, 1);
// onActivityResult has to be implemented in f because
// f extends Activity class
}
});
break;
case 1:
b1 = (Button) f.findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// get a new Contact
Intent i = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
f.startActivityForResult(i, 1);
}
});
break;
default:
}
}
}
You have mistaken in your switch case you named both of buttons b1 when the second object starts to be create b1 is no longer Button1 now it is Button2 so you set onclick listener only for Button2 use a custom class for your buttons or try to declear new objects like b2 b3
good luck soheil
Check the button names. They are the same (b1). Change the second variable name and everything should work perfectly. Damn copy-paste :P
I don't have the answer on why such behavior but it will definitively help you if you add few things as a part of clarification or rephrase your question else people consider it as some novice mistake.
- Clearly mention that you know that you are trying to use same variable name (b1)
- You want to stick to OO Principle by not creating separate object for achieving your goal.
- Also you can mention that you are expecting properties of 2 Objects as separate entity.
Hope it helps. All the best.

i wanted to start a new activity after pressing the three buttons ...please let me know how can i achieve this?

Hi i am creating a simple educational game in which user has to press three images in order to proceed to the next level and in next level user has to press 5 images.
i have gone through the on click but i using onIntent intent = new Intent(this.getApplicationContext(), Activity.class);
this.startActivity(intent, 0); i am only able to start new activity on single button pressed but i wanted to start new activity when user has finished pressing three image Buttons.
Thanks in advanced.
you can use global int variable and increase it every button click and if its more than your button number open new activity
public int btn = 0;
MyButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if(btn >= 2) {
//open your activity
}
else{
btn++;
}
}
});
Based on my understanding of your problem,I have provided a possible simple implementation.
Use a simple data structure like queue or stack. When a image is tapped, add information about the image to the data structure. After adding the information to data structure see if number of items in data structure is equal to 3 ? if yes check data structure has info about the required three images and not just info about the same image (happens if user taps on same image more than once). If condition is met then call startActivity(). Generalize this so that you can reuse the logic in different activities, irrespective of the number of images.
Hi i finally achieved my desired activity. now with this after checking two check box app will start new activity.
`private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay;
OnClickListener checkBoxListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
checkBoxListener =new OnClickListener() {
public void onClick(View v) {
if(chkIos.isChecked()&&chkAndroid.isChecked()) {
Intent i1=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
if(chkAndroid.isChecked()) {
Intent i2=new Intent(getApplicationContext(), Main1.class);
startActivity(i1);
}
}
}
};
chkIos.setOnClickListener(checkBoxListener);
chkAndroid.setOnClickListener(checkBoxListener);
}
}
`
thanks for all your answers.

Changing Activity on a clickable layout

I have this clickable LinearLayout view, im trying to have it change Activity when clicked by every time i click the object i get a error.
final LinearLayout lindet = (LinearLayout) findViewById(R.id.detials);
lindet.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(SellingActivity.this, DetailsActivity.class);
startActivity(i);
finish();
}
});
Did you remember to add the DetailsActivity to the AndroidManifest?
Supporting Macarse I would add that use SellingActivity.this.finish() instead of finish()
I believe this will prove to be your problem solver.

Categories

Resources