How to use Margin and Padding for your Android app development


In this lesson, you'll learn to set margins and paddings to add space between widgets. Run this app which shows a question and yes-no buttons.
Add attribute android:layout_margin with value 20dp to the TextView element. You can also use the designer. Add attribute android:layout_marginRight with value 30dp to the Yes Button element. Add attribute android:padding with value 20dp to the Yes Button element. Now add attribute android:padding with value 20dp to the No Button element as well.  Congratulations! You've learned about margins and paddings. Set some more margins and paddings in this layout on your own and see what happens. Then continue with the next lesson. 

REQUIREMENTS:
Download pdf

Instruction:
After you download all the files needed in this tutorial, we need to install and set-up a java JDK path to our environment variables, the step below will show you how.

--Go to Java JDK installation directory in "C:\Program Files\Java\jdk1.7.0\bin" Copy this path
--Go to the environment path by Clicking: Window taskbar--->right click Computer--->Click Advance System setting ---> then go to Advanced tab Then select "Environment Variable" then create new Variable name and variable value by clicking the New radio button. Within the "Variable name" input these "JAVA_HOME" then in the "Variable value" point it to your C:\Program Files\Java\jdk1.7.0\bin 
that's it we are on a way of creating our first game and deploy it to a different platform.

After that we will also set-up Android SDK, to its path by extracting first it to the directory you want for example in "drive D:" or "drive C:"  once we did this, we can now copy the root folder of this SDK which is likely to this format "D:\Android SDK" copy it then add it also to the "Environment path" like what you did before in "Java JDK".




COMPLETE CODES

MAIN XML CODE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android is the best?"
android:textSize="20dp"/>

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

<Button
android:text="Yes"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:padding="20dp"
android:layout_width="wrap_content"/>

<Button
android:text="No"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="20dp"/>

</LinearLayout>


</LinearLayout>


MAIN ACTIVITY CODE


import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        
        // Set main.xml as user interface layout
        setContentView(R.layout.activity_main);
    }


    }

Post a Comment

0 Comments