String s1 = (getIntent().getStringExtra("Value1"));
String s3 = (getIntent().getStringExtra("value2"));
String s2 = (getIntent().getStringExtra("value3"));
id1 = (TextView) findViewById(R.id.id1);
id2 = (TextView) findViewById(R.id.id2);
id3 = (TextView) findViewById(R.id.id3);
id4 = (TextView) findViewById(R.id.id4);
id1.setText((s1));
id2.setText((s2));
id3.setText((s3));
How to add the values of the tex view set text values and to store the value as integer in one text view
id1.setText((s1));
id2.setText((s2));
id3.setText((s3));
then your question, add the three values of textview and to store in one texview
int first = Integer.parseInt(id1.getText().toString());
int second = Integer.parseInt(id2.getText().toString());
int third = Integer.parseInt(id3.getText().toString());
int final = first+second+third;
id4.setText(""+final);
private void textview(String question) {
TextView clientAddress = new TextView(this);
clientName.setGravity(1);
clientAddress.setTypeface(Typeface.defaultFromStyle(1));
clientAddress.setText(question);
clientAddress.setLayoutParams(lparams);
linearLayout.addView(clientAddress);
clientAddress.setTextSize(20);
clientAddress.setTextColor(getResources().getColor(R.color.white));
clientAddress.setPadding(7, 10, 0, 10);
}
I am calling this function to create 6 TextViews dynamically. but i am confused , how to get text from these TextViews. can anyone suggest me the solution?
You have to create a different object for each and every TextView. and after using those object you can get values of TextView. using getText() property of TextView.
You can also create array of TextView.
Do something like this
private void textview(int position, String question) {
TextView clientAddress = new TextView(this);
clientName.setGravity(1);
clientAddress.setTypeface(Typeface.defaultFromStyle(1));
clientAddress.setText(question);
clientAddress.setTag(position);
clientAddress.setLayoutParams(lparams);
linearLayout.addView(clientAddress);
clientAddress.setTextSize(20);
clientAddress.setTextColor(getResources().getColor(R.color.white));
clientAddress.setPadding(7, 10, 0, 10);
}
public String getTextFromTextView(int positionOfTextView , LinearLayout mLinearLayout){
String result = "";
for (int i = 0; i <((ViewGroup)mLinearLayout).getChildCount() ; i++) {
if(((ViewGroup) mLinearLayout).getChildAt(i)!=null ){
View mView =((ViewGroup) mLinearLayout).getChildAt(i);
if(mView instanceof TextView){
TextView mTextView = (TextView)mView;
if(mTextView!=null && mTextView.getTag()!=null && positionOfTextView==(int) mTextView.getTag() ){
result =mTextView.getText().toString();
}
}else {
TextView mTextView = (TextView) mView.findViewWithTag(positionOfTextView);
if(mTextView!=null){
result =mTextView.getText().toString();
}
}
}
}
return result;
}
you can use the gettext() method to get the text from the text view. and if you wants to convert the fetched text into string then you can use tostring() .
String fetchedtext = clientAddress.getText().toString();
I want to get a precise number from an EditText then change in to a BigDecimal and show it in another TextView.
Here's a part of my code:
BigDecimal rate_string = new BigDecimal(0);
EditText rate = (EditText) findViewById(R.id.rate_edittext);
rate_string = (BigDecimal) rate.getText();
TextView test = (TextView) findViewById(R.id.textView1);
test.setText(rate_string.toString());
But it doesn't work!
What should I do?
Thanks
BigDecimal rate_string = new BigDecimal(0);
EditText rate = (EditText) findViewById(R.id.rate_edittext);
rate_string = (BigDecimal) rate.getText();
TextView test = (TextView) findViewById(R.id.textView1);
test.setText(rate_string.toString());
to
BigDecimal rate_string; // P.S here is useless the creation
EditText rate = (EditText) findViewById(R.id.rate_edittext);
rate_string = new BigDecimal( rate.getText().toString() );
TextView test = (TextView) findViewById(R.id.textView1);
test.setText(rate_string.toString());
You cannot convert a String to a BigDeciaml using a cast but BigDecimal have a constructor which takes the value as String.
P.S Make sure you pass a correct number to the constructor or it will throw a NumberFormatException!
Is there a way to make this code simpler? It seems repeating
The code is for two ImageViews that swap and change skin onClick, changing some labels at the same time.
Thanks!
public void imgChangeUnit(View v) {
TextView lbl_unT = (TextView) findViewById(R.id.unT);
TextView lbl_unOD = (TextView) findViewById(R.id.unOD);
TextView lbl_unID = (TextView) findViewById(R.id.unID);
TextView lbl_unit_res = (TextView) findViewById(R.id.lbl_unit_res);
ImageView myImg_mm = (ImageView)findViewById(R.id.imgMetric);
ImageView myImg_in = (ImageView)findViewById(R.id.imgImperial);
if (isUnit(lbl_unit_res)) {
lbl_unT.setText("inch");
lbl_unOD.setText("inch");
lbl_unID.setText("inch");
lbl_unit_res.setText("Feet");
myImg_in.setImageDrawable(getResources().getDrawable(R.drawable.img_but_imperial_on));
myImg_mm.setImageDrawable(getResources().getDrawable(R.drawable.img_but_metric_off));
} else {
lbl_unT.setText("mm");
lbl_unOD.setText("mm");
lbl_unID.setText("mm");
lbl_unit_res.setText("Meters");
myImg_mm.setImageDrawable(getResources().getDrawable(R.drawable.img_but_metric_on));
myImg_in.setImageDrawable(getResources().getDrawable(R.drawable.img_but_imperial_off));
}
}
This already looks a bit better.
At least you don´t have variable repetition any more.
You might want to create method like setToImperial() and setToMetric(), that would know what to get and set.
It would even be better.
Also, note that you shouldn´t hardcode Strings in android, but use the string resources file.
public void imgChangeUnit(View v) {
TextView lbl_unT = (TextView) findViewById(R.id.unT);
TextView lbl_unOD = (TextView) findViewById(R.id.unOD);
TextView lbl_unID = (TextView) findViewById(R.id.unID);
TextView lbl_unit_res = (TextView) findViewById(R.id.lbl_unit_res);
ImageView myImg_mm = (ImageView)findViewById(R.id.imgMetric);
ImageView myImg_in = (ImageView)findViewById(R.id.imgImperial);
String unit;
String unit2;
Drawable d1;
Drawable d2;
if (isUnit(lbl_unit_res)) {
unit = "inch";
unit2 = "Feet";
d1 = getResources().getDrawable(R.drawable.img_but_imperial_on);
d2 = getResources().getDrawable(R.drawable.img_but_metric_off);
}
else {
unit = "mm";
unit2 = "Meters";
d1 = getResources().getDrawable(R.drawable.img_but_metric_on);
d2 = getResources().getDrawable(R.drawable.img_but_imperial_off);
}
lbl_unT.setText(unit);
lbl_unOD.setText(unit);
lbl_unID.setText(unit);
lbl_unit_res.setText(unit2);
myImg_mm.setImageDrawable(d1);
myImg_in.setImageDrawable(d2);
}
I need help again.
I´m trying to get the current day, and set some specific text to textviews.
But my IF statement is not working correctly.
Actually, it sets the MONDAY-s texts for everyday and writes "Reede" (FRIDAY) on everyday.
So, it´s quite confusing to me, as I´m newbie for Android.
What I am doing wrong?
CODE:
private void setupTunniplaan() {
TextView paev = (TextView) findViewById(R.id.esmasp);
String[] tunnidesmasp = {"Matemaatika", "Bioloogia", "Füüsika", "Kehaline", "Geograafia", "Inglise keel", "Ajalugu","",""};
String[] tunnidteisip = {"Inglise keel", "Vene keel", "Karjääri õpetus", "Karjääri õpetus", "Matemaatika", "Eesti keel", "Kirjandus","", ""};
String[] tunnidkolmap = {"Füüsika", "Kehaline", "Keemia", "Matemaatika", "Kirjandus", "Kirjandus", "Kirjandus", "Inglise keel", "", ""};
String[] tunnidneljap = {"Inglise keel", "Ajalugu", "Matemaatika", " Muusika", "Geograafia", "Bioloogia", "Geograafia", "", ""};
String[] tunnidreede = {"Keemia", "UTA", "Eesti keel", "Matemaatika", "Vene keel", "Bioloogia", "Soome keel", "", ""};
Calendar kal = Calendar.getInstance();
int day = kal.get(Calendar.DAY_OF_WEEK);
if (day == Calendar.MONDAY); {
tunnid(tunnidesmasp[0], tunnidesmasp[1], tunnidesmasp[2],tunnidesmasp[3], tunnidesmasp[4], tunnidesmasp[5],
tunnidesmasp[6],tunnidesmasp[7], tunnidesmasp[8] );
paev.setText("Esmaspäev");
}
if (day == Calendar.TUESDAY); {
tunnid(tunnidteisip[0], tunnidteisip[1], tunnidteisip[2],tunnidteisip[3], tunnidteisip[4], tunnidteisip[5],
tunnidteisip[6],tunnidteisip[7], tunnidteisip[8] );
paev.setText("Teisipäev");
}
if (day == Calendar.WEDNESDAY); {
tunnid(tunnidkolmap[0], tunnidkolmap[1], tunnidkolmap[2],tunnidkolmap[3], tunnidkolmap[4], tunnidkolmap[5],
tunnidkolmap[6],tunnidkolmap[7], tunnidkolmap[8] );
paev.setText("Kolmapäev");
}
if (day == Calendar.THURSDAY); {
tunnid(tunnidneljap[0], tunnidneljap[1], tunnidneljap[2],tunnidneljap[3], tunnidneljap[4], tunnidneljap[5],
tunnidneljap[6],tunnidneljap[7], tunnidneljap[8] );
paev.setText("Neljapäev");
}
if (day == Calendar.FRIDAY); {
tunnid(tunnidreede[0], tunnidreede[1], tunnidreede[2],tunnidreede[3], tunnidreede[4], tunnidreede[5],
tunnidreede[6],tunnidreede[7], tunnidreede[8]);
paev.setText("Reede");
}
if (day == Calendar.SATURDAY); {
tunnid(tunnidesmasp[0], tunnidesmasp[1], tunnidesmasp[2],tunnidesmasp[3], tunnidesmasp[4], tunnidesmasp[5],
tunnidesmasp[6],tunnidesmasp[7], tunnidesmasp[8] );
}
if (day == Calendar.SUNDAY); {
tunnid(tunnidesmasp[0], tunnidesmasp[1], tunnidesmasp[2],tunnidesmasp[3], tunnidesmasp[4], tunnidesmasp[5],
tunnidesmasp[6],tunnidesmasp[7], tunnidesmasp[8] );
}
return;
}
//Sorry if I´m asking dumb questions.
EDIT: Thanks. The TextView paev is working correctly now.
But tunnid() is still not working correctly. You can see it in every if-statement as a first thing to do. My code still works through in every case MONDAY-s "tunnid()". So, the paev.setText() is working correctly, but tunnid() is not.
CODE of tunnid():
public void tunnid(String esimenetund, String teinetund, String kolmastund, String neljastund, String viiestund,
String kuuestund, String seitsmestund, String kaheksastund, String yheksastund) {
TextView tund1 = (TextView) findViewById(R.id.tund1);
TextView tund2 = (TextView) findViewById(R.id.tund2);
TextView tund3 = (TextView) findViewById(R.id.tund3);
TextView tund4 = (TextView) findViewById(R.id.tund4);
TextView tund5 = (TextView) findViewById(R.id.tund5);
TextView tund6 = (TextView) findViewById(R.id.tund6);
TextView tund7 = (TextView) findViewById(R.id.tund7);
TextView tund8 = (TextView) findViewById(R.id.tund8);
TextView tund9 = (TextView) findViewById(R.id.tund9);
TextView ylesanne1 = (TextView) findViewById(R.id.yl1);
TextView ylesanne2 = (TextView) findViewById(R.id.yl2);
TextView ylesanne3 = (TextView) findViewById(R.id.yl3);
TextView ylesanne4 = (TextView) findViewById(R.id.yl4);
TextView ylesanne5 = (TextView) findViewById(R.id.yl5);
TextView ylesanne6 = (TextView) findViewById(R.id.yl6);
TextView ylesanne7 = (TextView) findViewById(R.id.yl7);
TextView ylesanne8 = (TextView) findViewById(R.id.yl8);
TextView ylesanne9 = (TextView) findViewById(R.id.yl9);
tund1.setText(esimenetund);
tund2.setText(teinetund);
tund3.setText(kolmastund);
tund4.setText(neljastund);
tund5.setText(viiestund);
tund6.setText(kuuestund);
tund7.setText(seitsmestund);
tund8.setText(kaheksastund);
tund9.setText(yheksastund);
ylesanne1.setText("");
ylesanne2.setText("");
ylesanne3.setText("");
ylesanne4.setText("");
ylesanne5.setText("");
ylesanne6.setText("");
ylesanne7.setText("");
ylesanne8.setText("");
ylesanne9.setText("");
}
you have semicolon (;) after each one of your if statements:
if (day == Calendar.TUESDAY);
that means the if statement doesn't have a block to execute.
your application will execute each block after each if statement.
remove the semicolon (;)