Today we are going to make such a project that on clicking a button, all the Google email IDs in the phone will be displayed. First of all, we will create a project in Android Studio, select its language as Java and then create the project.After the project is created, take a button and a textview and when the button is clicked, all the emails in the phone will be displayed and whichever email id you select will be filled in the textview
1. Step xml file me is code ko paste kre
activity_main.xml
1. XML Layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <Button android:id="@+id/btnPickEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Pick Email" /> <TextView android:id="@+id/tvSelectedEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Selected Email will appear here" android:textSize="16sp" /> </LinearLayout>
2. Step MainActivity.java file me is code ko paste kre
import android.accounts.AccountManager; import android.content.Intent; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import com.google.android.gms.common.AccountPicker; public class MainActivity extends AppCompatActivity { private TextView tvSelectedEmail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btnPickEmail = findViewById(R.id.btnPickEmail); tvSelectedEmail = findViewById(R.id.tvSelectedEmail); // Activity Result Launcher ActivityResultLauncherpickAccountLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK && result.getData() != null) { String selectedEmail = result.getData().getStringExtra(AccountManager.KEY_ACCOUNT_NAME); if (selectedEmail != null) { tvSelectedEmail.setText(selectedEmail); } else { tvSelectedEmail.setText("No email selected"); } } }); // OnClickListener for the button btnPickEmail.setOnClickListener(v -> { Intent intent = AccountPicker.newChooseAccountIntent( null, null, new String[]{"com.google"}, false, null, null, null, null ); pickAccountLauncher.launch(intent); }); } }
3. Add Dependencies in your project
Dependencies
implementation ("com.google.android.gms:play-services-auth:20.7.0")
Conclusion
Follow all the steps mentioned above and still if you face any problem then you can visit android.google.com and
If you want to create more projects and tools of Android Studio then search for that project
If you want to get any project made from us then you can contact us, we are ready to help you.
Thank you !!
0 Comments