0% found this document useful (0 votes)
25 views72 pages

Day-1 App Development-Final

Intro to MAD xml

Uploaded by

cvlnpsg987
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)
25 views72 pages

Day-1 App Development-Final

Intro to MAD xml

Uploaded by

cvlnpsg987
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/ 72

Setting Up an Android Virtual Device (AVD) in Android Studio:

To add Android Virtual Device:


Click on start button to start android virtual device.
1. Creating a First App
activity_main.xml:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:
package com.example.helloworldsimpleapplication;

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

public class MainActivity extends AppCompatActivity {

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

}
}
Output:
Components of Android Development:
1. Linear Layout: (HelloWorld_SimpleApplication)
A. Vertical Linear Layout:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

</LinearLayout>

MainActivity.java:
package com.example.helloworld_simpleapplication;

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

public class MainActivity extends AppCompatActivity {

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

}
}
Output:
B. Linear Horizontal Layout:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

</LinearLayout>

MainActivity.java:
package com.example.helloworld_simpleapplication;

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

public class MainActivity extends AppCompatActivity {

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

output:
2. Relative Layout:( RelativeLayout_SimpleApplication )
activitymain.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/bta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ButtonA"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>

<Button
android:id="@+id/btb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ButtonB"
android:layout_above="@+id/bta"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

MainActivity.java:
package com.example.relativelayout_simpleapplication;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
};
}

Output:
3. Table Layout: ( TableLayout_SimpleApplication)
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
</TableRow>

<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />

<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
</TableRow>

</TableLayout>

MainActivity.java:
package com.example.tablelayout_simpleapplication;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}
}
Output:
4. TextView Component: It displays the text to user
activity_main.xml:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:paddingStart="20dp"
android:paddingTop="10dp"
tools:context=".MainActivity">

<TextView
android:id="@+id/textexample"
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_marginStart="150dp"
android:background="@color/design_default_color_error"
android:gravity="center_horizontal"
android:text="Hello World to the humans"
android:textColor="@color/white"
android:textSize="20dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:
package com.example.textview_simpleapplication;

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

import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView tv;

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

tv = findViewById(R.id.textexample);

tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

tv.setBackgroundColor(Color.YELLOW);
tv.setTextColor(Color.BLACK);
}
});

}
}
Output:
5. Button Component: It represents a push button that can be clicked or
pressed by user to perform an action.
activity_main.xml:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:paddingStart="20dp"
android:paddingTop="10dp"
tools:context=".MainActivity"
android:orientation="vertical">

<TextView
android:id="@+id/textexample"
android:layout_width="150dp"
android:layout_height="75dp"
android:background="@color/design_default_color_error"
android:gravity="center"
android:text="Hello World to the humans"
android:textColor="@color/white"
android:textSize="20dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<Button
android:id="@+id/btnEvening"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/design_default_color_on_secondary"
android:text="Do Magic!"
android:textSize="24dp"
app:layout_constraintTop_toBottomOf="@+id/textexample"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.1" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:
package com.example.button_simpleapplication;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView tv;
Button bt1;

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

tv = findViewById(R.id.textexample);
bt1 = findViewById(R.id.btnEvening);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

bt1.setBackgroundColor(Color.YELLOW);
bt1.setText("Magic Done!");
tv.setText("Have you seen Magic");
}
});

}
Output:
6. EditTexts Component: It is an UI control which allows user to enter or
modify the text.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/design_default_color_secondary"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<EditText
android:id="@+id/editTextPersonName"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="@android:drawable/alert_light_frame"
android:ems="10"
android:hint="Enter your name here"
android:inputType="text"
android:textSize="20sp"
/>

<Button
android:id="@+id/buttonOkay"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:backgroundTint="@color/white"
android:text="Submit"
android:textColor="@color/black"
android:textSize="40dp" />

<TextView
android:id="@+id/textViewResult"
android:layout_width="300dp"
android:layout_height="35dp"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="Result"
android:textColor="@color/black"/>

</LinearLayout>

MainActivity.java:
package com.example.edittexts_simpleapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText ed;
Button bt;
TextView tvresult;
String Username;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = findViewById(R.id.editTextPersonName);
bt = findViewById(R.id.buttonOkay);
tvresult = findViewById(R.id.textViewResult);

bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Username = ed.getText().toString();
tvresult.setText(Username);
}
});

}
}
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="design_default_color_secondary">#FF00FF00</color>

</resources>
Output:
7. ImageView Component: It is used to display an image file in android
application.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageViewExample"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/image1" />

<Button
android:id="@+id/buttonOkay"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="Change Image On Click"
android:textColor="@color/white"/>

</LinearLayout>
MainActivity.java:
package com.example.imageview_sampleproject;

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

public class MainActivity extends AppCompatActivity {

EditText et;
Button bt;
ImageView iv;

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

bt = findViewById(R.id.buttonOkay);
iv = findViewById(R.id.imageViewExample);

bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
iv.setImageResource(R.drawable.image2);
}
});
}
}
Output:
8. CheckBox Component: It is a type of two state button either unchecked
or checked in Android.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textViewResult"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="Choose your gender?"
android:textColor="@color/black" />

<CheckBox
android:id="@+id/checkBoxMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="false"
android:text="Male"
android:textColor="@color/design_default_color_primary" />

<CheckBox
android:id="@+id/checkBoxFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Female" />

</LinearLayout>

MainActivity.java:
package com.example.checkbox_sampleproject;

import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.CheckBox;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

TextView tvresult;
CheckBox m,f;

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

tvresult = findViewById(R.id.textViewResult);
m = findViewById(R.id.checkBoxMale);
f = findViewById(R.id.checkBoxFemale);

m.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (m.isChecked())
{
tvresult.setText("Male");
f.setChecked(false);
}
else {
tvresult.setText("Please select gender");
}
}
});

f.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (f.isChecked())
{
tvresult.setText("Female");
m.setChecked(false);
}
else {
tvresult.setText("Please select gender");
}
}
});
}
}

Output:
9. Radio button Component: It allows user to select only one option from a
set. You should use radio buttons for optional sets that are mutually
exclusive if you think that the user needs to see all available options side-
by-side.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<RadioGroup
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/radioButtonGreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="Green" />

<RadioButton
android:id="@+id/radioButtonRed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Red" />
<RadioButton
android:id="@+id/radioButtonYellow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Yellow" />
</RadioGroup>

<Button
android:id="@+id/buttonOkay"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="Okay"
android:textColor="@color/white" />

</LinearLayout>

MainActivity.java:
package com.example.radiobutton_sampleproject;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

Button btn;
RadioGroup rg;
RadioButton rbgreen,rbred,rbyellow;
LinearLayout linlayout;

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

btn = findViewById(R.id.buttonOkay);
rg = findViewById(R.id.group);
rbgreen = findViewById(R.id.radioButtonGreen);
rbred = findViewById(R.id.radioButtonRed);
rbyellow = findViewById(R.id.radioButtonYellow);
linlayout = findViewById(R.id.linear);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rbgreen.isChecked())
{
linlayout.setBackgroundColor(Color.GREEN);
}
else if (rbred.isChecked())
{
linlayout.setBackgroundColor(Color.RED);

}
else
{
linlayout.setBackgroundColor(Color.YELLOW);
}
}
});
}
}

Output:
10. Toggle Button Component: It allows user to change a setting between two
states.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageViewExample"
android:layout_width="300dp"
android:layout_height="300dp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/image1" />

<ToggleButton
android:id="@+id/toggleButtonShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
android:textOff="Hide Image"
android:textOn="Show Image" />

<TextView
android:id="@+id/textViewResult"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="Output shown here"
android:textColor="@color/black" />
</LinearLayout>

MainActivity.java:
package com.example.tooglebutton_sampleproject;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

TextView tvresult;
ImageView iv;
ToggleButton tb;

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

tvresult = findViewById(R.id.textViewResult);
iv = findViewById(R.id.imageViewExample);
tb = findViewById(R.id.toggleButtonShow);

tb.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
iv.setVisibility(View.INVISIBLE);
tvresult.setText("Image Hidden");
} else {
iv.setVisibility(View.VISIBLE);
tvresult.setText("Image Shown");
}
}
});
}
}

Output:
11. Spinner Component: It provides a quick way to select one value from a set
(Dropdown list).
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Select Country"
android:textSize="20dp" />

<Spinner
android:id="@+id/spinnerCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="@+id/textViewResult"
android:layout_width="300dp"
android:layout_height="30dp"
android:layout_marginTop="30dp"
android:background="@color/white"
android:text="Displays selected country name here"
android:textColor="@color/black" />

</LinearLayout>

MainActivity.java:
package com.example.spinner_sampleproject;

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

public class MainActivity extends AppCompatActivity {

TextView tvresult;
ImageView iv;
Spinner sp;
ArrayAdapter adpt;

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

tvresult = findViewById(R.id.textViewResult);
sp = findViewById(R.id.spinnerCountry);
adpt = ArrayAdapter.createFromResource(this,
R.array.Countries,android.R.layout.simple_spinner_item);
adpt.setDropDownViewResource(android.R.layout.simple_spinner_item);
sp.setAdapter(adpt);

sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
String country = parent.getItemAtPosition(position).toString();
tvresult.setText(country);
}

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

}
});
}
}
strings.xml:
<resources>
<string name="app_name">Spinner_SampleProject</string>
<string-array name="Countries">
<item>America</item>
<item>Australia</item>
<item>Bangladesh</item>
<item>Chirala</item>
<item>Dehradun</item>
<item>England</item>
<item>France</item>
<item>Germany</item>
<item>Hindustan</item>
</string-array>
</resources>
Output:
12. Add Menu Items to Tool Bar:
activity_main.xml:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/toolbar_menu"
app:navigationIcon="@drawable/baseline_menu_24"
app:navigationIconTint="?attr/colorOnPrimary"
app:subtitle="Development"
app:subtitleTextColor="?attr/colorOnPrimary"
app:title="TOP APP BAR MATERIAL 3"
app:titleTextColor="?attr/colorOnPrimary" />

</com.google.android.material.appbar.AppBarLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java:
package com.example.addmenu_toolbar;

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

import com.google.android.material.appbar.MaterialToolbar;

public class MainActivity extends AppCompatActivity {

MaterialToolbar mtb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mtb= findViewById(R.id.toolbar);

}
}
baseline_edit_24.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:tint="?attr/colorOnPrimary"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp">

<path android:fillColor="@android:color/white"
android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-
3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39
-1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>

</vector>

baseline_share_24.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:tint="?attr/colorOnPrimary"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp">

<path android:fillColor="@android:color/white"
android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -
1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-
4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -
3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -
3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -
0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -
2.92,-2.92z"/>

</vector>
baseline_menu_24.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:tint="#000000"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp">

<path android:fillColor="@android:color/white"
android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-
2L3,11v2zM3,6v2h18L21,6L3,6z"/>

</vector>

toolbar_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/Share"
android:icon="@drawable/baseline_share_24"
android:title="Share"
app:showAsAction="always" />
<item
android:id="@+id/Edit"
android:icon="@drawable/baseline_edit_24"
android:title="Edit"
app:showAsAction="always" />

</menu>

Output:
13. Toast message: In Android is a small, quick, and temporary pop-up message
that provides feedback to the user. It appears on the screen for a short duration
and automatically disappears after the specified time.
activity_main.xml:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/toolbar_menu"
app:navigationIcon="@drawable/baseline_menu_24"
app:navigationIconTint="?attr/colorOnPrimary"
app:subtitle="Development"
app:subtitleTextColor="?attr/colorOnPrimary"
app:title="TOP APP BAR MATERIAL 3"
app:titleTextColor="?attr/colorOnPrimary" />

</com.google.android.material.appbar.AppBarLayout>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.toastmessageapplication;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.appbar.MaterialToolbar;

public class MainActivity extends AppCompatActivity {

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

mtb= findViewById(R.id.toolbar);

mtb.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Toast.makeText(getApplicationContext(),"Navigation Icon is
Clicked",Toast.LENGTH_SHORT).show();

}
});

mtb.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
{
@Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId()==R.id.Share)
{
Toast.makeText(getApplicationContext(),"Share Icon is
Clicked",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Edit Icon is
Clicked",Toast.LENGTH_SHORT).show();
}

return true;
}
});
}
}
baseline_edit_24.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:tint="?attr/colorOnPrimary"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp">

<path android:fillColor="@android:color/white"
android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-
3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39
-1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>

</vector>

baseline_share_24.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:tint="?attr/colorOnPrimary"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp">

<path android:fillColor="@android:color/white"
android:pathData="M18,16.08c-0.76,0 -1.44,0.3 -
1.96,0.77L8.91,12.7c0.05,-0.23 0.09,-0.46 0.09,-0.7s-0.04,-0.47 -0.09,-0.7l7.05,-
4.11c0.54,0.5 1.25,0.81 2.04,0.81 1.66,0 3,-1.34 3,-3s-1.34,-3 -3,-3 -3,1.34 -
3,3c0,0.24 0.04,0.47 0.09,0.7L8.04,9.81C7.5,9.31 6.79,9 6,9c-1.66,0 -3,1.34 -
3,3s1.34,3 3,3c0.79,0 1.5,-0.31 2.04,-0.81l7.12,4.16c-0.05,0.21 -0.08,0.43 -
0.08,0.65 0,1.61 1.31,2.92 2.92,2.92 1.61,0 2.92,-1.31 2.92,-2.92s-1.31,-2.92 -
2.92,-2.92z"/>

</vector>

baseline_menu_24.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:tint="#000000"
android:viewportHeight="24"
android:viewportWidth="24"
android:width="24dp">

<path android:fillColor="@android:color/white"
android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-
2L3,11v2zM3,6v2h18L21,6L3,6z"/>

</vector>

toolbar_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/Share"
android:icon="@drawable/baseline_share_24"
android:title="Share"
app:showAsAction="always" />
<item
android:id="@+id/Edit"
android:icon="@drawable/baseline_mode_edit_24"
android:title="Edit"
app:showAsAction="always" />
</menu>
Output:
14. Snackbar Message: It is used to show messages in the bottom of the
application with swiping enabled.
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:gravity="center"
android:padding="16dp"
android:background="@color/design_default_color_secondary">

<Button
android:id="@+id/buttonShowSnackbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Snackbar"
android:textSize="18sp"
android:padding="10dp"
android:backgroundTint="@color/white"
android:textColor="@color/black" />
</LinearLayout>
MainActivity.java:
package com.example.snackbarapplication;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

// Find the button by its ID


buttonShowSnackbar = findViewById(R.id.buttonShowSnackbar);

buttonShowSnackbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "This is a Snackbar message",
Snackbar.LENGTH_INDEFINITE)
.setAction("Dismiss", new View.OnClickListener() {
@Override
public void onClick(View v)
{
}
}).show();
}
});
}
}

Output:

15. Dialog messages:


They are used to grab the user's attention, often requiring them to make a
decision or acknowledge something before continuing. Dialogs are more
intrusive than Toasts or Snackbars, as they typically block interaction with the
rest of the app until the user dismisses them.
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">

<EditText
android:id="@+id/editTextMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your message here"
android:textSize="18sp"
android:padding="10dp"/>

<Button
android:id="@+id/buttonClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:padding="10dp"/>

</LinearLayout>

MainActivity.java:
package com.example.dialoguemessageapplication;

import android.os.Bundle;
import android.view.View;
import android.content.DialogInterface;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText editTextMessage;
Button buttonClear;

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

// Initialize the views


editTextMessage = findViewById(R.id.editTextMessage);
buttonClear = findViewById(R.id.buttonClear);

// Set up the Clear button click listener


buttonClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create an AlertDialog to confirm the clear action
new AlertDialog.Builder(MainActivity.this)
.setTitle("Clear Text")
.setMessage("Are you sure you want to clear the text?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Clear the text in the EditText
editTextMessage.setText("");
}
})
.setNegativeButton("No", null) // Dismiss the dialog on No
.show();
}
});
}
}
Output:
16. ListView: Here, it groups several items and displays them in vertical
scrollable list.
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/listViewCountries"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollbars="vertical" />
</LinearLayout>

MainActivity.java:
package com.example.listviewapplication;

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

public class MainActivity extends AppCompatActivity {

ListView listViewCountries;

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

listViewCountries = findViewById(R.id.listViewCountries);

String[] countries = getResources().getStringArray(R.array.countries_array);

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


android.R.layout.simple_list_item_1, countries);

listViewCountries.setAdapter(adapter);

}
}
strings.xml:
<resources>
<string name="app_name">ListViewApplication</string>
<string-array name="countries_array">
<item>India</item>
<item>United States</item>
<item>Canada</item>
<item>Australia</item>
<item>United Kingdom</item>
<item>Germany</item>
<item>France</item>
<item>Japan</item>
<item>China</item>
<item>Brazil</item>
<item>Russia</item>
<item>South Africa</item>
<item>Mexico</item>
<item>Argentina</item>
<item>Italy</item>
<item>Spain</item>
<item>Sweden</item>

</string-array>
</resources>
Output:
Incomplete:
17. RecyclerView
18. Constraint Layout

You might also like