Android Layouts | Techbirds

Linear Layout A Linear Layout arranges all its children in one direction either vertical or horizontal.

this direction is set by using setOrientation() methd in Actiivty or android:orientation in .xml file. See the example.

To see the the Attributes and method of Linear Layout visit the page
Example: Vertical Linear Layout

main.xml Example: Horizontal Linear Layout

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

main.xml

Example: Horizontal Linear Layout

main.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

main.xml

Table Layout
Table Layout organizes content into rows and columns. The rows are defined in the layout XML, and the columns are determined automatically by Android. This is done by creating at least one column for each element. So, for example, if you had a row with two elements and a row with five elements then you would have a layout with two rows and five columns.

You can specify that an element should occupy more than one column using android:layout_span. This can increase the total column count as well, so if we have a row with two elements and each element has android:layout_span=”3? then you will have at least six columns in your table.

By default, Android places each element in the first unused column in the row. You can, however, specify the column an element should occupy using android:layout_column.

Table Layout Example
Here is an example of Table Layout in which there are 3 Rows and each rows has 1 or more Columns

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

Lets have a live example and create a User Login Screen
Create Login Screen Using Table Layout

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

Frame Layout FrameLayout is designed to display a single item at a time. You can have multiple elements within a FrameLayout but each element will be positioned based on the top left of the screen. Elements that overlap will be displayed overlapping. I have created a simple XML layout using FrameLayout that shows how this works.

Frame Layout Example

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

GridLayout
GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter.

GridView Example

GridViewActivity

public class GridViewActivity extends Activity { GridView gridView; static final String[] numbers = new String[] { “A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); gridView = (GridView) findViewById(R.id.gridView1); ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, numbers); gridView.setAdapter(adapter); gridView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(getApplicationContext(), ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); } }); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

public class GridViewActivity extends Activity {

GridView gridView;

static final String[] numbers = new String[] {

“A”, “B”, “C”, “D”, “E”,

“F”, “G”, “H”, “I”, “J”,

“K”, “L”, “M”, “N”, “O”,

“P”, “Q”, “R”, “S”, “T”,

“U”, “V”, “W”, “X”, “Y”, “Z”};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

gridView = (GridView) findViewById(R.id.gridView1);

ArrayAdapter adapter = new ArrayAdapter(this,

android.R.layout.simple_list_item_1, numbers);

gridView.setAdapter(adapter);

gridView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView parent, View v,

int position, long id) {

Toast.makeText(getApplicationContext(),

((TextView) v).getText(), Toast.LENGTH_SHORT).show();

}

});

}

}

692 total views, 1 views today

Share this Onfacebook-9351399twitter-9753978linkedin-5379892google-8753405 Tags: Android layouts