50 likes | 106 Views
In this lesson, we will discuss Types of Android UI layouts and it is a part of android app development course
E N D
Downloaded from: justpaste.it/72hy5 Types of Android UI layouts Layout is used in android to describe the user interface for an app or function, and it will carry the Android UI elements that appear to the user. In this article let us discuss different types of layouts in Android UI. Android UI layout An Android layout is a class that arranges how its kids show up on the computer. Anything that is a View (or View inherits) can be a layout body. All of the layouts are inherited from ViewGroup (which View inherits) so you can nest layouts. You can also create your own custom layout by creating a class which is inherited from ViewGroup. The image below illustrates the hierarchy of inheritance between views in Android. To take complete android course visit Online It Guru android app development course Blog. In the android app, the user interface is made from a set of View and ViewGroup objects. For instance, one or more tasks will be found in the android apps, and each activity is one screen of the app. The activities can contain multiple components of the Android UI and those components of the Android UI are instances of subclasses of View and ViewGroup. The View is a base class for all components of the Android UI in android and is used to build interactive components of the Android UI such as TextView, EditText, Checkbox, Radio Click, etc. and is responsible for handling and drawing events. The ViewGroup is a View subclass, and will serve as a base class for parameter layouts and layouts. The ViewGroup will provide a transparent container for holding certain Views or ViewGroups and defining properties for the layout.
Check out this Android View and ViewGroup to learn more about View and ViewGroup in smartphone apps. Within android, we can define a layout in two ways: ● Declare Android UI elements in XML ● Instantiate Layout The android framework will allow us to use one or both of those methods to define the Android UI of our application. Declare Android UI elements in XML In android, by using default View in android XML and ViewGroups in the XML format, we can build layouts the same as web pages in HTML. The layout file must contain only one root element, which must be an object called View or ViewGroup. After the root element is specified, additional layout objects or widgets may be added to build the View Hierarchy which defines our layout as child elements. The example below is the description of a layout in an XML file (activity main.xml) using LinearLayout to hold a TextView, EditText, and click. setContentView(R.layout.activity_main); } If you look at the above code, we name our l. Here our file name xml is activity main.xml so we used activity main as the file name. In general, the onCreate() callback method will be called by android framework during the start of our application in order to get the correct layout for an application. Instantiate Layout Elements at Runtime We need to build our own custom View and ViewGroup objects with appropriate templates programmatically if we want to instantiate layout elements at run time. The following is the example of building a layout using LinearLayout to programmatically keep a TextView, EditText, and Button in an operation using custom View and ViewGroup objects. public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TextView textView1 = new TextView(this); textView1.setText("Name:"); EditText editText1 = new EditText(this); editText1.setText("Enter Name"); Button button1 = new Button(this); button1.setText("Add Name"); LinearLayout linearLayout = new LinearLayout(this); linearLayout.addView(textView1); linearLayout.addView(editText1); linearLayout.addView(button1); setContentView(linearLayout); } } Width and height Using attributes layout width and layout height, when we define a layout using XML file, we need to set width and height for each View and ViewGroup feature. The following is an example of setting the width and height in XML layout file for View and ViewGroup components.
<?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"> <TextView android:id="@+id/fstTxt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Name" /> </LinearLayout> If we set the value wrap information, then the View or ViewGroup will attempt to change its information dependent width or height. Benefits of using XML layout in Android UI The benefits of XML-based interfaces based on XML are of great help if you know the components of the Android UI at the time of compilation. If components of the runtime Android UI are needed, they can be added using the XML code. The benefits of XML-based architecture are as follows: ● XML is a popular format which is widely used. A lot of developers are therefore very happy with that. ● This helps ensure that the Android UI is isolated from the application logic. It allowed versatility in modifying one without affecting much of the other. ● XML output generation is simpler than direct code writing, making it easier for android apps to have drag-and-drop Android UI tools to build interfaces. Application Layout Attributes In android we have a different type of attributes available for View and ViewGroup objects, such as layout width and layout height to describe the appearance of layouts based on our needs. Some of the common interface attributes used for the android application are as follows. Attribute Definitions android: id You use it to uniquely identify the view and ViewGroups android: layout width You use it to define the width for View and ViewGroup elements in an android layout: layout height Used to define the height for View and ViewGroup elements in an android layout: layout marginLeft Used to define the extra space for View and ViewGroup elemen in the left side. Types of layout The templates commonly used in android applications for implementing the necessary designs are as follows. ● Linear Layout ● Relative Layout ● Frame Layout ● Table Layout ● Web View ● List View
● Grid View Linear Layout LinearLayout in android is a ViewGroup subclass used to make all instances of child views one by one either in a horizontal or vertical direction depending on the orientation properties. Application Relative Layout RelativeLayout in android is a ViewGroup that is used to determine the location of the child View instances connected to each other (Child A on the left of Child B) or relative to the parent (Aligned to the top of the parent). Android Frame Layout Android Frame Layout In ios, FrameLayout is a subclass of ViewGroup which is used to define the location of the instances of View to which it belongs. Android Table Layout In ios, TableLayout is a subclass of the ViewGroup that shows elements of the child View in rows and columns. Android Web View In android, WebView is a browser which is used as part of our operation interface to view the web pages. Android List View In android, ListView is a ViewGroup that is used to display a scrollable list of items in a single column. Android Grid View In android, GridView is a ViewGroup that uses columns and rows to display objects in a scrollable grid. Conclusion: I hope you got an understanding of types of Android UI layouts. You can learn more from android online training