What I want to do is change the text of a TextView to that of an integer, so that when displayed, the page will display the number.
At the moment, the TextView says "VALUEFROMfirstResult". I want to change that to the integer of firstResult e.g. if the integer produced is 23, the TextView will say 23 instead of the aforementioned.
public class Second extends Activity{
TextView reslt;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
public static void insert(int firstResult2) {
// TODO Auto-generated method stub
reslt = (TextView) findViewById(R.id.tvResult);
How do I do this?
Very simple:
reslt.setText( String.valueOf(firstResult2) );
Or
reslt.setText(firstResult2 + "");
Related
I have the following code
public class Answer extends Activity implements KeyListener{
EditText ed1;
Button b;
LinearLayout l;
int flag=0;
int f=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.answer);
ed1=(EditText)findViewById(R.id.answer);
b=(Button)findViewById(R.id.ans);
l=(LinearLayout)findViewById(R.id.An);
ed1.setKeyListener(this);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(flag==1)
l.setBackgroundColor(Color.BLACK);
}
});
}
public boolean onKeyUp(View arg0, Editable arg1, int arg2, KeyEvent arg3) {
// TODO Auto-generated method stub
switch(arg2){
case KeyEvent.KEYCODE_A:
if(f==0){
ed1.setText("");
f++;
flag=1;
}
}
return false;
}
The problem with this code is that on pressing 'a' it is setting color but after that it doesn't show anything in the edittext - ie it is setting color but after that it is not showing anything in edittext.If we press 'a' followed by 'n' then it will hide both 'a' and 'n' but i want to hide 'a' only
If you are looking to remove all of the a's from your EditText every time that you type 'a', then ed1.setText(""); is the culprit (it's clearing your EditText value every time).
Try this edit in your switch case:
String currText = ed1.getText().toString();
ed1.setText(currText.replace("a", ""));
I have some strings and I want to get each string according to a number. For example. I have the Strings:
<string name="ln1">Line1</string>
<string name="ln2">Line2</string>
<string name="ln3">Line3</string>
And here is my activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 2;
box = (TextView) findViewById(R.id.textView1);
box.setText(R.string.ln1);
box.setOnClickListener(this);}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
box.setText(R.string.ln+ counter);
counter = counter + 1;
}
Right now, the text gets displayed as "R.string.ln2" when I click the TextView. How do I set up the TextView to display the next string (i.e: ln2) when I click the TextView?
I think you will be better off using string arrays in your resource file if you want to have a collection of strings to loop through:
<string-array name="somename">
<item>Line1</item>
<item>Line2</item>
<item>Line3</item>
</string-array>
And then access it in the code:
String[] lines;
int counter = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lines = getResources().getStringArray(R.string.somename);
box = (TextView) findViewById(R.id.textView1);
box.setText(lines[0]);
box.setOnClickListener(this);}
#Override
public void onClick(View arg0) {
counter = counter + 1;
box.setText(lines[counter]);
}
You can reference the <string> tags using: R.string.string_name
Example:
String lineOne = getResources().getString(R.string.ln1);
OR you can directly set the string to a TextView or any other widget:
setText(R.string.ln2);
you can use the string array
<string name="ln1">Line1</string>
<string name="ln2">Line2</string>
<string name="ln3">Line3</string>
Java file
String[] value;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value[0] = R.string.ln1;
value[1] = R.string.ln2;
value[2] = R.string.ln3;
counter = 0;
box = (TextView) findViewById(R.id.textView1);
box.setText(R.string.ln1);
box.setOnClickListener(this);}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
box.setText(value[counter]);
counter = counter + 1;
}
I have this code where a linearLayout contains 3 editTexts. although i have defined an id of each editText in the XML but when i inflate this view a couple of times in runtime how do i get to know which editText is being edited.
Also how will i get the text entered in the editText
public class ActivityMain extends Activity implements OnFocusChangeListener{
EditText date;
View row[]=new View[30];
int rowNo=0;
LinearLayout itemForm;
boolean done=false;
EditText item[]=new EditText[30];
EditText rate[]=new EditText[30];
EditText quant[]=new EditText[30];
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
date=(EditText)findViewById(R.id.date);
date.setOnFocusChangeListener(this);
itemForm=(LinearLayout)findViewById(R.id.item_form);
}
#Override
public void onFocusChange(View arg0, boolean arg1) {
// TODO Auto-generated method stub
if(arg1==false && done==false){
row[rowNo]=LinearLayout.inflate(getApplicationContext(), R.layout.row, null);
((LinearLayout)findViewById(R.id.item_form)).addView(row[rowNo]);
done=true;
I am making text to voice conversion app. I want to take text from user and change it to voice. when i made input text hard coded. my app works perfectly.
static final String[] texts = {"what's up dude"};
TextToSpeech tts;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)findViewById(R.id.btexttovoice);
b.setOnClickListener(this);
tts = new TextToSpeech(textvoice.this,new TextToSpeech.OnInitListener(){
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if(status!= TextToSpeech.ERROR)
{
tts.setLanguage(Locale.US);
}
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
if(tts!= null)
{
tts.stop();
tts.shutdown();
}
super.onPause();
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Random r = new Random();
String random = texts[r.nextInt(3)];
tts.speak(random, TextToSpeech.QUEUE_FLUSH, null);
}
}
it works great but if i want to take input from user through Edit text
i make the following changes:
String[] texts ;
text = (EditText)findViewById(R.id.ttexttovoice);
texts = text.getText().toString();
Here i get error since texts is the array type. How can i get text from edit text to array type of string?
if i do this without array string
String texts ;
text = (EditText)findViewById(R.id.ttexttovoice);
texts = text.getText().toString();
i get no error but i didn't find the desired output. In fact no voice is received.
it seems a simple problem but i am stuck here. kindly help me. Thanks in advance.
String[] texts = new String[3];
text = (EditText)findViewById(R.id.ttexttovoice);
texts[0] = text.getText().toString();
Ok i am developing a cows and bulls game, i am confused how to print messages to the user, i dont want to use dialogs or toasts! As far as i know, the only way is to update textviews , is that the only other option? or can i use something else to display stuff on the screen?
my code till now :
public class Play extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.play);
CowsNBulls();
}
private void CowsNBulls() {
// TODO Auto-generated method stub
int target=0;
final String targetStr = target +"";
boolean guessed= false;
final int guess;
final String Sguess;
System.out.print("Guess a 4-digit number with no duplicate digits");
EditText guess1 = (EditText)findViewById(R.id.etGuess);
Sguess=guess1.getText().toString();
Button ent = (Button) findViewById(R.id.bEnter);
guess= Integer.parseInt(Sguess);
ent.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int guesses=0;
int bulls=0;
int cows=0;
if(hasDupes(guess)||guess < 1000){
guesses++;
String guessStr = guess + "";
for(int i= 0;i < 4;i++){
if(guessStr.charAt(i) == targetStr.charAt(i)){
bulls++;
}else if(targetStr.contains(guessStr.charAt(i)+"")){
cows++;
}}}}});
any ideas?
TextView or EditText will be your only possibility. You'd have to attach it to the Layout and maybe even wrap it in a ScrollView to make room for enough statements.