-1

this Question is different because this is using api data to intent and then if you use normal if else condition its not working . check the right answer below and the condition of it ----------

hi, I'm trying to get android call service to a button click ,button is inside a ListView when I click one list item it goes to single item view in that single item view each single item has a mobile number attached to call button . Numbers are stored in parse data base and i'm calling using parse api .

Now when I'm clicking a number it goes to call function . what I need is, suppose a single view doesn't have a mobile number , the button should not be clickable.If anyone knows please help me.

edited answer as one of below anwer but this also not working

btn = (Button) findViewById(R.id.button56) ;
                btn.setClickable(false);

                btn.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {


                        String phno = object.getString("telephone");
                        Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" +phno));
                        startActivity(i);
                    }
                });

Below is my code

btn = (Button) findViewById(R.id.button56) ;
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    String phno = object.getString("telephone");
                    Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" +phno));
                    startActivity(i);
                }
            });
Faizal Munna
  • 515
  • 5
  • 21

4 Answers4

3

You can check and set your button enable to false , also clickable false, and you can and set above status to button within getView() of your Adapter so its pre disable if your mobile number field are empty in your data List

mButton.setClickable(false);
mButton.setEnabled(false);

put below code before you are adding Click Listener to button.

    mButton = (Button) findViewById(R.id.button56) ;
    String phno = object.getString("telephone");

    if(phno==null || phno.equals("")){
        mButton.setClickable(false);
        mButton.setEnabled(false);
    }
else{
        mButton.setEnabled(true);
        mButton.setClickable(true);
}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
2

One way to go at it is using Button.setClickable

Button button;//initialize, add onClickListener, styling, whatever you need
button.setClickable(true/false);//This is what sets it clickable/unclickable

So see if there is a number/value present(if you are using EditText or something else that requires input) and setClickable true or false depending on if there is a value or not

And to make sure there is a value, add to the onClickListener:

String string = editText.getText();
if(string.equals("")){
    //throw exception, show toast, whatever you feel like doing in the event that the button is pressed in an unclickable state
    return;
}

Example integration

    Button btn;//initialize, set listeners, etc
    btn.setClickable(false);//edittext has no input initially, so set the button as unclickable
    EditText et;//initialize
    et.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {}

        @Override
        public void beforeTextChanged(CharSequence s, int start,
                                      int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {
            //s is the input in form of a charsequence that has a length for each char. Meaning if it isn't 0, there is input and the button is clickable. If it is 0, there is no text and the button should not be clickable
            if(s.length() != 0)
                btn.setClickable(true);
            else
                btn.setClickable(false);
        }
    });
Zoe
  • 27,060
  • 21
  • 118
  • 148
1

Sounds like you will need to evaluate this before the onClick event.

Setup a boolean and evaluate if the field has data. Then use btn.setClickable(false); if the statement comes back as false.

BR89
  • 716
  • 1
  • 6
  • 23
  • btn = (Button) findViewById(R.id.button56) ; btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { btn.setClickable(false); String phno = object.getString("telephone"); Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" +phno)); startActivity(i); } }); – Faizal Munna Mar 05 '17 at 13:03
  • I added this but still empty is working – Faizal Munna Mar 05 '17 at 13:04
  • Comments codes are hard to read :P looks like you set the clickable inside the onClick event. Evaluate the setClickable prior to your onClick. Otherwise you're still executing the rest of the code in the onClick event – BR89 Mar 05 '17 at 13:05
  • i did that also but not working empty also showing – Faizal Munna Mar 05 '17 at 13:07
  • Can you edit your initial question with the new code? Hard to tell from comments. – BR89 Mar 05 '17 at 13:08
  • By empty also showing do you mean it's still clickable or that it is visible? – BR89 Mar 05 '17 at 13:14
  • if the phone number is empty in database also button is working ,, I need if the number is empty button should not be clickable or a toast .. – Faizal Munna Mar 05 '17 at 13:17
  • btn.setEnabled(false); try this instead of Clickable – BR89 Mar 05 '17 at 13:20
  • Process: app.com.anew.fbcapplication5, PID: 12093 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference app crashes this error showing – Faizal Munna Mar 05 '17 at 13:22
1
btn = (Button) findViewById(R.id.button56) ;
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String phno = object.getString("telephone");
                    if(! phno.equals(""))
                     {

                       Intent i=new Intent(Intent.ACTION_DIAL,Uri.parse("tel:" +phno));
                    startActivity(i);
                     }
                      else
                           //generate toast here


                }
            });

I hope this helps.