textview.setText isn't working in my code - java

All the setText methods below are not working. I could not understand why.
Can anyone help?
//imports
public class Main2Activity2 extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;
TextView P1Name;
TextView P2Name;
TextView P3Name;
TextView P4Name;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
setContentView(R.layout.activity_main2);
rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb1.setOnCheckedChangeListener(this);
rb2 = (RadioButton) findViewById(R.id.radioButton2);
rb2.setOnCheckedChangeListener(this);
rb3 = (RadioButton) findViewById(R.id.radioButton3);
rb3.setOnCheckedChangeListener(this);
rb4 = (RadioButton) findViewById(R.id.radioButton4);
rb4.setOnCheckedChangeListener(this);
return true;
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (buttonView.getId() == R.id.radioButton1) {
rb2.setChecked(false);
rb3.setChecked(false);
rb4.setChecked(false);
}
if (buttonView.getId() == R.id.radioButton2) {
rb1.setChecked(false);
rb3.setChecked(false);
rb4.setChecked(false);
}
if (buttonView.getId() == R.id.radioButton3) {
rb1.setChecked(false);
rb2.setChecked(false);
rb4.setChecked(false);
}
if (buttonView.getId() == R.id.radioButton4) {
rb1.setChecked(false);
rb2.setChecked(false);
rb3.setChecked(false);
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
P1Name = (TextView) findViewById(R.id.PlayerName1);
P2Name = (TextView) findViewById(R.id.PlayerName2);
P3Name = (TextView) findViewById(R.id.PlayerName3);
P4Name = (TextView) findViewById(R.id.PlayerName4);
Intent intent = getIntent();
String P1 = intent.getStringExtra("Player1Name");
String P2 = intent.getStringExtra("Player2Name");
String P3 = intent.getStringExtra("Player3Name");
String P4 = intent.getStringExtra("Player4Name");
P1Name.setText("hello");
Log.d("string content: ", P1Name.getText() + " <---- ");
P2Name.setText("working");
Log.d("string content: ", P2Name.getText() + " <---- ");
P3Name.setText(P3.toString());
Log.d("string content: ", P3Name.getText() + " <---- ");
P4Name.setText(P4);
}
}
Here is the 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:id="#+id/activity_main2"
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="katerji.malek.score.Main2Activity2">
<EditText
android:layout_width="85sp"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:ems="10"
android:id="#+id/editText3"
android:layout_below="#+id/tableLayout"
android:layout_alignLeft="#+id/tableLayout"
android:layout_alignStart="#+id/tableLayout"
android:textAlignment="center"
android:hint="score" />
<EditText
android:layout_width="85sp"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:ems="10"
android:id="#+id/editText"
android:layout_below="#+id/tableLayout"
android:layout_alignBaseline="#+id/editText3"
android:layout_alignBottom="#+id/editText3"
android:layout_toRightOf="#+id/editText3"
android:layout_toEndOf="#+id/editText3"
android:textAlignment="center"
android:hint="score" />
<EditText
android:layout_width="85sp"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:ems="10"
android:id="#+id/editText4"
android:layout_below="#+id/tableLayout"
android:layout_alignBaseline="#+id/editText"
android:layout_alignBottom="#+id/editText"
android:layout_toRightOf="#+id/editText"
android:layout_toEndOf="#+id/editText"
android:textAlignment="center"
android:hint="score" />
<EditText
android:layout_width="85sp"
android:layout_height="wrap_content"
android:inputType="numberSigned"
android:ems="10"
android:id="#+id/editText5"
android:layout_below="#+id/tableLayout"
android:textAlignment="center"
android:hint="score"
android:layout_alignBaseline="#+id/editText4"
android:layout_alignBottom="#+id/editText4"
android:layout_toRightOf="#+id/editText4"
android:layout_toEndOf="#+id/editText4" />
<RadioButton
android:layout_width="40dp"
android:layout_height="40sp"
android:id="#+id/radioButton1"
android:layout_below="#+id/editText"
android:layout_alignLeft="#+id/editText3"
android:layout_alignStart="#+id/editText3"
android:checked="false" />
<Button
android:layout_height="40sp"
android:id="#+id/button"
android:layout_below="#+id/editText3"
android:layout_toRightOf="#+id/radioButton1"
android:text="\\/"
android:layout_width="45sp" />
<RadioButton
android:layout_width="40dp"
android:layout_height="40sp"
android:id="#+id/radioButton2"
android:textAlignment="center"
android:layout_below="#+id/editText"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button" />
<Button
android:layout_height="40sp"
android:id="#+id/button4"
android:text="\\/"
android:layout_width="45sp"
android:layout_below="#+id/editText"
android:layout_toRightOf="#+id/radioButton2"
android:layout_toEndOf="#+id/radioButton2" />
<RadioButton
android:layout_width="40dp"
android:layout_height="40sp"
android:id="#+id/radioButton3"
android:textAlignment="center"
android:layout_below="#+id/editText4"
android:layout_toRightOf="#+id/editText"
android:layout_toEndOf="#+id/editText" />
<Button
android:layout_height="40sp"
android:id="#+id/button6"
android:text="\\/"
android:layout_width="45sp"
android:layout_below="#+id/editText4"
android:layout_toRightOf="#+id/radioButton3"
android:layout_toEndOf="#+id/radioButton3" />
<RadioButton
android:layout_width="30dp"
android:layout_height="40sp"
android:id="#+id/radioButton4"
android:textAlignment="center"
android:layout_below="#+id/editText5"
android:layout_alignLeft="#+id/editText5"
android:layout_alignStart="#+id/editText5" />
<Button
android:layout_height="40sp"
android:id="#+id/button7"
android:text="\\/"
android:layout_width="45sp"
android:layout_below="#+id/editText5"
android:layout_toRightOf="#+id/radioButton4"
android:layout_toEndOf="#+id/radioButton4" />
<Button
android:text="Sajjil"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:id="#+id/button8"
android:layout_width="200sp"
android:layout_below="#+id/button"
android:layout_centerHorizontal="true" />
<Button
android:text="New Game"
android:layout_width="200sp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:id="#+id/button10"
android:layout_below="#+id/button8"
android:layout_alignLeft="#+id/button8"
android:layout_alignStart="#+id/button8" />
<TableLayout
android:layout_height="wrap_content"
android:layout_width="340dp"
android:id="#+id/tableLayout"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView13"
android:textAlignment="center" />
<TextView
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView12"
android:textAlignment="center" />
<TextView
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView11"
android:textAlignment="center" />
<TextView
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView10"
android:textAlignment="center" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:text="Player 1"
android:layout_height="wrap_content"
android:id="#+id/PlayerName1"
android:layout_width="85sp"
android:textAlignment="center" />
<TextView
android:text="Player2"
android:layout_height="wrap_content"
android:id="#+id/PlayerName2"
android:layout_width="85sp"
android:textAlignment="center" />
<TextView
android:text="Player3"
android:layout_height="wrap_content"
android:id="#+id/PlayerName3"
android:layout_width="85sp"
android:textAlignment="center" />
<TextView
android:text="Player4"
android:layout_height="wrap_content"
android:id="#+id/PlayerName4"
android:layout_width="85sp"
android:textAlignment="center" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>

Maybe try declaring your String variables with a toString on the end like below:
P3Name.setText(P3.toString());

I deleted the implements declaration CompoundButton.OnCheckedChangeListener
and the 2 methods: nCreateOptionsMenu and onCheckedChanged.
This is the working code:
public class Main2Activity2 extends AppCompatActivity {
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
RadioButton rb4;
TextView P1Name;
TextView P2Name;
TextView P3Name;
TextView P4Name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
P1Name = (TextView) findViewById(R.id.PlayerName1);
P2Name = (TextView) findViewById(R.id.PlayerName2);
P3Name = (TextView) findViewById(R.id.PlayerName3);
P4Name = (TextView) findViewById(R.id.PlayerName4);
Intent intent = getIntent();
String P1 = intent.getStringExtra("Player1Name");
String P2 = intent.getStringExtra("Player2Name");
String P3 = intent.getStringExtra("Player3Name");
String P4 = intent.getStringExtra("Player4Name");
P1Name.setText(P1);
P2Name.setText(P2);
P3Name.setText(P3);
P4Name.setText(P4);
}
}

Related

How to calculate score using Radio buttons in Android? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have been developing a form which uses Radio buttons and has a list of questions.
When the user clicks on Yes radio button for every question, I want to increment the score by 20. If the user clicks the No radio button, the score should be 0.
Take a look at the code.
This is the Activity class
CoughTest.java
public class CoughTest extends AppCompatActivity {
int score= 0;
RadioGroup rg1,rg2,rg3,rg4,rg5;
RadioButton rb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cough_test);
rg1 = (RadioGroup) findViewById(R.id.rgroup1);
rg2 = (RadioGroup) findViewById(R.id.rgroup2);
rg3 = (RadioGroup) findViewById(R.id.rgroup3);
rg4 = (RadioGroup) findViewById(R.id.rgroup4);
rg5 = (RadioGroup) findViewById(R.id.rgroup5);
}
public void rbClicked(View view) {
int radioButtonId = rg1.getCheckedRadioButtonId();
rb =(RadioButton) findViewById(radioButtonId);
boolean checked = ((RadioButton) view).isChecked();
switch (view.getId()){
case R.id.yes1:
score = score + 20;
break;
case R.id.no1:
score = 0;
break;
case R.id.yes2:
score = score + 20;
break;
case R.id.no2:
score = 0;
break;
case R.id.yes3:
score = score + 20;
break;
case R.id.no3:
score = 0;
break;
case R.id.yes4:
score = score + 20;
break;
case R.id.no4:
score = 0;
break;
case R.id.yes5:
score = score + 20;
break;
case R.id.no5:
score = 0;
break;
}
}
}
This is the XML file
activity_cough_test.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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=".CoughTest">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="Do you have a cough?"
android:textColor="#100D40"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rgroup1">
<RadioButton
android:id="#+id/yes1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:onClick="rbClicked"
android:text="Yes"
android:textColor="#100D40" />
<RadioButton
android:id="#+id/no1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_bold"
android:layout_marginLeft="10dp"
android:onClick="rbClicked"
android:text="No"
android:textColor="#100D40" />
</RadioGroup>
<TextView
android:id="#+id/soar_throat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="Do you have a soar throat?"
android:textColor="#100D40"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rgroup2">
<RadioButton
android:id="#+id/yes2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:onClick="rbClicked"
android:text="Yes"
android:textColor="#100D40" />
<RadioButton
android:id="#+id/no2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_bold"
android:layout_marginLeft="10dp"
android:onClick="rbClicked"
android:text="No"
android:textColor="#100D40" />
</RadioGroup>
<TextView
android:id="#+id/fever"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="Do you have fever?"
android:textColor="#100D40"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rgroup3">
<RadioButton
android:id="#+id/yes3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:onClick="rbClicked"
android:text="Yes"
android:textColor="#100D40" />
<RadioButton
android:id="#+id/no3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_bold"
android:layout_marginLeft="10dp"
android:onClick="rbClicked"
android:text="No"
android:textColor="#100D40" />
</RadioGroup>
<TextView
android:id="#+id/tiredness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="Do you have tiredness?"
android:textColor="#100D40"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rgroup4">
<RadioButton
android:id="#+id/yes4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:onClick="rbClicked"
android:text="Yes"
android:textColor="#100D40" />
<RadioButton
android:id="#+id/no4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_bold"
android:layout_marginLeft="10dp"
android:onClick="rbClicked"
android:text="No"
android:textColor="#100D40" />
</RadioGroup>
<TextView
android:id="#+id/breathing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="Do you have difficulty in breathing?"
android:textColor="#100D40"
android:textSize="20sp" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/rgroup5">
<RadioButton
android:id="#+id/yes5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:onClick="rbClicked"
android:text="Yes"
android:textColor="#100D40" />
<RadioButton
android:id="#+id/no5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/roboto_bold"
android:layout_marginLeft="10dp"
android:onClick="rbClicked"
android:text="No"
android:textColor="#100D40" />
</RadioGroup>
</LinearLayout>
</ScrollView>
I will suggest this method:
CoughTest.java:
public class CoughTest extends AppCompatActivity {
private Button btnSubmit, btnReset;
private TextView finalScore;
private RadioButton cough, sourThroat, fever, tiredness, breathing;
private int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cough_test);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnReset = (Button) findViewById(R.id.btnReset);
finalScore = (TextView) findViewById(R.id.textFinalScore);
cough = (RadioButton) findViewById(R.id.cough);
sourThroat = (RadioButton) findViewById(R.id.sour_throat);
fever = (RadioButton) findViewById(R.id.fever);
tiredness = (RadioButton) findViewById(R.id.tiredness);
breathing = (RadioButton) findViewById(R.id.breathingDifficulty);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (cough.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (sourThroat.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (fever.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (tiredness.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
if (breathing.isChecked()) {
score = score + 20;
} else {
score = score + 0;
}
//Finally show your score.
Toast.makeText(getApplicationContext(), "Score is: " + score, Toast.LENGTH_SHORT).show();
}
});
//Resetting everything
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
score = 0;
cough.setChecked(false);
fever.setChecked(false);
breathing.setChecked(false);
tiredness.setChecked(false);
sourThroat.setChecked(false);
}
});
}
}
Coming back to your XML: I modified it.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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=".CoughTest">
<ScrollView 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=".CoughTest">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="Please check (✓) if you show the symptom."
android:textColor="#100D40"
android:textSize="16sp" />
<RadioButton
android:id="#+id/cough"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="cough"
android:textColor="#100D40" />
<RadioButton
android:id="#+id/sour_throat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="Sour Throat"
android:textColor="#100D40" />
<RadioButton
android:id="#+id/fever"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="Fever"
android:textColor="#100D40" />
<RadioButton
android:id="#+id/tiredness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="tiredness"
android:textColor="#100D40" />
<RadioButton
android:id="#+id/breathingDifficulty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginLeft="10dp"
android:fontFamily="#font/roboto_bold"
android:text="Breathing Difficulty"
android:textColor="#100D40" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:id="#+id/btnSubmit"
android:layout_marginStart="50sp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_marginTop="5sp"
android:id="#+id/btnReset"
android:layout_marginStart="50sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20sp"
android:layout_marginStart="20sp"
android:hint="Your Score Here"
android:textColor="#000000"
android:id="#+id/textFinalScore"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I have removed the radio groups. Just prompt your user to click it when they agree.
Means if their answer is YES, then please check the radio button else leave it unchecked.
Hope that helps.
By the way this is how it works:
just set score into your textView after your switch case :
yourTextView = String.ValueOf(score);

EditText's not changing on ImageButton press?

I am very new to coding so there could be all sorts of issues with this but after doing a fair bit of searching, I can't find whats wrong with it, I don't get any errors or crashes, the buttons just do nothing?
The aim is to have the orange arrows increase and decrease the number in the middle by 1.
App Screenshot:
I don't know what code to show you so here is most of it:
T3_TrampetFragment.java
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class T3_TrampetFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.t3_trampetlayout, container, false);
}
}
t3_trampetlayout.xml
<RelativeLayout 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="wrap_content"
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=".T3_TrampetFragment">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:id="#+id/tblrow_title"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Tariff Calculator"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="36sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tblrow_rotations"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/txt_rotations"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Rotations"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
<ImageButton
android:id="#+id/btn_r_minus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true"
android:rotation="180" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/Rnum"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:focusable="false"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/sRnum"
android:textAlignment="center"
android:textColor="?attr/colorControlNormal"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/btn_r_plus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tblrow_pike"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/txt_pike"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Pike Bonus"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
<ImageButton
android:id="#+id/btn_p_minus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true"
android:rotation="180" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/Pnum"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:focusable="false"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/sPnum"
android:textAlignment="center"
android:textColor="?attr/colorControlNormal"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/btn_p_plus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tblrow_straight"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/txt_straight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Straight Bonus"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
<ImageButton
android:id="#+id/btn_s_minus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true"
android:rotation="180" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/Snum"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:focusable="false"
android:inputType="number"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/sSnum"
android:textAlignment="center"
android:textColor="?attr/colorControlNormal"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/btn_s_plus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tblrow_twist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/txt_twist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Twists"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="24sp" />
<ImageButton
android:id="#+id/btn_t_minus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true"
android:rotation="180" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/Tnum"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:focusable="false"
android:inputType="numberDecimal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/sTnum"
android:textAlignment="center"
android:textColor="?attr/colorControlNormal"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="10dp"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/btn_t_plus"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:backgroundTint="?android:attr/colorBackground"
android:clickable="true"
android:foreground="#android:drawable/ic_media_play"
android:foregroundTint="#color/colorSecondary"
android:hapticFeedbackEnabled="true" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
</TableRow>
<TableRow
android:id="#+id/tblrow_result"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingTop="20dp">
<TextView
android:id="#+id/txt_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Difficulty"
android:textAlignment="center"
android:textColor="#color/colorPrimary"
android:textSize="24sp"
android:textStyle="bold" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/_space"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:rotation="180"
android:visibility="invisible"
app:backgroundTint="#color/colorSecondary"
app:fabSize="mini"
app:srcCompat="#android:drawable/ic_media_play" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/Resultnum"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:background="?android:attr/colorBackground"
android:focusable="false"
android:inputType="numberDecimal"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="#string/sResultnum"
android:textAlignment="center"
android:textColor="?attr/colorControlNormal"
android:textSize="24sp"
android:textStyle="bold" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/_space2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:visibility="invisible"
app:backgroundTint="#color/colorSecondary"
app:fabSize="mini"
app:srcCompat="#android:drawable/ic_media_play" />
<Space
android:layout_width="20dp"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
</RelativeLayout>
T3_TrampetActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
public class T3_TrampetActivity extends Activity implements View.OnClickListener {
ImageButton BtnRMinus, BtnRPlus, BtnPMinus, BtnPPlus, BtnSMinus, BtnSPlus, BtnTMinus, BtnTPlus;
EditText R_num, P_num, S_num, T_num, Result_num;
int R_counter = (1);
int P_counter, S_counter = (0);
double T_counter = (0.0);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.t3_trampetlayout);
R_num = (EditText) findViewById(R.id.Rnum);
P_num = (EditText) findViewById(R.id.Pnum);
S_num = (EditText) findViewById(R.id.Snum);
T_num = (EditText) findViewById(R.id.Tnum);
Result_num = (EditText) findViewById(R.id.Resultnum);
BtnRMinus = (ImageButton) findViewById(R.id.btn_r_minus);
BtnRPlus = (ImageButton) findViewById(R.id.btn_r_plus);
BtnPMinus = (ImageButton) findViewById(R.id.btn_p_minus);
BtnPPlus = (ImageButton) findViewById(R.id.btn_p_plus);
BtnSMinus = (ImageButton) findViewById(R.id.btn_s_minus);
BtnSPlus = (ImageButton) findViewById(R.id.btn_s_plus);
BtnTMinus = (ImageButton) findViewById(R.id.btn_t_minus);
BtnTPlus = (ImageButton) findViewById(R.id.btn_t_plus);
BtnRMinus.setOnClickListener(this);
BtnRPlus.setOnClickListener(this);
BtnPMinus.setOnClickListener(this);
BtnPPlus.setOnClickListener(this);
BtnSMinus.setOnClickListener(this);
BtnSPlus.setOnClickListener(this);
BtnTMinus.setOnClickListener(this);
BtnTPlus.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_r_minus:
if (R_counter > 1) {
--R_counter;
R_num.setText(String.valueOf(R_counter));
}
break;
case R.id.btn_r_plus:
if (R_counter < 3) {
++R_counter;
R_num.setText(String.valueOf(R_counter));
}
break;
case R.id.btn_p_minus:
if (P_counter > 0) {
--P_counter;
P_num.setText(String.valueOf(P_counter));
}
break;
case R.id.btn_p_plus:
if (P_counter < 3) {
++P_counter;
P_num.setText(String.valueOf(P_counter));
}
break;
case R.id.btn_s_minus:
if (S_counter > 0) {
--S_counter;
S_num.setText(String.valueOf(S_counter));
}
break;
case R.id.btn_s_plus:
if (S_counter < 3) {
++S_counter;
S_num.setText(String.valueOf(S_counter));
}
break;
case R.id.btn_t_minus:
if (T_counter > 0) {
T_counter = (T_counter - 0.5);
T_num.setText(String.valueOf(T_counter));
}
break;
case R.id.btn_t_plus:
if (T_counter < 5.5) {
T_counter = (T_counter + 0.5);
T_num.setText(String.valueOf(T_counter));
}
break;
default:
break;
}
}
}
strings.xml
<resources>
<string name="app_name">TG Mobile</string>
<string name="in_app_name">TeamGym Mobile</string>
<string name="sRnum">1</string>
<string name="sPnum">0</string>
<string name="sSnum">0</string>
<string name="sTnum">0.0</string>
<string name="sResultnum">0.00</string>
</resources>
Create T3_TrampetActivity like this And Call Your Fragment from Activity:
public class T3_TrampetActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new T3_TrampetFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_frame, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
}
Create activity_main.xml as like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Update T3_TrampetFragment.java
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class T3_TrampetFragment extends Fragment implements View.OnClickListener {
ImageButton BtnRMinus, BtnRPlus, BtnPMinus, BtnPPlus, BtnSMinus, BtnSPlus, BtnTMinus, BtnTPlus;
EditText R_num, P_num, S_num, T_num, Result_num;
int R_counter = (1);
int P_counter, S_counter = (0);
double T_counter = (0.0);
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.t3_trampetlayout, container, false);
R_num = (EditText) view.findViewById(R.id.Rnum);
P_num = (EditText) view.findViewById(R.id.Pnum);
S_num = (EditText) view.findViewById(R.id.Snum);
T_num = (EditText) view.findViewById(R.id.Tnum);
Result_num = (EditText) view.findViewById(R.id.Resultnum);
BtnRMinus = (ImageButton) view.findViewById(R.id.btn_r_minus);
BtnRPlus = (ImageButton)view.findViewById(R.id.btn_r_plus);
BtnPMinus = (ImageButton) view.findViewById(R.id.btn_p_minus);
BtnPPlus = (ImageButton) view.findViewById(R.id.btn_p_plus);
BtnSMinus = (ImageButton) view.findViewById(R.id.btn_s_minus);
BtnSPlus = (ImageButton) view.findViewById(R.id.btn_s_plus);
BtnTMinus = (ImageButton) view.findViewById(R.id.btn_t_minus);
BtnTPlus = (ImageButton) view.findViewById(R.id.btn_t_plus);
BtnRMinus.setOnClickListener(this);
BtnRPlus.setOnClickListener(this);
BtnPMinus.setOnClickListener(this);
BtnPPlus.setOnClickListener(this);
BtnSMinus.setOnClickListener(this);
BtnSPlus.setOnClickListener(this);
BtnTMinus.setOnClickListener(this);
BtnTPlus.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_r_minus:
if (R_counter > 1) {
--R_counter;
R_num.setText(String.valueOf(R_counter));
}
break;
case R.id.btn_r_plus:
if (R_counter < 3) {
++R_counter;
R_num.setText(String.valueOf(R_counter));
}
break;
case R.id.btn_p_minus:
if (P_counter > 0) {
--P_counter;
P_num.setText(String.valueOf(P_counter));
}
break;
case R.id.btn_p_plus:
if (P_counter < 3) {
++P_counter;
P_num.setText(String.valueOf(P_counter));
}
break;
case R.id.btn_s_minus:
if (S_counter > 0) {
--S_counter;
S_num.setText(String.valueOf(S_counter));
}
break;
case R.id.btn_s_plus:
if (S_counter < 3) {
++S_counter;
S_num.setText(String.valueOf(S_counter));
}
break;
case R.id.btn_t_minus:
if (T_counter > 0) {
T_counter = (T_counter - 0.5);
T_num.setText(String.valueOf(T_counter));
}
break;
case R.id.btn_t_plus:
if (T_counter < 5.5) {
T_counter = (T_counter + 0.5);
T_num.setText(String.valueOf(T_counter));
}
break;
default:
break;
}
}
}
t3_trampetlayout.xml Use same layou for Fragment.

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

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.

NullPointer exception when using getText().toString().trim();

I have the following class:
public class SHConfigureContactFragment extends Fragment{
private EditText name;
private EditText description;
private EditText primaryNumber;
private EditText secondaryNumber;
private EditText email;
private EditText skype;
private Byte[] photo;
private Boolean isDualPane;
private FragmentManager fragmentManager;
private SHContactMenuFragment menuFragment;
private SHConfigureContactFragment contactFragment;
private DatabaseControllerLibrary controller;
private Contact contact;
private SHPatient patient;
private int patientId;
public View rootView;
public int selectedIndex;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null) {
this.selectedIndex = savedInstanceState.getInt("SELECTED_INDEX");
this.contact = (Contact) savedInstanceState.getSerializable("CONTACT");
}
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("CONTACT", contact);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
controller = SHController.getInstance(getActivity());
patient = ((SHController) controller).getPatient();
patientId = patient.getId();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
rootView = inflater.inflate(R.layout.sh_fragment_contact_edit, container, false);
Button button = (Button) rootView.findViewById(R.id.addContactButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
boolean isValid = true;
// assigning IDs based on how many contacts are created. no reused IDs son!
int id = controller.getContactsCount();
id++;
String newName = name.getText().toString().trim();
String newDescription = description.getText().toString().trim();
String newPrimaryNumber = primaryNumber.getText().toString().trim();
String newSecondaryNumber = secondaryNumber.getText().toString().trim();
String newEmail = email.getText().toString().trim();
String newSkype = skype.getText().toString().trim();
if(newName.length()==0 || newName.matches(".*\\d.*")) {
name.setText("");
isValid = false;
}
if(newPrimaryNumber.length()==0) {
primaryNumber.setText("");
isValid = false;
}
if (isValid){
Contact updatedContact;
ContactConfiguration updatedConfiguration;
if (contact != null){
id = contact.getId();
}
updatedContact = new Contact(id, newName, newDescription, newPrimaryNumber, newSecondaryNumber, newEmail, newSkype, null);
updatedConfiguration = new ContactConfiguration(patientId, patientId, false, updatedContact);
if (contact == null){
controller.addContact(updatedContact, patientId);
}
else{
controller.updateContact(updatedContact, patientId);
}
}
}
});
return rootView;
}
private void setupTextFieldsByContact(Contact contact) {
name = (EditText) rootView.findViewById(R.id.editContactName);
description = (EditText) rootView.findViewById(R.id.editContactDescription);
primaryNumber = (EditText) rootView.findViewById(R.id.editContactPrimaryNumber);
secondaryNumber = (EditText) rootView.findViewById(R.id.editContactSecondaryNumber);
email = (EditText) rootView.findViewById(R.id.editContactEmail);
skype = (EditText) rootView.findViewById(R.id.editContactSkype);
if (contact != null) {
name.setText(contact.getName());
description.setText(contact.getDescription());
primaryNumber.setText(contact.getPrimaryNumber());
secondaryNumber.setText(contact.getSecondaryNumber());
email.setText(contact.getEmail());
skype.setText(contact.getSkype());
}
}
And I have the corresponding XML file for its layout:
<?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" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="Name"
android:textColor="#color/action_bar"
android:textSize="25sp" />
<EditText
android:id="#+id/editContactName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/border"
android:ems="10"
android:hint="Name"
android:imeOptions="actionNext"
android:inputType="textPersonName"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="Description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/action_bar"
android:textSize="25sp" />
<EditText
android:id="#+id/editContactDescription"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/border"
android:ems="10"
android:hint="Description"
android:imeOptions="actionNext"
android:inputType="textShortMessage"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
<TextView
android:id="#+id/numbers"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="15dp"
android:text="Primary and Secondary Numbers"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/action_bar"
android:textSize="25sp" />
<RelativeLayout
android:id="#+id/NumberFields"
android:layout_width="fill_parent"
android:layout_height="50dp" >
<EditText
android:id="#+id/editContactPrimaryNumber"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_marginLeft="35dp"
android:layout_weight="2"
android:background="#drawable/border"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:hint="Primary"
android:inputType="phone"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
<EditText
android:id="#+id/editContactSecondaryNumber"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:layout_weight="1"
android:background="#drawable/border"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:hint="Secondary"
android:inputType="phone"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
</RelativeLayout>
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="Email"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/action_bar"
android:textSize="25sp" />
<EditText
android:id="#+id/editContactEmail"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/border"
android:ems="10"
android:hint="Email"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
<TextView
android:id="#+id/skype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="Skype ID"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/action_bar"
android:textSize="25sp" />
<EditText
android:id="#+id/editContactSkype"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/border"
android:ems="10"
android:hint="Skype ID"
android:imeOptions="actionNext"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
<Button
android:id="#+id/addContactButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/sh_button_selector"
android:padding="#dimen/padding_medium"
android:text="Save Contact"
android:textColor="#color/text_white"
android:textSize="#dimen/text_size_take_survey_button"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Now when I run my app I get a NullPointerException within the onCreateView() within the onClick at the line:
String newName = name.getText().toString().trim();
Now I have tried printing out the variables, debugging but I just cannot seem to figure out why it is getting a NullPointerException. It may be something simple that I'm overlooking, I've just been looking at it for so long I may just be going crazy.
I think your EditText 'name' is not initialized.
You have not initialized those EditText, so you are getting NPE. initialize those components first ..From your code call this method setupTextFieldsByContact() in onCreate it will solve your problem.
Of course you're getting a NullPointerException...you never initialized your EditTexts in the onCreate method. Add this to your onCreate:
name = (EditText) findViewById(R.id.editContactName);
description = (EditText) findViewById(R.id.editContactDescription);
primaryNumber = (EditText) findViewById(R.id.editContactPrimaryNumber);
secondaryNumber = (EditText) findViewById(R.id.editContactSecondaryNumber);
email = (EditText) findViewById(R.id.editContactEmail);
skype = (EditText) findViewById(R.id.editContactSkype);
That way you'll be calling methods on objects that actually exist.
your EditText 'name' is not initialized. you have to initialized your EditText first

bring checked check box values to other pages

i am currently developing a mobile application using eclipse. right now o am facing problem for bringing checked check box to new page...
this is code for the check box page...
page name : mcpakej1.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"
android:layout_centerHorizontal="true"
tools:context="com.example.mobilecatering.MainActivity" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="#+id/pakejA1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
<CheckBox
android:id="#+id/pakejA2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nasiputih1"
android:layout_below="#+id/nasiputih1"
android:text="Nasi Beriyani" />
<CheckBox
android:id="#+id/pakejA3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/nasiberiyani1"
android:layout_below="#+id/nasiberiyani1"
android:text="Nasi Minyak" />
<CheckBox
android:id="#+id/pakejA4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:text="Ayam Masak Kurma" />
<CheckBox
android:id="#+id/pakejA5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ayamkurma1"
android:layout_below="#+id/ayamkurma1"
android:text="Ayam Masak Lemak" />
<CheckBox
android:id="#+id/pakejA6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ayamlemak1"
android:layout_below="#+id/ayamlemak1"
android:text="Ayam Kari" />
<CheckBox
android:id="#+id/pakejA7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ayamkari1"
android:layout_below="#+id/ayamkari1"
android:text="Ayam Rendang" />
<CheckBox
android:id="#+id/pakejA8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ayamrendang1"
android:layout_below="#+id/ayamrendang1"
android:text="Ayam Masak Merah" />
<CheckBox
android:id="#+id/pakejA9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4"
android:text="Perut Air Asam" />
<CheckBox
android:id="#+id/pakejA10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/perutairasam1"
android:layout_below="#+id/perutairasam1"
android:text="Daging Masak Tomato" />
<CheckBox
android:id="#+id/pakejA11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/dagingtomato1"
android:layout_below="#+id/dagingtomato1"
android:text="Daging Masak Singgang" />
<CheckBox
android:id="#+id/pakejA12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/dagingsinggang1"
android:layout_below="#+id/dagingsinggang1"
android:text="Daging Masak Merah" />
<CheckBox
android:id="#+id/pakejA13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/textView5"
android:text="Kerabu Taugeh" />
<CheckBox
android:id="#+id/pakejA14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/kerabutaugeh1"
android:layout_below="#+id/kerabutaugeh1"
android:text="Pindang Kacang Panjang" />
<CheckBox
android:id="#+id/pakejA15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pindangkacang1"
android:layout_below="#+id/pindangkacang1"
android:text="Jelatah" />
<CheckBox
android:id="#+id/pakejA16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/jelatah1"
android:layout_below="#+id/jelatah1"
android:text="Dalca Sayur" />
<CheckBox
android:id="#+id/pakejA17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/dalcasayur1"
android:layout_below="#+id/dalcasayur1"
android:text="Papadom" />
<CheckBox
android:id="#+id/pakejA18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/textView6"
android:text="Kari Ikan" />
<CheckBox
android:id="#+id/pakejA19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/pakejA18"
android:layout_below="#+id/kariikan1"
android:text="Kari Ikan Masin dan Nenas" />
<CheckBox
android:id="#+id/pakejA20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/textView7"
android:text="Sirap Ais" />
<CheckBox
android:id="#+id/pakejA21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/sirapais1"
android:layout_below="#+id/sirapais1"
android:text="Oren" />
<CheckBox
android:id="#+id/pakejA22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pakejA21"
android:layout_below="#+id/pakejA21"
android:text="Teh Tarik" />
<CheckBox
android:id="#+id/pakejA23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:layout_below="#+id/textView8"
android:text="Agar - Agar" />
<CheckBox
android:id="#+id/pakejA24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/agaragar1"
android:layout_below="#+id/agaragar1"
android:text="Bubur Kacang Hijau" />
<CheckBox
android:id="#+id/pakejA25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buburkacanghijau1"
android:layout_below="#+id/buburkacanghijau1"
android:text="Kuih Muih" />
<ImageButton
android:id="#+id/gobutton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignTop="#+id/homebtn"
android:layout_toRightOf="#+id/homebtn"
android:background="#drawable/gobutton"
android:onClick="goReceipt" />
</LinearLayout>
</ScrollView></RelativeLayout>`
this is the process in java.
page name : mcpakej1.java
`package com.example.mobilecatering;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
public class mcpakej1 extends Activity
{
CheckBox nsPutih,nsBeriyani,nsMinyak,aymKurma,aymLemak,aymKari,aymRendang,aymMerah,prtAsam, dggTomato, dggSinggang, dggMerah,kerabuTaugeh, pindangKacang,jelatah,dalcaSayur,papadom,kariIkan,ikanMasin, sirapAis, oren,tehTarik,agarAgar, buburKacang,kuih;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mcpakej1);
nsPutih = (CheckBox) findViewById(R.id.pakejA1);
nsBeriyani = (CheckBox) findViewById(R.id.pakejA2);
nsMinyak = (CheckBox) findViewById(R.id.pakejA3);
aymKurma = (CheckBox) findViewById(R.id.pakejA4);
aymLemak = (CheckBox) findViewById(R.id.pakejA5);
aymKari = (CheckBox) findViewById(R.id.pakejA6);
aymRendang = (CheckBox) findViewById(R.id.pakejA7);
aymMerah = (CheckBox) findViewById(R.id.pakejA8);
prtAsam = (CheckBox) findViewById(R.id.pakejA9);
dggTomato = (CheckBox) findViewById(R.id.pakejA10);
dggSinggang = (CheckBox) findViewById(R.id.pakejA11);
dggMerah = (CheckBox) findViewById(R.id.pakejA12);
kerabuTaugeh = (CheckBox) findViewById(R.id.pakejA13);
pindangKacang = (CheckBox) findViewById(R.id.pakejA14);
jelatah = (CheckBox) findViewById(R.id.pakejA15);
dalcaSayur = (CheckBox) findViewById(R.id.pakejA16);
papadom = (CheckBox) findViewById(R.id.pakejA17);
kariIkan = (CheckBox) findViewById(R.id.pakejA18);
ikanMasin = (CheckBox) findViewById(R.id.pakejA19);
sirapAis = (CheckBox) findViewById(R.id.pakejA20);
oren = (CheckBox) findViewById(R.id.pakejA21);
tehTarik = (CheckBox) findViewById(R.id.pakejA22);
agarAgar = (CheckBox) findViewById(R.id.pakejA23);
buburKacang = (CheckBox) findViewById(R.id.pakejA24);
kuih = (CheckBox) findViewById(R.id.pakejA25);
}
public void goHome(View v){
{
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(intent,0);}
}
public void goReceipt(View v)
{
Intent intent = new Intent(v.getContext(), doReceipt.class);
intent.putExtra("nsBeriyani", nsBeriyani.isChecked());
intent.putExtra("nsPutih", nsPutih.isChecked());
intent.putExtra("nsMinyak", nsMinyak.isChecked());
intent.putExtra("aymKurma", aymKurma.isChecked());
intent.putExtra("aymKari", aymKari.isChecked());
intent.putExtra("aymRendang", aymRendang.isChecked());
intent.putExtra("aymMerah", aymMerah.isChecked());
intent.putExtra("prtAsam", prtAsam.isChecked());
intent.putExtra("dggTomato", dggTomato.isChecked());
intent.putExtra("dggSinggang", dggSinggang.isChecked());
intent.putExtra("dggMerah", dggMerah.isChecked());
intent.putExtra("kerabuTaugeh", kerabuTaugeh.isChecked());
intent.putExtra("pindangKacang", pindangKacang.isChecked());
intent.putExtra("jelatah", jelatah.isChecked());
intent.putExtra("dalcaSayur", dalcaSayur.isChecked());
intent.putExtra("papadom", papadom.isChecked());
intent.putExtra("kariIkan", kariIkan.isChecked());
intent.putExtra("ikanMasin", ikanMasin.isChecked());
intent.putExtra("sirapAis", sirapAis.isChecked());
intent.putExtra("oren", oren.isChecked());
intent.putExtra("tehTarik", tehTarik.isChecked());
intent.putExtra("agarAgar", agarAgar.isChecked());
intent.putExtra("buburKacang", buburKacang.isChecked());
intent.putExtra("kuih", kuih.isChecked());
startActivityForResult(intent,0);
}
}`
after user click button id goButton, this page will do this process.
page name = doReceipt.java
package com.example.mobilecatering;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class doReceipt extends Activity
{
boolean nsPutih, nsBeriyani,nsMinyak,aymKurma,aymLemak,aymKari,aymRendang,aymMerah,prtAsam, dggTomato, dggSinggang, dggMerah,kerabuTaugeh, pindangKacang,jelatah,dalcaSayur,papadom,kariIkan,ikanMasin, sirapAis, oren,tehTarik,agarAgar, buburKacang,kuih;
TextView tvOutput;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.receipt);
tvOutput = (TextView) findViewById(R.id.textView1);
tvOutput = (TextView) findViewById(R.id.textView2);
tvOutput = (TextView) findViewById(R.id.textView3);
tvOutput = (TextView) findViewById(R.id.textView4);
tvOutput = (TextView) findViewById(R.id.textView5);
tvOutput = (TextView) findViewById(R.id.textView6);
tvOutput = (TextView) findViewById(R.id.textView7);
tvOutput = (TextView) findViewById(R.id.textView8);
tvOutput = (TextView) findViewById(R.id.textView9);
tvOutput = (TextView) findViewById(R.id.textView10);
tvOutput = (TextView) findViewById(R.id.textView11);
tvOutput = (TextView) findViewById(R.id.textView12);
tvOutput = (TextView) findViewById(R.id.textView13);
tvOutput = (TextView) findViewById(R.id.textView14);
tvOutput = (TextView) findViewById(R.id.textView15);
tvOutput = (TextView) findViewById(R.id.textView16);
tvOutput = (TextView) findViewById(R.id.textView17);
tvOutput = (TextView) findViewById(R.id.textView18);
tvOutput = (TextView) findViewById(R.id.textView19);
tvOutput = (TextView) findViewById(R.id.textView20);
tvOutput = (TextView) findViewById(R.id.textView21);
tvOutput = (TextView) findViewById(R.id.textView22);
tvOutput = (TextView) findViewById(R.id.textView23);
tvOutput = (TextView) findViewById(R.id.textView24);
tvOutput = (TextView) findViewById(R.id.textView25);
Bundle data = this.getIntent().getExtras();
nsPutih=data.getBoolean("nsPutih");
nsBeriyani=data.getBoolean("nsBeriyani");
nsMinyak=data.getBoolean("nsMinyak");
aymKurma=data.getBoolean("aymKurma");
aymLemak=data.getBoolean("aymLemak");
aymKari=data.getBoolean("aymKari");
aymRendang=data.getBoolean("aymRendang");
aymMerah=data.getBoolean("aymMerah");
prtAsam=data.getBoolean("prtAsam");
dggTomato=data.getBoolean("dggTomato");
dggSinggang=data.getBoolean("dggSinggang");
dggMerah=data.getBoolean("dggMerah");
kerabuTaugeh=data.getBoolean("kerabuTaugeh");
pindangKacang=data.getBoolean("pindangKacang");
jelatah=data.getBoolean("jelatah");
dalcaSayur=data.getBoolean("dalcaSayur");
papadom=data.getBoolean("papadom");
kariIkan=data.getBoolean("kariIkan");
ikanMasin=data.getBoolean("ikanMasin");
sirapAis=data.getBoolean("sirapAis");
oren=data.getBoolean("oren");
tehTarik=data.getBoolean("tehTarik");
agarAgar=data.getBoolean("agarAgar");
buburKacang=data.getBoolean("buburKacang");
kuih=data.getBoolean("kuih");
if(nsPutih==true)
{
tvOutput.setText("Nasi Putih");
}
if (nsBeriyani==true)
{
tvOutput.setText("Nasi Beriyani");
}
if (nsMinyak==true)
{
tvOutput.setText("Nasi Minyak");
}
if(aymKurma==true)
{
tvOutput.setText("Ayam Masak Kurma");
}
if (aymLemak==true)
{
tvOutput.setText("Ayam Masak Lemak");
}
if (aymKari==true)
{
tvOutput.setText("Ayam Masak Kari");
}
if (aymRendang==true)
{
tvOutput.setText("Ayam Masak Rendang");
}
if (aymMerah==true)
{
tvOutput.setText("Ayam Masak Merah");
}
if (prtAsam==true)
{
tvOutput.setText("Perut Air Asam");
}
if (dggTomato==true)
{
tvOutput.setText("Daging Masak Tomato");
}
if (dggSinggang==true)
{
tvOutput.setText("Daging Masak Singgang");
}
if (dggMerah==true)
{
tvOutput.setText("Daging Masak Merah");
}
if (kerabuTaugeh==true)
{
tvOutput.setText("Kerabu Taugeh");
}
if (pindangKacang==true)
{
tvOutput.setText("Pindang Kacang");
}
if (jelatah==true)
{
tvOutput.setText("Jelatah");
}
if (dalcaSayur==true)
{
tvOutput.setText("Dalca Sayur");
}
if (papadom==true)
{
tvOutput.setText("Papadom");
}
if (kariIkan==true)
{
tvOutput.setText("Kari Ikan");
}
if (ikanMasin==true)
{
tvOutput.setText("Kari Ikan Masin");
}
if (sirapAis==true)
{
tvOutput.setText("Sirap Ais");
}
if (oren==true)
{
tvOutput.setText("Oren");
}
if (tehTarik==true)
{
tvOutput.setText("Teh Tarik");
}
if (agarAgar==true)
{
tvOutput.setText("Agar Agar");
}
if (buburKacang==true)
{
tvOutput.setText("Bubur Kacang");
}
if (kuih==true)
{
tvOutput.setText("Kuih Muih");
}
}
}
this is the page where the checked check box should display text...
page name : receipt.xml
<?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" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="62dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView4"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView7"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView8"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView9"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView10"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView11"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView12"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView13"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView14"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView15"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView16"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView17"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView18"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView19"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView20"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView21"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView22"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView23"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView25"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView24"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView></RelativeLayout>
when i run eclipse's emulator, the page receipt.xml only display 1 checkbox value even if i checked on many checkbox.
help plz....tq
i tick on the checkbox Ayam Masak kurma and daging masak merah....
the output turn to be this way....
Ayam Masak Kurma
Daging Masak Merah
there are lot of spaces there...can u help me...i cannot upload the image of the emulator here...it says that i must get 10 point to post images....
The white spaces between your textviews are textviews that arent hidden, try to hide them when you have no data for them like:
if(nsPutih==true) {
tvOutput1.setText("Nasi Putih");
tvOutput1.setVisibility(View.VISIBLE);
} else {
tvOutput1.setVisibility(View.GONE);
}
However this is bad practice, you should try to dynamically create the textviews by putting them in a ListView. Also try to pass the data to the activity using a Map of objects.
with your intent try to pass your values with putextra and getextra method it will work

Categories

Resources