Aad QB Ans 2
Aad QB Ans 2
Aad QB Ans 2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonOpenURL = findViewById(R.id.buttonOpenURL);
buttonOpenURL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openURL();
}
});
}
<Button
android:id="@+id/buttonOpenURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open VCET Website"
android:layout_centerInParent="true"/>
</RelativeLayout>
Create a broadcast receiver for connection change in Android
JAVA
package com.example.broadcastconnectionchange;
import androidx.appcompat.app.AppCompatActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
protected void onResume() {
super.onResume();
// Register connectivity receiver
registerReceiver(connectivityReceiver, new
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
@Override
protected void onPause() {
super.onPause();
// Unregister connectivity receiver
unregisterReceiver(connectivityReceiver);
// Unregister local broadcast receiver
unregisterReceiver(connectivityStatusReceiver);
}
<TextView
android:id="@+id/connectivityStatusTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Checking connectivity..."
android:textSize="18sp"
android:padding="16dp" />
</RelativeLayout>
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
</application>
</manifest>
ConnectivityReceiver.java
package com.example.broadcastconnectionchange;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager connectivityManager = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextRollNumber = findViewById(R.id.editTextRollNumber);
editTextName = findViewById(R.id.editTextName);
buttonAdd = findViewById(R.id.buttonAdd);
buttonView = findViewById(R.id.buttonView);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String rollNumber = editTextRollNumber.getText().toString();
String name = editTextName.getText().toString();
buttonView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ViewStudentsActivity.class);
startActivity(intent);
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editTextRollNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Roll Number" />
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name" />
<Button
android:id="@+id/buttonAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Student" />
<Button
android:id="@+id/buttonView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View Students" />
</LinearLayout>
MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.LoginSQLite"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
DatabaseHelper.java
package com.example.loginsqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_students);
textViewStudents = findViewById(R.id.textViewStudents);
databaseHelper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public void onMapReady(GoogleMap googleMap) {
myMap = googleMap;
myMap.getUiSettings().setZoomControlsEnabled(true);
<androidx.fragment.app.FragmentContainerView
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
MANIFEST
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Mapgoogle"
tools:targetApi="31">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY "/>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Develop an android application to implement a custom design Menu Bar.
XML:
<?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">
<Button
android:id="@+id/home_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:text="Home"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/search_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:text="Search"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/profile_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="?attr/selectableItemBackground"
android:text="Profile"
android:textColor="#FFFFFF" />
</LinearLayout>
<TextView
android:id="@+id/home_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome_to_the_home_page"
android:textSize="24sp"
android:visibility="gone" />
<TextView
android:id="@+id/search_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/search_for_content_here"
android:textSize="24sp"
android:visibility="gone" />
<TextView
android:id="@+id/profile_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/your_profile_details"
android:textSize="24sp"
android:visibility="gone" />
</FrameLayout>
</RelativeLayout>
JAVA:
package com.example.a16_menubar;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homeButton = findViewById(R.id.home_button);
searchButton = findViewById(R.id.search_button);
profileButton = findViewById(R.id.profile_button);
homeContent = findViewById(R.id.home_content);
searchContent = findViewById(R.id.search_content);
profileContent = findViewById(R.id.profile_content);
homeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showContent(R.id.home_content);
}
});
searchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showContent(R.id.search_content);
}
});
profileButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showContent(R.id.profile_content);
}
});
</resources>
Create a new Android application to get Request permission to access the device's
location.
JAVA
package com.example.reqdevicelocation;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
// Permission granted
Toast.makeText(this, "Location permission granted",
Toast.LENGTH_SHORT).show();
} else {
// Permission denied
Toast.makeText(this, "Location permission denied",
Toast.LENGTH_SHORT).show();
}
}
}
}
MANIFEST:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ReqDeviceLocation"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Request Location Permission Example"
android:textSize="18sp"
android:layout_centerInParent="true"/>
</RelativeLayout>