Disabling edittext by using a checkbox error - java

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

Related

Cannot Resolve my CompundButton buttonview for Switch button

I am trying to disable EditText if Switch is off I have made If-else for it.
But my CompoundButton buttonView cannot be resolved. What can be the issue?
Here's my code:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
EditText editText = (EditText) findViewById(R.id.editText);
/** Called when the user taps the Send button */
public void sendMessage(View view) {
// Do something in response to button
Intent newxml = new Intent(this, DisplayMessageActivity.class);
String message = editText.getText().toString();
newxml.putExtra(EXTRA_MESSAGE, message);
startActivity(newxml);
TextView displayText = (TextView) findViewById(R.id.displayText);
displayText.setText(message);
String someText = "Your Message";
TextView yourMessage = (TextView) findViewById(R.id.yourMessage);
yourMessage.setText(someText);
}
final Switch switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// true if the switch is in the On position
if(isChecked){
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
}else{
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
}
}
});
}
Error image
Code Image
I have rewritten the portion of code below. You may try it at your end.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Switch switch1 = (Switch) findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// true if the switch is in the On position
if(isChecked){
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
}else{
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
}
}
});
}
..... then other code.

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

Null pointer on not null index? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Like in subject. I got null pointer, how it is possible ?
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}
You just forgot to do findViewById to your Controls in OnCreate()
button2 =(Button) findViewById(R.id.button2);
button = (Button) findViewById(R.id.btnID);
textView2 =(textView) findViewById(R.id.textView2);
Sample code
#Override
protected void onCreate( Bundle savedInstanceState ){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.mybtnID);
button2 = (Button) findViewById(R.id.button2);
textView2 = (textView) findViewById(R.id.textView2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick( View view ){
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick( View view ){
size = list.size();
textView2.setText(size);
}
});
}
You are missing
findViewById for all your widget
i.e.
Button button, button2;
TextView textView, textView2;
button = findViewById(R.id.id_of_button_1); // same for button2,textview,textview2
You can initialize with findById(R.id.myBtn) like everyone is saying. Or programatically like button = new Button(this);
Try this
You forgot to add findViewById
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button2=(Button)findViewById(R.id.button2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}
Try this are missing findViewById for all your widget
button = (Button) findViewById(R.id.btnID);
button2 =(Button) findViewById(R.id.button2);
textView = =(textView) findViewById(R.id.textView);
textView2 =(textView) findViewById(R.id.textView2);
Sample Code
public class MainActivity extends AppCompatActivity {
Button button, button2;
TextView textView, textView2;
String abc;
public List list = new ArrayList<>();
int size;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btnID);
button2 =(Button) findViewById(R.id.button2);
textView = =(textView) findViewById(R.id.textView);
textView2 =(textView) findViewById(R.id.textView2);
list.add("abc");
abc = (String) list.get(0);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
textView.setText(abc);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
size = list.size();
textView2.setText(size);
}
});
}
}

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

Variable visibility in setOnClickListener

I want to disable button B when making a treatement in button A.
public class GoJump extends Activity{
Button answerA, answerB;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jump);
answerA = (Button) findViewById(R.id.button_A);
answerB = (Button) findViewById(R.id.button_B);
answerA.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// Do something
// Disable button B
}
});
The problem is button B is not visible inside button A treatement. I have to declare it another time to disable it.
Is there any other method to do? Make a variable visible in all class.
Thank you.
Initialize your variable before onCreate().
// Before onCreate
private Button b, b2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
b2.setEnabled(false);
}
});
{
You just need to setEnabled to false. Read this javadoc.
public void onClick(View v)
{
answerB.setEnabled(false);
}

Categories