How to pass users EditText input in a Textview in another activity - java

I have 2 textboxes with a users name and Id number in my main activity.
When clicking the edit button the user is sent to second activity where they can edit their name and Id number.
Then when they click Save, the edited input should override the written text in the two existing textviews in the first activity.
But when I click the Save button, my app crashes...
Furthermore, main activity has also a capture profile pic. feauture, but when in landscape mode, my captured image. disappear.
I'm totally new to Android and this is a school assignment with due on Sunday... Can someone please help me!!
This is my first activity's 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.solveigdoan.cameraactivity.SecondActivity2"
android:background="#F0FFFF">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="8"
android:id="#+id/EditName"
android:layout_marginTop="42dp"
android:hint="Solveig Mortensen"
android:layout_marginLeft="160dp"
android:background="#87CEEB"
android:textSize="20dp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/EditName"
android:text="#string/edit_name"
android:textSize="20dp"
android:textColor="#000000"
android:id="#+id/NameLabel" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="8"
android:id="#+id/EditID"
android:hint="123456"
android:layout_below="#+id/EditName"
android:layout_alignLeft="#+id/EditName"
android:layout_alignStart="#+id/EditName"
android:layout_marginTop="40dp"
android:background="#87CEEB"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/EditID"
android:text="#string/edit_id"
android:textSize="20dp"
android:textColor="#000000"
android:id="#+id/IdLabel" />
<CheckBox
android:id="#+id/Yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Y"
android:onClick="onCheckboxClicked"
android:layout_marginTop="27dp"
android:layout_below="#+id/AndrStatus"
/>
<CheckBox android:id="#+id/NO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/N"
android:onClick="onCheckboxClicked"
android:layout_marginTop="30dp"
android:layout_below="#+id/Yes"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:id="#+id/buttonCancel"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#000000"
android:background="#87CEEB" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/save_btn"
android:id="#+id/buttonSave"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#000000"
android:background="#87CEEB" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/android_developer"
android:id="#+id/AndrStatus"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/ID"
android:layout_alignStart="#+id/ID"
android:textSize="20dp"
android:textColor="#000000" />
</RelativeLayout>
AND second activity's 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.solveigdoan.cameraactivity.CameraActivity">
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#F0FFFF" />
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/profile_picture"
android:layout_centerHorizontal="true"
android:layout_marginLeft="40dp"
android:textColor="#000000" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="#string/take_photo"
android:layout_below="#+id/textview1"
android:background="#87CEEB"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:layout_below="#+id/button1"
android:background="#BCC6CC" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:ems="9"
android:id="#+id/Name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignLeft="#+id/ID"
android:layout_alignStart="#+id/ID"
android:layout_below="#+id/imageView1"
android:textSize="20dp"
android:background="#87CEEB"
android:textColor="#000000"
android:hint="Solveig Mortensen" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_alignBaseline="#id/Name"
android:text="#string/name"
android:textSize="20dp"
android:textColor="#000000"
android:id="#+id/NameTitle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="9"
android:id="#+id/ID"
android:layout_alignTop="#+id/Name"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
android:textSize="20dp"
android:background="#87CEEB"
android:textColor="#000000"
android:hint="123456" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_alignBaseline="#id/ID"
android:text="#string/id_nr"
android:textSize="20dp"
android:textColor="#000000" />
<Button
android:id ="#+id/push_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="#string/edit_btn"
android:background="#drawable/button_bg_round"
android:padding="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="12dp"
android:textColor="#000000" />
<TextView
android:id="#+id/tvView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="#+id/ID"
android:layout_marginTop="20dp" />
</RelativeLayout>
My main java (named CameraActivity):
public class CameraActivity extends Activity {
Button BtnTakePhoto;
ImageView imgTakenPhoto;
Button push_button;
TextView Name;
TextView ID;
private static final int CAMERA_REQUEST = 1888;
private static final int EDIT_REQUEST = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
push_button = (Button) findViewById(R.id.push_button);
push_button.setOnClickListener(new push_buttonClicker());
imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);
BtnTakePhoto = (Button) findViewById(R.id.button1);
BtnTakePhoto.setOnClickListener(new BtnTakePhotoClicker());
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imgTakenPhoto.setImageBitmap(thumbnail);
}
if (requestCode == EDIT_REQUEST) {
Bundle b = getIntent().getExtras();
Name = (TextView) findViewById(R.id.Name);
ID = (TextView) findViewById(R.id.ID);
Name.setText(b.getCharSequence("Name"));
ID.setText(b.getCharSequence("ID nr"));
}
}
class BtnTakePhotoClicker implements Button.OnClickListener
{
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}class push_buttonClicker implements Button.OnClickListener{
public void onClick(View v){
Intent Intent = new Intent(CameraActivity.this, SecondActivity2.class);
CameraActivity.this.startActivityForResult(Intent, EDIT_REQUEST);
}}}
AND my secondActivity.java:
public class SecondActivity2 extends AppCompatActivity {
TextView tvView;
EditText EditName;
EditText EditID;
Button buttonCancel;
Button buttonSave;
CheckBox checkBox;
TextView Name;
TextView ID;
private static final int RESULT_OK = 10;
//private static final int RESULT_CANCELED = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second2);
// CheckBox Yes = (CheckBox) findViewById(R.id.Yes);
// CheckBox NO = (CheckBox) findViewById(R.id.NO);
buttonSave = (Button) this.findViewById(R.id.buttonSave);
buttonCancel = (Button) findViewById(R.id.buttonCancel);
EditName = (EditText) findViewById(R.id.EditName);
EditID = (EditText) findViewById(R.id.EditID);
buttonSave.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent data = new Intent();
data.putExtra("name", EditName.getText().toString());
data.putExtra("ID", EditID.getText().toString());
setResult(RESULT_OK, data);
finish();
}
});}}

change your code inside onActivityResult under EDIT_REQUEST condition
String name = data.getStringExtra("name");
String ID = data.getStringExtra("ID");
Name.setText(name);
ID.setText(ID);
no need to create a Bundle there, it will solve your problem.

Related

Button onClick() causing force stop

Hello StackOverFlowGeeks!
Currently, I'm learning how to write a basic Android app, that has few buttons.
I have four activities(Java Classes) and four xmls.
I have the RegisterScreen.java that has two buttons(Register and Back button). Currently, I am trying to get back from this RegisterScreen.java to SignInScreen.java. The problem is that when I click on the back button it causes a failure and the app is forced to shut down...
So I've uploaded this way:
1) RegisterScreen.java
2) SignInScreen.java
3) RegisterScreen XML file
4) SignInScreen XML file
Thanks in advance.
RegisterScreen:
public class RegisterScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_screen);
}
public void onClickRegisterButton(View view){
}
public void onClickBackButton(View view) {
Button backBtn = (Button) findViewById(R.id.backBtn);
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(RegisterScreen.this, SignInScreen.class);
RegisterScreen.this.startActivity(intent);
}
});
}
}
SignInScreen:
public class SignInScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_screen);
}
#SuppressLint("SetTextI18n")
public void onClickSignInButton(View view){
TextView username = (TextView)findViewById(R.id.username_text);
TextView password = (TextView)findViewById(R.id.password_text);
Button signIn = (Button)findViewById(R.id.sign_in_button);
if (username.getText().toString().equals("Ivan Simeonov") && password.getText().toString().equals("Ivan9603116245")){
signIn.setText("Welcome " + username.getText().toString());
signIn.setBackgroundColor(Color.YELLOW);
setContentView(R.layout.activity_logged_screen);
}else{
signIn.setText("The input data is incorrect! Try again!");
signIn.setBackgroundColor(Color.GREEN);
}
}
public void onClickRegisterButton(View view){
Button register = (Button)findViewById(R.id.register_button);
setContentView(R.layout.activity_register_screen);
}
public void onClickResetPasswordButton(View view){
Button resetPassword = (Button)findViewById(R.id.reset_password_button);
setContentView(R.layout.activity_reset_password_screen);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.megat0n.startproject.RegisterScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView_Register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/register_your_account_by_filling_up_the_following_data"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_username"
android:inputType="textPersonName" />
<EditText
android:id="#+id/first_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_firstname"
android:inputType="textPersonName" />
<EditText
android:id="#+id/last_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_lastname"
android:inputType="textPersonName" />
<EditText
android:id="#+id/birth_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_birthdate"
android:inputType="date" />
<EditText
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_email"
android:inputType="textPersonName" />
<EditText
android:id="#+id/email_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/confirm_email"
android:inputType="textPersonName" />
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/enter_password"
android:inputType="textPassword" />
<EditText
android:id="#+id/password_confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/confirm_password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/reg_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/register"
android:onClick="onClickRegisterButton"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/backBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/back"
android:onClick="onClickBackButton"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.megat0n.startproject.SignInScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_view"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="#string/welcome_to_the_home_screen"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
<EditText
android:id="#+id/username_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/username"
android:inputType="textPersonName"
android:textAlignment="center"
android:textStyle="bold" />
<EditText
android:id="#+id/password_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/password"
android:inputType="textPassword"
android:textAlignment="center"
android:textStyle="bold" />
<Button
android:id="#+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickSignInButton"
android:text="#string/sign_in"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickRegisterButton"
android:text="#string/register"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/reset_password_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickResetPasswordButton"
android:text="#string/reset_password"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Use this onClickBackButton() instead of yours. If you use onClick attribute in your layout, you don't need creating button and click listener.
public void onClickBackButton(View view) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
Try in RegisterScreen use onBackPressed() method:
backBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});

Get value of a EditText in another layout

I have an dialog that appears where the user can costumize how many points they get from each action. The dialog is based on a layout that is not the main_activity. the main activity is the layout that is used in setContentView in the onCreate method.
when I try to get the values I am only getting null, (because the layout where the EditText is is not set as the contentview.... maybe?)
How do I solve this problem so that I get the value of the edittext in the "settings" layout and can use that value in the MainActivity?
private int teamOnePoints=0;
private int teamTwoPoints=0;
private boolean teamOneField=true;
private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;
private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = (ToggleButton)
findViewById(R.id.field_team_toggle); //activates toggle button
toggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
//changes between the teams
teamOneField=false;
} else {
teamOneField=true;
}
}
});
FloatingActionButton fab = findViewById(R.id.points_edit_button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.settings, null);
builder.setView(v);
builder.setTitle(getResources().getString(R.string.edit_rules));
builder.setPositiveButton(getResources().getString(R.string.ok_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
applySettings();
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_button),
null);
builder.show();
}
private void applySettings(){
EditText newBurnedPoints = findViewById(R.id.burned_edit);
burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());
EditText newLyrePoints = findViewById(R.id.lyre_edit);
lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());
EditText newOneHandLyrePoints = findViewById(R.id.one_hand_lyre_edit);
oneHandLyrePoints=Integer.parseInt
(newOneHandLyrePoints.getText().toString());
EditText newHomeRunPoints = findViewById(R.id.home_run_edit);
homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());
EditText newRevPoints = findViewById(R.id.rev_edit);
revPoints=Integer.parseInt(newRevPoints.getText().toString());
EditText newTeamOneName = findViewById(R.id.name_one_edit);
teamOneName= newTeamOneName.getText().toString();
TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
setNewTeamOneName.setText(teamOneName);
EditText newTeamTwoName =findViewById(R.id.name_two_edit);
teamTwoName= newTeamTwoName.getText().toString();
TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
setNewTeamTwoName.setText(teamTwoName);
}
SETTINGS LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:id="#+id/settings_layout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/name_one_settings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/name_team_one"
style="#style/settings_headings"
/>
<TextView
android:id="#+id/name_two_settings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/name_team_two"
style="#style/settings_headings"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/name_one_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/team_one"
android:maxLength="20"
android:singleLine="true"
/>
<EditText
android:id="#+id/name_two_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/team_two"
android:maxLength="20"
android:singleLine="true"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/burned_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/burned_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lyre_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/lyre_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="3"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/one_hand_lyre_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/one_hand_lyre_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="5"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/home_run_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/home_run_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="5"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rev_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/rev_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
</LinearLayout>
MAIN LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<LinearLayout
android:id="#+id/team_names"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/name_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/team_one"
style="#style/Headings"
/>
<TextView
android:id="#+id/name_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/team_two"
style="#style/Headings"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/team_points"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/points_one"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:text="0"
style="#style/TeamPoints"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/field_team"/>
<ToggleButton
android:id="#+id/field_team_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="#string/team_one"
android:textOn="#string/team_two"
style="#style/buttons"/>
</LinearLayout>
<TextView
android:id="#+id/points_two"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:text="0"
style="#style/TeamPoints"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/events"
android:paddingTop="16dp"
style="#style/Headings"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
>
<Button
android:id="#+id/burned_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="burned"
android:text="#string/burned"
style="#style/buttons"/>
<Button
android:id="#+id/lyre_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="lyre"
android:text="#string/lyre"
style="#style/buttons"
/>
<Button
android:id="#+id/one_hand_lyre_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneHandLyre"
android:text="#string/oneHandLyre"
style="#style/buttons"
/>
<Button
android:id="#+id/homeRun_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="homeRun"
android:text="#string/homeRun"
style="#style/buttons"
/>
<Button
android:id="#+id/rev_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="rev"
android:text="#string/rev"
style="#style/buttons"
/>
</TableLayout>
<Button
android:id="#+id/reset_button"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginTop="16dp"
android:onClick="reset"
android:text="#string/reset"
style="#style/buttons"
/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/points_edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#android:drawable/ic_menu_edit"
android:layout_margin="16dp" />
</LinearLayout>
I would strongly recommend using SharedPreferences. This way, you can access the data even if you restart your application. If that does not work for you, you can use Intent.
Consider reading this StackOverflow post.
Using SharedPreferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.points), pointsPerActivity);
editor.commit();
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = 1;
// or you can store the value somewhere
// getResources().getInteger(R.integer.defaultPointsPerActivity);
int pointsPerActivity = sharedPref.getInt(pointsPerActivity, defaultValue);
pass view object in applySettings(v);
private int teamOnePoints=0;
private int teamTwoPoints=0;
private boolean teamOneField=true;
private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;
private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = (ToggleButton)
findViewById(R.id.field_team_toggle); //activates toggle button
toggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
//changes between the teams
teamOneField=false;
} else {
teamOneField=true;
}
}
});
FloatingActionButton fab = findViewById(R.id.points_edit_button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.settings, null);
builder.setView(v);
builder.setTitle(getResources().getString(R.string.edit_rules));
builder.setPositiveButton(getResources().getString(R.string.ok_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
applySettings(v);
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_button),
null);
builder.show();
}
private void applySettings(View view){
EditText newBurnedPoints = view.findViewById(R.id.burned_edit);
burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());
EditText newLyrePoints = view.findViewById(R.id.lyre_edit);
lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());
EditText newOneHandLyrePoints = view.findViewById(R.id.one_hand_lyre_edit);
oneHandLyrePoints=Integer.parseInt
(newOneHandLyrePoints.getText().toString());
EditText newHomeRunPoints = view.findViewById(R.id.home_run_edit);
homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());
EditText newRevPoints = view.findViewById(R.id.rev_edit);
revPoints=Integer.parseInt(newRevPoints.getText().toString());
EditText newTeamOneName = view.findViewById(R.id.name_one_edit);
teamOneName= newTeamOneName.getText().toString();
TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
setNewTeamOneName.setText(teamOneName);
EditText newTeamTwoName =view.findViewById(R.id.name_two_edit);
teamTwoName= newTeamTwoName.getText().toString();
TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
setNewTeamTwoName.setText(teamTwoName);
}

Can't set values in second activity

So I'm making a simple application that just takes some information from the user i.e Name, address etc and puts it in the the textviews in the next activity but when I put in the values and move to the next activity there's nothing displayed in the TextViews.
Here's my first activity
public class MainActivity extends Activity {
public String Name;
public String Age;
public String Address;
public String City;
public String phoneno;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText name = (EditText) findViewById(R.id.editText1);
Name = name.getText().toString();
EditText Amge = (EditText) findViewById(R.id.agee);
Age = Amge.getText().toString();
EditText Address2 = (EditText) findViewById(R.id.address);
Address = Address2.getText().toString();
EditText City2 = (EditText) findViewById(R.id.city);
City = City2.getText().toString();
EditText phone2 = (EditText) findViewById(R.id.phone);
phoneno = phone2.getText().toString();
final ImageView D = (ImageView) findViewById(R.id.done);
D.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, second.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);}}
Here's my second activity
public class second extends MainActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView N1 = (TextView) findViewById(R.id.name1);
N1.setText(Name);
TextView A1 = (TextView) findViewById(R.id.age1);
A1.setText(Age);
TextView Ad1 = (TextView) findViewById(R.id.address1);
Ad1.setText(Address);
TextView C1 = (TextView) findViewById(R.id.city1);
C1.setText(City);
TextView P1 = (TextView) findViewById(R.id.phone1);
P1.setText(phoneno);}
public static void main(String[] args) {
// TODO Auto-generated method stub
}}
Here's my xml for the main activity
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
android:background="#drawable/bg"
tools:context="com.example.randomtests.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:textSize="21dp"
android:textColor="#ffffff"
android:text="Please enter the required info"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView1"
android:src="#drawable/bar" />
<EditText
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/agee"
android:layout_below="#+id/agee"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="#string/address"
android:inputType="textPostalAddress"
android:textColor="#000000" />
<EditText
android:id="#+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/address"
android:layout_below="#+id/address"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="#string/city"
android:textColor="#000000" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:ems="10"
android:gravity="top"
android:textColor="#000000"
android:hint="#string/namu"
android:inputType="textPersonName" />
<EditText
android:id="#+id/agee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="#string/age"
android:inputType="phone"
android:textColor="#000000" />
<EditText
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/city"
android:layout_below="#+id/city"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/phone"
android:inputType="phone"
android:textColor="#000000" />
<ImageView
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/phone"
android:layout_marginRight="14dp"
android:layout_marginTop="12dp"
android:src="#drawable/button" />
Here's my xml for the second activity
<?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"
android:background="#drawable/bg" >
<TextView
android:id="#+id/info1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#ffffff"
android:textSize="33dp"
android:layout_centerHorizontal="true"
android:text="#string/yourinfo"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/info1"
android:textColor="#ffffff"
android:layout_marginLeft="19dp"
android:layout_marginTop="26dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/age1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/name1"
android:layout_below="#+id/name1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/address1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/age1"
android:layout_below="#+id/age1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/city1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/address1"
android:layout_below="#+id/address1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phone1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/city1"
android:layout_below="#+id/city1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
After you declare your Intent , try sending your data with this
//this use key-value
intent.putExtra("USER_NAME" , "your string");
intent.putExtra("USER_AGE" , "your string");
and etc.
and to other Activity try use
Bundle extra = getIntent().getExtras();
String userName = extra.getString("USER_NAME");
String userAge = extra.getString("USER_AGE");
When you declare your Intent here:
Intent intent = new Intent(MainActivity.this, second.class);
Try sending some data with it using Extras based on what view is being clicked on
For example:
intent.putExtra(Intent.EXTRA_INTENT, **Your String Here**);
And in the activity receiving the intent, use something like:
String data = intent.getStringExtra(Intent.EXTRA_INTENT);
To fetch the data from the intent

Motion event error on ImageButton click

public class AddActivity extends Activity implements OnClickListener{
String[] info = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
ImageButton submit = (ImageButton)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
submit.setOnClickListener((OnClickListener) this);
add1.setOnClickListener((OnClickListener) this);
add2.setOnClickListener((OnClickListener) this);
add3.setOnClickListener((OnClickListener) this);
int id = v.getId();
switch(id){
case R.id.submit:{
submitEntry(info);
break;
}
case R.id.add1:{
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
break;
}
}
}
}
This is the code.
<?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/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/key_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<EditText
android:id="#+id/keyString"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/site_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new" />
<EditText
android:id="#+id/site1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="invisible" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site3"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/submit_buttom" />
</LinearLayout>
</ScrollView>
</LinearLayout>
And this is the XML. The add1, add2, add3 and submit ImageButtons are all in a ScrollView.
When I press the add1 ImageButton, I want the add2 and site2 ImageButtons to become visible but instead, it throws the following error.
Motion event has invalid pointer count 0; value must be between 1 and 16.
What am I doing wrong?
PS: All the findViewById() calls are in the onClick() method because a NullPointerExeption is thrown if I call them in the onCreate().
Those findViewByIdcalls in onClickdon't make sense. Not sure why you are getting a null pointer exception calling them in onCreate.onClick is never called in this instance because nothing in the creation of the Activity is assigning the buttons to look at your onClick method; the buttons will default to having no listener assigned. It also doesn't look like a good idea to use the Activity as the onClickListener as well.
Your code should look something like this:
public class AddActivity extends Activity {
// https://source.android.com/source/code-style.html
// info -> mInfo; non-public, non-static field!
String[] mInfo = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
Button submit = (Button)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
add1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
}
});
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submitEntry(mInfo);
}
});
}
The findViewById() calls and especially the setOnClickListener() calls should have been inside the onCreate(). With setOnClickListener() inside the onClick() i doubt the onClick was ever called.
We would need more logs to find the exact issue.

Returning values from a custom dialog view

I'm trying to make a custom dialog for my android application. I get null pointer exceptions when I attempt to access the fields from the 'ok' button method. It seems like my fields fade from existence as soon as the onclick listener fires. Is there a better way to do this?
addchars.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/scrollBox">
<RelativeLayout
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/addChars">
<AutoCompleteTextView android:layout_height="wrap_content" android:id="#+id/inCharName" android:hint="#string/txtCharacterNameBox" android:layout_below="#+id/textView1" android:layout_centerHorizontal="true" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</AutoCompleteTextView>
<TextView android:id="#+id/textView2" android:layout_height="wrap_content" android:gravity="center" android:layout_width="wrap_content" android:text="#string/txtHP" android:layout_below="#+id/inCharName" android:layout_alignParentLeft="true" android:layout_marginLeft="22dp"></TextView>
<TextView android:id="#+id/textView3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/txtTempHP" android:layout_below="#+id/inCharName" android:layout_alignParentRight="true" android:layout_marginRight="57dp"></TextView>
<EditText android:id="#+id/inHP" android:digits="-0123456789" android:minEms="3" android:gravity="center" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_below="#+id/textView2" android:layout_alignLeft="#+id/textView2">
</EditText>
<EditText android:id="#+id/inTempHP" android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_alignBaseline="#+id/inHP" android:layout_alignBottom="#+id/inHP" android:layout_alignLeft="#+id/textView3"></EditText>
<TextView android:id="#+id/TextView01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inHP" android:layout_alignLeft="#+id/inHP" android:text="#string/txtInitRoll"></TextView>
<TextView android:id="#+id/TextView02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inTempHP" android:layout_alignLeft="#+id/inTempHP" android:text="#string/txtInitBonus"></TextView>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView01" android:layout_alignLeft="#+id/TextView01" android:id="#+id/inInitRoll"></EditText>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView02" android:layout_alignLeft="#+id/TextView02" android:id="#+id/inInitBonus"></EditText>
<TextView android:id="#+id/TextView03" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/txtAC" android:layout_below="#+id/inInitRoll" android:layout_alignRight="#+id/textView2"></TextView>
<EditText android:id="#+id/inAC" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView03" android:layout_alignLeft="#+id/TextView03"></EditText>
<TextView android:id="#+id/TextView04" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="#+id/inAC" android:layout_alignLeft="#+id/inInitBonus" android:text="#string/txtFortitude"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView04" android:layout_alignLeft="#+id/TextView04" android:id="#+id/inFort"></EditText>
<TextView android:id="#+id/TextView06" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inFort" android:layout_alignLeft="#+id/inFort" android:text="#string/txtWill"></TextView>
<TextView android:id="#+id/TextView05" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inAC" android:layout_alignLeft="#+id/inAC" android:text="#string/txtRef"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView05" android:layout_alignLeft="#+id/inInitRoll" android:id="#+id/inReflex"></EditText>
<EditText android:id="#+id/inWill" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_above="#+id/chkRoll" android:layout_alignLeft="#+id/TextView06"></EditText>
<CheckBox android:text="#string/txtRollBox" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/chkRoll" android:layout_below="#+id/inReflex" android:layout_alignLeft="#+id/TextView05" android:layout_alignParentRight="true"></CheckBox>
</RelativeLayout>
</ScrollView>
And this is my alertdialog:
addBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//this block is to prep the layout for the alert dialog.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = inflater.inflate(R.layout.addchars, (ViewGroup) findViewById(R.id.scrollBox));
//taking this part out to try a better way.
new AlertDialog.Builder(initiative.this)
.setTitle("Add a Character")
.setView(addView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//ui controls
AutoCompleteTextView characterName = (AutoCompleteTextView) findViewById(R.id.inCharName);
EditText hp = (EditText) findViewById(R.id.inHP);
EditText tempHp = (EditText) findViewById(R.id.inTempHP);
EditText initRoll = (EditText) findViewById(R.id.inInitRoll);
EditText initBonus = (EditText) findViewById(R.id.inInitBonus);
EditText ac = (EditText) findViewById(R.id.inAC);
EditText fort = (EditText) findViewById(R.id.inFort);
EditText reflex = (EditText) findViewById(R.id.inReflex);
EditText will = (EditText) findViewById(R.id.inWill);
CheckBox rollInit = (CheckBox) findViewById(R.id.chkRoll);
//variables for various checks
entriesOk=false;
needToRoll=false;
if(characterName.getText().length() > 0)
{
holdCharacterName = characterName.getText().toString();
if(hp.getText().length() > 0)
{
holdHp = hp.getText().toString();
if(initBonus.getText().length() > 0)
{
holdInitBonus = initBonus.getText().toString();
if(ac.getText().length() > 0)
{
holdAc = ac.getText().toString();
if(fort.getText().length() > 0)
{
holdFort = fort.getText().toString();
if(reflex.getText().length() > 0)
{
holdReflex = reflex.getText().toString();
if(will.getText().length() > 0)
{
holdWill = will.getText().toString();
if(tempHp.getText().length() == 0)
{
holdTempHp = "0";
tempHp.setText("0");
}
else
{
holdTempHp = tempHp.getText().toString();
}
if(rollInit.isChecked())
{
entriesOk = true;
needToRoll = true;
}
else
{
if(initRoll.getText().length() > 0)
{
holdInitRoll = initRoll.getText().toString();
entriesOk = true;
}
}
}
}
}
}
}
}
}
btnLogic();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//do nothing
}
}).show();
addView.findViewById(R.id.inHP) instead of findViewById(R.id.inHP)
The problem is that you are calling addView.findViewById inside your onClick event, the onclick doesn't really happen until somebody clicks on it, I'll suggest that you make addView class member variable so that it can persist in memory. That'll solve your problem.

Categories

Resources