Close
Topic : Using Android Assets - Using Android Assets:



Layout Resources-Android : infobrother

Layout Resources:

When we Create Our App Interface, we use some special view that acts as a container. These special views control how other views are placed on the smartphone/tablet screen. Android provides a collection of Layout Managers and each of them implements a different strategy to hold, manage, and place its children.


  • We can declare Layout in two different ways:
    » Using XML: Here we can use XML file to describe, how our interface look like. In XML file, we define the Views and layouts that appear in the user interface. At the same time we define their properties.
    » At Runtime: In this case, We can define layout and their properites programmatically setting their attributes.



To create a UI, The best option is XML Vocabulary with tags that define the various types of elements that can compose a view. The concept behind Android XML Layout files is very similar to the way HTML tags are used to define web pages or Microsoft's XAML tags are used to define Windows presentation foundation (WPF) user interfaces.

The following example shows a simple view using a Linear Layout and Containing a Button and an image view.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <Button
           android:text="ChangeImage"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:id="@+id/button1" />
      <ImageView
           android:src="@drawable/logo" 
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:id="@+id/imageView1"
           android:scaleType="fitCenter"
           android:layout_marginBottom="0.0dp" />
</LinearLayout>


Care must be taken to aligning the name for elements and attributes in the XML vocabulary with class and method names from the application framework. In above Example, the element LinearLayout, Button and ImageView correspond to class names in the Application framework.



IDs:

Each View have a Unique Integer ID associated with it and can be used to reference the view from within an application's code. In XML file, the ID is specified as a User Friendlly text name. For example, consider the following line of code.


android:id="@+id/imageView1"

  • In Above Example -
    » @ operator tells the parser that it should treat the remainder of the string as an ID resource.
    » + operator tells the parser that this is new resource name that should be added to the resource file.
    » The resource file defines integer constants that can be used to reference resources.



Using XML Layouts:

XML Layouts can easily be loaded by any application at runtime. This task is generally performed from within the onCreate() method of an activity using the setContentView() method. Consider the follow line of code;


SetContentView(Resource.Layout.Main);

Above line of code will set the content view or user interface of application as define in Resource/layout/main.axmlfile.



In this tutorial, we just knwo about Layout resources, in our upcoming tutorials, we will discuss Layout in depth.
















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement