Android Development - Intermediate
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View).
drawable - contains images and UI elements that can we drawn onto the screen
layout - contains our main UI views
menu - contains all menu UI views
mipmap - contains our application launcher icons
values: dimens - lets us specify global spacial dimensions for application UI
values: strings - lets us specify global string reources that can be used all across our applicaiton
values: styles - lets us tweak the default android UI theme and styles for a hyper custom application UI
public void buttonClicked(View view) {
TextView output = (TextView) findViewById(R.id.myOutputTextview);
output.setText("You Pressed the button!");
}
When the "buttonClicked" method is called, we find the view object in our UI that has the id of "myOutputTextView" and set its "text" property equal to "You Pressed the button!"
public void buttonClicked(View view) {
TextView output = (TextView) findViewById(R.id.myOutputTextview);
output.setText("You Pressed the button!");
}
public void buttonClicked(View view) {
Toast t = Toast.makeText(this, "Notification Text Here", Toast.LENGTH_LONG);
t.show();
}
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(summary);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
public void buttonClicked(View view) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Some Title");
adb.setMessage("Main Message");
AlertDialog dlg = adb.create();
dlg.show();
}
public void buttonClicked(View view) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Some Title");
adb.setMessage("Main Message");
OnClickListener ocl = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TextView tv = (TextView) findViewById(R.id.textViewTop);
if (which == AlertDialog.BUTTON_POSITIVE)
tv.setText("Positive button clicked");
else
tv.setText("Clicked was the Negative button");
}
};
adb.setPositiveButton("Pos+", ocl);
adb.setNegativeButton("Neg-", ocl);
AlertDialog dlg = adb.create();
dlg.show();
}
public void buttonClicked(View view) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Choose a flower:");
OnClickListener ocl = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TextView tv = (TextView) findViewById(R.id.textViewTop);
tv.setText(String.format("which is %d", which));
}
};
String[] flowers = { "Dahlia", "Roses", "Tulips", "Orchid" };
adb.setSingleChoiceItems(flowers, -1, ocl);
adb.setPositiveButton("Cancel", ocl);
adb.setNegativeButton("OK", ocl);
AlertDialog dlg = adb.create();
dlg.show();
}
public void buttonClicked(View view) {
AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setTitle("Choose a flower:");
OnMultiChoiceClickListener omccl = new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
TextView tv = (TextView) findViewById(R.id.textViewTop);
tv.setText(String.format("which is %d and isChecked is %s", which, isChecked ? "True" : "False"));
}
};
OnClickListener ocl = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TextView tv = (TextView) findViewById(R.id.textViewTop);
if (which == AlertDialog.BUTTON_POSITIVE)
tv.setText("Positive button clicked");
else
tv.setText("Clicked was the Negative button");
}
};
String[] flowers = { "Dahlia", "Roses", "Tulips", "Orchid" };
adb.setMultiChoiceItems(flowers, null, omccl);
adb.setPositiveButton("Cancel", ocl);
adb.setNegativeButton("OK", ocl);
AlertDialog dlg = adb.create();
dlg.show();
}
public void buttonSaveClicked(View view) {
SharedPreferences sp = getPreferences(MODE_PRIVATE);
Editor ed = sp.edit();
ed.putBoolean("IsCustomApp", true);
ed.putString("SomeInfo", "Testing");
ed.apply();
}
public void buttonLoadClicked(View view) {
SharedPreferences sp = getPreferences(MODE_PRIVATE);
boolean customApp = sp.getBoolean("IsCustomApp", false);
String info = sp.getString("SomeInfo", "");
Log.i("Output", info);
}
public void buttonSaveClicked(View view) {
SharedPreferences sp = getSharedPreferences("AppWideSP", MODE_PRIVATE);
Editor ed = sp.edit();
ed.putBoolean("IsCustomApp", true);
ed.putString("SomeInfo", "Testing");
ed.apply();
}
public void buttonLoadClicked(View view) {
SharedPreferences sp = getSharedPreferences("AppWideSP", MODE_PRIVATE);
boolean customApp = sp.getBoolean("IsCustomApp", false);
String info = sp.getString("SomeInfo", "");
Log.i("Output", info);
}
import java.io.Serializable;
public class Employee implements Serializable {
private String _name;
private int _age;
private String _cityOfBirth;
public String getName() {
return _name;
}
public void setName(String name) {
this._name = name;
}
public int getAge() {
return _age;
}
public void setAge(int age) {
this._age = age;
}
public String getCityOfBirth() {
return _cityOfBirth;
}
public void setCityOfBirth(String cityOfBirth) {
this._cityOfBirth = cityOfBirth;
}
}
public void saveButtonClicked(View view) {
Employee emp = new Employee();
emp.setName("John Doe");
emp.setAge(27);
emp.setCityOfBirth("St. Louis");
try {
FileOutputStream fos = openFileOutput("Employee.bin", MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(emp);
oos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void loadButtonClicked(View view) {
Employee emp;
try {
FileInputStream fis = openFileInput("Employee.bin");
ObjectInputStream ois = new ObjectInputStream(fis);
emp = (Employee)ois.readObject();
ois.close();
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void goToNewActivity(View view) {
Intent intent = new Intent(this, SecondaryActivity.class);
startActivity(intent);
}
Illustration of how an implicit intent is delivered through the system to start another activity: [1] Activity A creates an Intent with an action description and passes it to startActivity(). [2] The Android System searches all apps for an intent filter that matches the intent. When a match is found, [3] the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.