Sending double from one activity to another Android Studio - java

Hi,
I am trying to send data from one activity to another activity using this method: how to retrieve a Double value from one activity to another?
Yet every time I open up my application it crashes as its because my code is in the onCreate:
double lat, longi;
Intent getLocation;
Bundle extras;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocation = this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");
but when I put it in a button instead It can't resolve this.getIntent();
public void getCoordinates(View view){
Button coordinates = (Button) findViewById(R.id.getCoordinates);
coordinates.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
getLocation = this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");
}
});
}
I would like to receive data either automatically or using a button.
I am quite new in mobile computing so please don't roast me.

this meant isView.OnClickListener() in your code .
And we need YourActivity.this as Context
You should use
public void getCoordinates(View view){
Button coordinates = (Button) findViewById(R.id.getCoordinates);
coordinates.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
getLocation = YourActivity.this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");
}
});
}

Related

OnClick open Activity1 but send EXTRA_TEXT to Activity10...Is a String necessary or can I simply use intent? Can post XML if helpful

Multiple Activity App...Activity1 (SiteData) collects data and sends to Activity10 (DataReview)...but before going straight to Activity10 user must go through Activities2-9 (SanDiegoArea, etc...) collecting data and also passing EXTRA_TEXT to the other activity until Activity10...
This way:
(Activity1 -> Activity2 -> ... -> Activity10)
Activity10 will be able to review Activity1-9 EXTRA_TEXT for verification...Code is as follows but I'm getting no EXTRA_TEXT at Activity10 to display in it's TextViews for review? How can I solve the problem?
Activity1 - SITEDATA - data info input
public class SiteData extends AppCompatActivity {
public static final String EXTRA_TEXT = "MultipleActivities.EXTRA_TEXT";
public static final String EXTRA_NUMBER = "MultipleActivities.EXTRA_NUMBER";
public void clickFunction(View view) {
Intent intent = new Intent(getApplicationContext(), SanDiegoArea.class);
startActivity(intent);
Log.i("Info", "Login Details Entered");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sitedata);
//Possible coding needed for Autocomplete...see more
Button button = (Button) findViewById(R.id.buttonSend);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openSanDiegoArea();
}
});
}
public void openSanDiegoArea() {
EditText editText1 = (EditText) findViewById(R.id.editTextName);
String text = editText1.getText().toString();
EditText editText2 = (EditText) findViewById(R.id.editTextPhone);
int number = Integer.parseInt(editText2.getText().toString());
Intent intent = new Intent(this, DataReview.class);
intent.putExtra(EXTRA_TEXT, text);
intent.putExtra(EXTRA_NUMBER, number);
startActivity(intent);
}
}
ACTIVITY10 - DATAREVIEW Section getting info
public class DataReview extends AppCompatActivity {
public void clickFunction(View view) {
Intent intent = new Intent(getApplicationContext(), ThankYou.class);
startActivity(intent);
Log.i("Info", "Sample Data Sent to BWTF");
}
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datareview);
Intent intent = getIntent();
String text = intent.getStringExtra(SiteData.EXTRA_TEXT);
int number = intent.getIntExtra(SiteData.EXTRA_NUMBER, 0);
TextView textView1 = (TextView) findViewById(R.id.editTextName);
TextView textView2 = (TextView) findViewById(R.id.editTextPhone);
TextView textView3 = (TextView) findViewById(R.id.editTextEmail);
TextView textView4 = (TextView) findViewById(R.id.editTextDate);
TextView textView5 = (TextView) findViewById(R.id.editTextTime);
textView1.setText(text);
textView2.setText("" + number);
}
};
If Activity1 calls Activity2, it will have to pass data to it the way you are doing. ( I'm guessing here that your Activity2 is DataReview.class)
But Activity2 will call Activity3 and, in order for it not to loose the data received from activity 2 you will have to send it too. In other words, Activity1 sends Activity1's data to Activity2, Activity2 sends Activity1's and Activity2's data to Activity3 and so on.
In this way, Activity9 will receive (1, 2, 3, 4, 5, 6, 7, 8) data from it's caller Activity8 and will have to pass all this accumulated data to Activity10.
Considering that Activity3 is called by Activity2 it can't see the data sent from Activity1 because it don't know it unless Activity2 pass it too. Am I been clear?
If you are just learning Android it is ok to do it this way in order to learn but it is not the better way to solve this problem. I would use a same ViewModel on all activities in order to represent this data in order to always have just one place for holding it as it would make my code more clear. In reality I wouldn't even use all those Activities, I would use only one activity and work with 10 fragments, one for each page (it is just to provide navigation between then). So those are Ideas for you to improve your app if you feel It would be good.

Saving Activity State when switching activities

I'm currently trying to create an activity, which should be creating a new TextView on my main activity, everytime a Button is clicked on the first activity. However, instead of creating a new TextView everytime the button is clicked, it just changes the values of the first created TextView, so that there is always only one TextView. Is there a way to make it so that my first activity will not only create one single textview?
Here's the code from my "NewSubjectActivity":
**public class NewSubjectActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_subject);
Button SaveBtn = findViewById(R.id.SaveBtn);
nsaSaveBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Click();
}
});
}
protected void Click(){
Intent intent = new Intent(NewSubjectActivity.this, MainActivity.class);
Boolean createnewTextView = true;
intent.putExtra("createnewTextView",createnewTextView);
startActivity(intent);
}
}
And here's the (relevant) code from my MainActivity:
protected void ReceiveValue (){
//getting Extras
Intent nsareceivedvalues = getIntent();
boolean createTextView = false;
createTextView = nsareceivedvalues.getExtras().getBoolean("createnewTextView");
//declaring fixed Views
final LinearLayout mainLinearLayout = (LinearLayout)findViewById(R.id.mainLinearLayout);
//Params for TextView
RelativeLayout.LayoutParams Params = new RelativeLayout.LayoutParams(1000, 200);
Params.setMargins(0, 10, 0, 10);
while(createTextView) {
//creating a TextView
TextView newsubject = new TextView(MainActivity.this);
//applying values to the TextView
newsubject.setLayoutParams(Params);
newsubject.setGravity(CENTER);
newsubject.setBackgroundColor(Color.GRAY);
mainLinearLayout.addView(newsubject);
createTextView = false;
}
}
As I said, this only create one text view, everytime I press the button on my "NewSubjectActivity" I think this might be, because the previous text view is not saved and the MainActivity is reset everytime I switch between the activities.
Every help and advise is much appreciated <3
Maybe when you are going back to your main activity you should call to onBackPressed() function, so it will reset nothing at all. Here's the example in kotlin
val back_button:Button = findViewById(R.id.button2)
back_button.setOnClickListener { v -> run {
//Change activity
onBackPressed()
} }
in Java should be:
Button back_button = findViewById(R.id.button2);
back_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Change activity
onBackPressed()
}
});
Let's hope it works!

How can i use If method on Button getting ActionView link from realtime database, and if there's no link give toast message there's no link

I'm using Firebase RealTime database, and using retrieving data, I'm asking about if I want to add If method on OnClick() for the button and get the ActionView link from real time database, and if there's no link in the database give a toast message that "There's no link available ATM"
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.res_screen);
String data0= getIntent().getStringExtra("Name");
String data1= getIntent().getStringExtra("Address");
String data2= getIntent().getStringExtra("Phone1");
String data3= getIntent().getStringExtra("Phone2");
String data4= getIntent().getStringExtra("Phone3");
String data5= getIntent().getStringExtra("Offer");
final String data6= getIntent().getStringExtra("Facebook");
final String data7= getIntent().getStringExtra("Menu");
final String data8 = getIntent().getStringExtra("Menu");
TextView nm = findViewById(R.id.name);
TextView ad = findViewById(R.id.address);
TextView ph1 = findViewById(R.id.phone1);
TextView ph2 = findViewById(R.id.phone2);
TextView ph3 = findViewById(R.id.phone3);
TextView off = findViewById(R.id.closeoffer);
ImageView fb = findViewById(R.id.facebookbut);
Button menu = findViewById(R.id.menubut);
nm.setText(data0);
ad.setText(data1);
ph1.setText(data2);
ph2.setText(data3);
ph3.setText(data4);
off.setText(data5);
fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data6));
startActivity(intent);
}
});
menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(getIntent().getStringExtra("Menu")));
startActivity(i);
}
});
}
Maybe am a little confuse because my bad language.
I want to add if else into OnClick().
Mean.. if this button have link to visit ACTION_VIEW, then go. But if haven't link from realtime database, then give a Toast message "Sorry there's no link available at this moment"
Maybe you need to do this :
fb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//data6 may be having link to fb ,here we check if it is not null
if(data6!=null )
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(data6));
startActivity(intent);
}else{
//toast a message
Toast.makeText(Yourclass.this, "Sorry there's no link available at this moment", Toast.LENGTH_SHORT).show();
}
});

Android Studio defaultValue/ passing Variables [duplicate]

This question already has answers here:
How do I get extra data from intent on Android?
(16 answers)
Closed 4 years ago.
I'm new to Android Studio and java, so hopefully you can help me.
I want to pass a double variable from on activity to the next.
But I'm unsure what needs so go in the defaultValue in the receiving activity.
Here is the code from activity one:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button weiter = (Button)findViewById(R.id.weiter);
weiter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
EditText EingabeBreite = (EditText)findViewById(R.id.breite);
double breite = Double.parseDouble(EingabeBreite.getText().toString());
Intent rüber = new Intent(getApplicationContext(), Main2Activity.class);
getIntent().putExtra("next", breite);
startActivity(rüber);
Here is the code from the second activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView ergebnis = (TextView)findViewById(R.id.textView2);
Bundle extras = getIntent().getExtras();
double breite = extras.getDouble("next");
ergebnis.setText(Double.toString(breite));
add the code to second activity:
double breite=getIntent().getDoubleExtra("next",0d);
So easy
Intent weiter = new Intent(MainActivity.this,Main2Activity.class);
startActivity(weiter);
EditText EingabeBreite = (EditText)findViewById(R.id.breite);
double breite = Double.parseDouble(EingabeBreite.getText().toString());
Intent rüber = new Intent(getApplicationContext(),MainActivity.class);
getIntent().putExtra("next","breite");
startActivity(rüber);
First off- you're starting 2 intents. That's not what you want. Only one activity can be in the forefront, you want to do one of these. Not both.
Secondly- you don't want getIntent().putExtra(). You want ruber.putExtra(). You need to put the extra on the intent you send to the other activity. Calling getIntent will get the intent that started the current Activity, which isn't what you're sending to the next one.
use this code :
Intent mIntent = new Intent(HomeActivity.this, CenterActivity.class);
mIntent.putExtra("thevalue ", 0.0d);
startActivity(mIntent);
Intent intent = getIntent();
double d = Double.parseDouble(intent.getExtras().getString("thevalue "));

java physics app in android ( maths )

Basically, I'm new to java and I have to make an android application that can do simple physics equations such as I=Q*t.... etc etc, basically I cannot for the life of me get the result to output, has anyone got any idea's why this wont work on the emulator?
I have tried putting intents and all sorts in there. Help please
public class Current extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);
// Show the Up button in the action bar.
setupActionBar();
#SuppressWarnings("unused")
Intent intent = getIntent();
}
public void Main(String[]args){
Button calc1 = (Button)findViewById(string.Calculate_Current);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// IIIIII HATE JAVA
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double Time = Double.parseDouble(Time1.getText().toString());
#SuppressWarnings("unused")
Intent intent = new Intent();
Distances_answer.setText("" + charge + Time);
}
});
I feel like you are taking input of charge and time from two edittexts and then on click of a button you are updating a textview to place that product of charge and time there.Try this out
public class Current extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_name);
// Show the Up button in the action bar.
setupActionBar();
Button calc1 = (Button)findViewById(R.id.buttons_id_in_layout);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
}

Categories

Resources