Menu
NOTE - As of Knox 3.0, the preferred way to call Knox APIs is to upgrade an Android Work Profile to a Knox Workspace. This tutorial uses Android's Device Manager to call Knox APIs. See Upgrade Android Work Profile to Knox Workspace for more information.

Create the layout

This section describes how to create the app layout.

Create the buttons and Textview

This app contains 7 buttons.

  • ActivateAdminbtn – prompts the user to activate device administrator for the app.
  • ActivateLicencebtn – activates the Knox KPE licence key.
  • ShowEnabledAppsbtn – shows list of enabled apps in the textview.
  • ShowDisabledAppsbtn – shows list of disabled apps in the textview.
  • EnableAllDisabledAppsbtn – enables all disabled apps in the device.
  • EnableGivenAppbtn – prompts the user to type package name of the app that needs to be enabled.
  • DisableGivenAppbtn – prompts the user to type package name of the app that needs to be disabled.

Open activity_main.xml and insert the following xml mark up. This creates your screen layout.

NOTE – to resolve the @stringerrors, copy these 2 files from the sample app into your project.

  • background.xml:KnoxSdkSample\app\src\main\res\drawable\Background
  • strings.xml: KnoxSdkSample\app\src\main\res\values\strings
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainpage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.samsung.knox.example.appcontrol.MainActivity">

    <TextView
        android:id="@+id/logview_id"
        android:gravity="bottom"
        android:layout_width="320dp"
        android:layout_height="230dp"
        android:textColor="#3F51B5"
        android:background="@drawable/background"
        android:scrollbars="vertical"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:layout_marginTop="22dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/logview_id"
        android:layout_alignParentStart="true"
        android:layout_marginStart="0dp"
        android:layout_marginTop="10dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="10dp">


            <Button
                android:id="@+id/ToggleAdminbtn"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="@string/activate_admin" />

            <Button
                android:id="@+id/ActivateLicencebtn"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ToggleAdminbtn"
                android:layout_alignStart="@+id/ToggleAdminbtn"
                android:text="@string/activate_license" />

            <Button
                android:id="@+id/ShowEnabledAppsbtn"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ActivateLicencebtn"
                android:layout_alignStart="@+id/ActivateLicencebtn"
                android:text="@string/show_enabled_apps" />

            <Button
                android:id="@+id/ShowDisabledAppsbtn"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ShowEnabledAppsbtn"
                android:layout_alignStart="@+id/ActivateLicencebtn"
                android:text="@string/show_disabled_apps" />

            <Button
                android:id="@+id/EnableAllDisabledAppsbtn"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/ShowDisabledAppsbtn"
                android:layout_alignStart="@+id/ActivateLicencebtn"
                android:text="@string/enable_all_disabled_apps" />

            <Button
                android:id="@+id/EnableGivenAppbtn"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/EnableAllDisabledAppsbtn"
                android:layout_alignStart="@+id/ActivateLicencebtn"
                android:text="@string/enable_given_app" />

            <Button
                android:id="@+id/DisableGivenAppbtn"
                android:layout_width="200dp"
                android:layout_height="wrap_content"
                android:layout_below="@+id/EnableGivenAppbtn"
                android:layout_alignStart="@+id/ActivateLicencebtn"
                android:text="@string/disable_given_app" />

        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

You now see 7 buttons in the middle of the screen. ACTIVATE ADMIN, ACTIVATE LICENCE, SHOW ENABLED APPS, SHOW DISABLED APPS, ENABLE ALL DISABLED APPS, ENABLE GIVEN APP and DISABLE GIVEN APP. You will later assign methods to these buttons to control their respective actions. There is also a TextView at the top of the screen that will display any log messages.

Create the Package_Name_Input layout

In the layout folder which is present in res folder in Android view, create new Layout resource file and name it prompt_user_package_name.xml.

Open prompt_user_package_name.xml file and insert the following xml mark up. This creates your Package_Name_Input layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/prompt_user_package_name"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:paddingTop="20dp"
        android:paddingLeft="25dp"
        android:paddingRight="25dp"
        android:paddingBottom="10dp"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/PackageNametxt"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:text="@string/package_name" />

        
	<EditText
            android:id="@+id/PackageNamedtxt"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/PackageNametxt"
            android:hint="@string/package_name_hint" />

    </RelativeLayout>

</RelativeLayout>
	

You now see the following EditText on the screen. The EditText is used to receive package name input from the user.

For detailed information on creating Android layouts, see Google's page on how to Build a Simple User Interface.

Tutorial Progress

You are 4/9 done! Go to the next step.