Skip to main content

Android Layouts

There are two ways to create Layout.
  1. Layouts in XML
  2. Layouts in Code

1. Layouts in XML

Example: main.xml(Layout file)
     <?xml version=”1.0” encoding=”utf-8”?>
     <LinearLayout   xmlns:android=”http://schemas.android.com/apk/res/android”
                                android:orientation=”vertical”
                                android:layout_width=”fill_parent”
                                android:layout_height=”fill_parent” >
     <TextView   android:id=”@+id/mytxt”
                          android:layout_width=”fill_parent”
                          android:layout_height=”wrap_content”
                          android:text=”@string/Write here..” />
     </LinearLayout>

    • It's a preferred way of implementing your user interfaces (UIs) or defining your UI in XML.
    • As it decouples your application logic from your UI design.
    • Keeping the visual design decoupled from the application code, helps keep the code concise.
    • To get access to the UI elements we have to add identifier(id) attributes to them in the XML definition.
                                    android:id=”@+id/mytxt”

    • The above line generates an id named "mytxt" for TextView,which are stored in /gen/packageName/R.java of your poject hierarchy.
           In R.java file, following line generates:
         public final class R {
              public static final class id {
                            public static final int mytxt=0x7f050044;
                                        }
                             }

      2. Layouts in Code

           public class MainActivity extends Activity {
                public void onCreate(Bundle savedInstanceState) {
                     super.onCreate(savedInstanceState);

                    LinearLayout ll = new LinearLayout(this);
                    ll.setOrientation(LinearLayout.VERTICAL);

                    TextView myTxtView = new TextView(this);
                    EditText myEdtText = new EditText(this);
                    myTxtView.setText(“Enter Text”);
                    myEdtText.setText(“Write Text Here!”);

                    int lHeight = LinearLayout.LayoutParams.MATCH_PARENT;
                    int lWidth = LinearLayout.LayoutParams.WRAP_CONTENT;

                    ll.addView(myTxtView, new LinearLayout.LayoutParams(lHeight, lWidth));
                    ll.addView(myEdtText, new LinearLayout.LayoutParams(lHeight, lWidth));
                    setContentView(ll);
       }   }

        It's not a preferred way of implementing your user interfaces (UIs).

        Comments

        Top Research

        Make Custom Android Launcher

        There are thousands of Android Launcher available in Play Store. Each and every Android mobile user loves to customize their mobile with different Android Launcher. If you want to develop your own launcher, this post helps you to fulfill your wish. In this post, you will find a demo launcher with layout and code which include many features like Drag & Drop , rearrange the installed application, change the background wallpaper etc. Here is an example of demo launcher which display all the installed applications in 3*3 Grid View format compatible to all devices. First of all we need to find all the installed applications from device so we should implement a subclass of AsyncTaskLoader that loads the currently installed applications from the package manager. If you want to display all the installed applications in  List View you can use official Android Developer reference .

        Introduction to Android

        In Android, visual components are called Views . The resources for an Android project are stored in the /res folder of your project hierarchy, which includes layout, values, and a series of drawable sub-folders. The ADT plug-in interprets these resources to provide design-time access to them through the R variable. Dalvik Debug Monitoring Service (DDMS) :  Use to monitor and control the  services or processes of emulators. Android Debug Bridge (ADB) : A driver which provides link to virtual to physical devices. Logcat : Used to view the processes  of android logging system. Android Asset Packaging Tool (AAPT) : Used to generate .apk file.

        Layouts for Fragment

        Fragments are use to develop dynamic and flexible or consistent User Interface(UI) design that can be supported by all screen sizes from small to large. In short, we can build Multi-pane UI using Fragments. We can divide the Activities into different reusable components which are known as Fragments. Fragments can be reused within multiple activities. Each Fragment has its own lifecycle and Layout and they are independent to each other that are embedded into an Activity. There are two class, Fragment and FragmentActivity that must be extended by the subclass of it. To know more about Fragment VS FragmentActivity. We can define a static or dynamic layout using Fragment.