I have an EditText and a Button in my application.
When the button is clicked,the text entered in the EditText is added to a ListView.
I want to disable the Button if the EditText is empty.How to do this ?
This is my code for button click
ImageButton imb=(ImageButton)findViewById(R.id.btn_send);
imb.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
EditText et = (EditText)findViewById(R.id.EditText1);
String str = et.getText().toString();
web1.add(str);
Toast.makeText(ShoutSingleProgram.this, "You entered...."+str, Toast.LENGTH_SHORT).show();
adapter1.notifyDataSetChanged();
et.setText("");
}
});
}
How can i do this ?
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.toString().trim().length()==0){
button.setEnabled(false);
} else {
button.setEnabled(true);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Use TextChangedListener and initially disable ImageButton in onCreate().
Try this
public class MyActivity extends Activity {
ImageButton imb;
EditText et;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imb = (ImageButton) findViewById(R.id.btn_send);
et = (EditText) findViewById(R.id.EditText1);
imb.setEnabled(false); // set button disable initially
et.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (s.toString().equals("")) {
imb.setEnabled(false);
} else {
imb.setEnabled(true);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
}
}
Simple just check the condition in onCreate
if (et.getText().toString().trim().equals("")){
button.setEnabled(false);
}
else{
button.setEnabled(true);
}
Here's a super simple answer in Kotlin.
Just replace 'EditText' and 'Button' with your own.
Button.isEnabled = false
EditText.addTextChangedListener(object: TextWatcher {
override fun onTextChanged(s:CharSequence, start:Int, before:Int, count:Int) {
Button.isEnabled = s.toString().trim{ it <= ' ' }.isNotEmpty()
}
override fun beforeTextChanged(s:CharSequence, start:Int, count:Int,
after:Int) {
}
override fun afterTextChanged(s: Editable) {
}
})
For Multiple EditTexts go
Button.isEnabled = false
val editTexts = listOf(editText1, editText2, editText3, editText4, editText5, editText6)
for (editText in editTexts) {
editText.addTextChangedListener(object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
var et1 = editText1.text.toString().trim()
var et2 = editText2.text.toString().trim()
var et3 = editText3.text.toString().trim()
var et4 = editText4.text.toString().trim()
var et5 = editText5.text.toString().trim()
var et6 = editText6.text.toString().trim()
computeBtn.isEnabled = et1.isNotEmpty()
&& et2.isNotEmpty()
&& et3.isNotEmpty()
&& et4.isNotEmpty()
&& et5.isNotEmpty()
&& et6.isNotEmpty()
}
override fun beforeTextChanged(
s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(
s: Editable) {
}
})
}
Easy Solution
This is very easy to implement in Data-Binding. I hope you are aware of it at this time. You can manage Button with EditText via only XML.
android:enabled="#{etName.text.length() > 0 && etPassword.text.length() > 5}"
Which is equivalent to
button.setEnabled(etName.getText().length() > 0 && etPassword.getText().length() > 5 );
Here & is HTML entity which denotes to &. There can be any operator like &.
etName & etPassword are EditTexts ids.
Complete XML -
<LinearLayout
>
<EditText
android:id="#+id/etName"
/>
<EditText
android:id="#+id/etPassword"
/>
<Button
android:enabled="#{etName.text.length() > 5 && etPassword.text.length() > 5}"
/>
</LinearLayout>
I used TextUtils for a concise solution:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
button.setEnabled(!TextUtils.isEmpty(s.toString().trim()));
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Add a TextWatcher to your EditText, so that when you change the text inside it, you Button enables or disables itself.
Initally in onCreate() disable the button.
Then add a addTextChangedListenerto the edit text. within that check the edittext length and disable if it is 0 or otherwise enable it
If you want to use an object oriented solution and reuse your code
public abstract class EmptyTextWatcher implements TextWatcher
{
public abstract void onEmptyField();
public abstract void onFilledField();
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
if (s.toString().trim().length() == 0)
{
onEmptyField();
} else
{
onFilledField();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void afterTextChanged(Editable s)
{
}
}
so you can use it just doing
textView.addTextChangedListener(new EmptyTextWatcher()
{
#Override
public void onEmptyField()
{
button.setEnabled(false);
}
#Override
public void onFilledField()
{
button.setEnabled(true);
}
});
on Oncreate() , before button click you should check the condition as,
ImageButton imb=(ImageButton)findViewById(R.id.btn_send);
EditText et = (EditText)findViewById(R.id.EditText1);
if(et.getText().toString().equals("")
{
imb.setEnabled(false);
}
imb.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
EditText et = (EditText)findViewById(R.id.EditText1);
String str = et.getText().toString();
web1.add(str);
Toast.makeText(ShoutSingleProgram.this, "You entered...."+str, Toast.LENGTH_SHORT).show();
adapter1.notifyDataSetChanged();
et.setText("");
}
});
When you want to disable the editText there You will use below code
editText.setEnabled(false);
editText.setFocusable(false);
you check the status of an edittext at runtime using the text watcher.
the below code counts the text length and disables if the length is zero.
use this code:
EditText mEditText = new EditText(this);
mEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (s.length() == 0) {
button.setEnabled(false);
}
else {
button.setEnabled(true);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
if anyone was wondering here is the kotlin version of the code
editText1.addTextChangedListener(object: TextWatcher {
override fun onTextChanged(s:CharSequence, start:Int, before:Int, count:Int) {
if (s.toString().trim({ it <= ' ' }).isEmpty())
{
button.setEnabled(false)
}
else
{
button.setEnabled(true)
}
}
override fun beforeTextChanged(s:CharSequence, start:Int, count:Int,
after:Int) {
// TODO Auto-generated method stub
}
override fun afterTextChanged(s: Editable) {
// TODO Auto-generated method stub
}
})
Same as the top accepted answer, but simplified. Remember to wrap the condition in () so it uses the boolean.
editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
button.setEnabled((s.toString().trim().length()>0));
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
Here is my code:
EditText TextBox1, TextBox2;
Button btn;
//Code of onCreate() method is start here:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextBox1 = findViewById(R.id.TextBox1);
TextBox2 = findViewById(R.id.TextBox2);
TextBox1.addTextChangedListener(CheckEmptyTxtBoxes);
TextBox2.addTextChangedListener(CheckEmptyTxtBoxes);
btn = findViewById(R.id.button);
}
//Code of TechWatcher listener start here:
private TextWatcher CheckEmptyTxtBoxes = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence s, int i, int i1, int i2) {
try {
String txtbox1 = TextBox1.getText().toString().trim();
String txtbox2 = TextBox2.getText().toString().trim();
if (!txtbox1.isEmpty() && !txtbox2.isEmpty()) {
btn.setEnabled(true);
btn.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#0307F4")));
} else {
btn.setEnabled(false);
btn.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#8486FB")));
}
} catch (Exception e) {
Toast.makeText(MainActivity.this, "OnTextChanged method error", Toast.LENGTH_SHORT).show();
}
}
#Override
public void afterTextChanged(Editable editable) {
}
};
I wanna make a button that can show me how long time it gets pressed. If the button stops to get pressed the time still stays there, then you can keep pressing it. How can i do this?
With this code, I made a 5 second button-press open a new activity :)
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
final Handler handel = new Handler();
button1.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
handel.postDelayed(run, 5000/* OR the amount of time you want */);
break;
default:
handel.removeCallbacks(run);
break;
}
return true;
}
Runnable run = new Runnable() {
#Override
public void run() {
Intent i = new Intent(MainActivity.this, SecActivity.class);
startActivity(i);
}
};});
}}
if your mean a Button count the times it's pressed you can use:
int count = 0;
(your button).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
count++;
}
});
if you mean the longer you hold it you can use:
long time = 0;
(your button).setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_DOWN){
time = System.currentTimeMillis();
}else if( event.getAcion() == event.ACTION_UP){
time = System.currentTimeMillis() - time;
}
return true;
}
});
note: that your variables like (int count) and (long time) should be declared in the body of your class not into a method;
below code is a example code for recognizing double click on a view with Custom time;
boolean isPressed = false;
long delayed = 1000;
final Hanlder handler = new Handler();
Button b;
b.setOnClickListener(new View.onClickListener(){
public void onClick(View v){
if(isPressed == true){
// recognize double click;
}else{
Toast.makeText(getApplicationContext(),"tap again to exit",1).show();
isPressed = true;
handler.postDelayed(new Runnable(){
public void run(){
isPressed = false;
}
},delayed);
}
});
Try TouchListener:
Handler handel = new Handler();
b.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN:
handel.postDelayed(run, 5000);
break;
default:
handel.removeCallbacks(run);
break;
}
return true;
}
});
Later define run:
Runnable run = new Runnable() {
#Override
public void run() {
// Your code to run on long click
}
};
Set a listener for when the button is pressed and when the button is released. Create a timer that records after the button is pressed and stops when the button is released. There should be listeners for both if you are using JFrame or Android as your GUI.
How to enable the button when text is entered and disable when the textfield is empty?
OR
When the button is clicked check the textfield if empty alert else run the func();
I just want to know which method can do the above?
et.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// if length greater then 1 enable the button else disable it here
// TODO Auto-generated method stub
}
});
Try this code
Button btn = (Button)findViewById(R.id.buttonAddress);
EditText ed = (EditText)findViewById(R.id.EditText);
String value = ed.getText().toString
if(value.string.length() == 0 || value = null) {
ButtonName.setEnabled(false);
}
This should work
You can use the setEnabled(boolean) function for the view of the button. After you get the object through findViewById(). You can do the rest of your functionality by using the onClickListener.
View.OnClickListener myhandler = new View.OnClickListener() {
public void onClick(View v) {
//todo
}
}
i want to catch double click on textview for that i have used below code
but it still not working :(
TextView txtOne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOne = (TextView) findViewById(R.id.txtOne);
txtOne.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("DRAG");
return gestureDetector.onTouchEvent(event);
}
});
}
final GestureDetector gestureDetector = new GestureDetector(
new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onDoubleTap(MotionEvent e) {
System.out.println("Double Tap");
return super.onDoubleTap(e);
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
System.out.println("One Click");
return super.onSingleTapUp(e);
}
});
only drag is calling but not "Double Tap" and "One Click" never called
Try following steps.
Step 1
Write following code in your activity.
// initialize the Gesture Detector
gd = new GestureDetector(this,new OnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
// set the on Double tap listener
gd.setOnDoubleTapListener(new OnDoubleTapListener() {
#Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(SplashActivity.this,"Double Tap",Toast.LENGTH_LONG).show();
return false;
}
#Override
public boolean onDoubleTapEvent(MotionEvent e) {
// if the second tap hadn't been released and it's being moved
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
});
Step 2
Write following code for activity. Here gd will be GestureDetector object.
txt.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
gd.onTouchEvent(event);
return false;
}
});
Instead of:
GestureDetector.SimpleOnGestureListener()
Try:
GestureDetector.OnDoubleTapListener()
For SimpleGestureDetector, you need to override onDown() and return true to trigger double tap detector.
Whether or not you use GestureDetector.OnGestureListener, it's best practice to implement an onDown() method that returns true. This is because all gestures begin with an onDown() message. If you return false from onDown(), as GestureDetector.SimpleOnGestureListener does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods of GestureDetector.OnGestureListener never get called. This has the potential to cause unexpected problems in your app. The only time you should return false from onDown() is if you truly want to ignore an entire gesture.
Source: http://developer.android.com/training/gestures/detector.html
This is a good site for performing double click... I used it and worked
doubleCLICK
I would like to get user location when I tap/longpress on the screen using the mapforge API...
this code is not fired even if I longpressed on the screen.
mapView.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
toastLong("onlongclick");
return false;
}
});
With Little Modify
mapView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Long Clicked is working ", Toast.LENGTH_SHORT).show();
return true;
}
});