How to use ""Quickcontactbadge" by passing a number to the "assignContactFromPhone" method per parameter?

Asked

Viewed 52 times

0

Guys, I was practicing a little bit here (I’m a beginner) and I came across the following problem: When I try to use a "Quickcontactbadge" passing a number per parameter to the "assignContactFromPhone" method, the "Quickcontactbadge" does not work. I tried that way:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText phoneField = findViewById(R.id.phoneField);
        String number = phoneField.getText().toString();

        QuickContactBadge quickContactBadge = findViewById(R.id.quickContactBadge);
        quickContactBadge.assignContactFromPhone(number, true);
    }
}

When I pass the number directly to the method, it works normally:

QuickContactBadge quickContactBadge = findViewById(R.id.quickContactBadge);
    quickContactBadge.assignContactFromPhone("888888888", true);

Any idea why and how to solve the problem?

  • The Editext phoneField has some value assigned?

  • Yes, I wanted to enter the number for it and when clicking on "Badge" I could save it in the contacts. When I click on it, however, it seems that the method is not triggered.

1 answer

0


This happens because the moment you call the assignContactFromPhone(), Edittext is still empty. Add a button to your activity_main.xml file. So you click on it to call assignContactFromPhone() (but this time the call happens after you have filled in Edittext).

And the onCreate of your Mainactivity will look like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText phoneField = findViewById(R.id.phoneField);

    QuickContactBadge quickContactBadge = findViewById(R.id.quickContactBadge);

    Button botao = findViewById(R.id.button);
    botao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String number = phoneField.getText().toString();
            quickContactBadge.assignContactFromPhone(number, true);
        }
    });
}
  • Got it! Thanks for your help!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.