Why this text comparision fails? - java

I am trying to get the text in the EditText component named textfield and see if it equals "facebook". It does not work though and I can't for the life of me find out why. Also in the if statement my is supposed to be the ID for the picture.
public class Guess extends Activity {
ImageView image;
ImageView stat;
Button okButton;
EditText textfield;
Integer my;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guess);
final Bundle bundle = this.getIntent().getExtras();
int pic = bundle.getInt("myimage");
final int resid = bundle.getInt("resourceID");
my = pic;
image = (ImageView) findViewById(R.id.imageView1);
image.setImageResource(pic);
image.setAdjustViewBounds(true);
stat = (ImageView) findViewById(R.id.imageView2);
stat.setImageResource(R.drawable.incorrect);
okButton = (Button) findViewById(R.id.button1);
okButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
textfield = (EditText) findViewById(R.id.editText1);
if("2130837510".equals(my.toString()) && "facebook".equals(textfield.getText()));
{
stat.setImageResource(R.drawable.correct);
}
}
});
}
}

textfield.getText() returns an Editable not a String. To make it work pass the actual String object using textfield.getText().toString().

You have a semi colon on the end of your if statement
if(1 == 2);
{
System.out.println("passed");
}
so the code below will execute anyway.

Related

How to delete individual elements from textview on Android studio

I am doing this project, can anyone help me to delete individual elements from the text view?enter image description here
here's i implemented the 'button' to insert and 'button2' to delete, and here's the code down bellow
public class MainActivity extends Activity {
private EditText editText;
private Button button;
private TextView textView;
private static final String TAG = "MainActivity";
// To hold all data in different mode : Portrait and landscape
private final String text_context = "TX";
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: in");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById((R.id.textView));
textView.setText(""); // make it no text at runtime, but text at view
textView.setMovementMethod(new ScrollingMovementMethod()); // make it scrolling
editText.setText("");
final View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
String result = editText.getText().toString();
result += "\n";
textView.append(result);
editText.setText(""); // text on EDITTEXT will disappear as soon as we click the button
}
};
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
}
};
if(editText != null)
button.setOnClickListener(onClickListener);
Log.d(TAG, "onCreate:out");
}
}
Please help me TO IMPLEMENT DELETE ACTION VIA BUTTON2....
Right now your onclick listeners are generically listening for clicks on any view.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button events
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button2 events
}
});
Or better yet implement View.OnClickListener() on your activity
Use regex
String input = editText.getText().toString();
String regex = "\\b" + input + "\\b";
String tvText = textView.getText().toString();
tvText = tvText.replaceAll(regex, "");
textView.setText(tvText);
Put this code in
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
//code goes here
}
};
if(editText != null)
// add this onclick to your button2
button2.setOnClickListener(onClickListener1);
Log.d(TAG, "onCreate:out");
}

Get Position value of Dynamically added view in layout android

I m trying this tutorial : http://android-er.blogspot.in/2014/01/get-text-from-dynamically-added-view.html \
where i m trying to edit the text when user click the insert button, it works fine only if i put position as static value.
but when i try to get position this app unfortunat stop.
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
Button buttonShowAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
final TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
Button buttonInsert = (Button)addView.findViewById(R.id.insert);
buttonInsert.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//int position = (Integer)v.getTag(); // I m trying to get position of button here but stops the app
int position = 2; // Static value for changing the textview works fine
String showallPrompt = "";
View childView = container.getChildAt(position);
TextView childTextView = (TextView) (childView.findViewById(R.id.textout));
String childTextViewText = (String) (childTextView.getText());
showallPrompt += position + ": " + childTextViewText + "\n";
Toast.makeText(MainActivity.this,
showallPrompt,
Toast.LENGTH_LONG).show();
((TextView) (childTextView.findViewById(R.id.textout))).setText("HELLO ");
}});
container.addView(addView, 0);
}});
LayoutTransition transition = new LayoutTransition();
container.setLayoutTransition(transition);
}
}
So here, i used to get position by
//int position = (Integer)v.getTag(); // I m trying to get position of button here but stops the app
int position = 2; // Static value for changing the textview works fine
but not working . How to solve this problem.Actually i m tring to do listview with editable text and show as textview.

How to wait for specific String in TextView to proceed to next method

I've just started learning so please be tolerant.
I want to constantly check if my TextView is equal to "tata", and then to doStuff(), but can't figure out how to do it...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_new_game);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
final Button button3 = (Button) findViewById(R.id.button3);
final TextView tv = (TextView) findViewById(R.id.editText2);
// SyllableButtonListener appends TextView only if
// button.getText().equals("ta")
button1.setOnClickListener(new SyllableButtonListener(tv, "ta", button1));
button2.setOnClickListener(new SyllableButtonListener(tv, "ta", button2));
button3.setOnClickListener(new SyllableButtonListener(tv, "ta", button3));
// how to do it: ???
// if(tv.getText.equals("tata")){ doStuff() }
}
any kind of loops dosen't work. I've tried with Thread, but it's not working well ;)
add a TextChangedListener to your EditText
//considering it as a EditText instead of Textview since you would know when textView text changes to "tata"
New Code
final EditText editText = (EditText) findViewById(R.id.editText2);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
if(s.toString().equals("tata")) {
//do stuff
}
}
});
As per your post your textview is frequently changing text. At the time pressing a button get the text view instance and check for your text.
you have to implement onClick(View v) method.
public void onClick(View v){
//here v is the button view.
//you have to use the textview instance which you have already created in onCreate method
if(textView.getText().equals("YourText"){
//do your stuff
}
}
You need che id "tata" string is contained in textView
Can you try:
teextView.getText().contains("tata")

Disabling edittext by using a checkbox error

I have a problem here. I want to disable a edittext by using a checkbox. If checkbox is checked, edittext isa available, if not, it is disabled. But I'm having problems. Here is my code: Can anyone check on this. Accdng to eclipse, no error. But when I run it on my phone, the enabling/disabling is not functioning.
public class Order extends Activity {
private Button button1;
private EditText txtbox1,txtbox2;
private TextView tv;
CheckBox check1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText5);
txtbox2= (EditText) findViewById(R.id.editText2);
check1 = (CheckBox)findViewById(R.id.checkcheck);
button1.setOnClickListener(new clicker());
}
public void addListenerOncheck1() {
check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
txtbox1.setFocusable(true);
txtbox1.setFocusableInTouchMode(true);
} else {
txtbox1.setFocusable(false);
txtbox1.setFocusableInTouchMode(false);
}
}
});
}
class clicker implements Button.OnClickListener
{
public void onClick(View v)
{
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3;
tv.setText(vis.toString());
}
}
}
you are nowhere adding your Listener by calling ur method addListenerOncheck1();
so put addListenerOncheck1(); add the end of you OnCreate method.
But I highly recommend you not to use all these additional self-made Classes for just adding listeners. Use the following code instead and add it into your OnCreate Method:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
txtbox1= (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText5);
txtbox2= (EditText) findViewById(R.id.editText2);
check1 = (CheckBox)findViewById(R.id.checkcheck);
button1.setOnClickListener(new clicker());
check1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
txtbox1.setEnabled(false);
} else {
txtbox1.setEnabled(true);
}
}
});
button1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String a,b;
Integer vis;
a = txtbox1.getText().toString();
b = txtbox2.getText().toString();
vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3;
tv.setText(vis.toString());
}
});
// This will make sure the user can only type numbers into the EditTexts:
txtbox1.setInputType(InputType.TYPE_CLASS_NUMBER);
txtbox2.setInputType(InputType.TYPE_CLASS_NUMBER);
// Code to disable editTexts at start once:
txtbox1.setEnabled(false);
txtbox2.setEnabled(false);
}

Very basic java for android, why does program shut down when Button is pressed?

Just started checking out android development, but having problems already. The activity shuts down when button bConvert is pressed. The outputValue.setText is apparently used wrong. Whats up? Thank you for help! :)
public class StartingPoint extends Activity {
TextView outputValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialize();
}
private void initialize() {
Button bConvert = (Button) findViewById(R.id.bConvert);
EditText inputValue = (EditText) findViewById(R.id.etInput);
TextView outputValue = (TextView) findViewById(R.id.userOutput);
bConvert.setOnClickListener(convertHandler);
}
View.OnClickListener convertHandler = new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
outputValue.setText("Fahrenheit: "+ 100);
}
};
private float convert(float celsius) {
return ((celsius*9/5)+32);
}
}
Replace the TextView outputValue = (TextView) findViewById(R.id.userOutput); line with
outputValue = (TextView) findViewById(R.id.userOutput);
When you initialize outputValue, you are creating a local variable instead of initializing your class variable.

Categories