You are on page 1of 32

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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

XML FILE:- JAVA FILE:-


<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;
<LinearLayout import android.annotation.SuppressLint;
xmlns:android="http://schemas.android.com/apk import android.app.ProgressDialog;
/res/android" import android.os.Handler;
import android.os.Message;
xmlns:tools="http://schemas.android.com/tools" import android.os.Bundle;
android:id="@+id/activity_main" import android.view.View;
android:layout_width="match_parent" import android.widget.Button;
android:layout_height="match_parent" import androidx.appcompat.app.AppCompatActivity;
tools:context=".MainActivity" public class MainActivity extends AppCompatActivity {
android:orientation="vertical" Button b1, b2;
android:elevation="1dp"> ProgressDialog progressDialog;
<Button @SuppressLint("MissingInflatedId")
android:text="Click ME!" @Override
android:layout_width="wrap_content" protected void onCreate(Bundle savedInstanceState) {
android:layout_height="wrap_content" super.onCreate(savedInstanceState);
android:id="@+id/button2" setContentView(R.layout.activity_main);
/> b2 = (Button) findViewById(R.id.button2);
</LinearLayout> b2.setOnClickListener(new View.OnClickListener() {
Handler handle = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressDialog.incrementProgressBy(2);
}
};
@Override
public void onClick(View v) {
progressDialog = new
ProgressDialog(MainActivity.this);
progressDialog.setMax(100);
progressDialog.setTitle("File Downloading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZO
NTAL);
progressDialog.show();
progressDialog.setCancelable(false);
new Thread(new Runnable() {
@Override
public void run() {
try {
while (progressDialog.getProgress() <=
progressDialog.getMax()) {
Thread.sleep(200);

handle.sendMessage(handle.obtainMessage());
if (progressDialog.getProgress() ==
progressDialog.getMax()) {
progressDialog.dismiss();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
});
}
}
XML FILE:- JAVA FILE:-
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;
<LinearLayout import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
xmlns:android="http://schemas.android.com/apk/ import android.widget.ArrayAdapter;
res/android" import android.widget.ListView;
android:layout_width="match_parent" public class MainActivity extends AppCompatActivity {
android:layout_height="match_parent" ListView l;
android:orientation="vertical" String tutorials[]
android:padding="16dp"> ={
"Android","Java","PHP","Hadoop","Sap","Python","Ajax","C+
<ListView +","Roby","Rails" };
android:id="@+id/list" @Override
android:layout_width="match_parent" protected void onCreate(Bundle savedInstanceState) {
android:layout_height="match_parent" /> super.onCreate(savedInstanceState);
</LinearLayout> setContentView(R.layout.activity_main);
l = findViewById(R.id.list);
ArrayAdapter<String> arr = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
tutorials);
l.setAdapter(arr);
}
}
XML FILE:- JAVA FILE:-
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;
<LinearLayout
xmlns:android="http://schemas.android. import androidx.appcompat.app.AppCompatActivity;
com/apk/res/android" import android.os.Bundle;
android:layout_width="match_parent" import android.view.View;
android:layout_height="match_parent" import android.widget.Button;
android:orientation="vertical" import android.widget.ImageView;
android:padding="16dp">
public class MainActivity extends AppCompatActivity {
<ImageView
android:id="@+id/imageView" ImageView imageView;
android:layout_width="match_parent" Button buttonChangeImage;
android:layout_height="wrap_content" boolean isImage1 = true;
android:src="@drawable/a">
@Override
</ImageView> protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
<Button setContentView(R.layout.activity_main);
android:id="@+id/buttonChangeImage"
android:layout_width="wrap_content" imageView = findViewById(R.id.imageView);
android:layout_height="wrap_content" buttonChangeImage =
android:text="Change Image" /> findViewById(R.id.buttonChangeImage);
</LinearLayout> buttonChangeImage.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {

if (isImage1) {
imageView.setImageResource(R.drawable.a);
} else {
imageView.setImageResource(R.drawable.b);
}
isImage1 = !isImage1;
}
});
}
}
XML FILE:- JAVA FILE:-
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;
<RelativeLayout xmlns:android="http://schemas. import android.os.Bundle;
android.com/apk/res/android" import android.view.View;
xmlns:tools="http://schemas import android.view.ViewGroup;
.android.com/tools" import android.widget.BaseAdapter;
android:layout_width="match_parent" import android.widget.Button;
android:layout_height="match_parent" import android.widget.GridView;
tools:context=".MainActivity"> import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
<GridView private static final int NUM_BUTTONS = 15;
android:id="@+id/gridView" @Override
android:layout_width="match_parent" protected void onCreate(Bundle savedInstanceState) {
android:layout_height="match_parent" super.onCreate(savedInstanceState);
android:numColumns="3" setContentView(R.layout.activity_main);
android:columnWidth="100dp" GridView gridView = findViewById(R.id.gridView);
android:verticalSpacing="10dp" gridView.setAdapter(new ButtonAdapter());
android:horizontalSpacing="10dp" }
android:gravity="center"/> private class ButtonAdapter extends BaseAdapter {
@Override
</RelativeLayout> public int getCount() {
return NUM_BUTTONS;
}@Override
public Object getItem(int position) {
return null;
}@Override
public long getItemId(int position) {
return 0; }
@Override
public View getView(int position, View convertView,
ViewGroup parent) {
Button button;
if (convertView == null) {
button = new Button(MainActivity.this);
button.setLayoutParams(new
GridView.LayoutParams(200, 200));
button.setPadding(8, 8, 8, 8);
} else {
button = (Button) convertView;
}
button.setText("Button " + (position + 1));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { }
});
return button;
}
}
}

XML FILE:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_marginTop="1000dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your long text goes here..."
android:textSize="18sp" />
</ScrollView>
</RelativeLayout>
XML FILE:- JAVA FILE:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas. package com.example.myapplication;
android.com/apk/res/android" import android.os.Bundle;
xmlns:tools="http://schemas import android.widget.TimePicker;
.android.com/tools" import androidx.appcompat.app.AppCompatActivity;
android:layout_width="match_parent" public class MainActivity extends AppCompatActivity {
android:layout_height="match_parent" @Override
tools:context=".MainActivity"> protected void onCreate(Bundle savedInstanceState) {
<TimePicker super.onCreate(savedInstanceState);
android:id="@+id/timePicker24" setContentView(R.layout.activity_main);
android:layout_width="wrap_content" TimePicker timePicker24 =
android:layout_height="wrap_content" findViewById(R.id.timePicker24);
android:timePickerMode="spinner"/> timePicker24.setIs24HourView(true);
<TimePicker }
android:id="@+id/t" }
android:layout_width="wrap_content"
android:layout_marginTop="200dp"
android:layout_height="wrap_content"
android:timePickerMode="spinner"/>
<TimePicker
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_marginTop="400dp"
android:layout_height="wrap_content"
android:timePickerMode="clock"/>
</RelativeLayout>
XML FILE:- JAVA FILE:-
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;
<RelativeLayout xmlns:android="http://schemas import android.app.DatePickerDialog;
.android.com/apk/res/android" import android.app.TimePickerDialog;
xmlns:tools="http://schemas import android.os.Bundle;
.android.com/tools" import android.view.View;
android:layout_width="match_parent" import android.widget.*;
android:layout_height="match_parent" import androidx.appcompat.app.AppCompatActivity;
tools:context=".MainActivity"> import java.text.DateFormat;
<Button import java.util.Calendar;
android:id="@+id/selectDateButton" public class MainActivity extends AppCompatActivity {
android:layout_width="wrap_content" private TextView selectedDateTextView;
android:layout_height="wrap_content" private TextView selectedTimeTextView;
android:text="Select Date" @Override
android:layout_centerInParent="true" protected void onCreate(Bundle savedInstanceState) {
android:onClick="showDatePicker"/> super.onCreate(savedInstanceState);
<Button setContentView(R.layout.activity_main);
android:id="@+id/selectTimeButton" selectedDateTextView =
android:layout_width="wrap_content" findViewById(R.id.selectedDateTextView);
android:layout_height="wrap_content" selectedTimeTextView =
android:text="Select Time" findViewById(R.id.selectedTimeTextView); }
android:layout_below="@+id/select public void showDatePicker(View view) {
DateButton" final Calendar calendar = Calendar.getInstance();
android:layout_centerHorizontal="true" int currentYear = calendar.get(Calendar.YEAR);
android:layout_marginTop="16dp" int currentMonth = calendar.get(Calendar.MONTH);
android:onClick="showTimePicker"/> int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
<TextView DatePickerDialog datePickerDialog = new
android:id="@+id/selectedDateTextView" DatePickerDialog(this,
android:layout_width="wrap_content" new DatePickerDialog.OnDateSetListener() {
android:layout_height="wrap_content" @Override
android:text="" public void onDateSet(DatePicker view, int year, int
android:layout_below="@+id/select month, int dayOfMonth) {
TimeButton" calendar.set(Calendar.YEAR, year);
android:layout_centerHorizontal="true" calendar.set(Calendar.MONTH, month);
android:layout_marginTop="16dp"/> calendar.set(Calendar.DAY_OF_MONTH,
<TextView dayOfMonth);
android:id="@+id/selectedTimeTextView" updateSelectedDateTextView(calendar);} },
android:layout_width="wrap_content" currentYear,
android:layout_height="wrap_content" currentMonth,
android:text="" currentDay );
android:layout_below="@+id/selected datePickerDialog.show(); }
DateTextView" public void showTimePicker(View view) {
android:layout_centerHorizontal="true" final Calendar calendar = Calendar.getInstance();
android:layout_marginTop="16dp"/> int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
</RelativeLayout> int currentMinute = calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new
TimePickerDialog(
this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int
hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY,
hourOfDay);
calendar.set(Calendar.MINUTE, minute);
updateSelectedTimeTextView(calendar);
}

},
currentHour,
currentMinute,
true
);
timePickerDialog.show();
}
private void updateSelectedDateTextView(Calendar
calendar) {
DateFormat dateFormat = DateFormat.getDateInstance();
String selectedDate =
dateFormat.format(calendar.getTime());
selectedDateTextView.setText(selectedDate);
}
private void updateSelectedTimeTextView(Calendar
calendar) {
DateFormat timeFormat = DateFormat.getTimeInstance();
String selectedTime =
timeFormat.format(calendar.getTime());
selectedTimeTextView.setText(selectedTime);
}
}
JAVA FILE:-
package com.example.myapplication;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Activity created");
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: Activity started");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: Activity resumed");
}
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: Activity paused");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop: Activity stopped");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: Activity destroyed");
}
}
XML FILE:- JAVA FILE:-
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;
<LinearLayout xmlns:android="http://schemas. import androidx.appcompat.app.AppCompatActivity;
android.com/apk/res/android" import android.content.Intent;
xmlns:tools="http://schemas. import android.net.Uri;
android.com/tools" import android.os.Bundle;
android:layout_width="match_parent" import android.view.View;
android:layout_height="match_parent" import android.widget.*;
android:orientation="vertical" public class MainActivity extends AppCompatActivity {
tools:context=".MainActivity"> @Override
<EditText protected void onCreate(Bundle savedInstanceState) {
android:layout_width="wrap_content" super.onCreate(savedInstanceState);
android:layout_height="wrap_content" setContentView(R.layout.activity_main);
android:id="@+id/e1"/> Button b1=findViewById(R.id.b);
<Button EditText e1=findViewById(R.id.e1);
android:layout_width="wrap_content" b1.setOnClickListener(new View.OnClickListener() {
android:layout_height="wrap_content" @Override
android:text="Navigate" public void onClick(View v) {
android:id="@+id/b" String s=e1.getText().toString();
/> Intent i=new Intent(Intent.ACTION_VIEW, Uri.parse(s));
</LinearLayout> startActivity(i);
}
});
}
}
XML FILE:- JAVA FILE:-
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;
<LinearLayout xmlns:android="http://schemas. import androidx.appcompat.app.AppCompatActivity;
android.com/apk/res/android" import android.content.Intent;
xmlns:tools="http://schemas import android.net.Uri;
.android.com/tools" import android.os.Bundle;
android:layout_width="match_parent" import android.view.View;
android:layout_height="match_parent" import android.widget.*;
android:orientation="vertical" public class MainActivity extends AppCompatActivity {
tools:context=".MainActivity"> @Override
<Button protected void onCreate(Bundle savedInstanceState) {
android:layout_width="wrap_content" super.onCreate(savedInstanceState);
android:layout_height="wrap_content" setContentView(R.layout.activity_main);
android:text="Go to" Button b1=findViewById(R.id.b);
android:id="@+id/b" b1.setOnClickListener(new View.OnClickListener() {
/> @Override
</LinearLayout> public void onClick(View v) {
Intent i=new Intent(Intent.ACTION_DIAL,
Uri.parse("tel: +919325025671"));
startActivity(i);
}
});
}
}
XML FILE: - JAVA FILE: -
Activity_main.xml package com.example.myapplication;
<?xml version="1.0" encoding="utf-8"?> import androidx.appcompat.app.AppCompatActivity;
<LinearLayout xmlns:android="http://schemas import android.content.Intent;
.android.com/apk/res/android" import android.os.Bundle;
xmlns:app="http://schemas. import android.view.View;
android.com/apk/res-auto" import android.widget.*;
xmlns:tools="http://schemas.android.com/tools" public class MainActivity extends AppCompatActivity {
android:layout_width="match_parent" @Override
android:layout_height="match_parent" protected void onCreate(Bundle savedInstanceState) {
android:orientation="vertical" super.onCreate(savedInstanceState);
tools:context=".MainActivity"> setContentView(R.layout.activity_main);
<EditText Button b=findViewById(R.id.btn);
android:layout_width="wrap_content" final EditText et=findViewById(R.id.e);
android:layout_height="wrap_content" b.setOnClickListener(new View.OnClickListener()
android:id="@+id/e"/> {@Override
<Button public void onClick(View v) {
android:layout_width="wrap_content" int n= Integer.parseInt(et.getText().toString());
android:layout_height="wrap_content" int fact=1;
android:text="Calculate" for (int i = 1; i <= n; ++i) {
android:id="@+id/btn"/> fact=fact*i;
</LinearLayout> }
Intent i=new
Res.xml Intent(MainActivity.this,result.class);
<LinearLayout xmlns:android="http://schemas.a i.putExtra("factorial", String.valueOf(fact));
ndroid.com/apk/res/android" startActivity(i); }
android:layout_width="match_parent" });
xmlns:tools="http://schemas.android.com/tools" }
tools:context=".result" }
android:layout_height="match_parent">
<TextView Result.java
android:layout_width="wrap_content" package com.example.myapplication;
android:layout_height="wrap_content" import androidx.appcompat.app.AppCompatActivity;
android:id="@+id/t1"/> import android.os.Bundle;
</LinearLayout> import android.widget.*;
public class result extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.res);
TextView t = findViewById(R.id.t1);
String s = getIntent().getStringExtra("factorial");
t.setText(s);
}
}
Activity_Main.xml MainActivity.java
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;// InputActivity.java
<RelativeLayout import android.content.Intent;
xmlns:android="http://schemas. import android.os.Bundle;
android.com/apk/res/android" import android.view.View;
xmlns:tools="http://schemas. import android.widget.Button;
android.com/tools" import android.widget.EditText;
android:layout_width="match_parent"
android:layout_height="match_parent" import androidx.appcompat.app.AppCompatActivity;
tools:context=".MainActivity">
public class MainActivity extends AppCompatActivity {
<EditText
android:id="@+id/editTextName" @Override
android:layout_width="wrap_content" protected void onCreate(Bundle savedInstanceState) {
android:layout_height="wrap_content" super.onCreate(savedInstanceState);
android:hint="Enter your name" setContentView(R.layout.activity_main);
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" /> final EditText nameInput =
findViewById(R.id.editTextName);
<Button Button submitButton =
android:id="@+id/buttonSubmit" findViewById(R.id.buttonSubmit);
android:layout_width="wrap_content"
android:layout_height="wrap_content" submitButton.setOnClickListener(new
android:text="Submit" View.OnClickListener() {
@Override
android:layout_below="@id/editTextName" public void onClick(View v) {
android:layout_centerHorizontal="true" String name = nameInput.getText().toString();
android:layout_marginTop="16dp" /> Intent intent = new Intent(MainActivity.this,
DisplayActivity.class);
</RelativeLayout> intent.putExtra("NAME", name);
startActivity(intent);
}
});
}
}
Activity_display.xml DisplayActivity.java
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;// DisplayActivity.java
<RelativeLayout import android.os.Bundle;
xmlns:android="http://schemas. import android.widget.TextView;
android.com/apk/res/android"
xmlns:tools="http://schemas. import androidx.appcompat.app.AppCompatActivity;
android.com/tools"
android:layout_width="match_parent" public class DisplayActivity extends AppCompatActivity {
android:layout_height="match_parent"
tools:context=".DisplayActivity"> @Override
protected void onCreate(Bundle savedInstanceState) {
<TextView super.onCreate(savedInstanceState);
android:id="@+id/textViewName" setContentView(R.layout.activity_display);
android:layout_width="wrap_content"
android:layout_height="wrap_content" TextView nameTextView =
android:textSize="24sp" findViewById(R.id.textViewName);
android:layout_centerInParent="true"/>
// Retrieve the name from the intent
</RelativeLayout> String name = getIntent().getStringExtra("NAME");

// Display the name


nameTextView.setText("Your Name Is:"+name);
}
}
XML FILE: - JAVA FILE: -
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;
<LinearLayout xmlns:android="http://schemas. import android.app.Activity;
android.com/apk/res/android" import android.content.Context;
xmlns:app="http://schemas. import android.net.wifi.WifiManager;
android.com/apk/res-auto" import android.os.Bundle;
xmlns:tools="http://schemas. import android.view.View;
android.com/tools" import android.widget.Button;
android:layout_width="match_parent" import android.widget.Toast;
android:layout_height="match_parent" public class MainActivity extends Activity {
android:orientation="vertical" private WifiManager wifiManager;
tools:context=".MainActivity"> @Override
<Button protected void onCreate(Bundle savedInstanceState) {
android:id="@+id/start_wifi_button" super.onCreate(savedInstanceState);
android:layout_width="wrap_content" setContentView(R.layout.activity_main);
android:layout_height="wrap_content" wifiManager = (WifiManager)
android:text="Start WiFi" getApplicationContext().getSystemService(Context.WIFI_SERVICE);
/> Button startWifiButton =
<Button findViewById(R.id.start_wifi_button);
android:id="@+id/stop_wifi_button" Button stopWifiButton = findViewById(R.id.stop_wifi_button);
android:layout_width="wrap_content" startWifiButton.setOnClickListener(new
android:layout_height="wrap_content" View.OnClickListener() {
android:text="Stop WiFi" @Override
public void onClick(View v) {
android:layout_below="@id/start_wifi_button" if (!wifiManager.isWifiEnabled()) {
/> wifiManager.setWifiEnabled(true);
</LinearLayout> Toast.makeText(getApplicationContext(),"WiFi is
enabled",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"WiFi is
already enabled",Toast.LENGTH_LONG).show();
}
}
});
stopWifiButton.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
if (wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(false);
Toast.makeText(getApplicationContext(), "WiFi is
disabled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "WiFi is
already disabled", Toast.LENGTH_LONG).show();
}
}
});
}
}
XML FILE: - JAVA FILE: -
<LinearLayout xmlns:android="http://schemas. package com.example.myapplication;
android.com/apk/res/android" import android.content.Intent;
xmlns:app="http://schemas. import android.os.Bundle;
android.com/apk/res-auto" import android.view.View;
xmlns:tools="http://schemas. import android.widget.Button;
android.com/tools" import androidx.appcompat.app.AppCompatActivity;
android:layout_width="match_parent" public class MainActivity extends AppCompatActivity {
android:orientation="vertical" @Override
android:layout_height="match_parent" protected void onCreate(Bundle savedInstanceState) {
tools:context=".MainActivity"> super.onCreate(savedInstanceState);
<Button setContentView(R.layout.activity_main);
android:layout_width="match_parent" Button b1=findViewById(R.id.b1);
android:layout_height="wrap_content" Button b2=findViewById(R.id.b2);
android:text="Start Service" b1.setOnClickListener(new View.OnClickListener() {
android:id="@+id/b1"/> @Override
<Button public void onClick(View v) {
android:layout_width="match_parent" Intent serviceIntent = new
android:layout_height="wrap_content" Intent(getApplicationContext(), MyService.class);
android:text="Stop Service" startService(serviceIntent);
android:id="@+id/b2"/> }
</LinearLayout> });
b2.setOnClickListener(new View.OnClickListener() {
MyService.java @Override
package com.example.myapplication; public void onClick(View v) {
import android.app.Service; Intent serviceIntent = new
import android.content.Intent; Intent(getApplicationContext(), MyService.class);
import android.os.IBinder; stopService(serviceIntent);
import android.widget.Toast; }
public class MyService extends Service { });
@Override }
public IBinder onBind(Intent intent) { }
return null;
}
@Override
public int onStartCommand(Intent intent, int
flags, int startId) {

Toast.makeText(getApplicationContext(),"Service
Started",Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();

Toast.makeText(getApplicationContext(),"Service
Stopped",Toast.LENGTH_LONG).show();
}
}
JAVA FILE: - MyReceiver.java
package com.example.myapplication; package com.example.myapplication;
import android.content.Intent; import android.content.BroadcastReceiver;
import android.content.IntentFilter; import android.content.Context;
import android.os.Bundle; import android.content.Intent;
import android.view.View; import android.widget.Toast;
import android.widget.Button;
import public class MyReceiver extends
androidx.appcompat.app.AppCompatActivity; BroadcastReceiver {
public class MainActivity extends
AppCompatActivity { @Override
MyReceiver m=new MyReceiver(); public void onReceive(Context context, Intent
@Override protected void onCreate(Bundle intent) {
savedInstanceState) { Toast.makeText(context,"Time
super.onCreate(savedInstanceState); Changed",Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main); }
IntentFilter i=new }
IntentFilter(Intent.ACTION_TIME_CHANGED);
registerReceiver(m,i);
}}
XML FILE: - JAVA FILE: -
<LinearLayout package com.example.myapplication;
xmlns:android="http://schemas. import android.hardware.Sensor;
android.com/apk/res/android" import android.hardware.SensorManager;
xmlns:tools="http://schemas. import android.os.Bundle;
android.com/tools" import android.widget.TextView;
android:layout_width="match_parent" import androidx.appcompat.app.AppCompatActivity;
android:orientation="vertical" import java.util.List;
android:layout_height="match_parent" public class MainActivity extends AppCompatActivity {
tools:context=".MainActivity"> private TextView textViewSensors;
<TextView @Override
android:layout_width="wrap_content" protected void onCreate(Bundle savedInstanceState) {
android:layout_height="wrap_content" super.onCreate(savedInstanceState);
android:id="@+id/l"/> setContentView(R.layout.activity_main);
</LinearLayout> textViewSensors = findViewById(R.id.l);
SensorManager sensorManager = (SensorManager)
getSystemService(SENSOR_SERVICE);
List<Sensor> sensorList =
sensorManager.getSensorList(Sensor.TYPE_ALL);
StringBuilder sensorNamesBuilder = new StringBuilder();
for (Sensor sensor : sensorList) {
sensorNamesBuilder.append(sensor.getName()).append("\n");
}
textViewSensors.setText(sensorNamesBuilder.toString());
}
}
XML FILE: - JAVA FILE: -
<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;// MainActivity.java
<RelativeLayout import androidx.appcompat.app.AppCompatActivity;
xmlns:android="http://schemas. import android.os.Bundle;
android.com/apk/res/android" import android.view.View;
xmlns:tools="http://schemas. import android.widget.Button;
android.com/tools" public class MainActivity extends AppCompatActivity {
android:layout_width="match_parent" @Override
android:layout_height="match_parent" protected void onCreate(Bundle savedInstanceState) {
android:padding="16dp" super.onCreate(savedInstanceState);
tools:context=".MainActivity"> setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
<Button button.setOnClickListener(new View.OnClickListener() {
android:id="@+id/button" @Override
android:layout_width="wrap_content" public void onClick(View v) {
android:layout_height="wrap_content" // Call the method to show custom toast
android:text="Show Custom Toast" showCustomToast();
android:layout_centerInParent="true"/> }
});
</RelativeLayout> }
private void showCustomToast() {
Custom_Toast_layout.xml CustomToast.showToast(getApplicationContext(), "Custom
<?xml version="1.0" encoding="utf-8"?> toast message");
<LinearLayout }
xmlns:android="http://schemas. }
android.com/apk/res/android" CustomToast.java
android:id="@+id/customToastLayout" package com.example.myapplication;// CustomToast.java
android:layout_width="wrap_content" import android.content.Context;
android:layout_height="wrap_content" import android.view.LayoutInflater;
android:background="@drawable import android.view.View;
/ic_launcher_background" import android.widget.ImageView;
android:orientation="horizontal" import android.widget.TextView;
android:padding="16dp"> import android.widget.Toast;
public class CustomToast {
<ImageView public static void showToast(Context context, String message) {
android:id="@+id/imageView" LayoutInflater inflater = (LayoutInflater)
android:layout_width="wrap_content" context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
android:layout_height="wrap_content" View layout = inflater.inflate(R.layout.custom_toast_layout,
android:src="@drawable null); ImageView imageView =
/ic_launcher_foreground" /> layout.findViewById(R.id.imageView);
TextView textView = layout.findViewById(R.id.textView);
<TextView imageView.setImageResource(R.drawable.ic_launcher_foreground);
android:id="@+id/textView" textView.setText(message);
android:layout_width="wrap_content" Toast toast = new Toast(context);
android:layout_height="wrap_content" toast.setDuration(Toast.LENGTH_LONG);
android:textColor="#FFFFFF" toast.setView(layout);
android:text="Custom Toast" /> toast.show();
}
</LinearLayout> }
XML FILE: - Custom_Toast_Layout.xml
<RelativeLayout xmlns:android="http://schemas. <?xml version="1.0" encoding="utf-8"?>
android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.
xmlns:tools="http://schemas. android.com/apk/res/android"
android.com/tools" android:id="@+id/customToastLayout"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:padding="16dp" android:background="#ADA0A0"
tools:context=".MainActivity"> android:orientation="horizontal"
<CheckBox android:padding="16dp">
android:id="@+id/checkBox1" <TextView
android:layout_width="wrap_content" android:id="@+id/textView"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Pizza" android:layout_height="wrap_content"
android:layout_marginTop="20dp"/> android:textColor="#FF0000"
<CheckBox android:text="Custom Toast" />
android:id="@+id/checkBox2"
android:layout_width="wrap_content" </LinearLayout>
android:layout_height="wrap_content"
android:text="Burger"
android:layout_below="@id/checkBox1"
android:layout_marginTop="20dp"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coffe"
android:layout_below="@id/checkBox2"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:layout_below="@id/checkBox3"
android:layout_marginTop="20dp"/>
</RelativeLayout>
MainActivity.java CustomToast.java
package com.example.myapplication; package com.example.myapplication;
import import android.content.Context;
androidx.appcompat.app.AppCompatActivity; import android.view.LayoutInflater;
import android.os.Bundle; import android.view.View;
import android.view.View; import android.widget.TextView;
import android.widget.Button; import android.widget.Toast;
import android.widget.CheckBox; public class CustomToast {
public class MainActivity extends public static void showToast(Context context, String message)
AppCompatActivity { {
LayoutInflater inflater = (LayoutInflater)
CheckBox checkBox1, checkBox2, checkBox3; context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_toast_layout,
@Override null);
protected void onCreate(Bundle TextView textView = layout.findViewById(R.id.textView);
savedInstanceState) { textView.setText(message);
super.onCreate(savedInstanceState); Toast toast = new Toast(context);
setContentView(R.layout.activity_main); toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
checkBox1 = findViewById(R.id.checkBox1); toast.show();
checkBox2 = findViewById(R.id.checkBox2); }
checkBox3 = findViewById(R.id.checkBox3); }

Button button = findViewById(R.id.button);


button.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomToast();
}
});
}
private void showCustomToast() {
StringBuilder message = new
StringBuilder("Selected Checkboxes:\n");
if (checkBox1.isChecked()) {
message.append("- Pizza\n");
}
if (checkBox2.isChecked()) {
message.append("- Burger\n");
}
if (checkBox3.isChecked()) {
message.append("- Coffee\n");
}

CustomToast.showToast(getApplicationContext(),
message.toString());
}
}
MainActivity.java turnOffBtn.setOnClickListener(new
package com.example.myapplication; View.OnClickListener() {
@SuppressLint("MissingPermission")
import android.annotation.SuppressLint; @Override
import android.bluetooth.BluetoothAdapter; public void onClick(View v) {
import android.content.Intent; if (bluetoothAdapter.isEnabled()) {
import android.os.Bundle; bluetoothAdapter.disable();
import android.view.View; Toast.makeText(MainActivity.this, "Bluetooth turned
import android.widget.Button; off", To
import android.widget.Toast; ast.LENGTH_SHORT).show();
} else {
import androidx.appcompat.app.AppCompatActivity; Toast.makeText(MainActivity.this, "Bluetooth
is
public class MainActivity extends AppCompatActivity { already off", Toast.LENGTH_SHORT).show();
}
private BluetoothAdapter bluetoothAdapter; }
});
@Override
protected void onCreate(Bundle savedInstanceState) discoverableBtn.setOnClickListener(new
{ View.OnClickListener() {
super.onCreate(savedInstanceState); @SuppressLint("MissingPermission")
setContentView(R.layout.activity_main); @Override
public void onClick(View v) {
bluetoothAdapter = Intent discoverableIntent = new
BluetoothAdapter.getDefaultAdapter(); Intent(Bluetoot
hAdapter.ACTION_REQUEST_DISCOVERABLE);
Button turnOnBtn = findViewById(R.id.turnOnBtn); discoverableIntent.putExtra(Bluetooth
Button turnOffBtn = findViewById(R.id.turnOffBtn); Adapter.EXTRA_DISCOVERABLE_DURATION, 300
Button discoverableBtn = startActivity(discoverableIntent);
findViewById(R.id.discoverableBtn); }
Button showPairedDevicesBtn = });
findViewById(R.id.showPairedDevicesBtn);
showPairedDevicesBtn.setOnClickListener(new
turnOnBtn.setOnClickListener(new View.
View.OnClickListener() { OnClickListener() {
@SuppressLint("MissingPermission") @Override
@Override public void onClick(View v) {
public void onClick(View v) {
if (!bluetoothAdapter.isEnabled()) { Toast.makeText(getApplicationContext(),"Device N
Intent enableBtIntent = new ot found",Toast.LENGTH_LONG).show();
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); }
startActivity(enableBtIntent); });
} else { }
Toast.makeText(MainActivity.this, "Bluetooth }
is already on", Toast.LENGTH_SHORT).show();
}
}
});
turnOffBtn.setOnClickListener(new
View.OnClickListener() {
Activity_main.xml
<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">

<Button
android:id="@+id/turnOnBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Turn On Bluetooth" />

<Button
android:id="@+id/turnOffBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Turn Off Bluetooth" />

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

<Button
android:id="@+id/showPairedDevicesBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Paired Devices" />

</LinearLayout>
Activity_main.xml Fade_anim.xml
<RelativeLayout xmlns:android="http://schemas. <?xml version="1.0" encoding="utf-8"?>
android.com/apk/res/android" <alpha
xmlns:tools="http://schemas. xmlns:android="http://schemas.android.com/apk/res/android"
android.com/tools" android:duration="1000"
android:layout_width="match_parent" android:fromAlpha="0.0"
android:layout_height="match_parent" android:toAlpha="1.0" />
tools:context=".MainActivity">
<ImageView Slide_anim.xml
android:id="@+id/imageView" <?xml version="1.0" encoding="utf-8"?>
android:layout_width="200dp" <translate
android:layout_height="200dp" xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:src="@drawable/ic_launcher_background" android:fromXDelta="-100%"
android:layout_centerHorizontal="true" android:toXDelta="0%"
android:layout_marginTop="50dp"/> android:fromYDelta="0%"
<Button android:toYDelta="0%" />
android:id="@+id/buttonFade"
android:layout_width="wrap_content" Zoom_anim.xml
android:layout_height="wrap_content" <?xml version="1.0" encoding="utf-8"?>
android:text="Fade" <scale
android:layout_below="@id/imageView" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_alignParentStart="true" android:duration="1000"
android:layout_margin="16dp"/> android:fromXScale="0.0"
<Button android:fromYScale="0.0"
android:id="@+id/buttonZoom" android:pivotX="50%"
android:layout_width="wrap_content" android:pivotY="50%"
android:layout_height="wrap_content" android:toXScale="1.0"
android:text="Zoom" android:toYScale="1.0" />
android:layout_below="@id/imageView"
android:layout_alignParentEnd="true" rotate_anim.xml
android:layout_margin="16dp"/> <?xml version="1.0" encoding="utf-8"?>
<Button <rotate
android:id="@+id/buttonRotate" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:duration="1000"
android:layout_height="wrap_content" android:fromDegrees="0"
android:text="Rotate" android:toDegrees="360"
android:layout_below="@id/buttonFade" android:pivotX="50%"
android:layout_alignParentStart="true" android:pivotY="50%" />
android:layout_margin="16dp"/>
<Button
android:id="@+id/buttonSlide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Slide"
android:layout_below="@id/buttonZoom"
android:layout_alignParentEnd="true"
android:layout_margin="16dp"/>
</RelativeLayout>
MainActivity.java

package com.example.myapplication;
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 ImageView imageView;
private Button buttonFade, buttonZoom, buttonRotate, buttonSlide;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
buttonFade = findViewById(R.id.buttonFade);
buttonZoom = findViewById(R.id.buttonZoom);
buttonRotate = findViewById(R.id.buttonRotate);
buttonSlide = findViewById(R.id.buttonSlide);
buttonFade.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
Animation fadeAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_animation);
imageView.startAnimation(fadeAnimation);}
});
buttonZoom.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Animation zoomAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.zoom_animation);
imageView.startAnimation(zoomAnimation);
}
});
buttonRotate.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View v) {
Animation rotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate_animation);
imageView.startAnimation(rotateAnimation);
}
});
buttonSlide.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Animation slideAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_animation);
imageView.startAnimation(slideAnimation);
}
});
}

You might also like