0% found this document useful (0 votes)
6 views

layout and ui and view (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

layout and ui and view (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

LinearLayout :

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:background="#FFFFFF">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, LinearLayout!"
android:textSize="20sp"
android:textColor="#000000" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
</LinearLayout>
AbsoluteLayout
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello!"
android:textSize="20sp"
android:layout_x="50dp"
android:layout_y="100dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_x="50dp"
android:layout_y="150dp" />
</AbsoluteLayout>

RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="#FFFFFF">

<TextView
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, RelativeLayout!"
android:textSize="20sp"
android:textColor="#000000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_below="@id/myText"
android:layout_marginTop="20dp" />
</RelativeLayout>

TableLayout
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#FFFFFF">

<TableRow>
<TextView
android:text="Row 1, Col 1"
android:padding="10dp"
android:layout_column="0" />

<TextView
android:text="Row 1, Col 2"
android:padding="10dp"
android:layout_column="1" />
</TableRow>

<TableRow>
<TextView
android:text="Row 2, Col 1"
android:padding="10dp"
android:layout_column="0" />

<TextView
android:text="Row 2, Col 2"
android:padding="10dp"
android:layout_column="1" />
</TableRow>
</TableLayout>

GridLayout
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2"
android:padding="16dp"
android:alignmentMode="alignMargins"
android:useDefaultMargins="true">

<!-- Row 1, Column 1 -->


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />

<!-- Row 1, Column 2 -->


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />

<!-- Row 2, Column 1 -->


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />

<!-- Row 2, Column 2 -->


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 4" />
</GridLayout>

FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0E0E0"
android:padding="16dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a TextView"
android:textSize="20sp"
android:gravity="center"
android:background="#FFC107"
android:padding="20dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_gravity="center" />
</FrameLayout>

<?xml version="1.0" encoding="utf-8"?>


<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
android:background="#FFFFFF">

<!-- TextView -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the UI Demo"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_marginBottom="16dp" />

<!-- EditText -->


<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:layout_marginBottom="16dp" />

<!-- Button -->


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_marginBottom="16dp" />

<!-- ToggleButton -->


<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Wi-Fi ON"
android:textOff="Wi-Fi OFF"
android:layout_marginBottom="16dp" />

<!-- RadioGroup with multiple RadioButtons -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your gender:"
android:textSize="16sp"
android:layout_marginBottom="8dp" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="16dp">

<RadioButton
android:id="@+id/radio_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/radio_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
<RadioButton
android:id="@+id/radio_other"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Other" />
</RadioGroup>

<!-- Multiple CheckBoxes -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select your hobbies:"
android:textSize="16sp"
android:layout_marginBottom="8dp" />

<CheckBox
android:id="@+id/checkbox_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Music" />

<CheckBox
android:id="@+id/checkbox_sports"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sports" />

<CheckBox
android:id="@+id/checkbox_reading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reading"
android:layout_marginBottom="16dp" />

<!-- ImageButton -->


<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_camera"
android:contentDescription="Camera Button" />
</LinearLayout>
</ScrollView>
Here is the complete code for each of the examples (GridView, ListView, and ImageView)
combined in one Android app:

1. GridView Example

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:numColumns="3"

/>
</LinearLayout>

MainActivity.java:

package com.example.gridviewexample;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;

public class MainActivity extends AppCompatActivity {

GridView gridView;
String[] numbers = {"One", "Two", "Three", "Four", "Five", "Six"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gridView = findViewById(R.id.gridView);

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1, numbers);

gridView.setAdapter(adapter);
}
}
2. ListView Example

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>

MainActivity.java:

package com.example.listviewexample;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

ListView listView;
String[] items = {"Apple", "Banana", "Orange", "Mango"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
}
}

3. ImageView Example

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">

<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/sample_image" />
</LinearLayout>

MainActivity.java:

package com.example.imageviewexample;

import android.os.Bundle;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageView = findViewById(R.id.imageView);

}
}

ScrollView

1. activity_main.xml with ScrollView


<?xml version="1.0" encoding="utf-8"?>
<androidx.scrollview.widget.ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<!-- LinearLayout inside ScrollView for content arrangement -->


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<!-- Adding a TextView -->


<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is a TextView inside a ScrollView"
android:textSize="18sp"
android:paddingBottom="10dp" />

<!-- Adding an ImageView -->


<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/sample_image"
android:layout_marginBottom="20dp"
android:contentDescription="Image inside ScrollView"/>

<!-- Adding a ListView -->


<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

</LinearLayout>
</androidx.scrollview.widget.ScrollView>

2. MainActivity.java
package com.example.scrollviewexample;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

TextView textView;
ListView listView;
String[] items = {"Apple", "Banana", "Orange", "Mango"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Initialize views
textView = findViewById(R.id.textView);
listView = findViewById(R.id.listView);

// Set up a simple adapter for ListView


ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
}
}

You might also like