Skip to content

Instantly share code, notes, and snippets.

View AkaashSaini's full-sized avatar
🏠
Working from home

Akash Saini AkaashSaini

🏠
Working from home
  • searce
  • rajkot,GJ
View GitHub Profile
Multi-Select List
Create list – suppose sports is array String values
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, sports);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setAdapter(adapter);
Fetch Selected items – Write following code in click event of Button
SparseBooleanArray checked = listView.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
Spinner
It is used to create the drop down list of options from which we can select the option.
1. Layout
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
->activity_main.xml
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="84dp"
android:layout_marginHorizontal="30dp"
android:hint="Search Here!"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
Custom List using ListView
Step 1
Add ListView to main layout 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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
Using Checkbox
The checkbox is simple component. It is used to provide multiple choice to select.
XML Code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckBox
android:id="@+id/checkBox"
Button camera,gallery;
camera=promptView.findViewById(R.id.open_camera);
gallery=promptView.findViewById(R.id.open_gallery);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkSelfPermission(Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.CAMERA}, CAEMRA_REQUEST_CODE);
}
else{
Using SQLite
Android provides built-in RDBMS sqlite. It is a server less database. It is very user friendly small in size. To use the sqlite there is no need to install any other software.
Create the database using method openOrCreateDatabase(). It creates the new database if database does not exist otherwise open the existing database.
SQLiteDatabase db=openOrCreateDatabase(“mydatabase”,MODE_PRIVATE,null);
MODE_PRIVATE The database can be accessed only in application. The other application cannot use the database.
Create table using method execSQL().
db.execSQL(“CREATE TABLE IF NOT EXISTS mytable(…);”);
Insert values using method execSQL() method.
db.execSQL(“insert into mytable values();”);
Close the connection. It is necessary to close the connection.
Connectivity with MySql Database
Use following steps to connect the android application with mysql server database.
• Set the permissions for HTTP connection
StrictMode.enableDefaults();
• Set uses-permission for INTERNET in AndroidManifest
• Create the object of HttpClient
HttpClient httpclient=new DefaultHttpClient();
• Create the object HttpPost or HttpGet. It represents the url to be executed.
PHP
HttpPost httppost=new HttpPost("http://10.0.2.2/android/Test.php?name="+name+"&branch="+branch);
<------------redirect to dialer----------------->
Uri num=Uri.parse("tel:"+number);
Intent call=new Intent(Intent.ACTION_DIAL);
call.setData(num);
startActivity(call);
<------------redirect to message----------------->
Uri smsUri= Uri.parse("smsto:" +number);
Intent msg=new Intent(Intent.ACTION_SENDTO);
msg.setData(smsUri);
startActivity(msg);
Fragments
A Fragment is a piece of an activity which enable more modular activity design. It will not be wrong if we say, a fragment is a kind of sub-activity.
• A fragment has its own layout and its own behaviour with its own life cycle callbacks.
• You can add or remove fragments in an activity while the activity is running.
• You can combine multiple fragments in a single activity to build a multi-plane UI.
• A fragment can be used in multiple activities.
• Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped.
• A fragment can implement a behaviour that has no user interface component.
• Fragments were added to the Android API in Honeycomb version of Android which API version 11.