I am trying to have the user fill out a form then those fields auto-populate into an email. I was able to convert the EditText to strings as well as the spinner, but I am confused as to how to do it with RadioGroup. I want the text from the button which is selected to appear in the email. I know that getText won't work, but what will. Thank you
JAVA
//This happens when you press the submit button at the bottom of the form.
public void submit(View button) {
// Do click handling here
//What happens here is the input from each field is taken in and converted into
//Strings and placed into their correct variables.
final EditText FirstNameField = (EditText) findViewById(R.id.EditTextFirstName);
fName = FirstNameField.getText().toString();
final EditText LastNameField = (EditText) findViewById(R.id.EditTextLastName);
lName = LastNameField.getText().toString();
final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
email = emailField.getText().toString();
final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
feedback = feedbackField.getText().toString();
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
feedbackType = feedbackSpinner.getSelectedItem().toString();
final RadioGroup ApproveorReject = (RadioGroup) findViewById(R.id.radiochoice);
fAnswer = ApproveorReject.getText().toString();
//calls the sendEmail() from below to pull up a list of apps installed
//on the phone that can handle emailing.
sendEmail();
}
//This bit of code pulls up a list of apps that can handle emailing.
private void sendEmail() {
//When the user chooses an app of the list that pops up, these lines below
//will auto-populate the fields of the email with the input from above.
//The user can take one last look at the email before hitting that send button
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, feedbackType);
i.putExtra(Intent.EXTRA_TEXT, lName + ", " + fName + "\n" + email + "\n" + feedback + fAnswer );
startActivity(Intent.createChooser(i, "Send mail..."));
}
}
XML
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/feedbacktitle"
android:textSize="10pt">
</TextView>
<!--First Name-->
<EditText
android:id="#+id/EditTextFirstName"
android:layout_height="wrap_content"
android:hint="#string/feedbackfirst"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:layout_below="#+id/TextViewTitle">
</EditText>
<!--Last Name-->
<EditText
android:id="#+id/EditTextLastName"
android:layout_height="wrap_content"
android:hint="#string/feedbacklast"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:layout_below="#id/EditTextFirstName">
</EditText>
<!-- Email -->
<EditText
android:id="#+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="#string/feedbackemail"
android:inputType="textEmailAddress"
android:layout_width="fill_parent"
android:layout_below="#id/EditTextLastName">
</EditText>
<Spinner
android:id="#+id/SpinnerFeedbackType"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:entries="#array/Types_of_Errors"
android:layout_below="#+id/EditTextEmail">
</Spinner>
<EditText
android:id="#+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="#string/feedbackbody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent"
android:layout_below="#+id/SpinnerFeedbackType">
</EditText>
<RadioGroup
android:id="#+id/radiochoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/EditTextFeedbackBody"
android:orientation="horizontal">
<RadioButton
android:id="#+id/AcceptRadio"
android:layout_width="wrap_content"
android:layout_marginStart="50dp"
android:layout_height="wrap_content"
android:text="#string/acceptbutton" />
<RadioButton
android:id="#+id/RejectRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rejectbutton"
android:layout_marginStart="115dp" />
</RadioGroup>
<EditText
android:id="#+id/RejectResponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/rejectreason"
android:layout_marginTop="410dp"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:inputType="text"/>
<Button
android:id="#+id/ButtonSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sendform"
android:layout_centerHorizontal="true"
android:layout_marginTop="590dp"
android:onClick="submit"/>
</RelativeLayout>
Try this
RadioButton btn=(RadioButton)findViewById(ApproveorReject.getCheckedRadioButtonId());
String selectedText=btn.getText().toString();
Try
ApproveorReject.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb=(RadioButton)findViewById(checkedId);
String param = rb.getText();
}
});
Related
I have seen/tried the following questions, but mine is not the same:
Android keyboard next button issue on EditText
Move to another EditText when Soft Keyboard Next is clicked on Android
First, I have a CardView wherein I have a EditText, like below:
<RelativeLayout
android:id="#+id/topCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#26c2ef">
</RelativeLayout>
<RelativeLayout
android:id="#+id/cardTopTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp">
<TextView
android:id="#+id/lessonTitleHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Please provide a name:"
android:textColor="#000000"
android:textSize="18sp" />
<ImageView
android:id="#+id/horiLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/lessonTitleHeading"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:background="#60000000" />
<EditText
android:id="#+id/noteEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/horiLine"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:hint="Enter name here..."
android:imeOptions="actionNext" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
I then inflate this layout in another by using include like this:
<include
android:id="#+id/lessonTitle"
layout="#layout/activity_notes_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
and in my Activity class I do the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_leason);
View tvOne = findViewById(R.id.tvOne);
View tvTwo = findViewById(R.id.tvTwo);
View tvThree = findViewById(R.id.tvThree);
View tvFour = findViewById(R.id.tvFour);
final TextView tvOne = (TextView)vLessonTitle.findViewById(R.id.lessonTitleHeading);
final EditText etOne = (EditText) vLessonTitle.findViewById(R.id.noteEditText);
TextView tvTwo = (TextView)vlessonProbStu.findViewById(R.id.lessonTitleHeading);
final EditText etTwo = (EditText) vlessonProbStu.findViewById(R.id.noteEditText);
TextView tvThree = (TextView)vLessonWorkedOn.findViewById(R.id.lessonTitleHeading);
final EditText etThree = (EditText) vLessonWorkedOn.findViewById(R.id.noteEditText);
TextView tvFour = (TextView)vlessonWhatStudShouldWorkOn.findViewById(R.id.lessonTitleHeading);
final EditText etFour = (EditText)vlessonWhatStudShouldWorkOn.findViewById(R.id.noteEditText);
tvOne.setText("This is the first CardView:");
etOne.setHint("Enter Title Here...");
tvTwo.setText("This is the second CardView:");
etTwo.setHint("Enter Text Here...");
tvThree.setText("This is the third CardView:");
etThree.setHint("Enter Text Here...");
tvFour.setText("This is the fourth CardView:");
etFour.setHint("Enter Text Here...");
//I tried this, but it didn't work...
etOne.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
etLessonTitle.clearFocus();
etLessonProbStu.requestFocus();
return true;
}
return false;
}
});
}
The Layout now looks like this:
At this point I have a bunch of CardViews with EditTexts underneath each other like above. I would like to use the next button in the keyboard to go to the next CardsViews EditText.
If you have a look at my Activity class above, I tried using setOnKeyListener and clearFocus from the current EditText then requestFocus on the next EditText, but that didn't work.
Any ideas how I can achieve this?
Try this you need to set android:inputType="" to your Editext with android:imeOptions="actionNext"
<EditText
android:id="#+id/noteEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/horiLine"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:hint="Enter name here..."
android:inputType="text" // specifies your input type here
android:imeOptions="actionNext" />
I'm developing a simple app, the user will have to fill in their name, email, rate the app, and describe their experience(optional). If their is an empty field(name and email), an error message will pop up. I'm having trouble with Toast.LENGTH_LONG() at the end of the code. It keep saying 'method call expected'.
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
//Create an array adapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.rate_array, android.R.layout.simple_spinner_dropdown_item);
//Specify the layout for the drop down menu
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Apply the adapter to the spinner
spinner.setAdapter(adapter);
final EditText name = (EditText) findViewById(R.id.Name);
final EditText e_mail = (EditText) findViewById(R.id.email);
final Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(name.getText().length() == 0){
name.setError("Please fill in your name");
}
else{
if(e_mail.getText().length() == 0){
e_mail.setError("Please fill in your email");
}
else{
Toast.makeText(this, "Validation successful",
Toast.LENGTH_LONG()).show();
}
}
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.infinite_space.myapplication.MainActivity"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/textView"
android:layout_width="292dp"
android:layout_height="37dp"
android:text="Enter Feedback details to b sent to the developers"
android:visibility="visible"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp"
android:layout_weight="0.18" />
<EditText
android:id="#+id/Name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Your Name"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="67dp" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown"
tools:layout_editor_absoluteY="176dp"
tools:layout_editor_absoluteX="16dp"
android:prompt="#string/rate_level"
android:entries="#array/rate_array" />
<EditText
android:id="#+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Your email"
android:inputType="textEmailAddress"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="122dp" />
<EditText
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="103dp"
android:ems="10"
android:hint="Details..."
android:inputType="text"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="227dp" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Would you like an email respond ?"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="343dp" />
<Button
android:id="#+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send Feedback"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="385dp" />
</LinearLayout>
Toast.LENGTH_LONG is a (constant) field and not a method. Change LENGTH_LONG() to LENGTH_LONG.
Try this :
Toast.makeText(this, "Validation successful",Toast.LENGTH_LONG).show();
Should be either LENGTH_LONG for a long period of time or LENGTH_SHORT for a short period of time. Get rid of the () after LENGTH_LONG.
my problem is that I want a Radio Group that has 3 Radio Buttons, using the scheme below.
The three choices are:
1. [] Male
2. [] Female
3. [] Custom: (self-described identity)
However, the problem is that I want the user to type in their self-described identity into an EditText for me to retrieve.
So the following code is from my XML page, with some elements blocked out by "####".
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="####"
android:id="#+id/male_female_custom_choice"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_male"
android:checked="true" />
<RadioButton android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_female"
android:checked="false" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="####"
android:weightSum="1">
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/radio_button_custom"
android:checked="false" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="####"
android:hint="####"
android:focusableInTouchMode="true"
android:gravity="center"
android:layout_weight="1.05"
android:textSize="14sp" />
<TextView
android:layout_width="42dp"
android:layout_height="43dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="####"
android:id="####"
android:singleLine="true"
android:gravity="center"
android:layout_marginLeft="0dp"
android:textColor="#000000" />
</LinearLayout>
</RadioGroup>
As you can see, I have tried to use a LinearLayout to isolate the custom option.
However, there are unintended and undesired side effects.
1. The custom option can be selected in addition to the other 2 predefined genders.
2. The custom option cannot be selected on its own.
In the actual Java file for the activity, I have the following code:
// button, radio button, editText, and Spinner fields
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
// get selected radio button from RadioGroup
int selectedId = rSexGroup.getCheckedRadioButtonId();
// find radio button by returned id
rButton = (RadioButton)findViewById(selectedId);
// assign gender based on id of radio button
if (selectedId == 1) {
pat.gender = "male";
}
if (selectedId == 2) {
pat.gender = "female";
}
if (selectedId == 3) {
mEdit = (EditText)findViewById(R.id.####);
pat.gender = (mEdit.getText().toString());
}
Since I am a bit rusty with Java, it may be possible that I have some really newbish errors. Please advise.
Once again, I am looking for a way to get a set of 3 RadioButtons, each on an individual line, with the last RadioButton with an EditText adjacent to it from which I obtain the desired information.
EDIT: Here's a picture of what I want it to look like:
(http://i68.tinypic.com/ao2oow.png)
Unfortunately I need 10 reputation to post images. :(
Mohit's answer gives the EditText on a different line than the custom input.
(http://i63.tinypic.com/68u88x.png)
Please note that the orientation of the EditText is adjacent to the custom, and not below. I apologize for not clearly specifying enough what I wanted.
Because selectedId will not be 1,2 or 3....debug it you will get value..
The custom option cannot be selected on its own.
Remove your 3rd RadioButton from LinearLayout and replace below 2nd RadioButton and put your EditText and TextView inside LinearLayout..
On you listener get getCheckedRadioButtonId and getText() of that RadioButton and check it accordingly...
I dont no what is your task but here is how can get all three RadioButton working and get custom text too....
xml...
UPDATE
<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"
tools:context="com.ex.MainActivity" >
<RadioGroup
android:id="#+id/male_female_custom_choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radio_button_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Male" />
<RadioButton
android:id="#+id/radio_button_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="FeMale" />
<RadioButton
android:id="#+id/radio_button_custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="Custom" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/male_female_custom_choice"
android:layout_toRightOf="#+id/male_female_custom_choice"
android:orientation="horizontal"
android:weightSum="1" >
<EditText
android:id="#+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8"
android:ems="10"
android:focusableInTouchMode="true"
android:gravity="center"
android:hint="aa"
android:inputType="text"
android:textSize="14sp" />
<TextView
android:id="#+id/text"
android:layout_width="42dp"
android:layout_height="43dp"
android:layout_marginLeft="0dp"
android:layout_weight=".2"
android:gravity="center"
android:singleLine="true"
android:text="aaa"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
</LinearLayout>
<Button
android:id="#+id/but"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/male_female_custom_choice"
android:text="Get" />
</RelativeLayout>
.java file..
public class MainActivity extends Activity {
public EditText mEdit;
public RadioButton rButton;
public RadioGroup rSexGroup;
public Button but;
public String str = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rSexGroup = (RadioGroup)findViewById(R.id.male_female_custom_choice);
but= (Button) findViewById(R.id.but);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId = rSexGroup.getCheckedRadioButtonId();
rButton = (RadioButton)findViewById(selectedId);
if (rButton.getText().toString().equals("Male")) {
str = "Male";
}
if (rButton.getText().toString().equals("FeMale")) {
str = "FeMale";
}
if (rButton.getText().toString().equals("Custom")) {
mEdit = (EditText)findViewById(R.id.edit);
str = mEdit.getText().toString();
}
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
});
}
}
you can also set visibility of LinearLayout so that it only visible when custom in checked....
Hope it help..
put radio group in RelativeLayout
set third radiobutton text as empty/null
add EditText as layout_alignParentBottom="true"
now programmatically call EditText's onFocusChangeListener
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if(b)
thirdRadio.setCheched(true);
}
});
Here's my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<TextView android:id="#+id/welcome_text"
android:text = "Log-In"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:layout_centerHorizontal="true"/>
<TextView
android:id="#+id/username_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="20dip"
android:layout_marginTop="100dip"
android:layout_marginLeft="30dip"
android:text="Username:"
android:textColor="#000000"/>
<EditText
android:id="#+id/txt_username"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_toRightOf="#id/username_text"
android:layout_alignTop="#id/username_text"/>
<TextView
android:id="#+id/password_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/username_text"
android:layout_marginRight="20dip"
android:layout_marginTop="30dip"
android:layout_marginLeft="30dip"
android:text="Password:"
android:textColor="#000000"/>
<EditText
android:id="#+id/txt_password"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_toRightOf="#id/password_text"
android:layout_alignTop="#id/password_text"
android:layout_below="#id/txt_username"
android:layout_marginLeft="5dip"
android:password="true" />
<Button
android:id="#+id/login_button"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_below="#+id/txt_password"
android:layout_alignParentLeft="true"
android:layout_marginTop="35dip"
android:layout_marginLeft="110dip"
android:text="Submit" />
<TextView
android:id="#+id/tv_error"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:textSize="5pt"
android:layout_alignParentLeft="true"
android:layout_below="#id/txt_password"
android:layout_marginRight="9dip"
android:layout_marginTop="9dip"
android:layout_marginLeft="15dip"
android:textColor="#FF0000"
android:text=""/>
<TextView android:id="#+id/tv_login"
android:text = "#string/Login"
android:textColor="#000000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:layout_below="#id/login_button"
android:layout_centerHorizontal="true"/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_login"
android:orientation="horizontal"
android:layout_centerHorizontal="true">
<RadioButton android:id="#+id/on"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dip"
android:text="On"
android:textColor="#000000"/>
<RadioButton android:id="#+id/off"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="9dip"
android:layout_marginLeft="20dip"
android:text="Off"
android:textColor="#000000"/>
</RadioGroup>
</RelativeLayout>
and here's my java:
public class LogInActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginactivity);
final RadioButton radio_on = (RadioButton) findViewById(R.id.on);
final RadioButton radio_off = (RadioButton) findViewById(R.id.off);
Button launch = (Button)findViewById(R.id.login_button);
launch.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
EditText usernameEditText = (EditText) findViewById(R.id.txt_username);
EditText passwordEditText = (EditText) findViewById(R.id.txt_password);
TextView loginTextView = (TextView)findViewById(R.id.tv_error);
String sUserName = usernameEditText.getText().toString();
String sPassword = passwordEditText.getText().toString();
if(sUserName.equals("numlock") && sPassword.equals("numlock")) {
Intent intent = new Intent(LogInActivity.this, HomeActivity.class);
startActivity(intent);
}
else {
loginTextView.setText("Login failed. Username and/or password doesn't match.");
}
}
});
}
}
I want to assign the keep me logged in state on the radio buttons. Any help?
you should save the login credentials in sharedPreferences or Sqlite and on login activity check if you find any saved data then login with the saved data else ask user for credentials..
You can do it by using SharedPreference, Static variables or by Application class which will helps you in keeping the state of the User
EDIT: Thank you all for your help. I edited my Database class to contain the following
final EditText firstName = (EditText) findViewById(R.id.editText1); // First Name
final EditText middleName = (EditText) findViewById(R.id.editText2); // Middle Name
final EditText birthDate = (EditText) findViewById(R.id.editText4); // Birth Date
final String firstname = firstName.getText().toString(); // First Name
final String middlename = middleName.getText().toString(); // Middle Name
final String birthdate = birthDate.getText().toString(); // Birth Date
TextView firstNameText = (TextView)findViewById(R.id.firstname);
TextView middleNameText = (TextView)findViewById(R.id.middlename);
TextView birthDateText = (TextView)findViewById(R.id.birthdate);
firstNameText.setText(firstname);
middleNameText.setText(middlename);
birthDateText.setText(birthdate);
and my database.xml now shows
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:background="#aa0000"
android:gravity="center_horizontal"
android:layout_span="3"
android:id="#+id/firstname"/>
<TextView
android:background="#00aa00"
android:gravity="center_horizontal"
android:layout_span="3"
android:id="#+id/middlename"/>
<TextView
android:background="#0000aa"
android:gravity="center_horizontal"
android:layout_span="3"
android:id="#+id/birthdate"/>
</TableRow>
but when I run the emulator and I try to access that screen (by clicking a button from the previous screen) the application crashes? I set up the button correctly using the OnClickListener so I'm fairly sure the button is not the problem
If you're having trouble understanding the interface here's an example you can cut and paste.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name: "
android:id="#+id/firstLabel"
android:layout_marginTop="14dip"
android:layout_marginBottom="14dip"
/>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/firstEdit"
android:text="John"
android:layout_toRightOf="#id/firstLabel"
android:layout_alignParentRight="true"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Middle Name: "
android:id="#+id/middleLabel"
android:layout_below="#id/firstLabel"
android:layout_marginTop="14dip"
android:layout_marginBottom="14dip"/>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/middleEdit"
android:text="Phillip"
android:layout_below="#id/firstEdit"
android:layout_toRightOf="#id/middleLabel"
android:layout_alignParentRight="true"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name: "
android:id="#+id/lastLabel"
android:layout_below="#id/middleLabel"
android:layout_marginTop="14dip"
android:layout_marginBottom="14dip"
/>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/lastEdit"
android:text="Doe"
android:layout_below="#id/middleEdit"
android:layout_toRightOf="#id/lastLabel"
android:layout_alignParentRight="true"></EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birthdate: "
android:id="#+id/birthLabel"
android:layout_below="#id/lastLabel"
android:layout_marginTop="14dip"
android:layout_marginBottom="14dip"
/>
<EditText android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/birthEdit"
android:text="08/09/1977"
android:layout_toRightOf="#id/lastLabel"
android:layout_below="#id/lastEdit"
android:layout_alignParentRight="true"></EditText>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#id/birthEdit">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:background="#aa0000"
android:gravity="center_horizontal"
android:layout_span="3"
android:text="something"
android:id="#+id/firstTable"/>
<TextView
android:background="#00aa00"
android:gravity="center_horizontal"
android:layout_span="3"
android:text="something1"
android:id="#+id/middleTable"/>
<TextView
android:background="#0000aa"
android:gravity="center_horizontal"
android:layout_span="3"
android:text="something2"
android:id="#+id/birthTable"/>
</TableRow>
</LinearLayout>
</RelativeLayout>
DatabaseExample.java
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class DatabaseExample extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Setup edit fields
EditText firstEdit = (EditText)findViewById(R.id.firstEdit);
EditText middleEdit = (EditText)findViewById(R.id.middleEdit);
EditText lastEdit = (EditText)findViewById(R.id.lastEdit);
EditText birthEdit = (EditText)findViewById(R.id.birthEdit);
//Get the text and store in variables
String firstName = firstEdit.getText().toString();
String middleName = middleEdit.getText().toString();
String lastName = lastEdit.getText().toString();
String birthDate = birthEdit.getText().toString();
//setup the text fields
TextView firstTable = (TextView)findViewById(R.id.firstTable);
TextView middleTable = (TextView)findViewById(R.id.middleTable);
TextView birthTable = (TextView)findViewById(R.id.birthTable);
//change the text fields
firstTable.setText(firstName);
middleTable.setText(middleName);
birthTable.setText(birthDate);
}
}
Once again this would be just the interface for inputting and displaying data. Instead of storing the data in String variables you would use SQLite, SharedPreferences, or write to a file on the SDCard.
Try as follows
final EditText firstName = (EditText) findViewById(R.id.editText1);
TextView firstNameTxt = (TextView)findViewById(R.id.firstname);
firstNameTxt.setText(firstName);
You cannot store values from the user in the strings.xml file. You must have a data store such as SharedPreferences or SQLite. Here is another link for help on data storage with Android.