bring checked check box values to other pages - java

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

Related

textview.setText isn't working in my code

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

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.

How to use spinners in android?

I am new to android. I am trying to make simple unit converter. The layout contains EditText for input, two spinners (to and from), an ImageButton and EditText to display answer.
I want to add values to spinners and when these values are selected i want to perform action.
My xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".BitsBytes" >
<Spinner
android:id="#+id/SpinnerFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignRight="#+id/inputvalue"
android:layout_below="#+id/textView2"
android:entries="#array/bitsbytesfrom"
android:prompt="#string/from_prompt" />
<Spinner
android:id="#+id/SpinnerTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignRight="#+id/SpinnerFrom"
android:layout_below="#+id/textView3"
android:entries="#array/bitsbytesto"
android:prompt="#string/to_prompt" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/SpinnerFrom"
android:layout_below="#+id/SpinnerFrom"
android:layout_marginTop="16dp"
android:text="To:"
android:textColor="#000000"
android:textSize="20dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Value: "
android:textSize="20dp" />
<EditText
android:id="#+id/inputvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView6"
android:layout_alignBottom="#+id/textView6"
android:layout_toRightOf="#+id/textView6"
android:ems="10"
android:hint="Enter value"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/inputvalue"
android:layout_marginTop="16dp"
android:text="From:"
android:textColor="#000000"
android:textSize="20sp" />
<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_marginTop="19dp"
android:text="Bits/Bytes Conversion"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25dp" />
<ImageButton
android:id="#+id/convertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/SpinnerTo"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:src="#drawable/btnconvert"
android:clickable="true"/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/answer"
android:layout_centerHorizontal="true"
android:text="Answer"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
My java code:
package com.easy.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
public class BitsBytes extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bitsbytes);
ImageButton ib = (ImageButton) findViewById(R.id.convertButton);
ib.setOnClickListener(new View.OnClickListener() {
EditText etValue = (EditText) findViewById(R.id.inputvalue);
Spinner spBitsBytesFrom = (Spinner) findViewById(R.id.SpinnerFrom);
Spinner spBitsBytesTo = (Spinner) findViewById(R.id.SpinnerTo);
String txtFromSpinner = spBitsBytesFrom.getSelectedItem().toString();
String txtToSpinner = spBitsBytesTo.getSelectedItem().toString();
TextView Answer = (TextView) findViewById(R.id.answer);
public void onClick(View v)
{
if (txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
{
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
});
}
}
I am filling the spinners using string array. I am not able to figure out whats wrong in my code because there is no error in logcat.
Change this,
(txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
to
if((txtFromSpinner.getSelectedItem().toString().equals("Bits")) && (txtToSpinner.getSelectedItem().toString().equals("Bytes")))
this will get the text from the item selected in the spinner.

Moving imageview in XML causes crash

I am making some changes to an XML layout side of my application. There is a ImageView I am trying to bring to the front yet when I do the app crashes and I can seem to tell why.
Working XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true" />
</LinearLayout>
<TextView
android:id="#+id/place_name"
android:textIsSelectable="true"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/place_distance"
android:layout_toLeftOf="#+id/place_distance"
android:layout_toRightOf="#+id/place_img"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textSize="11dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_margin="2dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:src="#drawable/offline" />
XML that crashes:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true" />
<!-- android:autoLink="web|all" -->
</LinearLayout>
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<TextView
android:id="#+id/place_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/place_distance"
android:layout_toLeftOf="#+id/place_distance"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textIsSelectable="true"
android:textSize="11dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:layout_margin="2dp"
android:src="#drawable/offline" />
Im just trying to bring the #place_img to the front of the #wrapper.
java.lang.NullPointerException
at com.peekatucorp.peekatu.DiscussArrayAdapter.getView(DiscussArrayAdapter.java:172)
DiscussArrayAdapter.java:
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(type==1 || type==3)
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
else if(type==4 || type==5)
row = inflater.inflate(R.layout.listitem_users, parent, false);
else
row = inflater.inflate(R.layout.listitem_messages, parent, false);
}
//
final OneComment coment = getItem(position);
userComment = (TextView) row.findViewById(R.id.comment);
userImage = (ImageView) row.findViewById(R.id.place_img);
userName = (TextView) row.findViewById(R.id.place_name);
userOnlineImage = (ImageView) row.findViewById(R.id.pe_profile_pic);
commentDate = (TextView) row.findViewById(R.id.place_distance);
userComment.setText(coment.comment);
ImageLoader imageLoader = ImageLoader.getInstance();
// imageLoader = ImageLoader.getInstance();
// imageLoader.init(ImageLoaderConfiguration.createDefault(convertView.getContext()));
imageLoader.displayImage(coment.image, userImage);
userImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// it was the 1st button
final TabInfo tab = navact.getCurrentTabInfo();
final ProfileFragment fragment = new ProfileFragment().setUser(coment.userid).setNAV(navact);
// fragment.setText(characters[position]);
// second, you push the fragment. It becomes visible and the up button is
// shown
navact.pushFragment(tab, fragment);
/*
Intent i = new Intent(context, ProfileActivity.class);
i.putExtra("userID", coment.userid);
// Create the view using FirstGroup's LocalActivityManager
View view = ChatTabGroup.group.getLocalActivityManager()
.startActivity("show profile", i
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Again, replace the view
ChatTabGroup.group.replaceView(view);*/
}
});
if(coment.online.equalsIgnoreCase("1"))
userOnlineImage.setImageResource(R.drawable.online);
else
userOnlineImage.setImageResource(R.drawable.offline);
if(coment.gender.equalsIgnoreCase("M"))
userName.setTextColor(Color.parseColor("#878ff4"));
else if(coment.gender.equalsIgnoreCase("F"))
userName.setTextColor(Color.parseColor("#f487d6"));
else
userName.setTextColor(Color.parseColor("#969696"));
userName.setText(coment.username);
commentDate.setText(coment.time);
if(type==4){
commentDate = (TextView) row.findViewById(R.id.textView1);
SharedPreferences preferences = getContext().getSharedPreferences("MyPreferences", getContext().MODE_PRIVATE);
double distance = distFrom(Double.parseDouble(getItem(position).time.split(",")[0]),Double.parseDouble(getItem(position).time.split(",")[1]),
Double.parseDouble(preferences.getString("user_lat", "0.0")),Double.parseDouble(preferences.getString("user_lng", "0.0"))
);
commentDate.setText(""+String.format("%.2f", (distance*0.62))+"miles");
}
if(type==1 || type==3){
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);
}else{
}
//
return row;
}
Thank you.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/place_distance"
android:layout_width="wrap_content"
android:layout_height="14dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:layout_margin="5dip"
android:gravity="right"
android:text="28-12-1920 22:22"
android:textColor="#color/white"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/place_img"
android:layout_alignRight="#+id/place_distance"
android:layout_below="#+id/place_distance"
android:layout_marginTop="5dp"
android:background="#drawable/chatbox" >
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="43dp"
android:padding="9dp"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="#android:color/white"
android:textColorLink="#android:color/holo_blue_dark"
android:textIsSelectable="true"
android:typeface="serif"
android:autoLink="web|all"
/>
<!-- android:autoLink="web|all" -->
</LinearLayout>
<ImageView
android:id="#+id/place_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/icon_4860_1" />
<ImageView
android:id="#+id/pe_profile_pic"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_alignBottom="#+id/place_img"
android:layout_alignRight="#+id/place_img"
android:layout_margin="2dp"
android:src="#drawable/offline" />
<TextView
android:id="#+id/place_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/wrapper"
android:layout_toRightOf="#+id/place_img"
android:text="Tweeked"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/gray"
android:textIsSelectable="true"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="normal" />

Android XML file preventing Activity to open

I am learning android and so I wrote the following code for learning ScrollView and creating multiple linearlayouts inside a linearlayout. I have created a menu to launch different activities, but the problem is when I click the class associated with this xml file, the new class (activity) doesn't show up on the screen. What can be the problem and how can I fix it? here 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"
android:weightSum="100"
android:padding="25dp" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="30"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email Address" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Subject" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone No." />
<EditText
android:id="#+id/editText5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Message" />
<EditText
android:id="#+id/editText6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="40">
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="20dp"
android:text="Email" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="30"
android:orientation="vertical">
<AnalogClock
android:id="#+id/analogClock1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
The class Email code:
package com.umer.practice2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Email extends Activity {
TextView t1,t2,t3,t4,t5,t6;
EditText e1,e2,e3,e4,e5,e6;
Button butn;
AnalogClock clock;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.email);
initializeviews();
}
private void initializeviews() {
// TODO Auto-generated method stub
t1= (TextView) findViewById(R.id.textView1);
t2= (TextView) findViewById(R.id.textView2);
t3= (TextView) findViewById(R.id.textView3);
t4= (TextView) findViewById(R.id.textView4);
t5= (TextView) findViewById(R.id.textView5);
t6= (TextView) findViewById(R.id.textView6);
e1= (EditText) findViewById(R.id.editText1);
e2= (EditText) findViewById(R.id.editText2);
e3= (EditText) findViewById(R.id.editText3);
e4= (EditText) findViewById(R.id.editText4);
e5= (EditText) findViewById(R.id.editText5);
e6= (EditText) findViewById(R.id.editText6);
butn=(Button) findViewById(R.id.button1);
clock= (AnalogClock) findViewById(R.id.analogClock1);
}
}
Did you added the activity in AndroidManifest.xml? Every activity need to be added into the manifest file.
for example:
<activity android:name=".HelloGoogleMaps" android:label="#string/app_name"/>

Categories

Resources