Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Making your App responds to button clicks

 

In this tutorial, we want to make our App interactive by making it to respond to button clicks. When the user clicks on the button, an event will occur.

Project: To display a number on the screen and a button. So, when the button is clicked, the number will be updated. Anytime you click on the button, the default number is increased by 1.
So, let’s get started.
First off, you will have to create a new project, give it any name you want. If you don’t know how to create a new project in Android Studio. Check this link, I explained explicitly on how create a new Android Studio project www.rexandroid.blogspot.com

If you’ve launched Android Studio already and you are in project but want create another project.
Go to the top bar, Click on File > New Project and fill up the rest.

In activity_main, enter this following codes. Here is a link on how to understand the basics of XML, working with XML


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="com.android.rexben.demomapp.MainActivity">
    

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="Number"
        android:textSize="20sp" />
    
    
<!--The text that will be updated when a button is clicked-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="0"
        android:textSize="20sp"
        android:id="@+id/textView"/>
    
    
<!--The button that is responsible for add numbers-->
    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add" />
</LinearLayout>



In your MainActivity.java, enter this following lines of code
 

package com.android.rexben.demoapp;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    //This variable called field because it declared out a method
//It is variable where we store the values that are added
    private int numberUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Casting views by resource IDs
        final TextView defaultText = (TextView) findViewById(R.id.textView);
        Button addButton = (Button) findViewById(R.id.add);


        //setOnClickListener is responsible for happens when a button is clicked
        //Any codes writing inside the setOnClickListener will be executed when the button is clicked
        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //Anytime the button is clicked 1 is added to the screen
                numberUpdate = numberUpdate + 1;


                //Displays the numberUpdate to the screen
                //if you don't set the text, nothing will be display to 
                //the screen
                defaultText.setText("" + numberUpdate);
            }
        });
    }

}
When you are done, test run your App on your
Android device. If you don’ know how to run the App on your mobile device,
check this link Getting familiar with Android Studio



numberUpdate = numberUpdate + 1;
 
To:
numberUpdate = numberUpdate - 1; to decrease the value by 1 anytime there is a click.
numberUpdate = numberUpdate * 5; to multiply the value by 5 anytime there is a click.
numberUpdate = numberUpdate / 2; to divide the value by 2 anytime there is a click.
 
Just add the default value of your choice
private int numberUpdate = 10;
 
If you don’t want only the number to show, if you want to display other texts. For instance, instead of displaying only “1”, you can add “Number 1”.
Just add the text you want and add a space so that the text and the number won’t be together. You can enter a long text but it must be in between "".

defaultText.setText("Number " + numberUpdate);


Working with Relative Layout

Relative Layout

It is compound and complex compared to Linear Layout. In Linear Layout, the views fall in place horizontally or vertically but it is not the same in Relative Layout. If you don't put some "functions" in place, your views will just lie on top each other.

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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">
<TextView

        android:id="@+id/donald"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Donald"
        android:textSize="30sp" />
    <TextView

        android:id="@+id/soo_yii"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/donald"
        android:text="Soo Yii"
        android:textSize="30sp" />
    <TextView

        android:id="@+id/tunde"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/soo_yii"
        android:text="Tunde"
        android:textSize="30sp" />
    <TextView

        android:id="@+id/ben"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tunde"
        android:text="Ben"
        android:textSize="30sp" />
    <TextView

        android:id="@+id/alice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ben"
        android:text="Alice"
        android:textSize="30sp" />

</RelativeLayout>
 
To set text or image or button below any other view, you will use android:layout_below="@id/top"

Working with IDs

You and I were given different names so that we can be easily identified. So, to identify views, we assign ids to them.

To assign an id to a view

android:id="@+id/button"

Referencing a view

To reference an ImageView with id (image) in XML,
android:layout_below="@id/image".

To set a text to the right side of another text. Given that the first text's id is text1.
android:layout_toRightOf="text1"
To set a text to the left side of another text. Given that the first text's id is text1.
android:layout_toLeftOf="text1"
To set a text above another text. Given that the first text's id is text1.
android:layout_above="text1"

To set a text below another text. Given that the first text's id is text1.
android: layout_below="text1".
Another interesting thing about Relative Layout is that you can align views to the parent view group (e.g RelativeLayout).
To align a button to the parent bottom, android:layout_alignParentBottom="true"
To align a button to the top of a parent, android:layout_alignParentTop="true"
To align a button to the left of a parent, android:layout_alignParentLeft="true"
To align a button to the right of a parent, android:layout_alignParentRight="true"

To align a button to the center of a parent, android:layout_centerInParent="true"

Still working with Linear Layout

You can have more than one layout in your activity. You can have a relative layout and a linear layout in the same activity, that is why we call Nested View Group.
In this exercise, we will be making use of only linear layouts.
I will also show you how to use linear layout with horizontal orientation and vertical orientation.

Example 1.

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Donald"
        android:textSize="30sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Soo Yii"
        android:textSize="30sp" />

    <TextView
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tunde"
        android:textSize="30sp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ben"
        android:textSize="30sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Alice"
        android:textSize="30sp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Donald"
            android:textSize="20sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Soo Yii"
            android:textSize="20sp"
            android:layout_marginLeft="5dp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tunde"
            android:textSize="20sp"
            android:layout_marginLeft="5dp"  />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ben"
            android:textSize="20sp"
            android:layout_marginLeft="5dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Alice"
            android:textSize="20sp"
            android:layout_marginLeft="7dp" />

    </LinearLayout>

</LinearLayout>


 
 
Your App will look like this when you run it on your Android Phone
 
Example 2.
 
<?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:background="#87CEEB"
    android:orientation="vertical"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name: "
            android:textSize="30sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="James John"
            android:textSize="30sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Job: "
            android:textSize="30sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:text="Politician"
            android:textSize="30sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hobbies: "
            android:textSize="30sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Leading, Sleeping, Talking."
            android:textSize="30sp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Marital Status: "
            android:textSize="30sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="Engaged"
            android:textSize="30sp" />

    </LinearLayout>

</LinearLayout>

Your App will look like this when you run it on your Android Phone

To preview your Activity as you modify it on Android Studio, click Preview at the right side of your Android Studio.
Check the image below

You can add other views like ImageView and button to your activity.

Working with colors

You can use any HTML color you want. In the second example, I used HTML color by simply writing #87CEEEB.
Go on and check this link https://htmlcolorcodes.com/ for more colors that you can use.

Working with background
You can use colors for the background of your activity and you can use images likewise.
In the previous article I showed you how to display an image in your App. To set the background with an image having pasted the image(abc) in the drawable folder of your project
android:background="@drawable/abc".
To apply color as your activity background.
android: background=" #800080"


Working with XML

XML as you have learnt in the course of this tutorial is the acronym of eXtensible Markup Language, it is responsible for the layout of your app.

This is an example of an XML syntax.

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context="com.android.rexben.myapplication.MainActivity">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Hello World!"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintLeft_toLeftOf="parent"

        app:layout_constraintRight_toRightOf="parent"

        app:layout_constraintTop_toTopOf="parent" />





</android.support.constraint.ConstraintLayout>

Any tag you open in XML must be closed.
<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Hello">

    

</TextView>

While there are some self-closing tags
<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="Hello World!"

    android:textSize="30sp"

     />
Camel case is using uppercase for the first letter of words when two or more words are combined together. For instance: textColor, toBottomOf, TextView etc.

Working with TextViews

TextView is responsible for the plain texts that are displayed on your device. In order to display a text with XML, you will make use of the TextView.
Go to your activity_main.xml file, copy and paste the following line of code

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="My First Android App"

    android:textSize="30sp"

    android:textColor="@color/colorAccent"

    android:background="@color/colorPrimaryDark"

    />


android:layout_width="wrap_content" It is responsible for the width of your text, it could be set to match_parent, wrap_content, or to any size in dp (100dp, 50dp, 200dp). Same goes for the height.

android:text="My First Android App" Anything you enter here will show on your app
   
android:textSize="30sp" This affect how big your text will appear. Remember textSize are stored in “sp” and not “dp”.

android:textColor="@color/colorAccent" This affect the color of your text
  
  android:background="@color/colorPrimaryDark" This affect the background of your text and not your text.


Working with ImageView
ImageView is responsible for images displayed on your device. In order to display any type of image, you will make use of ImageView.

<ImageView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:src="@mipmap/ic_launcher"

    />

 android:src="@mipmap/ic_launcher" It is responsible for the source where your image is located.
@mipmap is the folder where the image is located and ic_launcher is the name of the image.

To display your Image
Go to your pictures or wherever you save the picture you want to use.
Copy it Ctrl + C or right click and select copy (for windows).
Paste on the drawable folder in res folder in your Android Studio project and click “Ok”
Then you use the image your app.

<ImageView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:src="@drawable/BeautyPlus_20171225181620_save"

    />

Working with Button

<Button
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"    android:text="Proceed"
   
/>

Working with margin and padding

When I started learning Android Programming, I had issues with margin and padding, I got used to margin and forgot padding with caused me little. Margin and Padding can be used interchangeably in some cases but I will spell out the difference.

You use margin when you want to adjust the whole view (textview, layout, imageview etc.). When you use android:layout_margin = “15dp”, it adjust the view at the top, bottom, left and right but if you want the margin for only top use android:layout_marginTop =”10dp”. For margin left, android:layout_marginLeft =”10dp”. For margin right, android:layout_marginRight =”10dp”. For margin bottom, android:layout_marginBottom=”10dp”.

You can use padding when you want to adjust the padding of a view. This is where it gets tricky, if use padding in a button, it will adjust the text inside the button and not the entire button. To adjust the whole button, you will use margin. Padding adjust the texts and not the whole view(TextView).
To use padding, android:padding =”16dp”, like margin it affects all the sizes. To set padding to the top, use android:paddingTop =”16dp”. To set padding to the left, android:paddingLeft =”16dp”. To set padding to the right, android:paddingRight =”16dp”  and to the bottom, android:paddingBottom =”16dp”.

Below is the example showing the different between margin and padding


As shown above, the first one is padding, you can see how padding was added to each sides, bringing all the text to the middle.
The second example is margin, you can see that it moved the TextView and not the text only.

Don’t forget to drop your comment. Tell us your challenges, having challenges is normal. I had a lot when I started the journey into Android Programming.


Walking with Layouts

Layouts are responsible for how your TextViews, ImageViews and other views are displayed. Layouts are called the parent view groups. I will introduce you to LinearLayout and RelativeLayouts for now.

LinearLayout
 As the name implies, it is linear and simple. Not complex or compound.

<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:padding="16dp"
    android:orientation="vertical"
    tools:context="com.android.rexben.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="30sp"
         />

</LinearLayout>

Copying this line of code will give you an error “unresolved package name”, to fix this;
Go to your MainActivity.java file and copy a line of code as indicated below
And paste in your activity_main.xml where I highlighted in the image below


As you enter more views, it will be arranged beside each other to the right (horizontal orientation) and below each other (vertical orientation).  
Whenever you are using LinearLayout, don’t forget to set your “orientation”, by default it is set to horizontal but if you want it to be set to vertical, use this code below.

android:orientation="vertical".