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.
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", ""));
BELOW IS MY CODE. This code is working, but I want to get the value of the display image in array to test, if the display country is correct. Please help me . I'm stuck in this activity. Thanks for all help .
public class MainActivity extends Activity implements OnClickListener {
private boolean blocked = false;
private Handler handler = new Handler();
ViewFlipper flippy;
Button show;
TextView view;
int flags[] = { R.drawable.afghan, R.drawable.albania, R.drawable.algeria };
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.flipper);
flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
show = (Button) findViewById(R.id.button1);
view = (TextView) findViewById(R.id.textView1);
for (int i = 0; i < flags.length; i++) {
setflipperimage(flags[i]);
}
}
private void setflipperimage(int i) {
// TODO Auto-generated method stub
Log.i("Set Filpper Called", i + "");
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(i);
flippy.addView(image);
}
do not call this in loop,use this on click or on touch:
i+=1;
flippy.setDisplayedChild(flags[0]);
This will get used to get the current child id
viewFlipper.getDisplayedChild();
Hello I am very new in android
I am trying to get integer value from an EditText, But When I am parsing string to Integer I got NumberFormatException.
Please help me to come out of this error.
thanks in advance.
Program is:
int day,month,year;
EditText expense,ammount;
String[] exp=new String[10];
int[] amt=new int[10];
int count=0;
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Calendar cal=Calendar.getInstance();
day=cal.get(Calendar.DAY_OF_MONTH);
month=cal.get(Calendar.MONTH)+1;
year=cal.get(Calendar.YEAR);
final TextView txtdate=(TextView)findViewById(R.id.txtdate);
expense=(EditText)findViewById(R.id.exp);
ammount=(EditText)findViewById(R.id.amnt);
final Button add=(Button)findViewById(R.id.btnadd);
final Button cancel=(Button)findViewById(R.id.btncancel);
final Button done=(Button)findViewById(R.id.btndone);
txtdate.setText(day+"/"+month+"/"+year);
add.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
getval();
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
clean();
}
});
done.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
total();
getval();
clean();
final TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(Integer.toString(total()));
}
private int total() {
int total = 0;
// TODO Auto-generated method stub
for(int i=0;i<=count;i++)
{
total+=amt[i];
}
return total;
}
});
}
protected void clean() {
// TODO Auto-generated method stub
expense.setText(" ");
ammount.setText(" ");
}
protected void getval() {
// TODO Auto-generated method stub
final Editable e2=expense.getText();
final Editable e1=ammount.getText();
final int i=Integer.parseInt(e1.toString());
amt[count]=i;
exp[count]=e2.toString();
System.out.println(amt[count]);
System.out.println(exp[count]);
count++;
}
}
Exception is:
java.lang.NumberFormatException: unable to parse ' 600' as integer
Remove any leading or trailing spaces from the number first:
int inputNumber = Integer.parseInt(editText.getText().toString().trim());
Alternatively, you can remove all non-numeric characters from the string using regular expressions:
String cleanInput = editText.getText().toString().replaceAll("[^\\d]", "");
int inputNumber = Integer.parseInt(cleanInput);
Though if non-numeric input characters is a problem you'd probably want to restrict the EditText to numeric only. See this question. It says to add the following attribute to the EditText:
android:inputType="number"
You have a space in your integer.
Add the following attribute to your EditText in your xml to only allow entering integers:
android:inputType="number"
Try this..
final int i=Integer.parseInt(e1.toString().trim());
bacause there is a space before that number see ' 600' that's why that error..
Hope this will help..
use this code
final int f = -1; // or other invalid value
if (edittext.getText().toString().length() > 0)
f = Integer.parseInt(edittext.getText().toString());
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();
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 + "");