Creating a Simple user Interface in Android Applications.
In my previsions lesson,we learnt how to pass variables from one activity to another.Today we are going to learn create a simple user interface using Linearlayout.
Android Projects defines resource layout.Layout xml contains all the elements that are contained in a application.These are the buttons,textfields,etc.
Resource String xml contains all the string that have been used in the application.For example the application name,button name Edittext hint message etc.
[
Above code creates LinearLayout.width and height are matched depending on the device screen size.
Looking at above code,we have created a EditText view and name it enter_message.Getting content from EditText View we need to findViewById.
Error may appear because we have not created string resources.So let go ahead and create
string resources.
Android Projects defines resource layout.Layout xml contains all the elements that are contained in a application.These are the buttons,textfields,etc.
Resource String xml contains all the string that have been used in the application.For example the application name,button name Edittext hint message etc.
Creating Linearlayout
Open your activity_layout.xml file and add below code.[
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
]
Above code creates LinearLayout.width and height are matched depending on the device screen size.
Adding Button and TextField.
[
<?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/enter_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/edit_message" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button_send" /> </LinearLayout>
]
Looking at above code,we have created a EditText view and name it enter_message.Getting content from EditText View we need to findViewById.
Error may appear because we have not created string resources.So let go ahead and create
string resources.
Adding String Resources
Below code creates all the string resources used in the application;
[
<resources> <string name="app_name">Gym Trainer</string> <string name="edit_message">Enter Message</string> <string name="button_send">Send</string>]
MainActivity Java Class Code
This is the class that starts the application to run.
[
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends AppCompatActivity { public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; public final static String EXTRA_NAME="com.example.myfirstapp.NAME"; public final static String EXTRA_EMAIL="com.example.myfirstapp.EMAIl"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //This method will be called once the user clicks send message button }
]