# How to inject custom code in DHIS2 Android Capture App/ Customize DHIS2 Android Capture App

**URL:** <https://community.dhis2.org/t/how-to-inject-custom-code-in-dhis2-android-capture-app-customize-dhis2-android-capture-app/54309>\
**Category:** SDK - Android Development\
**Tags:** android\
**Created:** [4 August 2023 11:17 UTC](https://community.dhis2.org/t/how-to-inject-custom-code-in-dhis2-android-capture-app-customize-dhis2-android-capture-app/54309 "2023-08-04T11:17:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beruck](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/beruck/32/37590_2.png) [@Beruck](https://community.dhis2.org/u/Beruck)\
**Post date:** [4 August 2023 11:17 UTC](https://community.dhis2.org/t/how-to-inject-custom-code-in-dhis2-android-capture-app-customize-dhis2-android-capture-app/54309/1 "2023-08-04T11:17:33Z")

</div>

Hi community

Could you assist me in incorporating custom code into the DHSI2 Android capture app? Our intention is to populate specific fields within the program stage from other android App. For instance, when the user clicks on the person contact fields, it would activate the Contact App to display the contact list. Once a contact is chosen, the system would then retrieve the phone number and display name information, seamlessly populating the designated field in the DHSI2 Android capture app. My challenge lies in determining the process to inject this code effectively into the DHSI2 app.

Best Regards  
Beruck

* * *

cc: @Gassim

---

<div class="post-metadata">

**Author:** ![Gassim](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/gassim/32/35347_2.png) [@Gassim](https://community.dhis2.org/u/Gassim)\
**Post date:** [4 August 2023 16:04 UTC](https://community.dhis2.org/t/how-to-inject-custom-code-in-dhis2-android-capture-app-customize-dhis2-android-capture-app/54309/2 "2023-08-04T16:04:04Z")

</div>

Hi @Beruck

The [@dhis2-android](https://community.dhis2.org/groups/dhis2-android) team would be best to answer this type of question. It’s an interesting idea to have the Contacts app in android easily populate the dhis2 capture app, so the idea is to get the DHIS2 Capture android app to import contact information from the phone into the system just the same way as when social and messenger apps ask for permission to make a copy of phone contacts.

Would you please share more about the use case and why this is a necessary feature? There might be a way to inject a code into an app but this means the app will not be the secure android app which receives updates so a better approach is to either create a feature request for the team to add it as a feature, or create an app that will populate the information independently and directly into the dhis2 server.

My point is that injecting a code is 100% only a temporary solution and doesn’t work with the secure app. 🙂

Thanks!

---

<div class="post-metadata">

**Author:** ![andresmr](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/andresmr/32/30266_2.png) [@andresmr](https://community.dhis2.org/u/andresmr)\
**Post date:** [7 August 2023 06:33 UTC](https://community.dhis2.org/t/how-to-inject-custom-code-in-dhis2-android-capture-app-customize-dhis2-android-capture-app/54309/3 "2023-08-07T06:33:59Z")

</div>

Hi @Beruck  
It is an interesting idea, I think the best way is to open a feature request, but you always can create your own app and use the SDK or fork the Android Capture app, modify and publish it.  
For now inject custom code into the Android Capture App is not available and we need to be very careful when implementing this for security reasons.  
Thanks!

---

<div class="post-metadata">

**Author:** ![Beruck](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/beruck/32/37590_2.png) [@Beruck](https://community.dhis2.org/u/Beruck)\
**Post date:** [29 August 2023 09:34 UTC](https://community.dhis2.org/t/how-to-inject-custom-code-in-dhis2-android-capture-app-customize-dhis2-android-capture-app/54309/4 "2023-08-29T09:34:57Z")

</div>

Hi @andresmr,  
Can you assist me or point me to How to start adding new buttons Which package or class is responsible for this task.  
Please  
Best Beruck

---

<div class="post-metadata">

**Author:** ![andresmr](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/andresmr/32/30266_2.png) [@andresmr](https://community.dhis2.org/u/andresmr)\
**Post date:** [1 September 2023 06:15 UTC](https://community.dhis2.org/t/how-to-inject-custom-code-in-dhis2-android-capture-app-customize-dhis2-android-capture-app/54309/5 "2023-09-01T06:15:12Z")

</div>

Hi @Beruck, can you be more specific?. Do you mean buttons by code, existing or new buttons, which is the use case?, do you have a fork or a pull request that we can check to understand which is your objective?

---

<div class="post-metadata">

**Author:** ![Beruck](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/beruck/32/37590_2.png) [@Beruck](https://community.dhis2.org/u/Beruck)\
**Post date:** [14 September 2023 07:20 UTC](https://community.dhis2.org/t/how-to-inject-custom-code-in-dhis2-android-capture-app-customize-dhis2-android-capture-app/54309/6 "2023-09-14T07:20:48Z")

</div>

Hi @Andres, I had some personal problems and was not able to respond quickly, I forked the dhis2-android-capture-app, my use case, and I want to add a new button at the activity\_program\_event\_detail that will call the Contact App and get the result (Contact Name and Phone number) and populate on Program event Detail with specifically on Data elements (contact name and contatc\_Phoone Number), like the coordinate Data type call Map app get the latitude and longitude and populate in the coordinate fields. I want the new button to be in the middle of the formal stage and near the contact data element  
The challenge are  
1.I am now able to locate where to insert the new button in activity\_program\_event\_detail.xml file  
2. Not able to know the android Id for the two data elements, how can I get the ID of the data element

The snipe code I want to insert is as follows

```
public void pickContact(View view) {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    // Verify it resolves
    PackageManager packageManager = getPackageManager();
    List<ResolveInfo> activities = packageManager.queryIntentActivities(pickContactIntent, 0);
    boolean isIntentSafe = activities.size() > 0;

        startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request it is that we're responding to    

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};

            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();

            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            int column1 = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            String number = cursor.getString(column);
            String named = cursor.getString(column1);
            EditText editText = (EditText) findViewById(R.id.contact_phone_number);
            editText.setText(number);
            TextView displayName= findViewById(R.id.contactName);
            displayName.setText(named);
          
        }
    }
}

```

Thanks  
Beruck

---

<div class="post-metadata">

**Author:** ![Beruck](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/beruck/32/37590_2.png) [@Beruck](https://community.dhis2.org/u/Beruck)\
**Post date:** [20 September 2023 06:45 UTC](https://community.dhis2.org/t/how-to-inject-custom-code-in-dhis2-android-capture-app-customize-dhis2-android-capture-app/54309/7 "2023-09-20T06:45:11Z")

</div>

Hi @andresmr, how are you, is there anything you want to suggest on my problems?

Best  
Beruck

---

<div class="post-metadata">

**Author:** ![Pablo](https://dhis2.b-cdn.net/user_avatar/community.dhis2.org/pablo/32/27191_2.png) [@Pablo](https://community.dhis2.org/u/Pablo)\
**Post date:** [25 September 2023 08:42 UTC](https://community.dhis2.org/t/how-to-inject-custom-code-in-dhis2-android-capture-app-customize-dhis2-android-capture-app/54309/8 "2023-09-25T08:42:07Z")

</div>

Hi @Beruck,

What you need to modify is the form.  
I would modify the **form\_phone\_number.xml** which is the layout for data elements of value type **PHONE\_NUMBER** and add the button next to the one for launching the call.

You can copy/follow the same format as this call button and modify the **UiEventType** enum to add a new event called CONTACT for example. Then handle it in the **UiEventFactoryImpl** class and have a new **RecyclerViewUiEvents** data class and finally in the “FormView” you can launch the requestActivityForResult. All info for the data element should be in the RecyclerViewUiEvent class.

Hope this helps a little bit 🙂
