Layouts in Android

Step 1: Start an android project.

Step 2: In the strings.xml file write the following code.

<resources>
    <string name="app_name">Layouts Demo</string>

    <string name="Linear_Layout">Linear Layout Demo</string>
    <string name="Linear_Layout_Horizontal">Linear Layout (Horizontal)</string>
    <string name="Linear_Layout_Vertical">Linear Layout (Vertical)</string>

    <string-array name="array_layouts">
        <item>Select a layout</item>
        <item>Linear Layout Demo</item>
        <item>Table Layout</item>
        <item>Frame Layout</item>
    </string-array>

</resources>

Step 3: Design the first page as shown in the video.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="269dp"
        android:layout_height="79dp"
        android:layout_marginTop="150dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Step 4: Create three pages namely linear_layout_demo, table_layout_demo, and frame_layout_demo and to do this, Right-click on layout folder and select New –> Activity –> Empty activity and then give a name to the activity. This will add new pages to the app.

Step 5: Design the newly added pages (linear_layout_demo, table_layout_demo, and frame_layout_demo) as shown in the video.

Step 6: Open MainActivity.java and write the following code.

package com.example.part_b2_layouts_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
        implements AdapterView.OnItemSelectedListener {

    String[] arr_layouts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Spinner spin=(Spinner)findViewById(R.id.spinner);
        spin.setOnItemSelectedListener(this);

        arr_layouts=getResources().getStringArray(R.array.array_layouts);

        //Creating the ArrayAdapter instance having the layout list
        ArrayAdapter aa = new
                ArrayAdapter(this,android.R.layout.simple_spinner_item,arr_layouts);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //Setting the ArrayAdapter data on the Spinner
        spin.setAdapter(aa);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String s=((TextView)view).getText().toString();
        if(s.equals("Linear Layout Demo"))
            startActivity(new Intent(view.getContext(), Linear_Layout_Demo.class));
        if(s.equals("Table Layout"))
            startActivity(new Intent(view.getContext(), Table_Layout.class));
        if(s.equals("Frame Layout"))
            startActivity(new Intent(view.getContext(), Frame_Layout_Demo.class));
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

Step 7: Run the project.