Reputation: 79
I'm trying to create an application that configures the planetary weight of someone on each planet using weight * planetary force. One the first screen they enter their weight, and then on the next they select which planet they want to see their weight from. I have it down mostly, but when it comes to figuring out how to configure each form in Java, I get lost...
My first xml, activity_main.xml, is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/askwtTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="17dp"
android:layout_marginTop="19dp"
android:text="@string/askwt" />
<EditText
android:id="@+id/inputwtEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/askwtTextView"
android:layout_below="@+id/askwtTextView"
android:layout_marginTop="26dp"
android:ems="10"
android:inputType="numberDecimal" />
<Button
android:id="@+id/enterButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/inputwtEditText"
android:layout_below="@+id/inputwtEditText"
android:layout_marginTop="38dp"
android:text="@string/enter" />
</RelativeLayout>
The planets.xml is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/planetTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/planet" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/mercuryRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/mercury" />
<RadioButton
android:id="@+id/venusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/venus" />
<RadioButton
android:id="@+id/earthRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/earth" />
<RadioButton
android:id="@+id/marsRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/mars" />
<RadioButton
android:id="@+id/jupiterRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/jupiter" />
<RadioButton
android:id="@+id/saturnRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/saturn" />
<RadioButton
android:id="@+id/uranusRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/uranus" />
<RadioButton
android:id="@+id/neptuneRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/neptune" />
<RadioButton
android:id="@+id/plutoRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pluto" />
</RadioGroup>
<Button
android:id="@+id/selectButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/select" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
And JAVA, the calcuation *2 is a place holder for another equation I might need... I think:
package com.deitel.planetaryweight;
import android.os.Bundle;
import android.app.Activity;
import android.widget.*;
import android.view.View;
import android.view.Menu;
public class MainActivity extends Activity {
//Global variable
double calculation = 0.0;
private Button select; // creates a button
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
select = (Button) findViewById(R.id.selectButton);
//Start with first screen
setContentView(R.layout.activity_main);
}
//buttonclick for form 1
public void buttonclick(View view){
TextView t = (TextView) findViewById(R.id.textView1);
//take the value of the text in the textview and multiply by 2
calculation = Double.parseDouble(t.getText().toString()) * 2;
//switch views to screen 2
setContentView(R.layout.main2);
//change the value of the textview on screen 2 to the calculation value
TextView t2 = (TextView)findViewById(R.id.textViewform2);
t2.setText(Double.toString(calculation));
}
//buttonclick for form 2!
public void buttonclick2(View view){
setContentView(R.layout.planets);
RadioButton mercury = (RadioButton) findViewById(R.id.mercuryRadio);
RadioButton venus = (RadioButton) findViewById(R.id.venusRadio);
RadioButton earth = (RadioButton) findViewById(R.id.earthRadio);
RadioButton mars = (RadioButton) findViewById(R.id.marsRadio);
RadioButton jupiter = (RadioButton) findViewById(R.id.jupiterRadio);
RadioButton saturn = (RadioButton) findViewById(R.id.saturnRadio);
RadioButton uranus = (RadioButton) findViewById(R.id.uranusRadio);
RadioButton neptune = (RadioButton) findViewById(R.id.neptuneRadio);
RadioButton pluto = (RadioButton) findViewById(R.id.plutoRadio);
//Makes a variable for the entered amount
Double mercurypf;
Double venuspf;
Double earthpf;
Double marspf;
Double jupiterpf;
Double saturnpf;
Double uranuspf;
Double neptunepf;
Double plutopf;
Double weight;
// constants
final double mercuryforce = 0.38;
final double venusforce = 0.91;
final double earthforce = 1.00;
final double marsforce = 0.38;
final double jupiterforce = 2.34;
final double saturnforce = 1.06;
final double uranusforce = .92;
final double neptuneforce = 1.19;
final double plutoforce = 0.06;
//Receives the input from the inputwt edittext, converts it to a double (number).
weight = Double.parseDouble(inputwt.getText().toString());
}
}
I do not want the answer and a step by step of how to complete the task, I just would like some guidance on where to go from here.
Upvotes: 0
Views: 59
Reputation: 5824
Based on you question title, you will want to have at least two activities. The first for the user to enter their weight, the second to select the planet. If you are showing their planetary weight on second "view" then you don't need a third.
For this you will want to create your first view similar to the manner you have already implemented, except you will set up the OnClickListener
for the enterButton
. Once the weight value is entered, and the user taps the enterButton
, your on OnClickListener
should create an Intent
, pass the weight value as an extra (putExtra
) and start the 2nd Activity
.
You do not need/want any of the planet.xml layout in the 1st Activity
.
Easiest way to finish up the 1st Activity
is the have it implement OnClickListener
, e.g.:
public class MainActivity extends Activity implements OnClickListener{
and implement the onClick
method to hand the enterButton
click.
From onClick
, start the 2nd activity (either in a separate method or directly in the onClick
method. Something like:
Intent selectPlanetIntent = new Intent(this, PlanetActivity.class);
selectPlanetIntent.putExtra("UserWeight", userWeight);
startActivity(selectPlanetIntent);
In your 2nd activity, setContentView
to your planet layout. Get the user's weight using getFloatExtra
. Something like:
Intent startingIntent = getIntent();
float userWeight = startingIntent.getFloatExtra("UserWeight", 0f);
Upvotes: 1