Intent onClick Animation
Today: anonymous inner classes, starting intents, animation, toast notifications. Today’s example code is all in an activity called Example1Activity — the key bits of code are pasted in below.
The R.xxx space – “res”
- @+id/button1 (in XML)
- R.id.button1 (in Java) (CT checking, autocomplete — neat)
- res/drawable/abbysmile.jpg (in file system)
- R.drawable.abbysmile (in Java) — uses filename (like our main.xml layout)
Add Image Resource and ImageView
- Added a abbysmile.jpg in the location res/drawable/abbysmile.jpg
- Drag out an ImageView into the vertical layout, set it to use your image. Maybe adjust its max size.
- Set “src” property, knows about existing drawables
- Set “center horizontal” in Graphical Layout editor to center it
- It works in the preview .. you don’t really have to run to see it.
Alternate Resources
The “drawable-hdpi” etc. folders provide alternate resources depending on runtime details — screen size, locale, etc. For example, the selector “-hdpi” is for high pixel devices, “-es” for Spanish. Plain “drawable” folder is the base default. Your code can just refer to R.drawable.abbysmile or whatever, and the system does the logic to select the right one automatically. More on this later.
Member Vars, Anonymous Inner Classes
// This class reviews member variables vs. local variables, and shows
// Anonymous Inner Classes which may be new material for some.
class Account {
private int mBalance;
// 1. Classic member variable (also known an "instance variable")
// Can use "m" at name start
// Exists in the Account object -- outlives any one method call.
public Account() {
mBalance = 0;
}
public void deposit(int amt) {
String logMsg = "deposit:" + amt;
// 2. logMsg is a local (stack) variable --
// it exists as temporary storage just while deposit() is running.
// Contrast to the mBalance variable.
mBalance = mBalance + amt;
}
/*
Variable rules:
-Use a member variable if you want storage in the object that outlives
any one method. Often set up in onCreate(), modified by methods over time
(mBalance is a good example of this pattern).
-Use a local variable to hold any temporary result or pointer needed *during*
a method run, but not needed after it exits. Local variables are simpler and
have better performance, so they are a good default strategy.
*/
// A basic example of an Anonymous Inner Class.
public void innerDemo() {
final int temp = mBalance / 10;
Runnable runnable =
// This is an "anonymous inner class" -- 3 parts
new Runnable() { // 1. The superclass
public void run() { // 2. method(s) inside the AIC
// 3. Code inside the method -- typically this is the part you care
// about. See notes below
System.out.println("run() is running");
deposit(temp); // Can call methods on the outer object
}
};
/*
Code inside an AIC method:
1. The AIC creates a separate object with its own methods.
However, the AIC keeps a pointer to its owning, outer object to refer
to its variables and call its methods too (e.g. deposit()).
2. "this" here refers to the AIC itself. Use the syntax "Outer.this" to get
a pointer to the outer object.
3. Cannot refer to local variables where the AIC was created (e.g. temp)
unless they are declared "final".
Fun Fact: in Java 8, AIC's are being replaced with a much simpler "lambda"
syntax -- yay!
*/
}
}
Button OnClickListener
Last bit from previous lecture — OnClickListener is an AIC.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Store a pointer to editText1. Set up button1 to add a !.
editText1 = (EditText) findViewById(R.id.editText1);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener( // Create a OnClickListener AIC
new OnClickListener() {
public void onClick(View v) {
editText1.setText(editText1.getText() + "!");
}
}
);
...
Intents
- Intents — the mechanism by which various activities/components invoke each other. This is just a first, simple example.
- Intent has an action and data. Here we specify the “open url” intent .. which will launch the appropriate new activity for that. The “back button” from there comes back to us.
Intent Example — Open Url
// Button2: start web intent
editText2 = (EditText) findViewById(R.id.editText2);
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW); // most common
intent.setData(Uri.parse(editText2.getText().toString()));
startActivity(intent); // Note: this is a ExampleActivity method
/*
// Alternately, starting a dial intent.
Intent intent = new Intent(Intent.ACTION_DIAL); // vs. _CALL
intent.setData(Uri.parse("tel:" + editText2.getText().toString()));
// uri form should be "tel:555-555-5555"
*/
}
}
);
Complaint Button
- Write as a separate doComplain() method, call it from a one line AIC.
- Write the animation in a separate res/anim/swirl.xml file
public void doComplain() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.swirl);
// This whole middle piece is optional
animation.setAnimationListener(
new AnimationListener() {
public void onAnimationStart(Animation anim)
{
};
public void onAnimationRepeat(Animation anim)
{
};
public void onAnimationEnd(Animation anim)
{
editText3.setText("");
Context context = getApplicationContext();
CharSequence text = "Your feedback has been filed appropriately!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
};
});
editText3.startAnimation(animation);
}
Animation XML
<
AnimationListener, Toast Alert
Previous code example: use AnimationListener AIC to listen for end of animation. At that moment, change text, put up standard “toast” alert.
Homework 2
Add to your homework1 app — you don’t need to turn this in, it’s just for practice.
- Start at least one intent based on a user action (web or otherwise)
- Add an animation to some widget on screen
- Use AnimationListener to do something on the start or end of the animation
Regards
MaximeTech | Best IT solutions Provider | Web Development |Mobile Applications
No comments:
Post a Comment