Ok, so I have two buttons. The first one is to "Load Text" and the second is to "Speak Out".
Now, I don't want the Speak button to be active while there is no text loaded.
I've managed to set value into the EditText by Load Text button's onClickListener method. Inside the same method I have called,
btnSpeak.setEnabled(true);
I have initialized this as,
btnSpeak = (Button) findViewById(R.id.button1);
The entire coding is,
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
//for checking
if(btnSpeak.isEnabled())
{
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should work!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should not work!", Toast.LENGTH_SHORT).show();
}
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
This is to check the status and assign language to the TTS for further use. I get the toast as "Button should work" but it doesn't get enabled. Why is it so? What's the work around?
I have in .xml file as,
<Button
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="200dp"
android:enabled="false"
android:text="#string/tts_text" />
Should I have it enabled in here and then disable and enable in runtime??
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btnSpeak.setEnabled(true);
//for checking
if(btnSpeak.isEnabled())
{
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
YourVoicemethod();
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should work!", Toast.LENGTH_SHORT).show();
}
});
}
else
{
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should not work!", Toast.LENGTH_SHORT).show();
}
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
You have to use setClickable
btnSpeak.setClickable(true);
btnSpeak.setEnabled(true);
Also, use isEnabled() to check the state
//for checking
if(btnSpeak.isEnabled())
{
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should work!", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(SimpleAndroidOCRActivity.this, "Button should not work!", Toast.LENGTH_SHORT).show();
}
Check your button is pressed or not and write your code.
Sample code.
if(button2.isPressed()){
button2.setEnabled(false);
button1.setEnabled(true);
}
else if(button1.isPressed()){
button1.setEnabled(false);
button2.setEnabled(true);
}
I hope this will help you.
May be is too late to answer and you already found a solution, but you can use invalidate to force the button to redraw itself according to its new state:
btnSpeak.setEnabled(true);
btnSpeak.invalidate();
you can achieve by disable and enabling the visibility of button
here is an example
View button = findViewById(R.id.buttonid);
button.setVisibility(View.GONE);
Use one listener for both buttons
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
listener = new OnClickListener({
#Override
public void onClick(View v) {
switch(button_id){
case id1:
button2.setEnabled(false);
button1.setEnabled(true);
break;
case id2:
button1.setEnabled(false);
button2.setEnabled(true);
break;
}
}
});
Related
This question already has answers here:
How do I check if my EditText fields are empty? [closed]
(30 answers)
Closed 4 years ago.
I want to display toast when fields are empty.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
}
});
Try this :
When the fields are empty, apply this check :-
if (edittext1.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"Edit text 1 can not be empty",Toast.LENGTH_LONG).show();
}
else if (edittext2.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"Edit text 2 can not be empty",Toast.LENGTH_LONG).show();
}
else{
//success code here
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
}
Before doing that
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener)
Try to check if data is empty as follow
if (edittext1.getText().toString().isEmpty() || edittext2.getText().toString().isEmpty()) {
Toast.makeText(this,"fill the required field",Toast.LENGTH_LONG).show();
} else {
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener)
}
Instead of toast, "setError" to edit texts. It would be better
editText.setError("This field can not be blank");
Try this,
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(edittext1.getText().toString().length()<=0){
Toast.makeText(this, "email is empty", Toast.LENGTH_SHORT).show();
}else if (edittext2.getText().toString().length()<=0){
Toast.makeText(this, "password is empty", Toast.LENGTH_SHORT).show();
}else{
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
}
}
});
I have ImageView in my activity with the following properties i have innitialized the imageview and also set OnClickListener on ImageView, but when i click on Imageview for the first time, onlick event doesnot work but when i click on Imageview for the second time it works
<ImageView
android:clickable="true"
android:focusable="false"
android:id="#+id/switchIcon"
android:layout_width="50dp"
android:layout_height="45dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/title"
android:layout_weight="0.1"
android:scaleType="fitXY"
android:src="#drawable/ic_play_circle_filled_black_24dp" />
i have following code in my activity class file :
ImageView switchIcon;
switchIcon = (ImageView) findViewById(R.id.switchIcon);
switchIcon.setOnClickListener(new View.OnClickListener() {
#Override
public boolean onClick(View v) {
if (switchStatus.equals("video")) {
switchStatus = "image";
LayoutImage.setVisibility(View.VISIBLE);
LayoutVideo.setVisibility(View.GONE);
mPlayer.pause();
Glide.with(getApplicationContext()).load(R.drawable.ic_play_circle_filled_black_24dp).into(switchIcon);
} else if (switchStatus.equals("image")) {
if (requestData.get("video_link").equals("")) {
Toast.makeText(getApplicationContext(), "No Video Found for this Recipe", Toast.LENGTH_LONG).show();
} else {
switchStatus = "video";
LayoutImage.setVisibility(View.GONE);
LayoutVideo.setVisibility(View.VISIBLE);
mPlayer.play();
Glide.with(getApplicationContext()).load(R.drawable.ic_photo_size_select_actual_black_24dp).into(switchIcon);
}
} else {
switchStatus = "image";
LayoutImage.setVisibility(View.VISIBLE);
LayoutVideo.setVisibility(View.GONE);
mPlayer.pause();
Glide.with(getApplicationContext()).load(R.drawable.ic_play_circle_filled_black_24dp).into(switchIcon);
}
}
});
I also tried adding the OnTouchListener() as OnClickListener() was not working with this, but OnTouchListener() is also not working with ImageView it also needs two clicks in order to get onclick event
switchIcon.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP){
if (switchStatus.equals("video")) {
switchStatus = "image";
LayoutImage.setVisibility(View.VISIBLE);
LayoutVideo.setVisibility(View.GONE);
mPlayer.pause();
Glide.with(getApplicationContext()).load(R.drawable.ic_play_circle_filled_black_24dp).into(switchIcon);
} else if (switchStatus.equals("image")) {
if (requestData.get("video_link").equals("")) {
Toast.makeText(getApplicationContext(), "No Video Found for this Recipe", Toast.LENGTH_LONG).show();
} else {
switchStatus = "video";
LayoutImage.setVisibility(View.GONE);
LayoutVideo.setVisibility(View.VISIBLE);
mPlayer.play();
Glide.with(getApplicationContext()).load(R.drawable.ic_photo_size_select_actual_black_24dp).into(switchIcon);
}
} else {
switchStatus = "image";
LayoutImage.setVisibility(View.VISIBLE);
LayoutVideo.setVisibility(View.GONE);
mPlayer.pause();
Glide.with(getApplicationContext()).load(R.drawable.ic_play_circle_filled_black_24dp).into(switchIcon);
}
return true;
}
return false;
}
});
I also tried changing ImageView to ImageButton but it also does not help me out, please tell me what action can i perform to solve this problem.
I think the issue is with your conditions and click is getting called on each click, try following code (show toast on click) :
switchIcon.setOnClickListener(new View.OnClickListener() {
#Override
public boolean onClick(View v) {
if (switchStatus.equals("video")) {
Toast.makeText(getApplicationContext(), "switchStatus changed to video", Toast.LENGTH_LONG).show();
switchStatus = "image";
LayoutImage.setVisibility(View.VISIBLE);
LayoutVideo.setVisibility(View.GONE);
mPlayer.pause();
Glide.with(getApplicationContext()).load(R.drawable.ic_play_circle_filled_black_24dp).into(switchIcon);
} else if (switchStatus.equals("image")) {
Toast.makeText(getApplicationContext(), "switchStatus image has clicked", Toast.LENGTH_LONG).show();
if (requestData.get("video_link").equals("")) {
Toast.makeText(getApplicationContext(), "No Video Found for this Recipe", Toast.LENGTH_LONG).show();
} else {
switchStatus = "video";
LayoutImage.setVisibility(View.GONE);
LayoutVideo.setVisibility(View.VISIBLE);
mPlayer.play();
Glide.with(getApplicationContext()).load(R.drawable.ic_photo_size_select_actual_black_24dp).into(switchIcon);
}
} else {
Toast.makeText(getApplicationContext(), "switchStatus was blank, changed to image.", Toast.LENGTH_LONG).show();
switchStatus = "image";
LayoutImage.setVisibility(View.VISIBLE);
LayoutVideo.setVisibility(View.GONE);
mPlayer.pause();
Glide.with(getApplicationContext()).load(R.drawable.ic_play_circle_filled_black_24dp).into(switchIcon);
}
}
});
I put this answer here in case some one lands on this same problem and has the same cause as mine.
The cause of mine was another view in my layout which retained focus after an on touch event on it because I forgot to change the return value from false to true when implementing its on touch listener. The consequence was that I had to click my image view twice after touching the other view for focus to be returned to the layout before the onclick event on the image view is called.
So in general ensure that no other view in layout retains focus perhaps after an on touch event on it in which case the implementation of its onTouch method should return true and not the default false value.
Been trying to use a ToggleButton to act as a bookmark kind of thing in my application. I am using this for the first time. I have declared my toggle button under onCreateView() as below:
bmark = (ToggleButton) v.findViewById(R.id.bmark);
bmark.setChecked(false);
I am trying to just toggle the state and show a Toast message! I tried the below:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
bmark.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean status;
if (bmark.isChecked()) status = true;
else status = false;
Log.w("Bmark status",String.valueOf(status));
if (status) {
bmark.setChecked(false);
Log.w("Bmark after true",String.valueOf(bmark.isChecked()));
Toast.makeText(getActivity(), "Bookmark removed!", Toast.LENGTH_SHORT).show();
} else {
bmark.setChecked(true);
Log.w("Bmark after false",String.valueOf(bmark.isChecked()));
Toast.makeText(getActivity(), "Post Bookmarked..!", Toast.LENGTH_SHORT).show();
}
}
});
Every time I press the button, the status is initially read "true", although I've set it to "false". After I call setChecked(false), it also becomes false. But when I click it again, it again reads "true" and instead of "false"
I dont know why its happening like this. I just want to toggle it every time I click it. Pls help me out! Thanks in advance :)
Change code to:
if (bmark.isChecked()){
status = true;
Toast.makeText(getActivity(), "Post Bookmarked..!",Toast.LENGTH_SHORT).show();
}
else {
status = false;
Toast.makeText(getActivity(), "Bookmark removed!", Toast.LENGTH_SHORT).show();
}
Toggle changes self checked status, You done that again so status was changed two times.
The problem is you're inverting the state of your button by the setChecked() calls in onClick().
Use an OnCheckedChangeListener instead of an OnClickListener, so you don't have to bother with keeping track of the status:
bmark.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// checked
Toast.makeText(getActivity(), "Post Bookmarked!", Toast.LENGTH_SHORT).show();
} else {
// not checked
Toast.makeText(getActivity(), "Bookmark removed!", Toast.LENGTH_SHORT).show();
}
}
});
You seem to be switching the button back to what it was previously in your statements. If you stop changing the button's state in the onClickListener, it should work just fine.
private boolean bmarkStatus = false;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
bmark.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bmarkStatus = bmark.isChecked();
if (bmark.isChecked()) Toast.makeText(getActivity(), "Bookmark removed!", Toast.LENGTH_SHORT).show();
else Toast.makeText(getActivity(), "Bookmark added!", Toast.LENGTH_SHORT).show();
}
});
}
Hi i have a small problem. I have spinner when i select "yes" edit text 1 and edit text 2 will display when i select "NO" edit text will disappear its working fine for me. But when i press on button for validating the edit text logcat as "AUDIO_OUTPUT_FLAG_FAST denied by client". this is the code.
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(edittext1.getText().toString().length()==0)
{
Toast.makeText(getApplicationContext(), "Please Enter Key NO(FX)", Toast.LENGTH_SHORT).show();
}
else if(sfpchangeddata.contains("SFP Changed"))
{
Toast.makeText(getApplicationContext(), "Please Select SFP changed or Not", Toast.LENGTH_SHORT).show();
}
else if(edittext2.getVisibility()==View.VISIBLE)
{
if(edittext2.getText().toString().length()!=0)
{
}
else
{
Toast.makeText(getApplicationContext(), "Please Enter Siga SFP serial No", Toast.LENGTH_SHORT).show();
}
}
else if(edittext3.getVisibility()==View.VISIBLE)
{
if(edittext3.getText().toString().length()!=0)
{
}
else
{
Toast.makeText(getApplicationContext(), "Please Enter Old Siga SFP serial No", Toast.LENGTH_SHORT).show();
}
}
}
});
isShown() method returns boolean value, so you can use this in your if loop.
if(edittext.isShown())
{
//Set the code here if the edittext is visible.
}
else
{
//Here the code which will run if ediitext is invisible.
}
Hope this will help you.
You can check the visibility for an editText by using isShown() on your ediText
I'm using a custom dialog in onpostexecute method in AsyncTask, it is being popped twice. When the user clicks on a button the dialog has to be closed, this seems to work fine.
Can someone shed some light on why it is being called twice ?
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog != null) {
pDialog.dismiss();
}
try {
if (responseFromServer.contains("x")) {
// Pop up to create password
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_password);
dialog.setTitle("Title...");
dialog.setCancelable(false);
final TextView etpassword = (TextView) dialog.findViewById(R.id.etpassword_dialog);
final Button btnpassword = (Button) dialog
.findViewById(R.id.btnsavepassword_dialog);
btnpassword.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (etpassword.getText().toString().length() == 0) {
Toast.makeText(getActivity(), "Enter password", Toast.LENGTH_SHORT)
.show();
} else if (etpassword.getText().toString().length() < 6) {
Toast.makeText(getActivity(),
"Password should contain minimmum 6 characters",
Toast.LENGTH_SHORT).show();
} else {
dialog.dismiss();
}
}
});
if (!dialog.isShowing()) {
dialog.show();
}
}
else {
Toast.makeText(getActivity(), "Unexpected error occurred. Please try again",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.v("Main FRagment FB async::::::", e.getMessage());
}
}
You can write following condition before display a custom dialog,
if ( !dialog.isShowing() )
{
dialog.show();
}
your code is correct there is no problem in code. Check button click. i think you are calling twice execute() method of AsyncTask.
Can you post the calling code like how are you calling AsyncTask fron button click.