I'm trying to make an app that asks the user maths questions. I have a number of buttons, such as multiplication, addition etc. I want my code to have a series of if/else statements that tells it what operator to use based on which button was clicked in the previous activity.
What do I use to access this information?
You can always pass argument to You activity using Extra and perform some actions based on its value.
int extraValue = 5;
Intent i = new Intent();
i.putExtra("EXTRA_NAME", extraValue);
startActivity(i);
Then You can obtain this extra by its name in your target activity.
extraValue = getIntent().getIntExtra("EXTRA_NAME", -1); // here -1 is default value.
Just put the button id in the intent. Like this;
Intent i=new Intent(context,MyActivity.class);
i.putExtra("button_id", buttonID);
context.startActivity(i);`
Then in your Activity;
Intent I =get intent();
Int buttonID = intent.getIntExtra("button_id");
A new activity is always started by an intent. If we want to start an activity with some information, we can put the same in that intent which is used to start it. This intent can be caught by getIntent() method in started activity.
Example:
In your first activity:
intent = new Intent(MainActivity.this,SecondActivity.class);
switch(radioGroup.getCheckedRadioButtonId()){
case R.id.add:
intent.putExtra("operator","add");
startActivity(intent);
break;
case R.id.subtract:
intent.putExtra("operator","subtract");
startActivity(intent);
break;
case R.id.multiply:
intent.putExtra("operator","multiply");
startActivity(intent);
break;
case R.id.divide:
intent.putExtra("operator","divide");
startActivity(intent);
break;
}
And in your second activity:
public class SecondActivity extends AppCompatActivity {
Intent intent;
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
intent = getIntent();
String operation = intent.getStringExtra("operator");
switch(operation){
case "add":
//your code
break;
case "subtract":
//your code
break;
case "multiply":
//your code
break;
case "divide":
//your code
break;
}
}
}
Related
What I am trying to achieve here is create a java class that will allow me to call the method inside the class for an activity transitioning.
For instance, I have 3 activities: Main.xml, A activity.xml, B activity.xml. I want to create a class that I can call from main's java class to transition to A and then from A to B.
I thought maybe it would be somewhere along that lines of:
[Transitioning activity class]
//Behind the scenes for activity transitioning, I don't know how to write this part or if calling it is correct
Intent intent = new Intent(?, ?); startActivity(intent); overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
[Main]
//calling intent from activity transitioning class
intent(this, A_activity.class);
[A Activity]
//calling intent from activity transitioning class
intent(this, B_activity.class);
Also I want to apologize, this is indeed my first rodeo, I am self-taught and have no professional experience in programming or developing. Thank you in advance!
So far what I am currently doing is creating the transitions in each class:
[A activity]
public void onClick(View v){
switch(v.getId()){
case R.id.commBtn:
inqList.clear();
inqAdd.addItem("Commercial");
System.out.println(inqList);
onTransition();
break;
case R.id.resBtn:
inqList.clear();
inqAdd.addItem("Residential");
System.out.println(inqList);
onTransition();
break;
default:
break;
}
}
//Method for transition after clicked
public void onTransition(){
Intent intent = new Intent(this, b_scene.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
**[Edit]**
///I did this and it works? not sure if its best practice but it worked.
[Transition Class]
public class Transition {
public static void onTrans(Activity activity, Class name) {
Intent intent = new Intent(activity, name);
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
}
[A Activity]
///Just put this in A activity
public void onClick(View v){
...
onTrans(this, B_Activity.class)
}
Sure you can but since that transition class is not an Activity instance (which has the startActivity() method), you need to pass the Activity instance as well, e.g.:
public static void navigateTo(Activity source, Activity destination) {
Intent intent = new Intent(activity, destination.class);
source.startActivity(intent);
source.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
Another common option is to use an abstract BaseActivity that has this method onTransition() and extend your activities from this one (you can also use composition instead of inheritance for a better practice).
An Activity X can be opened from the Button A in Activity A and from button B in Activity B, and it should change the label title in XML in Activity X accordingly.
I don't know how to create the following condition:
switch (i) {
case 0:
binding.lTitle.setText(getString(R.string.benefits_text));
break;
case 1:
binding.lTitle.setText(getString(R.string.coverages_text));
binding.lInfoText.setVisibility(View.GONE);
break;
}
Send the title as an intent extra (or an argument if you're using Navigation Component).
Intent intent = new Intent(this, ActivityX.class);
intent.putExtra("EXTRA_TITLE", strTitle);
startActivity(intent);
"strTitle" can be retrieved in ActivityX.
I have an Activity1 and inside this activity I have a button and when it is pressed, it should go to either ActivityA, ActivityB, or ActivityC based on a specific type passed to Activity1.
I currently have a switch statement that handles this, but the issues is when I want to add in an ActivityD, and ActivityE, I would have to change the code again and the switch statement will continue to grow.
What is a better way to do this?
private int type;
public void onCreate() {
// ...
type = getIntent().getIntExtra("type", 0);
}
#Override
public void onClick(View view) {
Intent intent;
switch (type) {
case 0:
intent = ActivityA.newIntent(this);
break;
case 1:
intent = ActivityB.newIntent(this, 1);
break;
case 2:
intent = ActivityC.newIntent(this, "str");
break;
default:
throw new RuntimeException();
}
startActivity(intent);
}
You can use multiple fragments in an activity and instead of changing the activity, just replace the fragments, however even with fragments you need to call them.
You also can create multiple views and change the view based on the intents, this method works better with static views.
Currently finishing up an app I have been working on but I'm kinda stuck on this last thing.
The app has 2 activities, one with buttons that are categories and the other shows the information according to the button pressed. For example if you click the button Fast food, it goes to the 2nd screen and displays info on that.
I'm trying to get a refresh button on the 2nd activity that will call a method in the 1st activity to refresh new information depending on the button pressed. The problem is that I don't know how to make it so the method keeps the same argument when called. What I mean is, if fast food was clicked, the refresh button would get new info that still relates to the fast food category.
Here's the relevant code in the Main activity:
public void yelpSearch(View view) {
switch (view.getId()) {
case R.id.button: buttoncategory = "Restaurant";
break;
case R.id.button2: buttoncategory = "Chinese restaurant";
break;
case R.id.button3: buttoncategory = "Fast Food";
break;
and this on the 2nd Activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
Refresh();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void Refresh() {
MainActivity mActivity = new MainActivity();
mActivity.yelpSearch();
}
I'm not sure what to put inside mActivity.yelpSearch();
I tried using (View view) but it'll say cannot resolve symbol view. And if I make a local variable for view, it'll say not initialized and I don't know what to set it as
Any help would be awesome, been googling on this for a while now and searched through tons of questions on here as well. I'm still new at this so bear with me
I think you want to pass data between activities?
First Activity(Send DATA):
String data="YourXXXdata"
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("DATANAME", data);
startActivity(intent)
second Activity(Receive DATA):
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("DATANAME");
}
The data now is in String value
*you have to put in data the value you need depend you preview select button.
Make your yelpSearch method static
and call it like this from 2nd activity MainActivity.yelpSearch();
Hi i am trying to make a public function in order to change activities/changes in my android application.
In a external class (Utl.java) I have this code.
public class Utl extends Activity{
public void onBtnClicked(View v) {
Intent i;
Integer data=0;
switch(v.getId()){
case R.id.btnStart:
i=new Intent(getApplicationContext(), LevelChoose.class);
startActivity(i);
break;
case R.id.btnEasy:
i=new Intent(getApplicationContext(), PlayGame.class);
startActivity(i);
break;
case R.id.btnMedium:
i=new Intent(getApplicationContext(), PlayGame.class);
data=48;
i.putExtra("extra", data);
startActivity(i);
break;
case R.id.btnHard:
i=new Intent(getApplicationContext(), PlayGame.class);
data=96;
i.putExtra("extra", data);
startActivity(i);
break;
}
}
}
and in my xml's files i use the on click function of the buttons...
<Button
android:id="#+id/btnStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:minHeight="110dp"
android:text="Start Game"
android:onClick="cls2.onBtnClicked(this)" />
I get that the function can't be found...
I wish to dynamically change screens.... Thank you for your time!!
You can use the constructor where you can assign the current context. Try this example
public void sampleMethod(final Activity cont)
{
Intent intent = new Intent(cont, XYZ.class);
cont.finish();
cont.startActivity(intent);
}
also move this method to a util class and call it from activity and pass the activity reference as follows
class ActivityB extends Activity
{
Util.sampleMethod(this);
}
You have multiple issues in your code. I'll try to list them here:
You cannot pass a method of any class to android:onClick.
You'll have to set the onClickListener for your Buttons in code, using
yourButton.setOnClickListener(new Utl());
Don't make Utl a subclass of Activity. Use Activity only for different "screens". You'll want to use Object instead here. If you need a Context in Utl, add a class member and take a Context in the constructor, like this:
private Context mContext;
public Utls (Context c){
mContext = c;
}
Now, instantiate Utls like this:
yourButton.setOnClickListener(new Utl(this));
Just change this line
android:onClick="cls2.onBtnClicked(this)"
to
android:onClick="onBtnClicked"
View will be passed to function automatically.
Happy coding..:)