Imp Mad Programs
Imp Mad Programs
XML
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
Android:orientation=”vertical”
Android:layout_width=”match_parent”
Android:layout_height=”match_parent”>
<ListView
Android:id=”@+id/listView”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content” />
</LinearLayout>
Java
Package com.example.listviewdemo;
Import android.app.Activity;
Import android.os.Bundle;
Import android.widget.ArrayAdapter;
Import android.widget.ListView;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] items = {“Item 1”, “Item 2”, “Item 3”, “Item 4”, “Item 5”};
Android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
Manifest
<application
…>
<activity android:name=”.MainActivity”>
<intent-filter>
</intent-filter>
</activity>
</application>
XML
<GridView xmlns:android=http://schemas.android.com/apk/res/android
Android:id=”@+id/gridView”
Android:layout_width=”match_parent”
Android:layout_height=”match_parent”
Android:numColumns=”4” />
Java
Packagecom.example.gridviewdemo;
Import android.app.Activity;
Import android.os.Bundle;
Import android.widget.ArrayAdapter;
Import android.widget.GridView;
@Override
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Prepare 16 items
Android.R.layout.simple_list_item_1, items);
gridView.setAdapter(adapter);
3. Image view
XML
<ImageView xmlns:android=http://schemas.android.com/apk/res/android
Android:id=”@+id/imageView”
Android:layout_width=”wrap_content”
Android:layout_height=”wrap_content”
Android:src=”@drawable/sample_image” />
Java
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Img.setImageResource(R.drawable.sample_image);
4. Design UI using table layout to display buttons with 09 numbers on it. Even display
Submit and clear button. When user clicks on particular buttons and later when clicks on
submit button, it should display the numbers clicked. (Note: Consider the appropriate
XML file. All attributes are not required. In java file all imports are not expected. Different
relevant logic/code can be considered.)
XML
<TableLayout xmlns:android=http://schemas.android.com/apk/res/android
Android:layout_width=”match_parent” android:layout_height=”match_parent”
Android:stretchColumns=”*”>
<TableRow>
<Button android:id=”@+id/b1” android:text=”1”/>
<Button android:id=”@+id/b2” android:text=”2”/>
<Button android:id=”@+id/b3” android:text=”3”/>
</TableRow>
<TableRow>
<Button android:id=”@+id/b4” android:text=”4”/>
<Button android:id=”@+id/b5” android:text=”5”/>
<Button android:id=”@+id/b6” android:text=”6”/>
</TableRow>
<TableRow>
<Button android:id=”@+id/b7” android:text=”7”/>
<Button android:id=”@+id/b8” android:text=”8”/>
<Button android:id=”@+id/b9” android:text=”9”/>
</TableRow>
<TableRow>
<Button android:id=”@+id/b0” android:text=”0”/>
<Button android:id=”@+id/submit” android:text=”Submit”/>
<Button android:id=”@+id/clear” android:text=”Clear”/>
</TableRow>
<TextView android:id=”@+id/output” android:text=”” />
</TableLayout>
Java
Super.onCreate(b);
setContentView(R.layout.activity_main);
View.OnClickListener numClick = v → {
Input.append(btn.getText());
};
R.id.b5,R.id.b6,R.id.b7,R.id.b8,R.id.b9};
For(int id : ids)
findViewById(id).setOnClickListener(numClick);
findViewById(R.id.clear).setOnClickListener(v → {
input.setLength(0);
out.setText(“”);
});
}
XML
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
Android:orientation=”vertical” android:layout_width=”match_parent”
Android:layout_height=”match_parent” android:padding=”16dp”>
Java
bAdapter = BluetoothAdapter.getDefaultAdapter();
list = findViewById(R.id.list);
findViewById(R.id.on).setOnClickListener(v → {
if (!bAdapter.isEnabled())
bAdapter.enable();
});
findViewById(R.id.off).setOnClickListener(v → {
if (bAdapter.isEnabled())
bAdapter.disable();
});
findViewById(R.id.paired).setOnClickListener(v → {
Set<BluetoothDevice> paired = bAdapter.getBondedDevices();
StringBuilder sb = new StringBuilder(“Paired Devices:\n”);
For (BluetoothDevice d : paired)
Sb.append(d.getName()).append(“\n”);
List.setText(sb.toString());
});
}
}
*Permission*
<uses-permission android:name=”android.permission.BLUETOOTH”/>
<uses-permission android:name=”android.permission.BLUETOOTH_ADMIN”/>
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”>
Manifest
<uses-permission android:name=”android.permission.CAMERA”/>
<uses-feature android:name=”android.hardware.camera” android:required=”true”/>
Java
Package com.example.captureimage;
Import android.content.Intent;
Import android.graphics.Bitmap;
Import android.os.Bundle;
Import android.provider.MediaStore;
Import android.widget.Button;
Import android.widget.ImageView;
Import androidx.appcompat.app.AppCompatActivity;
@Override
Protected void onCreate(Bundle savedInstanceState) {
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView);
Button btn = findViewById(R.id.captureBtn);
Btn.setOnClickListener(v → {
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, REQUEST_IMAGE);
});
}
@Override
Protected void onActivityResult(int reqCode, int resCode, Intent data) {
Super.onActivityResult(reqCode, resCode, data);
If (reqCode == REQUEST_IMAGE && resCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get(“data”);
Img.setImageBitmap(photo);
}
}
}
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”>
<EditText android:id=”@+id/inputText”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content”
Android:hint=”Enter text here” />
<Button android:id=”@+id/speakBtn”
Android:layout_width=”wrap_content”
Android:layout_height=”wrap_content”
Android:text=”Speak” />
</LinearLayout>
Java
Package com.example.texttospeech;
Import android.os.Bundle;
Import android.speech.tts.TextToSpeech;
Import android.widget.Button;
Import android.widget.EditText;
Import androidx.appcompat.app.AppCompatActivity;
Import java.util.Locale;
TextToSpeech tts;
@Override
Protected void onCreate(Bundle savedInstanceState) {
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Speak.setOnClickListener(v → {
String text = input.getText().toString();
Tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
});
}
@Override
Protected void onDestroy() {
If (tts != null) {
Tts.stop();
Tts.shutdown();
}
Super.onDestroy();
}
}
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”>
Java
Package com.example.getlocation;
Import android.Manifest;
Import android.content.pm.PackageManager;
Import android.location.Location;
Import android.os.Bundle;
Import android.widget.Button;
Import android.widget.TextView;
Import androidx.annotation.NonNull;
Import androidx.appcompat.app.AppCompatActivity;
Import androidx.core.app.ActivityCompat;
Import com.google.android.gms.location.*;
FusedLocationProviderClient locationClient;
TextView locationText;
@Override
Protected void onCreate(Bundle b) {
Super.onCreate(b);
setContentView(R.layout.activity_main);
locationText = findViewById(R.id.locationText);
Button getLocBtn = findViewById(R.id.getLocBtn);
locationClient = LocationServices.getFusedLocationProviderClient(this);
getLocBtn.setOnClickListener(v → {
if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
Return;
}
locationClient.getLastLocation().addOnSuccessListener(this, location → {
if (location != null) {
double lat = location.getLatitude();
double lon = location.getLongitude();
locationText.setText(“Location: “ + lat + “, “ + lon);
} else {
locationText.setText(“Location not available”);
}
});
});
}
@Override
Public void onRequestPermissionsResult(int req, @NonNull String[] perms, @NonNull int[]
grantResults) {
If (req == 1 && grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
Recreate(); // retry getting location after permission
}
}
}
Manifest permission
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>
Gradle
Implementation ‘com.google.android.gms:play-services-location:21.0.1’
XMl
<Button android:id=”@+id/btnNext”
Android:layout_width=”wrap_content”
Android:layout_height=”wrap_content”
Android:text=”Go to Second”/>
Main activity 1
Main activity 2
Manifest
Xml
<fragment xmlns:android=http://schemas.android.com/apk/res/android
Android:id=”@+id/map”
Android:name=”com.google.android.gms.maps.SupportMapFragment”
Android:layout_width=”match_parent”
Android:layout_height=”match_parent”/>
Java
Manifest
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION”/>
<uses-permission android:name=”android.permission.INTERNET”/>
<meta-data
Android:name=”com.google.android.geo.API_KEY”
Android:value=”YOUR_API_KEY”/>
<Button
Android:id=”@+id/btnDirections”
Android:layout_width=”wrap_content”
Android:layout_height=”wrap_content”
Android:text=”Get Directions to MSBTE” />
</LinearLayout>
Java
Package com.example.directionsapp;
Import android.content.Intent;
Import android.net.Uri;
Import android.os.Bundle;
Import android.view.View;
Import android.widget.Button;
Import androidx.appcompat.app.AppCompatActivity;
btnDirections.setOnClickListener(new View.OnClickListener() {
@Override
Public void onClick(View view) {
// Directions from current location to MSBTE, Bandra
Uri uri = Uri.parse(“google.navigation:q=MSBTE+Bandra”);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
Intent.setPackage(“com.google.android.apps.maps”);
startActivity(intent);
}
});
}
}
Manifest
<uses-permission android:name=”android.permission.ACCESS_FINE_LOCATION” />
<uses-permission android:name=”android.permission.INTERNET” />
12. Develop an android application for taking student feedback with database connectivity.
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=”20dp”>
<EditText
Android:id=”@+id/name”
Android:hint=”Name”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content”/>
<EditText
Android:id=”@+id/feedback”
Android:hint=”Feedback”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content”/>
<Button
Android:id=”@+id/save”
Android:text=”Save”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content”/>
</LinearLayout>
Java
Package com.example.feedbackapp;
Import android.os.Bundle;
Import android.view.View;
Import android.widget.*;
Import androidx.appcompat.app.AppCompatActivity;
@Override
Protected void onCreate(Bundle b) {
Super.onCreate(b);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
feedback = findViewById(R.id.feedback);
save = findViewById(R.id.save);
db = new MyDB(this);
save.setOnClickListener(v → {
db.saveData(name.getText().toString(), feedback.getText().toString());
Toast.makeText(this, “Saved”, Toast.LENGTH_SHORT).show();
});
}
}
Database.java
Package com.example.feedbackapp;
Import android.content.*;
Import android.database.sqlite.*;
MainActivity.java
Package com.example.simpledb;
Import android.os.Bundle;
Import androidx.appcompat.app.AppCompatActivity;
MainAct.java
Package com.example.smsdemo;
Import android.os.Bundle;
Import android.telephony.SmsManager;
Import android.widget.*;
Import androidx.appcompat.app.AppCompatActivity;
@Override
Protected void onCreate(Bundle b) {
Super.onCreate(b);
setContentView(R.layout.activity_main);
number = findViewById(R.id.number);
message = findViewById(R.id.message);
send = findViewById(R.id.send);
send.setOnClickListener(v → {
SmsManager sms = SmsManager.getDefault();
Sms.sendTextMessage(number.getText().toString(), null, message.getText().toString(),
null, null);
Toast.makeText(this, “SMS Sent”, Toast.LENGTH_SHORT).show();
});
}
}
Smsreceiver.java
Package com.example.smsdemo;
Import android.content.*;
Import android.os.Bundle;
Import android.telephony.SmsMessage;
Import android.widget.Toast;
<ListView
Android:id=”@+id/list”
Android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
MainAct.java
Package com.example.sensorlist;
Import android.hardware.Sensor;
Import android.hardware.SensorManager;
Import android.os.Bundle;
Import android.widget.ArrayAdapter;
Import android.widget.ListView;
Import androidx.appcompat.app.AppCompatActivity;
Import java.util.*;
@Override
Protected void onCreate(Bundle b) {
Super.onCreate(b);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list);
<EditText
Android:id=”@+id/email”
Android:hint=”Email Address”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content”/>
<EditText
Android:id=”@+id/subject”
Android:hint=”Subject”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content”/>
<EditText
Android:id=”@+id/body”
Android:hint=”Message”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content”/>
<Button
Android:id=”@+id/send”
Android:text=”Send Email”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content”/>
</LinearLayout>
MainAct.java
Package com.example.sendemail;
Import android.content.Intent;
Import android.net.Uri;
Import android.os.Bundle;
Import android.widget.*;
Import androidx.appcompat.app.AppCompatActivity;
@Override
Protected void onCreate(Bundle b) {
Super.onCreate(b);
setContentView(R.layout.activity_main);
email = findViewById(R.id.email);
subject = findViewById(R.id.subject);
body = findViewById(R.id.body);
send = findViewById(R.id.send);
send.setOnClickListener(v → {
Intent i = new Intent(Intent.ACTION_SENDTO);
i.setData(Uri.parse(mailto: + email.getText().toString()));
i.putExtra(Intent.EXTRA_SUBJECT, subject.getText().toString());
i.putExtra(Intent.EXTRA_TEXT, body.getText().toString());
startActivity(i);
});
}
}