Reputation: 3505
I have a very simple question about menu control in Android. I'm writing a program that performs some simple numeric calculations and outputs an answer. My question is how do make the program move to second screen when a button is pressed, and how can I let the user move back to the original screen. Here's a quick example
Screen 1
"Add Numbers"
Input 1st # ____
Input 2nd # ____
(Add)
Screen 2
The answer is "____"
The user inputs two integers. presses add, and then the program moves to the second screen which displays the answer. If they user wants they can return to the first screen and start over.
I know this has been covered but with so many resources I don't know what to look for. Answers or links to resources would be most helpful. Thank you!
Upvotes: 1
Views: 376
Reputation: 26535
You can use the following layout for the screen to input numbers. Name the file first.xml.
<TextView
android:id="@+id/firstnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Input Ist:"
android:textSize="20px"
android:textStyle="bold"
/>
<EditText
android:id="@+id/first"
android:layout_height="wrap_content"
android:layout_width="250px"
/>
<TextView
android:id="@+id/secondnumber"
android:text = "Input 2nd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px"
android:textStyle="bold"
/>
<EditText
android:id="@+id/second"
android:layout_height="wrap_content"
android:layout_width="250px"
/>
<Button
android:id="@+id/add_button"
android:text="Add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px"
android:layout_centerHorizontal="true"
/>
</LinearLayout>
To add these numbers
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button add = (Button) findViewById(R.id.add);
add.setOnClickListener(this);
EditText num1 = (EditText)findViewById(R.id.first);
EditText num2 = (EditText)findViewById(R.id.second);
}
The click listener code
public void onClick(View v) {
int n1 = Integer.parseInt(num1.getText().toString());
int n2 = Integer.parseInt(num2.getText().toString());
int result = n1 + n2;
String res = Integer.toString(result);
Intent intent = new Intent(getApplicationContext(), Result.class);
intent.putExtra("result", result); //Passing the result to the second activity using intent
startActivity(intent);
}
In the Result.java class.
public class Result extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
Intent intent = getIntent();
String result = intent.getStringExtra("result");
TextView t1 = (TextView)findViewById(R.id.result);
t1.setText(result);
}
}
Hope this helps..
Upvotes: 3
Reputation: 95
ViewFlipper will work well. You could also try "startActivityForResult" to pass your answers to the next screen.
Upvotes: 1
Reputation: 13739
Take a look at the ViewFlipper class. It works like tabs, but without the tabs UI. You can use an xml layout to display your 2 (or more) screens, and showNext() or showPrevious() to flip between the screens.
Upvotes: 0
Reputation: 661
May I know what type of language are u using? I recently just did it with C#. My flow is that, when I start a program, it will auto be forced to the second screen, with some power options.
But if yours is just moving the program the diff screens, it will be easy in C#.
Screen Srn = Screen.PrimaryScreen; // gives your information of Primary Screen.
Screen[] Srn = Screen.AllScreens; //gives u an array of all your available screen( srn[0] is primary screen, etc)
So, using the intellisense from the IDE, should be able to get the width, height, etc. Cant remember exactly, but something like src.width or src.bounds.width.
Easiest way to move your program will be to move the program x axis to the respective screen.
Upvotes: 1