This is one of the most common actions you will be doing if you develop something for Android or some other mobile platform.
Buttons are the usual way of interaction between the user and Android. As such they require proper visibility and functionality which will be implemented.
After you have instantiate your button as a variable in MainActivity:
Button buttonOsvjezi;
you need to assign a view to the button so that Android can know which button is being reference:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
buttonOsvjezi = (Button) findViewById(R.id.button_osvjezi);
...
Once your button has been referenced, you can assign some text over it or you can define its behavior. If you want to define 'what does this button do' when you press it, you do it by setting an OnClickListener like this:
buttonOsvjezi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Do something...
}
});
As you can see, after setting OnClickListener, that very listener has a built in method onClick which can be overridden. Using onClick method you can set what will your button do. It can, write a text, start a new activity, display an image, or any other function you like.
For example, this snippet will write a text in a textView which is somewhere on the screen:
TextView textview1;
buttonOsvjezi.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
textView1 = (TextView) findViewById(R.id.textView1);
textView1("Hi, this text is displayed after clicking the button.");
}
});
Android assistent
nedjelja, 31. kolovoza 2014.
petak, 15. kolovoza 2014.
How to start new Activity using Intent - android
When programming for Android it is very simple to create and switch to another "screen". It is as intuitive as it can be. As you can think yourself, you need to implement new UI (xml file), new Activity (which will 'do the job') and start that Activity from the existing one.
There are, however, things that you need to care about, like: what if you have data which you want to transfer between Activities, or how to handle those data when receiving them, or how to 'send' the data to another Activity.
Data management between Activities will be covered in another post. For now, let's just show how easy is to start new Activity from the existing one.
First of all, you need to create a new Intent. It is something like token which is signalizing the creation of a new Activity. It can even carry your data to new Activity. New Intent is created like this:
Intent intent = new Intent(this, NovoUlaganjeActivity.class);
As you can see, you need to specify the context and new Activity which is going to be created and started. In order to execute the creation and starting of new Activity, just add this line to your code:
startActivity(intent);
And that is it.
If you need to close the newly created Activity and return to the one which started it, just call
finish();
in your new Activity.
There are, however, things that you need to care about, like: what if you have data which you want to transfer between Activities, or how to handle those data when receiving them, or how to 'send' the data to another Activity.
Data management between Activities will be covered in another post. For now, let's just show how easy is to start new Activity from the existing one.
First of all, you need to create a new Intent. It is something like token which is signalizing the creation of a new Activity. It can even carry your data to new Activity. New Intent is created like this:
Intent intent = new Intent(this, NovoUlaganjeActivity.class);
As you can see, you need to specify the context and new Activity which is going to be created and started. In order to execute the creation and starting of new Activity, just add this line to your code:
startActivity(intent);
And that is it.
If you need to close the newly created Activity and return to the one which started it, just call
finish();
in your new Activity.
Oznake:
activity,
android,
closing activity,
context,
creating,
finish,
Intent,
new Intent,
parameter,
startActivity,
starting,
starting activity,
transfering data
četvrtak, 14. kolovoza 2014.
How to set Date picker within dialog - android
Instead of having one big date picker across the screen which can hold the big part of the screen (that is the reason to avoid it, in my opinion), you can choose to have Date picker launched within dialog box which can make things more elegant in my opinion.
So, before editing code, lets prepare GUI for this. In UI xml file, make sure to have a trigger for event of entering the date. In this example we will insert a button which will normally show actual date, but the user can change the date any time he/she wants.
Most of coding goes to activity file. First of all, we have to instance new button, and place an onClickListener on it.
So, as you can see, while implementing onClickListener we have called showDialog method, which is deprecated in this version of Android (version 19), but is still capable of doing the trick.
Afterwards make sure you have instantiated private attributes and set them on right values (which are actual date, at the beginning). You can do it along with all the others necessary private attributes:
Afterward, make sure you set attributes to right default values:
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
updateDisplay() is a custom method used to make a string out of numeric values of year, month and date. And along the way, it corrects the months because they start with zero.
private void updateDisplay() {
//add +1 to month, because months start with zero
date = pad(mDay)+"."+pad(mMonth+1)+"."+pad(mYear);
button_odaberiDatum.setText(date);
}
pad(int c) is a custom method used to return string values instead of integers.
private String pad(int c) {
if (c>=10)
return String.valueOf(c);
else
return ("0"+String.valueOf(c));
}
Afterwards, we need to instantiate DatePickerDialog within onCreateDialog method, with onDateSetListener implementing onDateSet method, which is done as follows:
@Override
protected Dialog onCreateDialog(int id) {
switch (id){
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
And that's it.
So, before editing code, lets prepare GUI for this. In UI xml file, make sure to have a trigger for event of entering the date. In this example we will insert a button which will normally show actual date, but the user can change the date any time he/she wants.
Most of coding goes to activity file. First of all, we have to instance new button, and place an onClickListener on it.
Afterwards make sure you have instantiated private attributes and set them on right values (which are actual date, at the beginning). You can do it along with all the others necessary private attributes:
Afterward, make sure you set attributes to right default values:
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
updateDisplay() is a custom method used to make a string out of numeric values of year, month and date. And along the way, it corrects the months because they start with zero.
private void updateDisplay() {
//add +1 to month, because months start with zero
date = pad(mDay)+"."+pad(mMonth+1)+"."+pad(mYear);
button_odaberiDatum.setText(date);
}
pad(int c) is a custom method used to return string values instead of integers.
private String pad(int c) {
if (c>=10)
return String.valueOf(c);
else
return ("0"+String.valueOf(c));
}
Afterwards, we need to instantiate DatePickerDialog within onCreateDialog method, with onDateSetListener implementing onDateSet method, which is done as follows:
@Override
protected Dialog onCreateDialog(int id) {
switch (id){
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
And that's it.
How to display toast message notification - android
Simply, add method like show below showToast:
Make sure to enter last line toast.show() in order to diplay your message in notification window.
Make sure to enter last line toast.show() in order to diplay your message in notification window.
How to create and add menu item - android
First go res --> menu --> and open xml file which contains menu. Usually, it is called menu.xml. As shown on picture:
Edit menu.xml file so it contains new menu entries. It can look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/menuAction_unesiNoviUlaganje"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_unesiNovoUlaganje"/>
</menu>
Afterwards, go to MainActivity.java and enter new method onCreateOptionsMenu which will create menu according to xml file menu.xml:
Again in MainActivity.java, enter new method onOptionsItemSelected which will handle menu calls:
And that's it. Bear in mind, that first case action_settings is deafault value which is present, and you need to add your own and process is it as you like. Here, in this example, by pressing new menu item, a new activity is being called.
Edit menu.xml file so it contains new menu entries. It can look like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/menuAction_unesiNoviUlaganje"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/menu_unesiNovoUlaganje"/>
</menu>
Afterwards, go to MainActivity.java and enter new method onCreateOptionsMenu which will create menu according to xml file menu.xml:
Again in MainActivity.java, enter new method onOptionsItemSelected which will handle menu calls:
And that's it. Bear in mind, that first case action_settings is deafault value which is present, and you need to add your own and process is it as you like. Here, in this example, by pressing new menu item, a new activity is being called.
Pretplati se na:
Postovi (Atom)