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);
for(int i=1;i<=3;i++)
{
String my_id="Ezequiel_1_"+i;
final TextView modelTextview = (TextView) findViewById(R.id.my_id);
modelTextview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
the_controller.buttonController(v);
}
});
}
I arranged my ids in the previous manner and I don't want to set the method one by one. Is it possible to iterate like that?
Not directly, because strings aren't ids. There's two ways to do what you want though:
1)Data based
int textViewIds[] = [R.id.Ezequiel_1_1, R.id.Ezequiel_1_2, R.id.Ezequiel_1_3,...]
for(int id: textViewIds) {
TextView tv = (TextView) findViewById(id);
...
}
2)Name based
for(int i=0; i<numView, i++) {
int resourceId = resources.getIdentifier("Ezequiel_1_"+i, "id",
context.getPackageName());
TextView tv = (TextView) findViewById(id);
...
}
I prefer method 1 as it gives you clearer code and protection against off by 1 errors (they won't compile).
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'm trying to make an app that only allows a user to enter between 1 and 10, I've tried writing my own method where if the number is out of that range, it changes the text views to ERROR and clears what's in the edit text, however it crashes the app. Does anyone know what's wrong with it? New to android.
Code:
public void buttonClick (View v)
{
TextView tvNum = (TextView) findViewById(R.id.tvNumEnt);
TextView tvName = (TextView) findViewById(R.id.tvNumEnt);
TextView tvNameEnt = (TextView) findViewById(R.id.NameEnt);
TextView tvNumEnt = (TextView) findViewById(R.id.NumEnt);
EditText num = (EditText) findViewById(R.id.ETnumber);
EditText name = (EditText) findViewById(R.id.ETname);
String nameContent = name.getText().toString();
String numContent = num.getText().toString();
tvName.setText(nameContent);
int value = Integer.parseInt(num.getText().toString());
if (value > 10)
{
tvNum.setText("ERROR");
num.getText().clear();
name.getText().clear();
}
else if (value < 1)
{
tvNum.setText("ERROR");
num.getText().clear();
name.getText().clear();
}
else
{
tvNum.setText(numContent);
tvNameEnt.setVisibility(View.VISIBLE);
tvNumEnt.setVisibility(View.VISIBLE);
tvName.setVisibility(View.VISIBLE);
tvNum.setVisibility(View.VISIBLE);
}
}
You have issue on this line
int value = Integer.parseInt(num.toString());
change to:
int value = Integer.parseInt(num.getText().toString());
Now you call toString() method from Object for EditText object. You have to call getText() method for it as first and after then call toString() method for CharSequence
UPDATE:
You find two times the same view with the same ids. Look at the code below:
TextView tvNum = (TextView) findViewById(R.id.tvNumEnt);
TextView tvName = (TextView) findViewById(R.id.tvNumEnt);
there should be R.id.tvNumEnt in the second time, I think so...
TextView tvNum = (TextView) findViewById(R.id.tvNumEnt);
TextView tvName = (TextView) findViewById(R.id.tvNumEnt);
The main problem here is that you are trying to get the number in the edittext with: num.toString(), instead you have to use: num.getText().toString()
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 (;)