Hiding character in edittext - android

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", ""));

Related

can't get integer number from EditText

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

how do i assign a unique object name to every editText being generated

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;

Need help to change the textview content on button click

This is my code. In this code I am trying to change the content of four text on a button click on first time and on same button click I am trying to change the content of only three textview. But it's not working.
public class Home_page_Activity extends Activity {
Button next;
int textids[]={R.id.text1,R.id.text2,R.id.text3,R.id.text4};
String Question[]={"1","2","3","4"};
String three[]={"7","27","37"};
TextView t1,t2,t3,t4,t5,t6,t7;
int count=0;
int check=0;
//int button_id[]={R.id.next};
//Button btn[];
int test=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page_);
t1=(TextView)findViewById(R.id.text1);
t2=(TextView)findViewById(R.id.text2);
t3=(TextView)findViewById(R.id.text3);
t4=(TextView)findViewById(R.id.text4);
t5=(TextView)findViewById(R.id.text5);
t6=(TextView)findViewById(R.id.text6);
t7=(TextView)findViewById(R.id.text7);
next=(Button)findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(test==1)
{
String get=(String) t1.getText();
//int inter=Integer.parseInt(get);
Log.i("t1", get);
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
threetext();
}
nextquestion();
}
});
}
public void nextquestion()
{
t1.setText(Question[count]);
t2.setText(Question[count+1]);
t3.setText(Question[count+2]);
t4.setText(Question[count+3]);
Log.i("count", "value" +count);
test++;
}
public void threetext()
{
Log.i("threetext", "working");
t1.setText(three[check]);
t2.setText(three[check+1]);
t3.setText(three[check+2]);
t4.setText("");
}
Could you increment a counter in the onClick and change the views based on the counter value?

Printing Stuff on Screen in Android

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.

How to handle an onClickListener for a Button within the Spinner onItemSelected(...) method?

I have a Spinner.onItemSelected() method and I want to be able to have an event happen once a Button is clicked after selecting the Spinner item.
For example if you select Beginner for your Spinner and click Java for another spinner. Under that I have a button that says Start. How do I have a Button.onClick event correspond with the selected spinner options?
I did something like this but what I assigned as the setOnClickListener() value is not being read by the View.OnClickListener.
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selected = (Integer) arg0.getItemAtPosition(0);
position = spinner.getSelectedItemPosition();
start = (Button)findViewById(R.id.start);
start.setOnClickListener(phaseHandler);
View.OnClickListener phaseHandler = new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
}
The phaseHandler I declared is not being read by start.setOnClickListener(phaseHandler) so this results in my View.OnCLickListenr invocation not working because phaseHandler is not being set to the Button start. In Eclipse my phaseHandler has the red underlining curly for an error where it says start.setOnClickListener(phaseHandler);
Any Ideas?
move start.setOnClickListener(phaseHandler); down
this may help you
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
selected = (Integer) arg0.getItemAtPosition(0);
position = spinner.getSelectedItemPosition();
start = (Button)findViewById(R.id.start);
View.OnClickListener phaseHandler = new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
start.setOnClickListener(phaseHandler);
}

Categories

Resources