I'm creating my first android app. It seems fairly simple. But I'm a total noob with Java and Android (I'm more familiar with C, C++ and the like). I'm sorry if this is the dumbest question ever. Anyway, I followed the steps on the android dev website.
The app is supposed to have the person enter their Name and click on 1st, 2nd, or 3rd shift radio buttons and when they click on Downtime Button, they'll be brought to another page (activity) that displays their name and the shift they picked and then displays another textbox and a time input.
So far, I got the MainActivity.java done like this:
package com.cyapps.downtime;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.cyapps.downtime.MESSAGE";
public void clickedButton1(View view) {
Intent intent = new Intent(this, WinderDTActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void clickedButton2(View view) {
Intent intent = new Intent(this, ClamperDTActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
public void clickedButton3(View view) {
Intent intent = new Intent(this, OtherDTActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and the activity_main.xml like this:
<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"
android:orientation="horizontal" >
<EditText
android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="@string/edit_message" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_message"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="@string/radio_button1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioButton1"
android:layout_centerHorizontal="true"
android:text="@string/radio_button2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioButton2"
android:layout_centerHorizontal="true"
android:text="@string/radio_button3" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true"
android:text="@string/button_send1"
android:onClick="clickedButton1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_centerHorizontal="true"
android:text="@string/button_send2"
android:onClick="clickedButton2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:text="@string/button_send3"
android:onClick="clickedButton3" />
</RelativeLayout>
I now have another activity that shows up when you finish entering in your name and clicking on a shift. This page is supposed to show your name and the shift number and have a textbox to write some other stuff in it and a time input and a submit button. I know how to do buttons and I see the time input on the interface of Eclipse. But I don't understand how to make the radio buttons be able to be "submitted" and shown on the page and how to edit the activity to show certain stuff. I'm confused.
This is how the WinderDTActivity.java looks like:
package com.cyapps.downtime;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class WinderDTActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
}
And this is what the activity_winder_dt.xml looks like:
<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" >
</RelativeLayout>
Thank you so much in advance if you help me. You have no idea how grateful I'll be. I've been trying really hard to understand this, but I'm thoroughly confused on how to get xml and java working together. Please help!
You're already sending the contents of the text box to the second activity just fine, right? Then you only need to do two more things to send the radio button state through as well:
Implement onClick handlers for the radio buttons. You've already done that for the submit buttons, so I assume you've figured out onClick stuff. In the radio button onClick, set a variable in MainActivity.
Package the variable the radio flags set into the intent's extra data, like so:
intent.putExtra("radioButtonState", radioButtonState);
You can read the result back in your second activity using the appropriate intent.getxxxxExtra()
function (getIntExtra
if you saved an int, for example).