samedi 16 mars 2013
mercredi 23 janvier 2013
Android lessons for beginners (startandroid.ru)
Recently I have started translating lessons on startandroid.ru into English. It's a famous web site for those who want to start programming Android and have no priror experience with it. The lessons are really awesome and now English speaking audience will be able to read them. Each lesson describes one subject or feature so you can read only the lesson you need.
Join the community on Google+ to follow the news.
I've started learning Android on this web-site and now I would like to pay back the author and make some translations. The following lessons are already published:
You can find the complete list of lessons published here.
Join the community on Google+ to follow the news.
I've started learning Android on this web-site and now I would like to pay back the author and make some translations. The following lessons are already published:
- Lesson 1. Introduction
- Lesson 2. Installing and configuring development environment Eclipse and SDK Tools
- Lesson 3. Creating AVD. The first application. Android project structure
- Lesson 4. Layout elements and their properties
- Lesson 5. Layout-file for Activity. XML Representation. Changing screen orientation.
- Lesson 6. Layout types. Key differences and properties.
- Lesson 7. Layout parameters for View-elements.
- Lesson 8. Working with layout elements from code.
- Lesson 9. Event listeners with Button example
- Lesson 10. Optimizing event listeners implementation
- Lesson 11. res/values folder. Using application resources
- Lesson 12. Logs and Toast messages.
- Lesson 13. Creating a simple menu
- Lesson 14. Menu, groups, order. MenuInflater and xml-menu
- Lesson 15. Context menu
- Lesson 16. Creating layout programmatically. LayoutParams
- Lesson 17. Creating View-components in a running application
- Lesson 18. Changing layoutParams in a running application
- Lesson 19. Creating a simple calculator
- Lesson 20. Animation
- Lesson 21. Creating and starting an Activity
- Lesson 22. Intent, Intent Filter, Context - theory
- Lesson 23. Activity Lifecycle. Activity states
- Lesson 24. Activity Lifecycle, example about changing states with two Activities
- Lesson 25. Task. What is it and how it is formed.
- Lesson 26. Intent Filter - practice
- Lesson 27. Reading action from Intent
- Lesson 28. Extras - passing data using Intent
- Lesson 29. Invoking Activity and getting a result. startActivityForResult method.
You can find the complete list of lessons published here.
vendredi 18 janvier 2013
How to enable USB debugging on Android 4.2
To enable USB Debugging and make Developer options appear in Settings menu, just go to Settings ->About phone/tablet and tap Build number 7 times.
That's it! Now you have Developer options available in your device Settings.
That's it! Now you have Developer options available in your device Settings.
samedi 12 janvier 2013
Eclipse doesn't open Android XML Library files.
Recently I ran into an issue that Eclipse didn't want to open XML files that are located in android.jar/res.layout directory. The reason is that android uses Android Common XML Editor. The only thing to do to open binary xml files is to change default XML Editor to Android Binary XML Editor. To do so go to Window -> Preferences -> General -> Editors -> File Associations and change default XML Editor to Android Binary XML Editor.
![]() |
| Could not open the editor. |
![]() |
| Problem Solution |
dimanche 30 décembre 2012
Intellij Idea key scheme for Eclipse
Most of the time I use Intellij Idea Community Edition for Android development, but recently I had to switch to Eclipse for some reasons. It could be really painful because I really got used to Intellij Idea default key scheme (pdf). But, thankfully, there is simple and elegant solution for this by Santhosh Kumar.
Just download the jar file and place it into plugins folder in your eclipse folder. Than restart eclipse. In eclipse go to Window -> Preferences (Windows) or ADT -> Preferences (Mac) than to General -> Keys and in the "Scheme" droplist choose "Intellij Idea" and press apply. That's it! Now you have the default Intellij Idea Keymap in your Eclipse workspace.
Links:
Just download the jar file and place it into plugins folder in your eclipse folder. Than restart eclipse. In eclipse go to Window -> Preferences (Windows) or ADT -> Preferences (Mac) than to General -> Keys and in the "Scheme" droplist choose "Intellij Idea" and press apply. That's it! Now you have the default Intellij Idea Keymap in your Eclipse workspace.
Links:
Developing Notepad Application - Part 1 (Creating SQLite database)
Application minimum required SDK version - 14 (ICS)
Contents
- Intro
- Creating SQLite database
- Accessing SQLite database from ADB shell.
- Links
1. Intro
In this tutorial we will develop a simple notepad Application for Android that allows you to create, read and edit text notes. To understand this tutorial you've got to have basic knowledge about relational databases and SQL.The app and the idea are not mine, almost all of the code is taken from Android developers web site. The tutorial there is just awesome but there are some topics that I would like to highlight in more detail. There are also some things that are deprecated and I will replace them with new implementations and point out where it happens.
We will start from a scratch and add some functionality step by step. Before reading this tutorial I highly recommend to look through the original article from Android web site.
In this tutorial we are going to create a very simple database that will serve us as data storage for our app.
At the end of this tutorial you will know how to:
- create a simple SQLite database in Android.
- get access to SQLite database via the command prompt (terminal) and execute some simple queries.
2. Short facts about SQLite database
First, some facts about SQLite in general:
- SQLite is free and open-source embedded SQL database engine.
- SQLite reads and writes directly to ordinary disk files.
- A complete SQL database is contained in a single disk file.
- The database file format is cross-platform.
- It supports standard relational database features including syntax, transactions and prepared statements. Transactions are ACID.
Database Structure
Our database will contain only one table and the following columns to persist notes:
- title -- text (note title)
- body -- text (note body text)
- _id -- integer, autoincrement (the id of the note)
Creating the Database
Let's create a new project with default settings which contains a single activity called NotesListActivity. Now in the same package with our activity create a class called NotesDbAdapter which will contain a subclass of SQLiteOpenHelper as a subclass which is a class to manage database creation and it's versioning. NotesDbAdapter will grant us an access to our database by defining CRUD methods in it. So let's create the wrapper class for our database and define all the constants we need and static inner class to get a database connection.
NotesDbAdapter is a wrapper class that holds the instance of SQLiteOpenHelper and provides API for database access (We will define basic CRUD operations in the next tutorials).
Now we need to create an instance of our NotesDbAdapter in our activity an call open() method to create the database. That's all the code we need to have a working database.
Emulator/device instances store SQLite3 databases in the folder:
To issue SQL queries to your database enter a remote shell on the emulator instance and enter sqlite3 command following with the full path to our database. Here is an example:
NotesDbAdapter.java
package com.blogspot.android_by_example.notepad;The code looks quite big but actually there is nothing special here. We create static subclass of SQLiteOpenHelper and implement abstract methods onCreate() and onUpgrade(). The onCreate() method is invoked when there is no database available for our app, so in this method we have to execute all the statements to create our database. onUpgrade() is invoked when the version of the current database version is smaller than new version (database version is provided when SQLiteOpenHelper subclass instance is created and this version is passed as a parameter to a superclass constructor).
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class NotesDbAdapter {
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_BODY = "body";
public static final String COLUMN_ID = "_id";
public static final String TAG = NotesDbAdapter.class.getSimpleName();
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "notes.db";
private static final String TABLE_NAME = "notes";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null);";
private final Context mContext;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
Log.d(TAG, "onCreate() database");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
Log.d(TAG, "onUpdate() database");
}
}
public NotesDbAdapter(Context context) {
this.mContext = context;
}
public NotesDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mContext);
mDb = mDbHelper.getWritableDatabase();
return this;
}
}
NotesDbAdapter is a wrapper class that holds the instance of SQLiteOpenHelper and provides API for database access (We will define basic CRUD operations in the next tutorials).
Now we need to create an instance of our NotesDbAdapter in our activity an call open() method to create the database. That's all the code we need to have a working database.
NotesListActivity.java
package com.blogspot.android_by_example.notepad;
import android.os.Bundle;
import android.app.Activity;
public class NotesListActivity extends Activity {
private NotesDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notes_list);
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
}
}
3. Accessing SQLite Database from ADB Shell
Now we need to make sure that our database is created and in a working state. Let's open DDMS perspective in Eclipse (or launch monitor from tools folder in your SDK location) and locate the database file on the device file system.Emulator/device instances store SQLite3 databases in the folder:
/data/data/<package_name>/databases/<database_name>So in our case the database will be located at:
/data/data/com.blogspot.android_by_example.notepad/databases/notes.db
| Database file location |
C:\Users\Taras Osiris>adb shell
root@android:/ # sqlite3 /data/data/com.blogspot.android_by_example.notepad/databases/notes.db
.android_by_example.notepad/databases/notes.db <
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>On Windows to acces ADB directly from command prompt add path of your SDK to your environment variables. On Mac OS follow this instructions.
If you have only one USB device or emulator connected you don't need to specify what device is the target for your commands. Otherwise you need to direct your commands to the targeted device by specifying arguments after adb command. For more details read about issuing ADB commands.
adb [-d|-e|-s <serialNumber>] shell
Now you can execute your SQL queries directly to your database. Let's insert a few rows into our database and than query all of them.
sqlite> INSERT INTO notes (title, body) VALUES ("Title1", "Body1");
INSERT INTO notes (title, body) VALUES ("Title1", "Body1");
sqlite> INSERT INTO notes (title, body) VALUES ("Title2", "Body2");
INSERT INTO notes (title, body) VALUES ("Title2", "Body2");
sqlite> INSERT INTO notes (title, body) VALUES ("Title3", "Body3");
INSERT INTO notes (title, body) VALUES ("Title3", "Body3");
sqlite> SELECT * FROM notes;
SELECT * FROM notes;
1|Title1|Body1
2|Title2|Body2
3|Title3|Body3As you can see, everything is great, our database is fine and ready to use. In the next part of tutorial we are going to work on ListActivity that will display a list of our notes from database.
Links and Materials Used
samedi 22 décembre 2012
Using ArrayAdapter (Creating list or spinner with a set of predefined data)
Application minimum required SDK version - 8
Contents
- Preface
- When should ArrayAdapter<T> be used?
- Using ArrayApater<T> default implementation in practice
- Conclusion
- Links
1. Preface
For this article to be useful, basic knowledge about Adapter objects in Android is required.For those readers who know Russian, have a look at this article - Android Adapters Overview (rus).
Unfortunately, I haven't found an article of the same type in English, that's why I am going to write an overview of Android adapters soon.
In this post I will make a brief overview of the simplest default Android Adapter implementation - ArrayAdapter<T>.
android.widget.ArrayAdapter<T> is a concrete class that can be instantiated with the help of one of the six constructors or using static factory method (Effective Java. Item 1)
2. When should ArrayAdapter<T> be used?
A parameterized class ArrayAdapter<T> is a very simple adapter implementation.
The default implementation is used when you have an array or List of objects of any type and you need to show user their string representation in the form of ListView, Spinner, GridView or any other subclass of AdapterView. For string representation of an object, this adapter will use toString() method.
By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource. (Documentation)
By default this class expects that the provided resource id references a single TextView. If you want to use a more complex layout, use the constructors that also takes a field id. That field id should reference a TextView in the larger layout resource. (Documentation)
3. Using ArrayAdapter<T> in practice
![]() ![]() |
| ListView and Spinner of planets |
Suppose we need to display a list of planets in our application (As a basis I took the API guide on developer.android.com about Spinner). The simplest case is when the set of data we need to display is predefined (we exactly know how many items we want to show and have their string representation). In this situation our set of data should be defined in the resource file and static factory method createFromResource() should be used.
Create a new project with a blank activity.
Create a new project with a blank activity.
First, let's define our data set in res/values/strings.xml resource file:
<?xml version="1.0" encoding="utf-8"?>Now let's create a layout file (res/layout/activity_planets.xml) which contains a single ListView:
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"And finally our PlanetsActivity.java :
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listViewPlanets"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
package com.blogspot.android_by_example;And don't forget to include the activity into your AndroidManifest.xml file.
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class PlanetsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_planets);
ListView listViewContacts = (ListView) findViewById(R.id.listViewPlanets);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, // context
R.array.planets_array, // source of data
android.R.layout.simple_list_item_1); // layout file to make views
listViewContacts.setAdapter(adapter);
}
}
That's the whole code for creating a simple list of items. In this case the created adapter takes the array of data declared in the resource file and builds each item in the list using the default layout for the Android list item.
The same result can be achieved by using constructor. It accepts array of objects (T[]) as in the code above or it can receive a List<T> of objects. In our case the code gets a little bit more verbose because of fetching the array from application resources. Note that in the code below the constructor receives a List of Strings.
package com.blogspot.android_by_example;Note that in this example ListView can be substituted by any AdapterView (Spinner, GridView)
import java.util.Arrays;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class PlanetsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_planets);
ListView listViewContacts = (ListView) findViewById(R.id.listViewPlanets);
String[] planets = getResources().getStringArray(R.array.planets_array);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, Arrays.asList(planets));
listViewContacts.setAdapter(adapter);
}
}
4. Conclusion
- ArrayAdapter<T> is a concrete class. It should be used when we need a string representation of data in array or List is required. You can subclass it if the default implementation lacks functionality.
- To instantiate an ArrayAdapter<T> object one of the constructors can be used. Or you can use static factory method createFromResource(), when data set is in the application resource file. In each case you have to provide ArrayAdapter<T> an object with the data set for string representation and a layout file for making a View
- We've developed the most simple application that shows a list of planets using ArrayAdapter<T>.
Inscription à :
Articles (Atom)
.jpg)

.png)




