Android Studio Activity Layout Android

In previous tutorial you learn about TextView and about it’s method.Android studio activity layout android tutorial will teach how to create new activity.How to start an Activity and in the end i’ll give introduction about Android Manifest.

Basically you will create a new activity and then you will create new layout for your newly created activity.Next you will start new activity via Intent.

Let’s start with some basic introduction of Layouts and different type of layouts.

Layout

Layout is visual structure for user on which you can use different UI components like button for user interaction.Layout basically created for an activity because when ever you press any button it will show Toast because you have coded that Toast in your Activity.

You can create layout in two ways

  • Declare a UI component in XML
  • Instantiate layout elements at runtime

for more detail you can visit Android Official Website.

Always remember one thing that every UI component in layout including layout have width and height factor.

Different type of Layouts.

  • Relative Layout
  • Linear Layout
  • Table Layout
  • Absolute Layout
  • Frame Layout

You can learn about these layouts in detail from Eclipse tutorial section.

Android Manifest

Android Manifest is an essential component of an Android Application.You can say it’s back bone of Android Application.It stores all information about the application like activities,services etc.For Basic it contains three most important parameters.

  • Manifest : – <manifest> Manifest is the root element which contains application,activities and can be declared just once.
  • Application : –  <application> this is used to declare application whatever within the application.Can be declared once.It can also be used to declare other components like services,broadcast receiver etc.
  • Activity : – <activity> Used to declare  classes.

and also within root element <manifest> it contains sdk version which application target min-max.You can also add permission into that like camer permission,contacts permission to read etc.This might help you better. For more Detail.

Android Studio Activity,android studio activity layout,android studio activity layout android,activity layout android,

Alright let’s start create new android studio activity layout android application project.Go to your app > res > layout > activity_main.xml file and add a button.

Android Studio Activity,android studio activity layout,android studio activity layout android,activity layout android,

Now you will create new Layout and Activity/class.First go for layout so for that purpose go to app > res > Layout.You will find activity_main.xml layout there don’t touch it.Next right click on Layout directory and select New > Layout Resource File and give your layout name home.xml.After creating new layout just drop a single Textview.

Android Studio Activity,android studio activity layout,android studio activity layout android,activity layout android,

Next Activity go to app > java > your_package.It will be on the top of java directory.Right click on it New > Java Class click and name your activity/class Home.java.

Android Studio Activity,android studio activity layout,android studio activity layout android,activity layout android,

Ok you created new activity next what you have to do is extend this activity with AppCompatActivity or any other class which you want.In may case here is the Home.java class.

Home.java

/**
 * Created by Anonymous on 9/5/2016.
 */
public class Home extends AppCompatActivity {

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

Now next step is most important step which mostly beginner’s forgot to do.Declare your newly created activity/class within Android Manifest.xml  as i have given some information above about this file.

Android Studio Activity,android studio activity layout,android studio activity layout android,activity layout android,

At last go to your MainActivity .java class and do the following steps.

  • Create button variable and refer id.
  • Create an intent to start new activity.
  • Apply click event on button and put intent within that click.

MainActivity.java

public class MainActivity extends AppCompatActivity {
    Button startBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        startBtn = (Button)findViewById(R.id.btn);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent n = new Intent(getApplicationContext(),Home.class);
                startActivity(n);
            }
        });
    }
}

Run your android application project.

Android Studio Activity,android studio activity layout,android studio activity layout android,activity layout android,

that’s it for this tutorial.

Thanks.