Layout Utils : Layout « UI « Android






Layout Utils

    
//package com.onerm.ui;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;

public class LayoutUtils {

  public enum Layout {
    WidthFill_HeightFill,
    WidthWrap_HeightWrap,
    WidthWrap_HeightFill,
    WidthFill_HeightWrap;

    public void applyViewGroupParams(View component) {
      applyViewGroupLayoutParamsTo(this, component);
    }

    public void applyLinearLayoutParams(View linearlayout) {
      applyLinearLayoutParamsTo(this, linearlayout);
    }

    public void applyTableRowParams(View cell) {
      applyTableRowLayoutParamsTo(this, cell);
    }

    public void applyTableLayoutParams(View row) {
      applyTableLayoutParamsTo(this, row);
    }

  }

  private static void applyLinearLayoutParamsTo(Layout layout, View view) {

    switch (layout) {
      case WidthFill_HeightFill:
        view.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.FILL_PARENT));
        break;
      case WidthFill_HeightWrap:
        view.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.FILL_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
        break;
      case WidthWrap_HeightFill:
        view.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.FILL_PARENT));
        break;
      case WidthWrap_HeightWrap:
        view.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
        break;
    }

  }

  private static void applyViewGroupLayoutParamsTo(Layout layout, View view) {

    switch (layout) {
      case WidthFill_HeightFill:
        view.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.FILL_PARENT));
        break;
      case WidthFill_HeightWrap:
        view.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.FILL_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
        break;
      case WidthWrap_HeightFill:
        view.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.FILL_PARENT));
        break;
      case WidthWrap_HeightWrap:
        view.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
        break;
    }

  }

  private static void applyTableRowLayoutParamsTo(Layout layout, View view) {
    switch (layout) {
      case WidthFill_HeightFill:
        view.setLayoutParams(new TableRow.LayoutParams(
            TableRow.LayoutParams.FILL_PARENT,
            TableRow.LayoutParams.FILL_PARENT));
        break;
      case WidthFill_HeightWrap:
        view.setLayoutParams(new TableRow.LayoutParams(
            TableRow.LayoutParams.FILL_PARENT,
            TableRow.LayoutParams.WRAP_CONTENT));
        break;
      case WidthWrap_HeightFill:
        view.setLayoutParams(new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.FILL_PARENT));
        break;
      case WidthWrap_HeightWrap:
        view.setLayoutParams(new TableRow.LayoutParams(
            TableRow.LayoutParams.WRAP_CONTENT,
            TableRow.LayoutParams.WRAP_CONTENT));
        break;
    }

  }

  private static void applyTableLayoutParamsTo(Layout layout, View view) {
    switch (layout) {
      case WidthFill_HeightFill:
        view.setLayoutParams(new TableLayout.LayoutParams(
            TableLayout.LayoutParams.FILL_PARENT,
            TableLayout.LayoutParams.FILL_PARENT));
        break;
      case WidthFill_HeightWrap:
        view.setLayoutParams(new TableLayout.LayoutParams(
            TableLayout.LayoutParams.FILL_PARENT,
            TableLayout.LayoutParams.WRAP_CONTENT));
        break;
      case WidthWrap_HeightFill:
        view.setLayoutParams(new TableLayout.LayoutParams(
            TableLayout.LayoutParams.WRAP_CONTENT,
            TableLayout.LayoutParams.FILL_PARENT));
        break;
      case WidthWrap_HeightWrap:
        view.setLayoutParams(new TableLayout.LayoutParams(
            TableLayout.LayoutParams.WRAP_CONTENT,
            TableLayout.LayoutParams.WRAP_CONTENT));
        break;
    }
  }

  public static void addLeftOfRule(RelativeLayout.LayoutParams params, View view) {
    params.addRule(RelativeLayout.LEFT_OF, view.getId());
  }

  public static void addRightOfRule(RelativeLayout.LayoutParams params, View view) {
    params.addRule(RelativeLayout.RIGHT_OF, view.getId());
  }
}

   
    
    
    
  








Related examples in the same category

1.Load Layout from xml layout file
2.android:layout_width and android:layout_height
3.Provide layout for different screen size
4.Config your own layout through xml
5.extends FrameLayout
6.Table layout inside a Linear layout
7.Set layout alignment base line
8.Using more than one layout xml
9.Using LayoutInflater
10.res\layout\main.xml
11.Using static string in layout xml file from strings.xml
12.Layout Orientation
13.Set Layout Parameters in your code
14.Layout widget with code only
15.Using two layout xml file for one Activity
16.Create the user interface by inflating a layout resource.
17.Layout input form
18.Layout Animation
19.extends ViewGroup to do layout
20.A layout that arranges its children in a grid.
21.Use the animateLayoutChanges tag in XML to automate transition animations as items are removed from or added to a container.
22.Use LayoutTransition to automate transition animations as items are hidden or shown in a container.
23.Layout gravity bottom
24.Layout gravity center vertical
25.Layout align Baseline, align Right
26.Layout grid fade
27.Layout bottom to top slide
28.Layout random fade
29.Layout grid inverse fade
30.Layout wave scale
31.Layout animation row left slide
32.Demonstrates a simple linear layout. The height of the layout is the sum of its children.
33.Demonstrates a simple linear layout. The layout fills the screen, with the children stacked from the top.
34.A simple linear layout fills the screen, with the children stacked from the top. The middle child gets allocated any extra space.
35.Demonstrates a horizontal linear layout with equally sized columns
36.Demonstrates a nesting layouts to make a form
37.Demonstrates a horizontal linear layout with equally sized columns. Some columns force their height to match the parent.
38.A simple layout which demonstrates stretching a view to fill the space between two other views.
39.Demonstrates using a relative layout to create a form
40.Demonstrates wrapping a layout in a ScrollView.
41.This example shows how to use cell spanning in a table layout.
42.Tabs that uses labels TabSpec#setIndicator(CharSequence) for indicators and views by id from a layout file TabSpec#setContent(int)
43.This sample application shows how to use layout animation and various transformations on views.
44.Create Linear Layout
45.Create LayoutParam
46.Inflate Layout
47.Set text Size
48.Set right margin
49.Baseline nested
50.Set baselineAlignedChildIndex
51.Align along with Parent
52.Using LayoutParams