PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 6, 2022

[FIXED] How to press Save Contact Button Programmatically

 November 06, 2022     android, android-intent, contacts, java     No comments   

Issue

I Have code which takes my to the save contact screen, fills the details i need, however i need it to now just automatically press save contact too. Does anyone know how? Or if it is even possible.

public class MainActivity extends AppCompatActivity {

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

        Intent contactIntent = new Intent(ContactsContract.Intents.Insert.ACTION);
        contactIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);

        contactIntent
                .putExtra(ContactsContract.Intents.Insert.NAME, "Contact Name")
                .putExtra(ContactsContract.Intents.Insert.PHONE, "123456789");

        startActivityForResult(contactIntent, 1);

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if (requestCode == 1)
        {
            if (resultCode == Activity.RESULT_OK) {
                Toast.makeText(this, "Added Contact", Toast.LENGTH_SHORT).show();
            }
            if (resultCode == Activity.RESULT_CANCELED) {
                Toast.makeText(this, "Cancelled Added Contact", Toast.LENGTH_SHORT).show();
            }
        }
    }

}

Solution

You can use button.performClick(). If you need demonstrate that button was clicked you can use my sample:

class MainActivity : AppCompatActivity() {
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<Button>(R.id.button)
        // this code demonstrate success work
        button.setOnClickListener {
            Toast.makeText(this, "event occurred", Toast.LENGTH_LONG).show()
        }

        // create event with your params
        var ev = MotionEvent.obtain(1L, System.currentTimeMillis(),0, button.x, button.y, 0)


        val b2 = findViewById<Button>(R.id.button2)
        b2.setOnClickListener {
            button.onTouchEvent(ev)
        }
    }
}


Answered By - IlyaFromRussia
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing