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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.