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

0

People, 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 one number per parameter to the "assignContactFromPhone" method, "QuickContactBadge" does not work. I tried it this 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?

    
asked by anonymous 26.02.2018 / 15:50

1 answer

0

This happens because the moment you call assignContactFromPhone() , EditText is still empty. Add a button in your activity_main.xml file. So you will click on it to call assignContactFromPhone() (but this time the call happens after you have filled 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);
        }
    });
}
    
26.02.2018 / 18:16