Wednesday, 29 May 2013
choose image from gallery android
choose image from gallery android in my app, the user need to upload their profile from gallery pop up. I write simple project to demonstrate how to code. ^^
clear notification android onclick
clear notification android onclick
When you create notification , you need to set flag property:
Notification noti = mBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
When you create notification , you need to set flag property:
Notification noti = mBuilder.build();
noti.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
Tuesday, 28 May 2013
multi language android
multi language android is easy to config. Once you’ve decided on the languages you will support, create the resource subdirectories and
string resource files.
Create additional
For example:
English (default locale),
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources> Spanish,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">Hola Mundo!</string>
</resources>
Create additional
values
directories inside
res/
that include a hyphen and the ISO country code at the end of the
directory name. See ISO country codeFor example:
MyProject/ res/ values/ strings.xml values-es/ strings.xml values-fr/ strings.xml
For example, the following are some different string resource files for different languages.
English (default locale),
/values/strings.xml
:<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Hello World!</string>
</resources> Spanish,
/values-es/strings.xml
:<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">Mi Aplicación</string>
<string name="hello_world">Hola Mundo!</string>
</resources>
ISO country code
Monday, 27 May 2013
mvc in android example
mvc in android example : I like very much from Josh 's example. So, I summary in this blog to easier to understand.
1) MVC General Concepts:
To quickly review, MVC is a compound pattern meaning several patterns comprise this architectural pattern.
The Model represents our application’s state. Remember, objects have state (properties) and behaviors (methods). And so applications have state (the model). In essence, models are just big name-value objects that dispatch events when its state changes.
Views are the things you see and interact with. They bind to the model by registering themselves as observers.When the model changes, the view is notified and it updates itself.
When a user interacts with the view, it sends events to a Controller. The controller is responsible for handling the input logic. It interprets the user gestures, updates the model, and may send messages back to the view. Below is a typical MVC diagram showing this relationship.
2) Organize packages:
activities – Should be should be obvious, we’ll put all our activities here.
controllers – Controllers that are the brain for our Views here.
daos – We’ll put our persistence logic here.
lists – This one I’m not incredibly found of, but I put all my custom list adapters here. I’m still experimenting.
models – These are the models are Views will bind to.
utils – All of my static classes and utilities go in here.
vos – I put all of my Value Objects also know as POJOs or Data Transfer Objects here.
widgets – This is borrowed from the Android convention. I put custom widgets controls in here.
3) Explain code
MODEL:
CounterVo.java
SimpleObservable.java
So notice that our model really only does two things:
VIEW: is activity package:
1) binding to the model.
2) sending messages to the controller.
3) handling messages from the controller.
Role 1, Data Binding:
Let’s look at TapActivity and let’s bind some data.
@Override
public void onCreate(Bundle savedInstanceState) {
// MODEL (Vos package)
counter = new CounterVo();
counter.addListener(this);
// CONTROLLER
controller = new TapController(counter);
/*
* when MODEL change onChange will be called.
* */
@Override
public void onChange(CounterVo counter) {
runOnUiThread(new Runnable() {
@Override
public void run() {
updateView();
}
});
}
Our Activity is registered as an observer and whenever our model is updated we know our UI is going to stay right in-sync.
Role 2, Sending Messages:
TapActivity
when PlusBtn is clicked.
then MODEL CounterVo change ===> onChange will be called in TapActivity.
to update UI.
Role 3: Handling Messages
check out TapListActivity
http://www.therealjoshua.com/2011/11/android-architecture-part-6-putting-it-together-2/
1) MVC General Concepts:
To quickly review, MVC is a compound pattern meaning several patterns comprise this architectural pattern.
The Model represents our application’s state. Remember, objects have state (properties) and behaviors (methods). And so applications have state (the model). In essence, models are just big name-value objects that dispatch events when its state changes.
Views are the things you see and interact with. They bind to the model by registering themselves as observers.When the model changes, the view is notified and it updates itself.
When a user interacts with the view, it sends events to a Controller. The controller is responsible for handling the input logic. It interprets the user gestures, updates the model, and may send messages back to the view. Below is a typical MVC diagram showing this relationship.
2) Organize packages:
activities – Should be should be obvious, we’ll put all our activities here.
controllers – Controllers that are the brain for our Views here.
daos – We’ll put our persistence logic here.
lists – This one I’m not incredibly found of, but I put all my custom list adapters here. I’m still experimenting.
models – These are the models are Views will bind to.
utils – All of my static classes and utilities go in here.
vos – I put all of my Value Objects also know as POJOs or Data Transfer Objects here.
widgets – This is borrowed from the Android convention. I put custom widgets controls in here.
3) Explain code
CODE: http://www.therealjoshua.com/wp-content/uploads/2011/11/TapCounter.zip
MODEL:
CounterVo.java
SimpleObservable.java
So notice that our model really only does two things:
- It holds state. The properties id, label, count, and locked represent the object. It’s just a big name-value object: a Plain Old Java Object (POJO).
- It notifies its observers when properties have changed. The workhorse of this done in the subclass SimpleObservable. (Side note: Instead of using Java’s Observable, class which doesn’t have an interface and forces you to subclasses, I made my own Observable, which has an interface. This creates the option to subclass instead of being forced. Favor composition over inheritance. Bad Java, bad.)
VIEW: is activity package:
1) binding to the model.
2) sending messages to the controller.
3) handling messages from the controller.
Role 1, Data Binding:
Let’s look at TapActivity and let’s bind some data.
@Override
public void onCreate(Bundle savedInstanceState) {
// MODEL (Vos package)
counter = new CounterVo();
counter.addListener(this);
// CONTROLLER
controller = new TapController(counter);
/*
* when MODEL change onChange will be called.
* */
@Override
public void onChange(CounterVo counter) {
runOnUiThread(new Runnable() {
@Override
public void run() {
updateView();
}
});
}
Our Activity is registered as an observer and whenever our model is updated we know our UI is going to stay right in-sync.
Role 2, Sending Messages:
TapActivity
plusBtn.setOnClickListener(new View.OnClickListener() {
minusBtn.setOnClickListener(new View.OnClickListener() {
when PlusBtn is clicked.
controller.handleMessage(TapController.MESSAGE_INCREMENT_COUNT);
then MODEL CounterVo change ===> onChange will be called in TapActivity.
to update UI.
Role 3: Handling Messages
check out TapListActivity
http://www.therealjoshua.com/2011/11/android-architecture-part-6-putting-it-together-2/
observer pattern
observer pattern summary from wikipedia
public class EventSource extends Observable implements Runnable {
}
public class ResponseHandler implements Observer {
}
main()
final EventSource eventSource = new EventSource();
// create an observer
final ResponseHandler responseHandler = new ResponseHandler();
// subscribe the observer to the event source
eventSource.addObserver(responseHandler);
// starts the event thread
Thread thread = new Thread(eventSource);
thread.start();
full link: http://en.wikipedia.org/wiki/Observer_pattern
public class EventSource extends Observable implements Runnable {
}
public class ResponseHandler implements Observer {
}
main()
final EventSource eventSource = new EventSource();
// create an observer
final ResponseHandler responseHandler = new ResponseHandler();
// subscribe the observer to the event source
eventSource.addObserver(responseHandler);
// starts the event thread
Thread thread = new Thread(eventSource);
thread.start();
full link: http://en.wikipedia.org/wiki/Observer_pattern
Subscribe to:
Posts (Atom)