Android Development: History, AVD, and UI Components
History of Mobile Devices
- 1973: First mobile phone call (Martin Cooper, Motorola).
- 1983: First commercial mobile phone – Motorola DynaTAC.
- 1990s: Feature phones with SMS and basic games (e.g., Snake on Nokia).
- 2000s: Introduction of smartphones (BlackBerry, Windows Mobile).
- 2007: Apple iPhone revolutionized touchscreen devices.
- 2010s: Android dominates the market; the app ecosystem grows.
- 2020s: Foldable phones, 5G devices, AI integration, and IoT connectivity.
Android Version History
- 2003 – Android Inc. Founded: By Andy Rubin & team; originally for cameras, later shifted to phones.
- 2005 – Google Acquisition: Google bought Android to create an open-source mobile OS.
- 2007 – Official Announcement: Android introduced with the Open Handset Alliance.
- 2008 – First Phone: HTC Dream (T-Mobile G1) launched with Android 1.0.
- 2009–2011 – Android 2.x: Eclair, FroYo, Gingerbread; improved UI, apps, gaming, and NFC.
- 2011 – Android 3.x Honeycomb: Optimized for tablets.
- 2011–2013 – Android 4.x: Ice Cream Sandwich, Jelly Bean, KitKat; better UI, performance, and Google Now.
- 2014–2015 – Android 5–6: Lollipop (Material Design) and Marshmallow (permissions, Doze mode).
- 2016–2017 – Android 7–8: Nougat (split-screen) and Oreo (Picture-in-Picture, Project Treble).
- 2018–2019 – Android 9–10: Pie (gesture navigation, AI) and Android 10 (privacy, dark mode).
Android Virtual Device (AVD)
An Android Virtual Device (AVD) is a software-based emulation of an Android device. It allows developers to test their apps on different virtual devices with various configurations, Android versions, and screen sizes. AVDs are helpful for app testing and development without the need for physical devices.
Procedure to Create an AVD
- Open Android Studio: Launch Android Studio on your computer.
- Access AVD Manager: Go to Tools > AVD Manager from the top menu.
- Create AVD: Click Create Virtual Device.
- Choose Hardware Profile: Select a device definition that matches the type of device you want to emulate (e.g., phone, tablet).
- Select System Image: Choose the Android version and API level you want to emulate. If not already downloaded, click Download to get the system image.
- Configure AVD: Give your AVD a name and adjust settings like RAM size and storage capacity as needed.
- Complete Configuration: Click Finish to create the AVD.
- Emulate AVD: Select the newly created AVD from the list and click the green Play button to start the emulator.
- Test Your App: The emulator will boot up with the chosen configuration. Test your Android app on the virtual device.
- Exit Emulator: When done, close the emulator by clicking the X in the emulator window or selecting Stop in the AVD Manager.
Implementing RecyclerView
Add Dependency (build.gradle)
implementation 'androidx.recyclerview:recyclerview:1.3.1'XML Layout
Adapter Class
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private String[] data;
public MyAdapter(String[] data) { this.data = data; }
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(android.R.id.text1);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.textView.setText(data[position]);
}
@Override
public int getItemCount() { return data.length; }
}Activity Code
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
String[] items = {"Item 1", "Item 2", "Item 3"};
MyAdapter adapter = new MyAdapter(items);
recyclerView.setAdapter(adapter);Dialogs in Android
Creating a DialogFragment
public class MyDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Alert")
.setMessage("This is a dialog")
.setPositiveButton("OK", (dialog, id) -> dialog.dismiss());
return builder.create();
}
}Displaying the Dialog
MyDialogFragment dialog = new MyDialogFragment();
dialog.show(getSupportFragmentManager(), "MyDialog");Menus in Android
Create menu XML in res/menu/menu_main.xml.
Inflate Menu in Activity/Fragment
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}Handle Menu Item Clicks
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}Student Details Application with Fragments
Develop an Android application to display student names in a ListView and show student details in a Fragment on item selection.
activity_main.xml
MainActivity.java
public class MainActivity extends AppCompatActivity {
String[] name = {"Ram", "Shyam", "Hari"};
String[] roll = {"1", "2", "3"};
String[] address = {"Kathmandu", "Pokhara", "Chitwan"};
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
ListView lv = findViewById(R.id.listView);
lv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, name));
lv.setOnItemClickListener((a, v, p, i) -> {
DetailFragment f = DetailFragment.newInstance(roll[p], name[p], address[p]);
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, f).commit();
});
}
}DetailFragment.java
public class DetailFragment extends Fragment {
static DetailFragment newInstance(String r, String n, String a) {
DetailFragment f = new DetailFragment();
Bundle b = new Bundle();
b.putString("r", r);
b.putString("n", n);
b.putString("a", a);
f.setArguments(b);
return f;
}
public View onCreateView(LayoutInflater i, ViewGroup c, Bundle b) {
TextView t = new TextView(getActivity());
Bundle x = getArguments();
t.setText("Roll: " + x.getString("r") + "\nName: " + x.getString("n") + "\nAddress: " + x.getString("a"));
return t;
}
}Retrieving Content from a Remote Server
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnLoad"
android:text="Load Data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txtData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"/>
</LinearLayout>MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView txt;
protected void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.txtData);
findViewById(R.id.btnLoad).setOnClickListener(v -> new FetchData().execute());
}
class FetchData extends AsyncTask<Void, Void, String> {
protected String doInBackground(Void... v) {
try {
URL url = new URL("https://jsonplaceholder.typicode.com/posts/1");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
return br.readLine();
} catch (Exception e) {
return "Error";
}
}
protected void onPostExecute(String s) {
txt.setText(s);
}
}
}Working with ListView
XML Layout
Activity Code (Java)
String[] items = {"Item 1", "Item 2", "Item 3"};
ListView listView = findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
listView.setOnItemClickListener((parent, view, position, id) -> {
Toast.makeText(this, "Clicked: " + items[position], Toast.LENGTH_SHORT).show();
});Working with GridView
XML Layout
Activity Code (Java)
Integer[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3};
GridView gridView = findViewById(R.id.gridView);
ArrayAdapter<Integer> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, images);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener((parent, view, position, id) -> {
Toast.makeText(this, "Clicked image: " + position, Toast.LENGTH_SHORT).show();
});