Dynamic Xml (I think) - java

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.

Related

click button crashing when click android studio

I am trying to make a form that includes students details and again show that detail in text views. When the user types the details in edit texts save them into the model class then show them again in text views. first, I have created a model class to save variables of students.
Logcat:
2021-11-09 13:12:47.784 30040-30040/com.example.studentapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.studentapp, PID: 30040
java.lang.NumberFormatException: For input string: "stephen"
at java.lang.Integer.parseInt(Integer.java:608)
at java.lang.Integer.parseInt(Integer.java:643)
at com.example.studentapp.MainActivity$1.onClick(MainActivity.java:52)
at android.view.View.performClick(View.java:6291)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
XML file:
<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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/name"
android:autofillHints="" />
<EditText
android:id="#+id/edit_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:autofillHints="" />
<EditText
android:id="#+id/edit_grade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Grade"
android:autofillHints="" />
<EditText
android:id="#+id/edit_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Address"
/>
<EditText
android:id="#+id/edit_distance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Distance"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:orientation="vertical">
<TextView
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="#+id/txtAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="#+id/txtGrade"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="#+id/txtAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<TextView
android:id="#+id/txtDistance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
<Button
android:text="View"
android:id="#+id/btnView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Model class:
package com.example.studentapp;
public class Student {
public String name = "";
public int age = 0;
public int grade = 0;
public String address = "";
public double distance = 0;
}
This is .java file:
package com.example.studentapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button buttonView;
EditText editName;
EditText editAge;
EditText editGrade;
EditText editAddress;
EditText editDistance;
TextView textName, textAge, textGrade, textAddress, textDistance;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editName = findViewById(R.id.edit_name);
editAge = findViewById(R.id.edit_age);
editGrade = findViewById(R.id.edit_grade);
editAddress = findViewById(R.id.edit_address);
editDistance = findViewById(R.id.edit_distance);
textName = findViewById(R.id.txtName);
textAge = findViewById(R.id.txtAge);
textGrade = findViewById(R.id.txtGrade);
textAddress = findViewById(R.id.txtAddress);
textDistance = findViewById(R.id.txtDistance);
buttonView = findViewById(R.id.btnView);
buttonView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Student student1 = new Student();
student1.name = editName.getText().toString();
student1.age = Integer.parseInt(editAge.getText().toString());
student1.grade = Integer.parseInt( editName.getText().toString());
student1.address = editName.getText().toString();
student1.distance = Double.parseDouble( editName.getText().toString());
textName.setText(student1.name);
textAge.setText(""+student1.age);
textGrade.setText(student1.grade+" Years");
textAddress.setText(student1.address);
textDistance.setText(student1.distance+"km");
}
});
}
}
Please, bear in mind that I am a very beginner at android development.
student1.distance = Double.parseDouble(editName.getText().toString());
You are using editName for getting distance grade and address. Please, change them according to your needs.
Your code
student1.grade = Integer.parseInt( editName.getText().toString());
student1.distance = Double.parseDouble( editName.getText().toString());
Expected code
student1.grade = Integer.parseInt( editGrade.getText().toString());
student1.distance = Double.parseDouble( editDistance.getText().toString());
Your are trying to parse string to integer Integer.parseInt( editName.getText().toString()). It won't work for non numbers input.
Looking at your Locat message, it seems you're saving the String name in an Integer variable, kindly look at your codebase to see if you're making that mistake, if so reassign the name variable to a string variable.
You are using wrong editText for grade and distance.
your code:
student1.grade = Integer.parseInt(editName.getText().toString());
change editName to editGrade :
student1.grade = Integer.parseInt(editGrade.getText().toString());
aswell for:
student1.distance = Double.parseDouble(editName.getText().toString());
change editName to editDistance:
student1.distance = Double.parseDouble(editDistance.getText().toString());

java.lang.IllegalStateException: Could not find method btnSubmit(View) in a parent or ancestor Context for android:onClick

This Java takes the inputs of Edit Text fields and outputs them into Toast.
I have no idea where or what the error is, any help would be greatly appreciated:
Java
package colonyapplication.colony;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class tenantReg extends Activity {
EditText fullName = (EditText) findViewById(R.id.etFname);
EditText userEmail = (EditText) findViewById(R.id.etEmail);
EditText userPass = (EditText) findViewById(R.id.etPass);
String fName = fullName.getText().toString();
String uEmail = userEmail.getText().toString();
String uPass = userPass.getText().toString();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tenant_reg);
}
public void btnSubmit() {
String tenant = "Tenant";
Toast.makeText(tenantReg.this, "UserName is: " + fName + ". Password is: " + uPass + "email is: " + uEmail + tenant, Toast.LENGTH_LONG).show();
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:background="#fcdfaa"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="colonyapplication.colony.tenantReg">
<ImageView
android:id="#+id/imageView2"
android:layout_width="300dp"
android:layout_height="300dp"
android:src="#drawable/colonylogo"
android:layout_above="#+id/passTextBox"
android:layout_centerHorizontal="true"
android:layout_marginBottom="61dp" />
<EditText
android:id="#+id/etFname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/imageView2"
android:layout_alignStart="#+id/imageView2"
android:layout_below="#+id/imageView2"
android:ems="10"
android:hint="Full Name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/etFname"
android:layout_alignStart="#+id/etFname"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/etPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:layout_below="#+id/etEmail"
android:layout_alignStart="#+id/etEmail"
android:layout_alignEnd="#+id/etEmail" />
<EditText
android:id="#+id/etPassconfirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="34dp"
android:ems="10"
android:hint="Confirm Password"
android:inputType="textPassword"
android:layout_below="#+id/etPass"
android:layout_alignStart="#+id/etPass"
android:layout_alignEnd="#+id/etPass" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="47dp"
android:background="#EE7600"
android:onClick="btnSubmit"
android:text="Submit"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="17sp"
android:layout_below="#+id/etPassconfirm"
android:layout_alignStart="#+id/etPassconfirm" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:background="#EE7600"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="17sp"
android:layout_alignBaseline="#+id/button"
android:layout_alignBottom="#+id/button"
android:layout_alignEnd="#+id/etPassconfirm"
android:layout_marginEnd="33dp" />
</RelativeLayout>
For whatever reason, when the submit button is clicked, the program stops in the emulator. The error in the Android Monitor states: "java.lang.IllegalStateException: Could not find method btnSubmit(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button'"
As I see you have 2 Button but I dont know which one is submit button.
well also I can not find the meaning of public void btnSubmit(). As I know you should define tn submit:
Button btnSubmit = (Button) findViewById(R.id.your_btnSubmit_id);
and then use onClickListener method to do somthing when btn clicked
btnsubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do somthing
}
});
Edit:
You can use your code just replace
public void btnSubmit()
with this:
public void btnSubmit(View v)

Android Studio - EditText text , both hint and normal text , not displaying

I know this question has been asked several times, and with different solutions, but I have tried all I could find here, and none worked. As the title suggest, in Android Studio, my EditText widget's field isn't showing any type of text, even what it has by default (like the hint and default text). The text is there if I print it out , but not displaying. It is for a simple login page.
This is the xml file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:text="Name : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="30dp"
android:layout_marginTop="40dp"
android:id="#+id/atcoNameTag" />
<TextView
android:text="Password : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/atcoNameTag"
android:layout_marginTop="30dp"
android:id="#+id/passTag"
android:layout_alignStart="#+id/atcoNameTag"/>
<TextView
android:text="Role : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:id="#+id/roleNameTag"
android:layout_below="#+id/passTag"
android:layout_alignStart="#+id/atcoNameTag"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:layout_above="#+id/passTag"
android:layout_centerHorizontal="true"
android:id="#+id/insertName"
android:layout_alignTop="#+id/atcoNameTag"
android:layout_toEndOf="#+id/atcoNameTag"
android:layout_alignStart="#+id/insertPass"
android:hint="username"
android:cursorVisible="true"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_alignBottom="#+id/passTag"
android:layout_toEndOf="#+id/passTag"
android:id="#+id/insertPass"
android:layout_alignTop="#+id/passTag"
android:hint="password"
android:cursorVisible="true"/>
<Spinner
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_alignTop="#+id/roleNameTag"
android:layout_toEndOf="#+id/roleNameTag"
android:layout_alignStart="#+id/insertPass"/>
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/loginButton"
android:onClick="onClick"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp" />
</RelativeLayout>
And this is the java file :
package mecals.mecalsapp;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
Button nameBtn = (Button) findViewById(R.id.loginButton);
nameBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText nameEntry = (EditText) findViewById(R.id.insertName);
nameEntry.setTextColor(Color.BLACK);
String name = nameEntry.getText().toString();
//TODO username checking, username not showing , need devC Team's work
if (name.length() > 0) {
Toast toastYes = Toast.makeText(getApplicationContext(), "Hi there, " + name + "!", Toast.LENGTH_SHORT);
toastYes.show();
Intent intent = new Intent(getApplicationContext(),HomeActivity.class);
startActivity(intent);
} else {
Toast toastNo = Toast.makeText(getApplicationContext(), "Please insert name !", Toast.LENGTH_SHORT);
toastNo.show();
}
EditText passwordEntry = (EditText) findViewById(R.id.insertPass);
passwordEntry.setTextColor(Color.BLACK);
String pass = passwordEntry.getText().toString();
//TODO password checking , need devC Team's work
}
});
}
}
And these are the solutions I have tried , to no avail :
Difference between content_main.xml and activity_main.xml?
Android edittext typed text not showing
Android EditText typed value not showing
Text not displayed in Android editText when changing android:hint to android:text
EditText in Android doesn't show text when typing while using the on-screen keyboard
Edit Text hints not appearing on later version SDK
edittext not showing the typed text in android
In short, I have
-set the text colour to Black , with nameEntry.setTextColor(Color.BLACK);
-I have added android:windowSoftInputMode="adjustPan" in the manifest
-I have NOT used something like android:gravity , just android:layout
-the cursor is set to visible
-I even checked the height of the widget
-I checked the size of the text with android:ems to be 10
-and the input type is set to text for the first field and textPassword for the second
Help !
PS : before anyone asks , my Android Studio version is 2.2.3 , and the API I am using is min 21.
The problem is that the height of the EditText field is much to small to display the text. This is present because both the top and the bottom of the EditText field are set according to the TextView left of the EditText. But since the height of the TextView is smaller than the normal height of the EditText you force it to be very narrow (and so there is not enough space for the Text). If you remove this line
android:layout_alignTop="#+id/atcoNameTag"
you can see that the text gets displayed normally.
There are actually several ways to fix this:
you could increase the text size of the TextViews
you could add enough padding on top and on the bottom of the TextViews
you could decrease the text size of the EditText
you could decouple the height of the EditText from the TextView
It is probably a good idea to go with the last solution and put the Textview and EditText fields that belong together into a separate LinearLayout and remove the align calls like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:id="#+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:layout_marginTop="40dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:weightSum="100"
>
<TextView
android:layout_weight="30"
android:text="Name : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/atcoNameTag" />
<EditText
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:id="#+id/insertName"
android:hint="username"
android:cursorVisible="true"/>
</LinearLayout>
<LinearLayout
android:id="#+id/second"
android:layout_below="#+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:weightSum="100"
>
<TextView
android:layout_weight="30"
android:text="Password : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/passTag"/>
<EditText
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:id="#+id/insertPass"
android:hint="password"
android:cursorVisible="true"/>
</LinearLayout>
<LinearLayout
android:layout_below="#+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="30dp"
android:weightSum="100"
android:layout_marginTop="10dp"
>
<TextView
android:layout_weight="30"
android:text="Role : "
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/roleNameTag"/>
<Spinner
android:layout_weight="60"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/spinner"/>
</LinearLayout>
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/loginButton"
android:onClick="onClick"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp" />
</RelativeLayout>

Android Radio Button Group with EditText imbedded

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);
}
});

Cannot resolve method getText for a RadioGroup

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();
}
});

Categories