Android Programming
Android Programming
Development
CS 306: ICT Workshop - II
Android Introduction
∙ Storage: Uses SQLite, a light weight relational database storage for data storage (really helpful when limited
mobile memory storage is to be considered).
∙ Media Support: Include support for large number of media formats for Images, Audio as well as for Video, like:
H.263, H.264, MPEG-4 SP, AMR, AMR WB, AAC, MP3, MIDI, WAV, JPEG, PNG, GIF & BMP.
∙ Messaging: Both SMS and MMS are supported.
∙ Web Browser: Based on Open Source WebKit, now known as Chrome.
∙ Connectivity: Supports large group of networks like: GSM/EDGE, IDEN, CDMA, EV-DO, UMTS, Bluetooth,
WiFi, LTE and WiMAX.
∙ Hardware Support: Accelerometer Sensor, Camera, Digital Compass, Proximity Sensor & GPS and a lot more.
∙ Multi-Touch: Supports multi-touch screen.
∙ Multi-Task: Supports application multi-tasking.
∙ Flash Support: Supports Flash.
∙ Tethering: Supports sharing of Internet as wired or wireless hotspots.
Android Version
Android Architecture
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called", Toast.LENGTH_LONG).show();
}
Or
TableLayout android:layout_height="match_parent"
android:layout_marginTop="100dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >
<TableRow android:background="#0079D6" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
• android:layout_height="wrap_content"
android:layout_weight="1"
android:text="UserId" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="User Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Location" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Suresh Dasari" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hyderabad" />
</TableRow>
</TableLayout>
FrameLayout <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imgvw1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:src="@drawable/flimg" />
<TextView
android:id="@+id/txtvw1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:background="#4C374A"
android:padding="10dp"
android:text="Grand Palace, Bangkok"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<TextView
android:id="@+id/txtvw2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:background="#AA000000"
android:padding="10dp"
android:text="21/Aug/2017"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</FrameLayout>
ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<ListView
android:id="@+id/userlist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
public class MainActivity extends AppCompatActivity {
GridView gridView;
static final String[] numbers = new String[] {
"1", "2", "3", "4", "5",
"6", "7", "8", "9", "10",};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView.setAdapter(adapter);
}
}
WebView <?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Welcome to Tutlane"
android:textColor="#86AD33"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:textAllCaps="true" />
<TextView
android:id="@+id/textView3"
public android:layout_width="wrap_content"
class MainActivity extends AppCompatActivity { android:layout_height="wrap_content"
@Override android:text="Welcome to Tutlane"
protected void onCreate(Bundle android:textStyle="bold"
savedInstanceState) { android:textColor="#fff"
super.onCreate(savedInstanceState); android:background="#7F3AB5"
setContentView(R.layout.activity_main); android:layout_marginBottom="15dp"/>
TextView tv = <TextView
(TextView)findViewById(R.id.textView2); android:id="@+id/textView4"
tv.setText("Welcome to Tutlane"); android:layout_width="wrap_content"
} android:layout_height="wrap_content"
} android:autoLink="email|web"
android:text="For more details visit http://tutlane.com and send mail to [email protected]" />
</LinearLayout>
EditText <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<EditText
android:id="@+id/txtPhone"
android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:paddingLeft="40dp" android:ems="10"
android:orientation="vertical" android:id="@+id/linearlayout" > android:hint="Phone Number"
<EditText android:inputType="phone"
android:id="@+id/txtName" android:textColorHint="#FE8DAB"/>
android:layout_width="wrap_content"
</LinearLayout>
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="Name"
android:inputType="text"
android:selectAllOnFocus="true" />
<EditText
android:id="@+id/txtPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password 0 to 9"
android:inputType="numberPassword" />
<EditText
android:id="@+id/txtEmai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:ems="10"
android:hint="Date"
android:inputType="date" />
AutoCompleteTextView
<?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" >
<AutoCompleteTextView
android:id="@+id/autoComplete_Country"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ProgressBar <ProgressBar
android:id="@+id/pBar" android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizonta style="?android:attr/progressBarStyleHorizontal"
l" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:indeterminate="true"/>
android:max="100"
android:progress="50" />
Spinner (Dropdown List)
<Spinner android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
String[] users = { "Suresh Dasari", "Trishika Dasari", "Rohini Alavala", "Praveen Kumar", "Madhav Sai" };
Spinner spin = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, users);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);
DatePicker
<DatePicker android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TimePicker android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar android:id="@+id/seekBar1"
android:layout_width=" wrap_content"
android:layout_height="wrap_content"
android:max="100"
android:indeterminate="false"
android:progress="50" />
RatingBar Value
• Context Menu is like a floating menu and that appears when the user
performs a long press or click on an element and it is useful to
implement actions that affect the selected content or context frame.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btnShow);
registerForContextMenu(btn);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Upload");
menu.add(0, v.getId(), 0, "Search");
}
Popup Menu
• We have two ways to define the Toast duration, either in LENGTH_SHORT or LENGTH_LONG to
display the toast notification for a short or longer period of time.