Developing a screen for user input

When clicking the Settings button, our app will take the user to a screen where they can enter the Lat and Lng values for the new location that user wants the alarm to be set for.

We have a very simple screen for input purposes. We have a LinearLayout that has a  couple of EditText, one for the latitude and the other for longitude input. These edit texts are followed by a button that allows for the submission of the new location coordinates entered by the user.

We also have an onClickButton method linked to the button to be called upon as and when the user clicks on the button:

<?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">

<EditText
android:id="@+id/latText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint='Latitude'
android:inputType="numberDecimal" />

<EditText
android:id="@+id/langText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Longitude"
android:inputType="numberDecimal" />

<Button
android:id="@+id/alarmbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickButton"
android:text="Ok" />

</LinearLayout>

We have the XML layout for the user input ready; now let's create a new Kotlin activity class that will use this setting's XML and interact with the user.

The class SettingsActivity extends AppCompatActivity and contains a couple of edit text elements and button element initialized. The variables are identified and set to the correct resources from the resources file by their IDs. The activity loads the settings_activity XML as and when the activity is invoked and loaded.

In the onClickButton method, we have a simple Toast message that says Alarm Set. In the following chapter, we will be saving the input entered and will have the alarm trigger when the user enters the location of interest:

class SettingsActivity : AppCompatActivity() {

public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.settings_activity)

}
fun onClickButton(view: View) {
Toast.makeText(this, "Alarm Set", Toast.LENGTH_LONG).show()
}
}

When the user clicks on the OK button after entering the Lat and Lng details, the Toast message will be displayed, as shown:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset