Android TutorialsLayouts In Android

Layouts In Android

In our blog today you will learn to develop a simple layout in android. We will show you the simple steps to follow it.

Steps to develop simple layout in android:

  • What are layouts?

Layouts are the container controls that contains another controls and also organize their display.

    Two types of layouts:
Linear Layout : In this layout controls are aligned either vertically or horizontally within the layout.

Relative Layout : In this layout controls are aligned with respect to the center and edges of the layout ,and with each other.

In relative layout we get the freedom to place the control where ever we want.

  • Implementing layouts :
  1. Create an android application project with their respective .java and .xml files.
  2. Create one more android xml file.
  3. Now in your activity_main.xml file take a radio button and a simple button.
  4. This will be by default a LinearLayout hence the buttons will be aligned horizontally or can be aligned vertically with the help of orientation but not centrally.
  5. So now your activity_ main view will be..
  6. 1

  7. Now take the second .Xml file and set the LinearLayout to RelativeLayout.
  8. Now in your graphical Layout you can take a radio button and a simple button where ever you want but only in the second file where you have set the layout to RelativeLayout.
  9. Graphical Layout will be seen as…
  10. 2

  11. In the strings.xml file add the resources for the respective radiobutton and simple button that we have used.
  12. 3

  13. Now for the onClick i.e the button property in the second xml file we have to write the method in the MainActivity.java file.
  14. The code for the following is as follows :
  15. [code language=”java”]
    public void btnTouch(View v)
    {
    RadioButton radio = (RadioButton) findViewById(R.id.radioButton1);
    if(radio.isChecked())radio.setChecked(false);
    else radio.setChecked(true);
    }
    [/code]

  16. The btnTouch is the value given to the onClick property.
  17. So now when you click on the button in the application your radio button wlii be set on or off depending on its status .
  18. Remember to set the content view in the onCreate ()
  19. Method in the MainActivity from activity_main.xml to your second xml file .
  20. Now run the MainActivity.java file.
  21. 4

  22. Now when the radio button is off its status can be set ON/OFF by clicking on the ON/OFF button.
  23. Thus we have implemented the Layouts.

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -