Button not disabling if text missing in edittext - java

Could anyone tell me why my button isn't disabled when the text in the edittext is empty? I've tried to do this so many ways but it never works! This is the simplified code I have at the minute.
Code:
public class MapStartActivity extends FragmentActivity {
EditText mapName;
Button NextPageStart;
private TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkFieldsForEmptyValues();
}
#Override
public void afterTextChanged(Editable s) {
}
};
private void checkFieldsForEmptyValues(){
String s1 = mapName.getText().toString();
if (s1.trim().isEmpty()) {
NextPageStart.setEnabled(true);
} else {
NextPageStart.setEnabled(false);
NextPageStart.setAlpha(.5f);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mapName.addTextChangedListener(textWatcher);
}
});
}
}

Add text watcher outside click listener like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_start);
NextPageStart = (Button) findViewById(R.id.NextStatLocBut);
mapName = (EditText) findViewById(R.id.MapNameText);
// To disable the button intially
NextPageStart.setEnabled(false);
mapName.addTextChangedListener(textWatcher);
//Click Listener for button
NextPageStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// You can do some click action here
}
});
}

In your code change these below lines only
#Override
public void afterTextChanged(Editable s) {
String textFromEditText = mapName.getText();
if(TextUtils.isEmpty(textFromEditText)){
NextPageStart.setEnabled(false);
} else{
NextPageStart.setEnabled(true);
}
}

I don't know why it's not working not for you, but I guess it should work fine. but you should learn about name convention first.

Related

Implement search bar to filter Item and showing new activity

I am making a application which use Search bar to filter item in list view. when user search and choose an item they want, application will display activity of that item.
I try to find solution but each time user select item they want to go. the application always shows the activity of the first item. can anyone please help me how to solves this issue. below is my java code:
BottomNavigationView bottomNavigationView;
ListView listView;
FloatingActionButton share;
MaterialSearchBar timkiembar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reading);
listView = findViewById(R.id.Lv);
listView.setSelected(true);
share = findViewById(R.id.share);
timkiembar = findViewById(R.id.timkiembar);
String[] storyname = getResources().getStringArray(R.array.stories_names);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,R.layout.row,R.id.row_txt,storyname);
listView.setAdapter(adapter);
timkiembar.addTextChangeListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
adapter.getFilter().filter(s.toString());
adapter.notifyDataSetChanged();
}
#Override
public void afterTextChanged(Editable s) {
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Reading.this, BilingualEssayDetails.class);
intent.putExtra("story_key",position);
startActivity(intent);
}
});
and below is code of BilingualEssayDetails.class:
translate = findViewById(R.id.translate);
dich = findViewById(R.id.dich);
nihon = findViewById(R.id.nihon);
paint = findViewById(R.id.paint);
layout = findViewById(R.id.layout);
txt_essay = findViewById(R.id.txt_essay);
colors= getSharedPreferences("MyColor",MODE_PRIVATE);
int colorcode= colors.getInt("color", 0);
if (colorcode!=0){
layout.setBackgroundColor(colorcode);
}
int position = getIntent().getIntExtra("story_key",0);
if (position ==0){
txt_essay.setText(""+getString(R.string.essay1));
translate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1_eng));
}
});
dich.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1_vn));
}
});
nihon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1));
}
});
}
if (position ==1){
txt_essay.setText(""+getString(R.string.essay2));
translate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay1_eng));
}
});
dich.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay2_vn));
}
});
nihon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt_essay.setText(""+getString(R.string.essay2));
}
});
}

Disable button upon removing textview content

public class ActivityMain extends AppCompatActivity {
Button button;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button_chatbox_send);
button.setEnabled(false);
tv= findViewById(R.id.edittext_chatbox);
tv.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {
if (charSequence.toString().equals("")) {
button.setEnabled(false);
} else {
button.setEnabled(true);
}
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
}
});
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//Call other method with string from text view as parameter
}
}
});
}
I added a TextChangedListener to my TextView to disabled the Button, while the text view contains no string. During runtime, after I entered a string into the TextView, the Button is still enabled, even if I deleted all the text. How do I solve this problem, the method which I use during on click can not work with an empty string?
Edit
Problem is solved ty.
Update your button in onTextChanged or after textChanged
tv.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
button.setEnabled(!TextUtils.isEmpty(s.toString())); // Update button here OR
}
#Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
button.setEnabled(count>0); // Update button here
}
});
move it to afterTextChanged
#Override
public void afterTextChanged(Editable editable) {
button.setEnabled(!TextUtils.isEmpty(editable.toString()));
}
You should disable the button inside the onTextChanged method of the TextWatcher -
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
button.setEnabled(charSequence.toString().length() > 0);
}

decrease a counter when a key is pressed (ANDROID)

i have to decrease a counter When a char is written , but my code is decreasing two chars Instead of one but Only When delete key is pressed , but if
if I press another key does not work
private Button send;
private TextView max;
private TextView msg;
int limit=140;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
max = (TextView) findViewById(R.id.max);
send = (Button) findViewById(R.id.send);
msg = (TextView) findViewById(R.id.msg);
msg.setOnKeyListener(this);
}
#Override
public void onClick(View view) {
}
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
max.setText(String.valueOf(limit--));
return false;
}
The problem is that onKey is called twice, once for down and one for up.
You can use another method such as onKeyDown:
onKeyDown documentation
public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}
Or use a filter using onKey.
onKey gets fired twice. Once when the key is pressed down, and once when it is released.
Or you can try:
onKeyDown()
onKeyUp()
KeyEvent.getAction().
In this java image, you can see that the person made both a released and a pressed method. This picture is for java, but the concept for android is the same. Use the methods I gave you in that numbered list.
Otherwise, right now, you are listening for two events.
On the right is key pressed, and the left is key released:
If this was helpful, please mark as best answer. If you need more help, let me know!
Okey i have found the answer,
first of all msg is not a TextView, is a EditText
and then we can use addTextChangeListener.
thanks all of you who responds
private Button send;
private TextView max;
private EditText msg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
max = (TextView) findViewById(R.id.max);
send = (Button) findViewById(R.id.send);
msg = (EditText) findViewById(R.id.msg);
msg.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
max.setText(String.valueOf(140- (msg.getText().toString().length())));
if(msg.getText().toString().length()>=140){
send.setEnabled(false);
}else
send.setEnabled(true);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
#Override
public void onClick(View view) {
}

get edit text value from view

I want to get the editext value and bitmap image from the every view that has bean created dynamically.I am not getting how to use it.I have tried to get the details from view vx.But it's not happening.Here is my code.
Please help...
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
View addView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<String> value=new ArrayList<String>();
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
Button button = (Button)findViewById(R.id.all);
container = (LinearLayout)findViewById(R.id.container);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0, c = container.getChildCount(); i < c; i++) {
View vx = container.getChildAt(i);
}
}
});
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
addView = layoutInflater.inflate(R.layout.row, null);
EditText textOut = (EditText)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
textOut.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) {
}
});
final ImageView buttonimg = (ImageView)addView.findViewById(R.id.remove);
buttonimg.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//((LinearLayout)addView.getParent()).removeView(addView);
buttonimg.setImageResource(R.drawable.logo);
}});
container.addView(addView);
}});
}
}
For retrieving the text:
((EditText)vx.findViewById(R.id.textout)).getText()
I am posting the answer since I haven't got the answer about how to get the imagebitmap. This code is working
ImageView v2=((ImageView) vx.findViewById(R.id.remove));
Bitmap bm=((BitmapDrawable)v2.getDrawable()).getBitmap();

Using keypress in android

I am creating an android application, a converter. Every time I am going to press any number, I want it to be displayed automatically on a textfield. I don't know how to use the keypress in android application. Is it just like using keypress in a simple java program, let's say ran on netbeans?
Um ...
public class MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(new EditText());
}
}
Your question is not very clear.. but i think you are saying that your layout contain a EditText and TextView, when user type in a number in EditText at same time the TextView should also set the same text at rumtime. if this is your requirement then you can do it as follows:
public class MainActivity extends Activity
{
#Override
public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main_activity);
TextView tv = (TextView)findViewById(R.id.textview1);
EditText et = (EditText)findViewById(R.id.edittext1);
et.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
tv.setText(s);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void afterTextChanged(Editable s)
{
}
});
}
}

Categories