Q.1 Write a code to display message hello world.
*Code for XML file.
<?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:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="20sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for JAVA file.
package com.example.practical;
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:
Q.2 Write a program to show the Lifecycle of Android
*Code for XML file
<?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:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="20sp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for JAVA file
package com.example.practical;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),"Oncreate Executed",Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(),"onStart Executed",Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(),"onResume Executed",Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(),"onRestart Executed",Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(),"onPause Executed",Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(),"onStop Executed",Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(),"onDestroy Executed",Toast.LENGTH_SHORT).show();
}
}
Output:
Q.3. To display a Toast on screen with using XML file
*Code for XML file
<?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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="240dp"
android:text="Click me!"
android:onClick="display"/>
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for JAVA file
package com.example.practcal;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void display(View v){
Toast.makeText(getApplicationContext(),"Button is Clicked!!",Toast.LENGTH_SHORT).show();
}
}
Output:
Q.4 Write a android applications to enter input or a message a edittext control and display in the
flash message when click on a button.
*Code for XML file
<?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">
<Button
android:id="@+id/button"
android:layout_width="139dp"
android:layout_height="77dp"
android:text="Click"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.455"
app:layout_constraintVertical_bias="0.564"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/ed1"
android:layout_width="318dp"
android:layout_height="53dp"
android:hint="Enter a message"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.447"
app:layout_constraintVertical_bias="0.275"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for JAVA file
package com.example.helloworld;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.EditText;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button;
EditText ed1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
ed1 = findViewById(R.id.ed1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = ed1.getText().toString();
Toast.makeText(getApplicationContext(),name + " ",Toast.LENGTH_SHORT).show();
}
});
}
}
Output:
Q.5 Write a program to device screenorientation portrait and landscape.
*Code for activity_main.XML file
<?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">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="112dp"
android:layout_marginBottom="8dp"
android:minHeight="48dp"
android:onClick="onClick"
android:text="Launch next activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.612"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText1"
app:layout_constraintVertical_bias="0.613" />
<TextView
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="124dp"
android:ems="10"
android:textSize="22dp"
android:text="This activity is portrait orientation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for MainAcitvity.JAVA file
package com.example.screenorientationpotrait;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
}
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,secondActivity.class);
startActivity(intent);
}
}
*Code for activity_second.XML file
<?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=".secondActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="180dp"
android:text="this is landscape orientation"
android:textSize="22dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for SecondAcitvity.JAVA file
package com.example.screenorientationpotrait;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class secondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
Output:
Q.6 Write a program to Addition of two
numbers.
*Code for a XML file.
<?xml version="1.0" encoding="utf-8"?> app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
<androidx.constraintlayout.widget.ConstraintLayout app:layout_constraintTop_toBottomOf="@+id/first_number"
/>
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" <Button
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/button"
android:id="@+id/main" android:layout_width="139dp"
android:layout_width="match_parent" android:layout_height="57dp"
android:layout_height="match_parent" android:layout_marginTop="84dp"
tools:context=".MainActivity"> android:text="@string/Button_value"
app:layout_constraintEnd_toEndOf="parent"
<TextView app:layout_constraintStart_toStartOf="parent"
android:id="@+id/textView1" app:layout_constraintTop_toBottomOf="@+id/textView2"
android:layout_width="150dp" />
android:layout_height="48dp"
android:layout_marginStart="10dp" <TextView
android:layout_marginTop="52dp" android:id="@+id/result"
android:gravity="center_vertical" android:layout_width="150dp"
android:text="@string/first_textView" android:layout_height="40dp"
android:textSize="16sp" android:layout_marginTop="88dp"
app:layout_constraintEnd_toStartOf="@+id/first_number" android:gravity="center"
app:layout_constraintStart_toStartOf="parent" android:text="@string/result"
app:layout_constraintTop_toTopOf="parent" /> android:textSize="16sp"
android:visibility="gone"
<EditText app:layout_constraintBottom_toBottomOf="parent"
android:id="@+id/first_number" app:layout_constraintEnd_toStartOf="@+id/result_value"
android:layout_width="150dp" app:layout_constraintStart_toStartOf="parent"
android:layout_height="48dp" app:layout_constraintTop_toBottomOf="@+id/button"
android:layout_marginTop="52dp" app:layout_constraintVertical_bias="0.006" />
android:ems="10"
android:hint="@string/first_number_hint" <TextView
android:inputType="number" android:id="@+id/result_value"
android:textSize="16sp" android:layout_width="150dp"
app:layout_constraintEnd_toEndOf="parent" android:layout_height="40dp"
app:layout_constraintStart_toEndOf="@+id/textView1" android:gravity="center"
app:layout_constraintTop_toTopOf="parent" /> android:textSize="16sp"
android:visibility="gone"
<TextView app:layout_constraintEnd_toEndOf="parent"
android:id="@+id/textView2" app:layout_constraintStart_toEndOf="@+id/result"
android:layout_width="150dp" app:layout_constraintTop_toTopOf="@+id/result" />
android:layout_height="48dp" </androidx.constraintlayout.widget.ConstraintLayout>
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:text="@string/second_textView"
android:textSize="16sp"
app:layout_constraintEnd_toStartOf="@+id/second_number"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView1"/>
<EditText
android:id="@+id/second_number"
android:layout_width="175dp"
android:layout_height="48dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="10dp"
android:ems="10"
android:hint="@string/second_number_hint"
android:inputType="number"
android:textSize="16sp"
*Code for a JAVA file.
package com.example.addingtwonumbers;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText number1,number2;
Button Add_button;
TextView temp,result;
int ans=0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number1=(EditText)
findViewById(R.id.first_number);
number2=(EditText)
findViewById(R.id.second_number);
Add_button=(Button) findViewById(R.id.button);
result=(TextView) findViewById(R.id.result_value);
temp=(TextView) findViewById(R.id.result);
Add_button.setOnClickListener(new
View.OnClickListener() {
public void onClick(View v){
double num1 =
Double.parseDouble(number1.getText().toString());
double num2 =
Double.parseDouble(number2.getText().toString());
double sum = num1 + num2;
result.setText(Double.toString(sum));
temp.setVisibility(View.VISIBLE);
result.setVisibility(View.VISIBLE);
}
});
}
}
*Code for a STRING file.
<resources>
<string
name="app_name">Addingtwonumbers</string>
<string name="first_textView">First number</string>
<string name="second_textView">Second
number</string>
<string name="Button_value">ADD</string>
<string name="first_number_hint">Input
Number</string>
<string name="second_number_hint">Input
Number</string>
<string name="result">Result</string>
</resources>
Output:
Q.7 Write a program to Implicit Intent.
*Code for a XML file
<?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">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:ems="10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.575"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginLeft="156dp"
android:layout_marginTop="172dp"
android:text="Visit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for a JAVA file
package com.example.implicitintent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.content.Intent;
import android.net.Uri;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button button;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url=editText.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
}
}
Output:
Q.8 Write a program to Explicit Intent.
*Code for a activity_main.XML file
<?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:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="First Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="392dp"
android:onClick="callSecondActivity"
android:text="Call second activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for a MainActivity.JAVA file
package com.example.explicitintent;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void callSecondActivity(View view) {
Intent i = new Intent(getApplicationContext(), secondActivity.class);
startActivity(i);
}
}
*Code for a activity_second.XML file
<?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=".secondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Second Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="392dp"
android:onClick="callFirstActivity"
android:text="Call first activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for a SecondActivity.JAVA file
package com.example.explicitintent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;
public class secondActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Bundle extras = getIntent().getExtras();
}
public void callFirstActivity(View view){
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}.
Output:
Q.9 Write a program on RecyclerView List.
*Code for a activity_main.xml file
<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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for a custom_row_layout.xml file
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:elevation="10dp"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_gravity="center">
<TextView
android:id="@+id/profileLetter"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="A"
android:gravity="center"
android:textSize="30dp"
android:textColor="@color/black"
android:textStyle="bold"
android:background="@drawable/custom_background"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="20dp">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact Name"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="@color/black"/>
<TextView
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="+91 9637334618" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
*Code for a custom_background.xml file
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="25dp" />
<solid android:color="@android:color/holo_purple" />
</shape>
*Code for a MainActivity.java file
package com.andriod.recyclerview;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerViewAdapter adapter;
private ArrayList<Model> modelArrayList;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
modelArrayList = new ArrayList<>();
recyclerView = findViewById(R.id.recyclerview);
modelArrayList.add(new Model("Harshad Badnale", "+91 9637334618"));
modelArrayList.add(new Model("Paresh Waichale", "+91 9637334619"));
modelArrayList.add(new Model("Sainath Kedase", "+91 9637334620"));
modelArrayList.add(new Model("Pratik Dhoble", "+91 9637334621"));
modelArrayList.add(new Model("Sumit Shelhale", "+91 9637334622"));
modelArrayList.add(new Model("Wishnu Waghmare", "+91 9637334623"));
modelArrayList.add(new Model("Arohi Patil", "+91 9637334624"));
modelArrayList.add(new Model("Shinde Divya", "+91 9637334625"));
modelArrayList.add(new Model("Ambika Deshpande", "+91 9637334626"));
modelArrayList.add(new Model("Samiksha patil", "+91 9637334627"));
modelArrayList.add(new Model("Vaishnavi Patil", "+91 9637334628"));
modelArrayList.add(new Model("Rani Singh", "+91 9637334629"));
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new RecyclerViewAdapter(modelArrayList, this);
recyclerView.setAdapter(adapter);
}
}
*Code for a RecyclerViewAdapter.java file
package com.andriod.recyclerview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter <RecyclerViewAdapter.ViewHolder> {
private ArrayList<Model> modelArrayList;
private Context context;
public RecyclerViewAdapter(ArrayList<Model> modelArrayList, Context context) {
this.modelArrayList = modelArrayList;
this.context = context;
}
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_row_layout, parent, false);
return new ViewHolder(view);
}
public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
Model model = modelArrayList.get(position);
holder.profileLetter.setText(model.getProfileLetter().toString());
holder.name.setText(model.getName());
holder.number.setText(model.getNumber());
}
public int getItemCount() {
return modelArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView profileLetter, name, number;
public ViewHolder(@NonNull View itemView) {
super(itemView);
profileLetter = itemView.findViewById(R.id.profileLetter);
name = itemView.findViewById(R.id.name);
number = itemView.findViewById(R.id.number);
}
}
}
*Code for a Model.java file
package com.andriod.recyclerview;
public class Model {
String name, number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Model(String name, String number) {
this.name = name;
this.number = number;
}
public Character getProfileLetter(){
return name.charAt(0);
}
}
Output:
Q.10 Write a program to display ListView items.
*Code for a XML file
<?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">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for a STRINGS file
<resources>
<string name="app_name">ListView</string>
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>Php</item>
<item>Hadoop</item>
<item>Sap</item>
<item>Python</item>
<item>Ajax</item>
<item>C++</item>
<item>Ruby</item>
<item>Rails</item>
<item>.Net</item>
<item>Perl</item>
</string-array>
</resources>
*Code for a JAVA file
package com.andriod.layoutview;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ListView listView;
TextView textView;
String[] listItem;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.listView);
textView=(TextView)findViewById(R.id.textView);
listItem = getResources().getStringArray(R.array.array_technology);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, listItem);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
// TODO Auto-generated method stub
String value=adapter.getItem(position);
Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show();
}
});
}
}
Output:
Q.11 Write a program on to show GridView List.
*Code for a activity_main.xml file
<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"
tools:context=".MainActivity">
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3"
android:horizontalSpacing="6dp"
android:verticalSpacing="6dp"
android:id="@+id/my_grid_view" />
</LinearLayout>
*Code for a grid_view_item_list.xml file
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="6dp"
app:cardElevation="6dp"
app:cardMaxElevation="6dp"
app:cardUseCompatPadding="true"
app:cardPreventCornerOverlap="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white"
android:padding="6dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/icon_id"
android:src="@drawable/download"/>
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginTop="18sp"
android:text="Icon name here ..."/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
*Code for a MainActivity.java file
package com.andriod.gridview;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.andriod.gridview.design.GridviewAdapter;
import com.andriod.gridview.model.Alphabet;
import com.andriod.gridview.model.ArrayListModel;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = findViewById(R.id.my_grid_view);
GridviewAdapter gridviewAdapter = new GridviewAdapter(MainActivity.this,new ArrayListModel().setListData());
gridView.setAdapter(gridviewAdapter);
gridView.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent,View view, int position,long l) {
Alphabet model= (Alphabet) parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this,"Clicked By : " +model.getletters(), Toast.LENGTH_SHORT).show();
}
}
*Code for a ArrayListModel.java file
package com.andriod.gridview.model;
import com.andriod.gridview.R;
import java.util.ArrayList;
public class ArrayListModel {
public ArrayList<Alphabet> setListData() {
ArrayList<Alphabet> arrayList = new ArrayList<>();
arrayList.add(new Alphabet(R.drawable.download, "Create"));
arrayList.add(new Alphabet(R.drawable.download1, "Scorpio"));
arrayList.add(new Alphabet(R.drawable.images5, "Verna"));
arrayList.add(new Alphabet(R.drawable.images, "Datasun"));
arrayList.add(new Alphabet(R.drawable.images1, "Tiago"));
arrayList.add(new Alphabet(R.drawable.images2, "Curve"));
arrayList.add(new Alphabet(R.drawable.images3, "i20"));
arrayList.add(new Alphabet(R.drawable.images4, "Swift"));
arrayList.add(new Alphabet(R.drawable.download2, "Venue"));
arrayList.add(new Alphabet(R.drawable.images6,"Venuetop"));
arrayList.add(new Alphabet(R.drawable.images7, "Renualt"));
return arrayList;
}
}
*Code for a GridviewAdapter.java file
package com.andriod.gridview.design;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.andriod.gridview.R;
import com.andriod.gridview.model.Alphabet;
import java.util.ArrayList;
public class GridviewAdapter extends ArrayAdapter<Alphabet>{
public GridviewAdapter(@NonNull Context context, ArrayList<Alphabet> alphabets) {
super(context, 0,alphabets);
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
HolderView holderView;
if(convertView == null){
convertView = LayoutInflater.from(getContext()). inflate(R.layout.gird_view_item_list,parent,false);
holderView = new HolderView(convertView);
convertView.setTag(holderView);
}
else {
holderView = (HolderView) convertView.getTag();
}
Alphabet model = getItem(position);
holderView.icons.setImageResource(model.getIconId());
holderView.tv.setText(model.getletters());
return convertView;
}
private static class HolderView {
private final ImageView icons;
private final TextView tv;
public HolderView(View view){
icons=view.findViewById(R.id.icon_id);
tv= view.findViewById(R.id.textview);
}
}
}
*Code for a Adapter.java file
package com.andriod.gridview.model;
public class Alphabet {
private final Integer iconId;
private final String letters;
public Alphabet(Integer iconId, String letters) {
this.iconId = iconId;
this.letters = letters;
}
public Integer getIconId() {
return iconId;
}
public String getletters() {
return letters;
}
}
output:
Q.12 Write a program on visit to WebView.
*Code for activity_main.xml file
<?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">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for MainActivity.java file
package com.andriod.webview;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("https://www.google.com");
WebSettings webSettings = webView.getSettings();
webSettings.getJavaScriptEnabled();
}
@Override
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
}else {
super.onBackPressed();
}
super.onBackPressed();
}
}
output:
Q.13 Write a program to a Checkboxes.
*Code for XML file.
<?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">
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="68dp"
android:text="Pizza"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="28dp"
android:text="Coffee"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="28dp"
android:text="Burger"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox2" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="184dp"
android:text="Order"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox3" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for JAVA file.
package com.example.zoominzoomout;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
CheckBox pizza,coffee,burger;
Button buttonOrder;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
pizza=(CheckBox)findViewById(R.id.checkBox);
coffee=(CheckBox)findViewById(R.id.checkBox2);
burger=(CheckBox)findViewById(R.id.checkBox3);
buttonOrder=(Button)findViewById(R.id.button);
buttonOrder.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
int totalamount=0;
StringBuilder result=new StringBuilder();
result.append("Selected Items:");
if(pizza.isChecked()){
result.append("\nPizza 100Rs");
totalamount+=100;
}
if(coffee.isChecked()){
result.append("\nCoffee 50Rs");
totalamount+=50;
}
if(burger.isChecked()){
result.append("\nBurger 120Rs");
totalamount+=120;
}
result.append("\nTotal: "+totalamount+"Rs");
//Displaying the message on the toast
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).show();
}
});
}
}
Output:
Q.14 Write a Program to a RadioButtons.
*Code for XML file.
<?xml version="1.0" encoding="utf-8"?> android:layout_height="wrap_content"
<LinearLayout android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
xmlns:android="http://schemas.android.com/apk/ android:checked="false"
res/android" android:text="@string/Harshad"
android:textColor="#154"
xmlns:tools="http://schemas.android.com/tools" android:textSize="20sp"
android:layout_width="match_parent" android:textStyle="bold" />
android:layout_height="match_parent"
android:orientation="vertical" <RadioButton
android:id="@+id/Paresh"
tools:context="com.example.webview.MainActivit android:layout_width="wrap_content"
y">
android:layout_height="wrap_content"
<LinearLayout android:layout_marginLeft="20dp"
android:layout_width="fill_parent" android:layout_marginTop="10dp"
android:layout_height="wrap_content" android:checked="false"
android:background="#e0e0e0" android:text="@string/Paresh"
android:orientation="vertical"> android:textColor="#154"
android:textSize="20sp"
<TextView android:textStyle="bold" />
android:layout_width="wrap_content"
android:layout_height="wrap_content" <RadioButton
android:text="Select your favourite android:id="@+id/Sumit"
Student:: " android:layout_width="wrap_content"
android:textColor="#000" android:layout_height="wrap_content"
android:textSize="20sp" android:layout_marginLeft="20dp"
android:textStyle="bold" /> android:layout_marginTop="10dp"
android:checked="false"
<RadioGroup android:text="@string/Sumit"
android:layout_width="wrap_content" android:textColor="#154"
android:layout_height="wrap_content"> android:textSize="20sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/Dipali"
android:layout_width="wrap_content" <RadioButton
android:layout_height="wrap_content" android:id="@+id/Pratik"
android:layout_marginLeft="20dp" android:layout_width="wrap_content"
android:layout_marginTop="10dp" android:layout_height="wrap_content"
android:checked="true" android:layout_marginLeft="20dp"
android:text="@string/Dipali" android:layout_marginTop="10dp"
android:textColor="#154" android:checked="false"
android:textSize="20sp" android:text="@string/Pratik"
android:textStyle="bold" /> android:textColor="#154"
android:textSize="20sp"
<RadioButton android:textStyle="bold" />
android:id="@+id/Harshad"
android:layout_width="wrap_content" </RadioGroup>
<Button setContentView(R.layout.activity_main);
android:id="@+id/submitButton" Dipali = (RadioButton)
android:layout_width="wrap_content" findViewById(R.id.Dipali);
android:layout_height="wrap_content" Harshad = (RadioButton)
android:layout_gravity="center" findViewById(R.id.Harshad);
android:layout_margin="20dp" Paresh = (RadioButton)
android:background="#0f0" findViewById(R.id.Paresh);
android:padding="10dp" Sumit = (RadioButton)
android:text="Submit" findViewById(R.id.Sumit);
android:textColor="#607D8B" Pratik = (RadioButton)
android:textSize="20sp" findViewById(R.id.Pratik);
android:textStyle="bold" /> submit = (Button)
</LinearLayout> findViewById(R.id.submitButton);
</LinearLayout> submit.setOnClickListener(new
View.OnClickListener()
*Code for a STRING.XML file {
public void onClick(View v) {
<resources> if (Harshad.isChecked()) {
<string selectedStudent =
name="app_name">WebView</string> Harshad.getText().toString();
<string name="hello_world">Hello } else if (Pratik.isChecked()) {
world!</string> selectedStudent =
<string Pratik.getText().toString();
name="action_settings">Settings</string> } else if (Dipali.isChecked()) {
<string name="Dipali">Dipali</string> selectedStudent =
<string name="Harshad">Harshad</string> Dipali.getText().toString();
<string name="Paresh">Paresh</string> } else if (Sumit.isChecked()) {
<string name="Sumit">Sumit</string> selectedStudent =
<string name="Pratik">Pratik</string> Sumit.getText().toString();
</resources> } else if (Paresh.isChecked()) {
selectedStudent =
Paresh.getText().toString();
*Code for JAVA File }
package com.example.webview; Toast.makeText(getApplicationContext(),
selectedStudent, Toast.LENGTH_LONG).show(); //
import print the value of selected super star
androidx.appcompat.app.AppCompatActivity; }
import android.os.Bundle; });
import android.view.View; }
import android.widget.Button; }
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends
AppCompatActivity {
RadioButton Dipali, Harshad, Paresh, Sumit,
Pratik;
String selectedStudent;
Button submit;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
Output:
Q.15 Write a program on ToggleButton On/Off.
*Code for a activity_main.xml file
<?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">
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="80dp"
android:text="ToggleButton"
android:textOff="Off"
android:textOn="On"
app:layout_constraintEnd_toStartOf="@+id/toggleButton2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ToggleButton
android:id="@+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_marginTop="80dp"
android:text="ToggleButton"
android:textOff="Off"
android:textOn="On"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="144dp"
android:layout_marginLeft="148dp"
android:text="Submit"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for a MainActivity.java file
package com.andriod.togglebutton;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
private ToggleButton toggleButton1, toggleButton2;
private Button buttonSubmit;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}
public void addListenerOnButtonClick(){
toggleButton1=(ToggleButton)findViewById(R.id.toggleButton);
toggleButton2=(ToggleButton)findViewById(R.id.toggleButton2);
buttonSubmit=(Button)findViewById(R.id.button);
buttonSubmit.setOnClickListener(new View.OnClickListener(){
public void onClick(View view) {
StringBuilder result = new StringBuilder();
result.append("ToggleButton1 : ").append(toggleButton1.getText());
result.append("\nToggleButton2 : ").append(toggleButton2.getText());
Toast.makeText(getApplicationContext(), result.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
output:
Q.16 Write a program to Spinner.
*Code for activity_main.xml file
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:minHeight="48dp">
</Spinner>
</RelativeLayout>
*code for Strings.xml file
<resources>
<string name="app_name">spinner</string>
<string-array name="spinnerlist">
<item>Select Language</item>
<item>C++</item>
<item>JAVA</item>
<item>PHP</item>
<item>HTML</item>
</string-array>
</resources>
*Code for MainActivity.java file
package com.andriod.spinner;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Spinner spn;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spn=findViewById(R.id.spn);
ArrayAdapter<CharSequence>adapter=ArrayAdapter.createFromResource(this,R.array.spinnerlist,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spn.setAdapter(adapter);
spn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String data=adapterView.getItemAtPosition(i).toString();
Toast.makeText(getApplicationContext(),data,Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
Output:
Q.17 Write a program to show OptionMenu.
*Code for acivity_main.xml file
<?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">
<TextView
android:id="@+id/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>
*Code for options_menu.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_setting"
android:title="Setting" />
<item
android:id="@+id/menu_share"
android:title="Share" />
<item
android:id="@+id/menu_about"
android:title="About" />
<item
android:id="@+id/menu_logout"
android:title="Logout" />
</menu>
*Code for java file
package com.andriod.optionmenu;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = new MenuInflater(this);
menuInflater.inflate(R.menu.options_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id=item.getItemId();
if (id==R.id.menu_setting) {
Toast.makeText(MainActivity.this, "Setting Clicked", Toast.LENGTH_SHORT).show();
}
if (id==R.id.menu_share) {
Toast.makeText(MainActivity.this, "Share Clicked", Toast.LENGTH_SHORT).show();
}
if (id==R.id.menu_about) {
Toast.makeText(MainActivity.this, "About Clicked", Toast.LENGTH_SHORT).show();
}
if (id==R.id.menu_logout) {
Toast.makeText(MainActivity.this, "Logout Clicked", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
Output:
Q.18 Write a program to display dialogue box.
*Code for activity_main.xml file
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="Close app"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for a Strings.xml file
<resources>
<string name="app_name">dialoguebox</string>
<string name="dialog_message">Welcome to Alert Dialog</string>
<string name="dialog_title">Javatpoint Alert Dialog</string>
</resources>
*Code for a MainActivity.java file
package com.andriod.dialoguebox;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button closeButton;
AlertDialog.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
closeButton = (Button) findViewById(R.id.button);
builder = new AlertDialog.Builder(this);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);
builder.setMessage("Do you want to close this application ?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
Toast.makeText(getApplicationContext(),"you choose yes action for
alertbox",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Toast.makeText(getApplicationContext(),"you choose no action for
alertbox",
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.setTitle("AlertDialogExample");
alert.show();
}
});
}
}
output:
Q.19 Write a program to display CustomList View.
*Code for activity_main.xml file
<?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”>
<ListView
android:id=”@+id/list”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_alignParentBottom=”true”
android:layout_marginBottom=”0dp”>
</ListView>
</RelativeLayout>
*Code for mylist.xml file
<?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="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="76dp"
android:layout_height="56dp"
android:padding="5dp" />
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
*Code for MainActivity.java file
package com.andriod.customlist;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] maintitle ={
"Linkdin","Watsapp",
"Twitter","Facebook",
"Instagram",
};
String[] subtitle ={
"Sub Title 1","Sub Title 2",
"Sub Title 3","Sub Title 4",
"Sub Title 5",
};
Integer[] imgid={
R.drawable.download_1,R.drawable.download_2,
R.drawable.download_3,R.drawable.download_4,
R.drawable.download_5,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyListAdapter adapter=new MyListAdapter(this, maintitle, subtitle,imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
if(position == 0) {
//code specific to first list item
Toast.makeText(getApplicationContext(),"Place Your First Option Code",Toast.LENGTH_SHORT).show();
}
else if(position == 1) {
//code specific to 2nd list item
Toast.makeText(getApplicationContext(),"Place Your Second Option Code",Toast.LENGTH_SHORT).show();
}
else if(position == 2) {
Toast.makeText(getApplicationContext(),"Place Your Third Option Code",Toast.LENGTH_SHORT).show();
}
else if(position == 3) {
Toast.makeText(getApplicationContext(),"Place Your Forth Option Code",Toast.LENGTH_SHORT).show();
}
else if(position == 4) {
Toast.makeText(getApplicationContext(),"Place Your Fifth Option Code",Toast.LENGTH_SHORT).show();
}
}
});
}
}
*Code for MyListView.java file
package com.andriod.customlist;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
class MyListAdapter extends ArrayAdapter<String>{
private final Activity context;
private final String[] maintitle;
private final String[] subtitle;
private final Integer[] imgid;
public MyListAdapter(Activity context, String[] maintitle,String[] subtitle, Integer[] imgid) {
super(context, R.layout.mylist, maintitle);
this.context=context;
this.maintitle=maintitle;
this.subtitle=subtitle;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView titleText = (TextView) rowView.findViewById(R.id.title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView subtitleText = (TextView) rowView.findViewById(R.id.subtitle);
titleText.setText(maintitle[position]);
imageView.setImageResource(imgid[position]);
subtitleText.setText(subtitle[position]);
return rowView;
};
}
output:
Q.20 Write a program to display Styles & Themes.
*Code for activity_main.xml file
<?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:theme="@style/MyTextBold"
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" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="160dp"
android:text="Play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.621"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for themes.xml file
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.Stylesandthemes"
parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/design_default_color_primary</item>
<item name="colorPrimaryDark">@color/design_default_color_primary_dark</item>
<item name="colorAccent">@color/design_default_color_secondary_variant</item>
</style>
<style name="Mytheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/design_default_color_primary</item>
<item name="colorPrimaryDark">@color/design_default_color_primary_dark</item>
<item name="colorAccent">@color/design_default_color_secondary_variant</item>
</style>
<style name="MyTextBold">
<item name="android:textSize">34sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#FF6D00</item>
</style>
</resources>
*Code for MainActivity.java file
package com.andriod.stylesandthemes;
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:
Q.21 Write a program to display Play Audio.
*Code for activity_main.xml file
<?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:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/btnplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp"
android:text="Play" />
<Button
android:id="@+id/btnpause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="11dp"
android:layout_marginEnd="11dp"
android:text="Pause" />
<Button
android:id="@+id/btnstop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>
*Code for MainActivity.java file
package com.andriod.playaudio;
import android.annotation.SuppressLint;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.Button;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnpause,btnplay,btnstop;
btnpause = findViewById(R.id.btnpause);
btnplay = findViewById(R.id.btnplay);
btnstop = findViewById(R.id.btnstop);
MediaPlayer mp=new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
String apath = "android.resource://"+getPackageName()+"/raw/android_11";
Uri audioURI = Uri.parse(apath);
try {
mp.setDataSource(this,audioURI);
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
btnpause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.pause();
}
});
btnplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
});
btnstop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.pause();
mp.seekTo(0);
}
});
}
}
output:
Q.22 Write a program to display Play Video.
*Code for activity_main.xml file
<?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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<VideoView
android:layout_width="match_parent"
android:layout_height="250dp"
android:id="@+id/videoView"/>
</LinearLayout>
</LinearLayout>
*Code for MainActivity.java file
package com.andriod.playvideo;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView videoView = findViewById(R.id.videoView);
String vpath = "android.resource://"+getPackageName()+"/raw/android_11v";
Uri videoURI = Uri.parse(vpath);
videoView.setVideoPath(vpath);
videoView.start();
MediaController mediaController = new MediaController(this);
videoView.setMediaController(mediaController);
mediaController.setAnchorView(videoView);
}
}
output:
Q.23 Write a program to display FadeIn/FadeOut Animation.
*Code for activity_main.xml file
<?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:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="@drawable/ga" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fade In"
android:textSize="30dp"
android:layout_marginTop="15dp"
android:id="@+id/fadeIn"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fade Out"
android:textSize="30dp"
android:layout_marginTop="15dp"
android:id="@+id/fadeOut"/>
</LinearLayout>
*Code for fadein.xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="6000"
android:fromAlpha="0.1"
android:toAlpha="1.0" />
</set>
*Code for fadeout.xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="6000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
</set>
*Code for MainActivity.java file
package com.andriod.fadeinfadeout;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ImageView img;
Button fadeIn, fadeOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.img);
fadeIn = findViewById(R.id.fadeIn);
fadeOut = findViewById(R.id.fadeOut);
fadeIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation =
AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadein);
img.startAnimation(animation);
}
});
fadeOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation1 =
AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fadeout);
img.startAnimation(animation1);
}
});
}
}
output:
Q.24 Write a program to display ZoomIn/ZoomOut.
*Code for activity_main.xml file
<?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:orientation="vertical"
android:padding="30dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/myImageView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="30dp"
android:src="@drawable/ba" />
<Button
android:id="@+id/btn_zoomIn"
android:layout_width="match_parent"
android:layout_height="65dp"
android:backgroundTint="#D7138E"
android:text="Zoom In"
android:textColor="#ffffff"
android:textSize="23sp"
android:layout_marginTop="45dp"/>
<Button
android:id="@+id/btn_zoomOut"
android:layout_width="match_parent"
android:layout_height="65dp"
android:backgroundTint="#D7138E"
android:text="Zoom Out"
android:textColor="#ffffff"
android:textSize="23sp"/>
</LinearLayout>
*Code for zoom_in.xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5">
</scale>
</set>
*Code for zoom_out.xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.5"
android:toYScale="0.5">
</scale>
</set>
*Code for MainActivity.java file
package com.andriod.zoominzoomout;
import android.graphics.drawable.Animatable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
ImageView myImage;
Button btn_zoom_in, btn_zoom_out;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImage = (ImageView)findViewById(R.id.myImageView);
btn_zoom_in = (Button)findViewById(R.id.btn_zoomIn);
btn_zoom_out = (Button)findViewById(R.id.btn_zoomOut);
btn_zoom_in.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom_in);
myImage.startAnimation(animation);
}
});
btn_zoom_out.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom_out);
myImage.startAnimation(animation);
}
});
}
}
output:
Q.25 Write a program to display ScaleAnimation.
*Code for activity_main.xml file
<?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:orientation="vertical"
tools:context=".MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Scale Animation"
android:textColor="@color/white"
android:textStyle="bold"
android:textSize="25dp"
android:layout_margin="50dp"
android:id="@+id/btn_scale"/>
<ImageView
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@drawable/moon"
android:id="@+id/img_moon"/>
</LinearLayout>
*Code for scale.xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:fromXScale="1"
android:toXScale="2"
android:fromYScale="1"
android:toYScale="2"
android:pivotY="50%"
android:pivotX="50%"
android:duration= "5000"
android:repeatcount = "infinite"
android:repeatMode = "reverse"/>
</set>
*Code for MainActivity.java file
package com.andriod.scaleanimation;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
Button btn_scale;
ImageView img_moon;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
btn_scale = findViewById(R.id.btn_scale);
img_moon = findViewById(R.id.img_moon);
Animation scale = AnimationUtils.loadAnimation(this,R.anim.scale);
btn_scale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
img_moon.startAnimation(scale);
}
});
}
}
output:
Q.26 Write a program to display RotateAnimation.
*Code for activity_main.xml file
<?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"
android:paddingLeft="10dp"
android:paddingRight="10dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imgvw"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:src="@drawable/messi" />
<Button
android:id="@+id/btnRC1k"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imgvw"
android:layout_marginLeft="70dp"
android:layout_marginTop="20dp"
android:text="Clockwise" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnRACK1K"
android:layout_alignBottom="@+id/btnRC1k"
android:layout_toRightOf="@+id/btnRC1k"
android:text="Anti Clockwise" />
</RelativeLayout>
*Code for rotate_clockwise.xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/cycle_interpolator">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" />
</set>
*Code for rotate_anticlockwise.xml file
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/cycle_interpolator">
<rotate android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="5000" />
</set>
*Code for MainActivity.java file
package com.andriod.rotateanimation;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button btnrclock;
private Button btnrantick;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnrclock = (Button) findViewById(R.id.btnRC1k);
btnrantick = (Button) findViewById(R.id.btnRACK1K);
img = (ImageView) findViewById(R.id.imgvw);
btnrclock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation aniRotateC1K = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_clockwise);
img.startAnimation(aniRotateC1K);
}
});
btnrantick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animRotateAc1K = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate_anticlockwise);
img.startAnimation(animRotateAc1K);
}
});
}
}
output:
Q.27 Write a program to display 2D and 3D Graphics.
*Code for activity_main.xml file
<?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">
</RelativeLayout>
*Code for MainActivity.java file
package com.andriod.a2dgraphics;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
public class MyView extends View{
public MyView(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int x=getWidth();
int y=getHeight();
int radius;
radius = 100;
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(70.0f);
paint.setColor(Color.parseColor("#da4747"));
canvas.drawCircle(x/2, y/2, radius, paint);
canvas.drawRoundRect(new RectF(200, 350, 500, 550), 20, 20, paint);
canvas.drawText("SVM Udgir", 20, 660, paint);
}
}
}
output:
Q.28 Write a program to save file in Internal Storage.
*Code for activity_main.xml file
<?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">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/abc" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:layout_marginTop="260dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="24dp"
android:ems="10"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:text="File Name:" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:text="Data:" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginLeft="8dp"
android:layout_marginTop="18dp"
android:text="save" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="read" />
</RelativeLayout>
*Code for MainActivity.java file
package com.andriod.internalstorage;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText editTextFilename,editTextData;
Button saveButton,readButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextFilename=(EditText) findViewById(R.id.editText1);
editTextData=(EditText) findViewById(R.id.editText2);
saveButton=(Button) findViewById(R.id.button1);
readButton=(Button) findViewById(R.id.button2);
saveButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
String filename=editTextFilename.getText().toString();
String data=editTextData.getText().toString();
FileOutputStream fos;
try {
fos=openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(data.getBytes());
fos.close();
Toast.makeText(getApplicationContext(),filename + "saved", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
}
});
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filename=editTextFilename.getText().toString();
StringBuffer stringBuffer = new StringBuffer();
try {
BufferedReader myReader = new BufferedReader(new InputStreamReader(openFileInput(filename)));
String inputString;
while ((inputString= myReader.toString()) != null) {
stringBuffer.append(inputString + "\n");
}
}catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),stringBuffer.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
Output:
Q.29 Write a program to save file in External Storage.
*Code for activity_main.xml file
<?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">
<TextView
android:layout_width="410dp"
android:layout_height="50dp"
android:textSize="40dp"
android:layout_marginLeft="50dp"
android:text="External Storage" />
<ImageView
android:layout_width="410dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:src="@drawable/images" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="20dp"
android:layout_marginTop="270dp"
android:ems="10" >
<requestFocus/>
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="24dp"
android:ems="10" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignParentLeft="true"
android:textSize="25dp"
android:text="File name:" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText2"
android:layout_alignBottom="@+id/editText2"
android:layout_alignParentLeft="true"
android:textSize="25dp"
android:text="Data:" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_below="@+id/editText2"
android:layout_marginLeft="7dp"
android:layout_marginTop="18dp"
android:text="save" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@id/button1"
android:layout_toRightOf="@id/button1"
android:text="Read" />
</RelativeLayout>
*Code for MainActivity.java file
package com.andriod.externalstorage;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
EditText editTextFilename,editTextData;
Button saveButton,readButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextFilename=(EditText) findViewById(R.id.editText1);
editTextData=(EditText) findViewById(R.id.editText2);
saveButton=(Button) findViewById(R.id.button1);
readButton=(Button) findViewById(R.id.button2);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String filename=editTextFilename.getText().toString();
String data=editTextData.getText().toString();
FileOutputStream fos;
try {
File myfile=new File("/sdcard/"+filename);
myfile.createNewFile();
fos=new FileOutputStream(myfile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fos);
myOutWriter.append(data);
myOutWriter.close();
fos.close();
Toast.makeText(getApplicationContext(), filename + "saved", Toast.LENGTH_LONG).show();
}catch
(FileNotFoundException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
}
});
readButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
String filename=editTextFilename.getText().toString();
StringBuffer stringBuffer=new StringBuffer();
String aDataRow="";
String aBuffer="";
try {
File myfile=new File("/sdcard/"+filename);
FileInputStream fin = new FileInputStream(myfile);
BufferedReader myReader=new BufferedReader(new InputStreamReader(fin));
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
myReader.close();
}catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),aBuffer,Toast.LENGTH_LONG).show();
}
});
}
}
Output:
Q.30 Write a program to show Proximity Sensor.
*Code for activity_main.xml file
<?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">
</androidx.constraintlayout.widget.ConstraintLayout>
*Code for MainActivity.java file
package com.andriod.sensor;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
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);
SensorManager sensorManager = (SensorManager)
getSystemService(SENSOR_SERVICE);
final Sensor proximitySensor =
sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
SensorEventListener sensorEventListener = new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
if(event.values[0]<proximitySensor.getMaximumRange()){
getWindow().getDecorView().setBackgroundColor(Color.RED);
}else {
getWindow().getDecorView().setBackgroundColor(Color.GREEN);
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
sensorManager.registerListener(sensorEventListener,proximitySensor,2 * 1000 * 1000);
}
}
output: