how to compare a string in if statement - java

i am trying to make a program which compares a string
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
InputMethodManager imm=(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
if(inpt==null)op.setText("Please Enter");
else if (inpt.equals("a")) op.setText("a entered");
else if (inpt.equals("b")) op.setText("b entered");
else op.setText("unknown keyword");
but the else statement is always executed. Please help me correct this code
i am a beginner and don't know much yet.......

Its because this inpt string is declared final. Means value can not be change and string inpt have to update every time when onClick action is performed. Otherwise it will take the same value and gets excuted the else part. So i removed the final keyword and declared the string inside the onClick function. Try this.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ip=(EditText)findViewById(R.id.ip);
final TextView op=(TextView)findViewById(R.id.op);
//final String inpt=ip.getText().toString();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String inpt=ip.getText().toString();
InputMethodManager imm=(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
if(inpt==null)op.setText("Please Enter");
else if (inpt.equals("a")) op.setText("a entered");
else if (inpt.equals("b")) op.setText("b entered");
else op.setText("Unknown keyword");
}
});
}

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

every button click to change text view

i coded this lines to change textview1 Continuous when user clicked button but it sucks at value 1 in output why :\
public class dobeyti extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = 0;
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Becuase you defined the num value inside the onclick method so it will be reassigned to 0 every time.
Just put int num=0 outside the method like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
int num = 0;
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Move int num = 0; outside OnClickListener. Every time you click button value on variable num is set to 0 -> increased to 1 and set as text, that is why it stuck at 1.
int num = 0;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}

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

Continue from where it was left off?

If I am adding a number in my app let's say upto 15, I want to make sure the next time I open my app no matter if it crashed, phones was restarted, or app was exited; it should continue from number 15 rather than the 0. How can I work on that?
I tried something like this. This is probably wrong not sure how to fix it.
public class MainActivity extends Activity {
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.but);
final TextView txt = (TextView) findViewById(R.id.txt);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = app_preferences.edit();
i++;
txt.setText(" " + i);
i++;
editor.putInt("Scores: ", i);
editor.commit();
}
});
}
}
public class MainActivity extends Activity {
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// let i be previously saved value or default to zero
i = app_preferences.getInt("Scores: ",0);
Button btn = (Button) findViewById(R.id.but);
final TextView txt = (TextView) findViewById(R.id.txt);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences.Editor editor = app_preferences.edit();
i++;
txt.setText(" " + i);
//i++; maybe DONT really increment twice (unless thats what you intended)
editor.putInt("Scores: ", i);
editor.commit();
}
});
}
}

How can I pass data from one view to another in android?

I have two views named: first_view and second_view.
The first view consists of a button and an editable text view. The second view consists of a single text view.
In my first view, I want to put a number in the datable text view. As I click the button, it should display the number in the second view.
How can I code Java classes for the two views?
I am assuming that you want to setText within the same Activity, if not so then tell me, Ill change my answer.
Here is what you have to do.
public class MyActivity extends Activity {
Button btn;
EditText et;
TextView tv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.yourbtnID);
et = (EditText) findViewById(R.id.yourEtID);
tv = (TextView) findViewById(R.id.yourtvID);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String myText = et.getText().toString();
tv.setText(myText);
}
});
}
}
If you want to pass text between two Activities then use Intent.
In your current Activity do this.
Intent i = new Intent(YourCurrentActivity.this, YourNextActivity.class);
String str = yourEditText.getText().toString();
i.putExtra("edittextvalue" , str);
startActivity(i);
Then in next Activity do this..
Bundle extras = getIntent().getExtras();
String myEtText;
if (extras != null) {
myEtText = extras.getString("edittextvalue");
yourTextView.setText(myEtText);
}
if Two Views in the same Activity , you can do that
Button btn;
EditText txtInput;
TextView txtShow;
//btn=firstView.findViewWithTag(tag)
btn=firstView.findViewById(R.id.**);
txtInput=firstView.findViewById(R.id.**);
txtShow=secondView.findViewById(R.id.**);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String input=txtInput.getText().toString();
txtShow.setText(input);
}
});
if you have Two Activity :
Button btn;
EditText txtInput;
String VALUE_KEY="show";
private void test()
{
btn=(Button)findViewById(R.id.**);
txtInput=(Button)findViewById(R.id.**);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
String input=txtInput.getText().toString();
Intent intent=new Intent(this, AnotherActivity.Class);
intent.putExtra(VALUE_KEY, input);
}
});
}
On the AnotherActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=this.getIntent();
String value=intent.getStringExtra(VALUE_KEY);
TextView view=(TextView)findViewById(R.id.txt);
view.setText(value);
}
Try this,
put your value in String: String et_str = EditText.getText().toString();
When you call the other intent,
Intent i = new Intent(first_view .class, second_view.class);
i.putExtra("REF",et_str);
StartActivity(i);
In The Second View, get this value using getExtra()

Categories