Hide button when get intent from a specific class - Android - android

I want to hide a button when it get intent from a specific class.
Is it possible?
For instance, i have classes named A_activity.class, B_activity.class and C_activity.class . In C_activity.class , if it get intent from A_activity.class, the button is visible. But if it get from B_activity.class, the button is invisible.
This is my source code
Intent i=getIntent();
buttonTTS = (AppCompatButton) findViewById(R.id.button);
buttonTTS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
speakOut();
}
});
What should i do with my code?

You should do it like this. When you're creating the Intent from activity A you should add extra:
intent.putExtra("buttonVisible", true);
And from activity B
intent.putExtra("buttonVisible", true);
And in your activity C:
if (getIntent().getBooleanExtra("buttonVisible", false)){
buttonTTS.setVisibility(View.VISIBLE)
} else
buttonTTS.setVisibility(View.GONE);

From A_Activity putExtra("showButton", true) and from B_Activity putExtra("showButton", false)
Now in C_Activity, getExtras and hide and show button.
hope this will help you.

You can send a tag with the intent and then check the tag with the class value then you can make hide or show the button.
putExtra("tag","A_activity");
Now in class C you can check it by taking it in string and compare it by value.
String tag = getIntent().getStringExtra("tag");
if(tag.equals("A_activity")){
button.setVisibility(View.GONE);
}
else{
....
}

Related

How to put EditText info into a Intent variable

I'm trying to get the user information typed in the edit text. I want it saved into the Intent result variable. Trying to sending it back to the main activity afterwards. Keep getting the cannot resolve method. I'm thinking it must be that I'm missing a parameter in the putExtra() method
public class EnterDataActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enterdata);
Button doneButton = (Button) findViewById(R.id.button_done);
final EditText getData = (EditText) findViewById(R.id.enter_data_here);
doneButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent result = new Intent();
result.putExtra(getData);
setResult(RESULT_OK, result);
finish(); // Ends sub-activity
}//ends onClick
});
}//ends onCreate void button
}
Well if you are trying to send the EditText that's not possible.
If you are trying to send the text that are on that EditText that's possible.
How to do it?
Declare a String to save the data (You can avoid this step, but that's more clear)
String mText = getData.getText().toString();
Then you'll use the getExtra() method to send the String to the new Activity
Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_from_editText", mText);
startActivity(i);
Then the last step (You don't ask for it, but you'll need it), get the text.
//onCreate() of the second Activity
Intent i = getIntent();
String mText = i.getStringExtra("text_from_editText");
Replace result.putExtra(getData); with result.putExtra(getData.getText().toString()); to get the text from the EditText. Right now you're trying to put the entire EditText object into the intent as an extra instead of just the text from the EditText.

How can different activities pass data to the same class? [duplicate]

This question already has answers here:
Passing data between activities in Android
(3 answers)
Closed 7 years ago.
I have two buttons.
Button1 go to A then C
Button2 go to A then B and finally C.
There are values pass between these activities. The problem I faced now is how do I check whether they are data pass from B to C or it is from A to C only so I can set different condition to them.
Activity A
btnNext.setOnClickListener(new View.OnClickListener() { //if button1 is clicked
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),C.class); //pass spinner value and date to next class
Name = name.getSelectedItem().toString();
Weather = weather.getSelectedItem().toString();
Status=status.getSelectedItem().toString();
intent.putExtra("Name",Name);
intent.putExtra("Weather",Weather);
intent.putExtra("Status",Status);
intent.putExtra("date2",date2);
startActivity(intent);
}
});
btnForce.setOnClickListener(new View.OnClickListener() { // if button2 clicked
#Override
public void onClick(View v) {
Intent intent=new Intent(getApplicationContext(),B.class);
Name = name.getSelectedItem().toString();
Weather = weather.getSelectedItem().toString();
Status=status.getSelectedItem().toString();
intent.putExtra("Name",Name);
intent.putExtra("Weather",Weather);
intent.putExtra("Status",Status);
intent.putExtra("date2",date2);
startActivity(intent);
}
});
Activity B
goDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) { // if next button is clicked
Intent intent = new Intent(getApplicationContext(), C.class);
sub = SubContractors.getText().toString();
noP = NoPerson.getText().toString();
noH=NoHours.getText().toString();
intent.putExtra("sub",sub);
intent.putExtra("noP",noP);
intent.putExtra("noH",noH);
intent.putExtra("name",name);
intent.putExtra("weather",weather);
intent.putExtra("status",status);
intent.putExtra("date",date);
startActivity(intent);
}
});
}
Activity c
name = getIntent().getExtras().getString("Name"); // receive name from Information
weather = getIntent().getExtras().getString("Weather"); //receive weather
date = getIntent().getExtras().getString("date2"); //receive date
status = getIntent().getExtras().getString("Status"); // receive status
If has data passes from B, what should I write ?
You need to pass some value to know from where the call came from.
You can uses example ρяσѕρєя K gave you. Or better jet try
startActivityForResult(_new, REQUEST_CODE);
And then
getIntent().getAction()
Take a look at this post to see more examples:
Using startActivityForResult, how to get requestCode in child activity?

Activity That has Several Extras from Other Activities?

I have an Activity that uses the following code to retrieve information from another activity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
int tok = extras.getInt("Token");
tempToken += tok;
}
This is the Code inside the first other class that sends this information:
final Button mainMen = (Button) findViewById(R.id.toMainMenu);
mainMen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
Menu.class);
i.putExtra("Token", tok + teTok);
startActivity(i);
}
});
Now i have another Activity that also wants to sen information to the Main Activity like so:
maMenu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Campaign.this, Menu.class);
intent.putExtra("Token", player.tokens);
intent.putExtra("Round", player.round);
intent.putExtra("Rank", player.rank);
intent.putExtra("Score", player.score);
intent.putExtra("Sec", player.secondsTapped);
intent.putExtra("Min", player.minutesTapped);
intent.putExtra("Hour", player.hoursTapped);
intent.putExtra("Day", player.daysTapped);
intent.putExtra("LifeTap", player.tapsInLife);
intent.putExtra("SecTap", player.tapsPerSec);
intent.putExtra("TapRo", player.tapps);
startActivity(intent);
}
});
Now my question is, how do i handle these different extras from multiple Activities inside the one Main Activity?
Thank You for your time :)
There are two ways to solve your problem..
1)
You can pass one boolean value to or and int variable with some value.. And retrieve this in your new Activity and check with boolean value or int value and get correct data correspond to Activity.
2) You can save your all Data in Shared Preference. And get your all Data in any Activity.
you can send one boolean value that data is in first class or second class and in MainActivity check the value and get the correct data

Building a back button into my application and storing edittext fields

I have 2 views in my app. A form input and a screen to review the form data and confirm. The user inputs the data on the form submission screen in edittext fields. When the user clicks submit, they are presented with the data they input and a 'back' and 'confirm' button. I want it so that if the user presses back, they are returned to the form input screen and the data they input should still be in the editText fields.
At the moment, I am using an onClickListener to point to the form submission screen but the forms are all blank. How do I keep the data in them?
This is the onClickListener:
bBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent j = new Intent(FormConfirm.this, FormCreate.class);
startActivity(j);
}
});
i don't know how to write code on android apps, but i think, why you not to store them into variable flag and call them on edit text field?
for example, if you already fill the form and click submit, you can make a function for store data first on variable flag and show the data in form submission, so when you click back button, you can call the value that you store on variable flag into edittext.
i hope my answer can give you some inspiration
As stated you should save the values somehow, for example by using SharedPreferences.
As an example, when you go from the Input-form Activity to the Submit-form Activity:
bSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent j = new Intent(FormCreate.this, FormSubmit.class);
saveInput();
startActivity(j);
}
});
Where the method saveInput() could look something like:
private void saveInput() {
EditText input = (EditText) findViewById(R.id.someId);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
preferences.edit().putString("input",input.getText().toString()).commit();
}
And then, when you press back, the back action could simply be something like:
bBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
FormConfirm.this.finish();
}
});
That will cause the current activity to exit and your previous activity will be visible. If you want to display the last saved input when the Input-form Activity starts, you can simply do something like this:
private void loadInput(){
EditText input = (EditText) findViewById(R.id.someId);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String savedText = getString("input", "default input text");
input.setText(savedText);
}
and call that method in the onCreate-method in your Input-form Activity.
Had an inspiration and found a workaround that might not necessarily be correct but it works :) just changed the Java code for the activity to the following:
bBack.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});

Android Intent issue

I want to show the details of a calculation in another activity when click on a button.
How to achieve this.
my first activity java code is
public void onClick(View v)
{
if(v.getId()==R.id.Button07)
{
Intent intent=new Intent(AdvancedCalculator.this,calculatedData.class);
startActivity(intent);
}
The calculation i want to do is
String a,b;
Integer vis;
a = txtbox3.getText().toString();
b = textview1.getText().toString();
vis = (Integer.parseInt(a)*Integer.parseInt(b))/100;
tv.setText(vis.toString());
i want the result 'tv' to be shown in next activity when i press the submit button.
Where I need to include this calculation.and what are the further steps
Any help is greatly appreciated
Thanks
In your first activity:
public void onClick(View v) {
//Put your calculation code here
Bundle b = new Bundle();
b.putString("answer", youranswer);
//You could also use putInteger, whichever you prefer.
Intent intent=new Intent(AdvancedCalculator.this,calculatedData.class);
intent.putExtras(b);
startActivity(intent);
}
In your second activity, in the onCreate put this:
Bundle b = getIntent().getExtras();
String answer = b.getString("answer");
Answer is your key. it is used to identify what you want to get from the bundle. Using unique keys means you can pass more than one value to the next activity, if you want.

Categories

Resources