Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Aad QB Ans 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Develop an android app to open an URL(www.vcet.ac.

in) in a browser by using implicit


intent.
JAVA
package com.example.openurl;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private Button buttonOpenURL;

@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();
}
});
}

private void openURL() {


// Create an intent to open the URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vcet.ac.in"));
// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
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/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;

public class MainActivity extends AppCompatActivity {

private ConnectivityReceiver connectivityReceiver;


private TextView connectivityStatusTextView;
private BroadcastReceiver connectivityStatusReceiver;

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

// Initialize connectivity receiver


connectivityReceiver = new ConnectivityReceiver();

// Find the TextView in the layout


connectivityStatusTextView = findViewById(R.id.connectivityStatusTextView);

// Perform actions based on initial connectivity status


updateConnectivityStatus(isConnected());

// Initialize local broadcast receiver to listen for connectivity changes


connectivityStatusReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
boolean isConnected = intent.getBooleanExtra("isConnected", false);
updateConnectivityStatus(isConnected);
}
};
}

@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);
}

private boolean isConnected() {


ConnectivityManager connectivityManager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
return false;
}

private void updateConnectivityStatus(boolean isConnected) {


if (isConnected) {
// Connected to the internet, perform actions
connectivityStatusTextView.setText("Connected to the Internet");
Toast.makeText(this, "Performing online actions", Toast.LENGTH_SHORT).show();
// Add more actions like starting a network request, updating data, etc.
} else {
// Disconnected from the internet, perform actions
connectivityStatusTextView.setText("Disconnected from the Internet");
Toast.makeText(this, "Performing offline actions", Toast.LENGTH_SHORT).show();
// Add more actions like saving data locally, notifying the user, etc.
}
}
}
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">

<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">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


<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.BroadcastConnectionChange"
tools:targetApi="31">
<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>
<receiver
android:name=".ConnectivityReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>

</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;

public class ConnectivityReceiver extends BroadcastReceiver {

public static final String CONNECTIVITY_ACTION =


"com.example.networkmonitor.CONNECTIVITY_CHANGE";

@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();

// Send a local broadcast with the connectivity status


Intent connectivityIntent = new Intent(CONNECTIVITY_ACTION);
connectivityIntent.putExtra("isConnected", isConnected);
context.sendBroadcast(connectivityIntent);
}
}
}
}
Develop an application to save and retrieve students data using SQLite database
JAVA
package com.example.loginsqlite;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private EditText editTextRollNumber, editTextName;


private Button buttonAdd, buttonView;
private DatabaseHelper databaseHelper;

@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);

databaseHelper = new DatabaseHelper(this);

buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String rollNumber = editTextRollNumber.getText().toString();
String name = editTextName.getText().toString();

boolean isInserted = databaseHelper.insertStudent(rollNumber, name);


if (isInserted) {
Toast.makeText(MainActivity.this, "Student Added",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Error Adding Student",
Toast.LENGTH_SHORT).show();
}
}
});

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" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>

<!-- View Students Activity -->


<activity android:name=".ViewStudentsActivity" />
</application>

</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;

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "school.db"; // New database name


private static final int DATABASE_VERSION = 1;

private static final String TABLE_NAME = "student_records"; // New table name


private static final String COLUMN_ID = "id";
private static final String COLUMN_ROLL_NUMBER = "roll_number";
private static final String COLUMN_NAME = "name";

public DatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_ROLL_NUMBER + " TEXT, " +
COLUMN_NAME + " TEXT)";
db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public boolean insertStudent(String rollNumber, String name) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_ROLL_NUMBER, rollNumber);
contentValues.put(COLUMN_NAME, name);

long result = db.insert(TABLE_NAME, null, contentValues);


return result != -1;
}

public Cursor getAllStudents() {


SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
}
}
ViewStudentsActivity.java
package com.example.loginsqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;

public class ViewStudentsActivity extends AppCompatActivity {

private TextView textViewStudents;


private DatabaseHelper databaseHelper;

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

textViewStudents = findViewById(R.id.textViewStudents);
databaseHelper = new DatabaseHelper(this);

Cursor cursor = databaseHelper.getAllStudents();


if (cursor.getCount() == 0) {
textViewStudents.setText("No students found");
} else {
StringBuilder stringBuilder = new StringBuilder();
while (cursor.moveToNext()) {
stringBuilder.append("ID: ").append(cursor.getInt(0)).append("\n");
stringBuilder.append("Roll Number: ").append(cursor.getString(1)).append("\n");
stringBuilder.append("Name: ").append(cursor.getString(2)).append("\n\n");
}
textViewStudents.setText(stringBuilder.toString());
}
}
}
Implement location tracking to display the user's current location on the map, Create a
new Android application to integrate Google Maps API, Implement zoom controls to
allow users to zoom in and out of the map.
JAVA
package com.example.mapgoogle;
import androidx.fragment.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap myMap;

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

SupportMapFragment mapFragment = (SupportMapFragment)


getSupportFragmentManager().findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(this);
}
}

@Override
public void onMapReady(GoogleMap googleMap) {
myMap = googleMap;
myMap.getUiSettings().setZoomControlsEnabled(true);

LatLng myLocation = new LatLng(9.8920605, 78.0534300);


myMap.addMarker(new MarkerOptions().position(myLocation).title("Home"));
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 15)); //
Added zoom level for better view
}
}
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">

<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">

<uses-permission android:name="android.permission.INTERNET" />


<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<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">

<!-- Custom Menu Bar -->


<LinearLayout
android:id="@+id/menu_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#6200EE"
android:orientation="horizontal"
android:padding="16dp">

<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>

<!-- Main Content -->


<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/menu_bar"
android:padding="16dp">

<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;

public class MainActivity extends AppCompatActivity {

private Button homeButton;


private Button searchButton;
private Button profileButton;
private TextView homeContent;
private TextView searchContent;
private TextView profileContent;

@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);
}
});

// Show home content by default


showContent(R.id.home_content);
}

private void showContent(int contentId) {


homeContent.setVisibility(contentId == R.id.home_content ? View.VISIBLE :
View.GONE);
searchContent.setVisibility(contentId == R.id.search_content ? View.VISIBLE :
View.GONE);
profileContent.setVisibility(contentId == R.id.profile_content ? View.VISIBLE :
View.GONE);
}
}
<!-- res/values/styles.xml -->
<resources>

<!-- Base application theme -->


<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here -->
<item name="colorPrimary">#6200EE</item>
<item name="colorPrimaryDark">#3700B3</item>
<item name="colorAccent">#03DAC5</item>
</style>

<!-- Custom styles for menu bar and buttons -->


<style name="MenuButton">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:background">?attr/selectableItemBackground</item>
<item name="android:textColor">#FFFFFF</item>
</style>

</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;

public class MainActivity extends AppCompatActivity {


private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;

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

// Check if location permissions are granted


if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Request location permissions
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
} else {
// Permissions already granted
Toast.makeText(this, "Location permissions already granted",
Toast.LENGTH_SHORT).show();
}
}

@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">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
/>

<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" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

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

You might also like