New blog

Android R us

Hi, I’ve got a new blog, that i will update at least once a week. There will be beginner guide, intermediate and pro, as i’m learning in the process. I’ll give tips, indispensable code and stuff like that.

You will be able to ask for question on Twitter : @Tsunaze and i’ll try to answer them if i can.

Thank you.

Android R us

Http get in Asynctask for android part 1

This code is for the people who want to get information off the internet, like a Webservice (Json, Xml and such).

Here’s my example for when i want to parse a json from a webservice.

First step is your Client :

An HttpClient is some kind of interface that is required for all internet activities. You can see much more details here.

It takes cookies, authentification and so on i count, like it does with every browser.

The default constructor i use is :

 HttpClient client = new DefaultHttpClient();

The second step is HttpGet :

HttpGet is the one object that will retrieve your data as an entity (not really comprehensible at this point).

You can your either the constructer :

HttpGet http_get = new HttpGet(YourStringUrl);

Or you can use the method :


HttpGet http_get = new HttpGet();

http_get.setURI(YourStringUrl);

The third step is the Response :

How do you want to handle the response you get from the httpGet . You can use the ResponseHandler or the BasicResponseHandler.

The difference between the two of them is that the ResponseHandler can return any type of variables, and the BasicResponseHandler can only return String. In most of the cases, you’ll use the BasicResponseHandler, it’s more common. Here’s two way to get your response :

ResponseHandler<String> responseHandler = new BasicResponseHandler();

Or :

BasicResponseHandler responseHandler = new BasicResponseHandler();

The fourth step is… To get it to work together, will see the asynctask later :

HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(URL);
BasicResponseHandler response = new BasicResponseHandler();
String result = client.execute(get, response);

When you type the last line that execute the client it will brought some try and catch, you should have something like this :

HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(URL);
BasicResponseHandler response = new BasicResponseHandler();

try {

String content = client.execute(httpGet, reponse);

} catch (ClientProtocolException e) {

e.printStackTrace();

content = null;

} catch (IOException e) {

e.printStackTrace();

content = null;

}

Check the part 2 for the Asynctask part.

Android tutorial 4 : basics

Now that you can link a widget to its xml part, you can make some changes, everything you do. As java is a POO, you can widget are no different from Classes, you can see variables, function (methods) and constructors.

As for the constructors, there are only 3 of them that you will see in each widget :

– with one parameter : Context.

– with two parameters : Context and AttributeSet.

– with three parameters : Context, AttributeSet and int.

They’re just the basic constructors (that you will need if you want to make your own widget) that will define your widget. The context is more like where is the widget (in your app), and the two others constructors are used to enable the display of your

And for the methods you need to know them, not by heart but you’ll use them. So in the next few weeks, i’ll give you an intensive training on all the widget that you will use a lot.

How to broadcast your own data with Android

What’s a broadcast first ? A broadcast is a “way” to intercept “voices”. For example your going to a room and ask if anybody is here. If someone respond, you do something.

There is 3 things to do to make it work :

– Declare your broadcastreceiver class :


public class BroadCastClick extends BroadcastReceiver{

@Override

public void onReceive(Context context, Intent intent) {

String id = intent.getStringExtra("id");

Toast.makeText(context, "Broadcast id = "+id, Toast.LENGTH_SHORT).show();

}

}

The second step is the AndroidManifestXML :


<receiver

android:name="com.example.broadcast.BroadCastClick"

android:exported="false" >

<intent-filter>

<action android:name="com.example.main.click" />

</intent-filter>

</receiver>

And the third step is in your own code, for example :


protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn_test1 = (Button) findViewById(R.id.btn_test1);

btn_test1.setOnClickListener(new OnClickListener() {

&nbsp;

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent = new Intent();

intent.putExtra("id", "ID1");

intent.setAction("com.example.main.click");

sendBroadcast(intent);

}

});

btn_test2 = (Button) findViewById(R.id.btn_test2);

btn_test2.setOnClickListener(new OnClickListener() {

&nbsp;

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

Intent intent = new Intent();

intent.putExtra("id", "ID2");

intent.setAction("com.example.main.click");

sendBroadcast(intent);

}

});

}

As you can see and understand (i hope), my app send a broadcast when i click on a button, to make a processing in the background, thanks to the broadcastreceiver (here it’s just a toast but you can see the point). But one more this, i set the same action to both intents, so you can, for example compare with extras you put in. For example you can do something like this :


public class BroadCastClick extends BroadcastReceiver{

@Override

public void onReceive(Context context, Intent intent) {

String id = intent.getStringExtra("id");

if(id.equals("ID1")){

Toast.makeText(context, "Broadcast id = ID1", Toast.LENGTH_SHORT).show();

}else{

Toast.makeText(context, "Broadcast id = ID2 or other", Toast.LENGTH_SHORT).show();

}

}

}

Feel free to comment if you don’t understand something, or you do want to know something else.

Android tutorial 3 : linking java and xml

First, what is a view ?

A view is somekind of a bloc or an object for the User Interface (UI), you can divide views into two categories : Layout (container) and Widget (content).

For the container part there is all sorts of layout, but i’m gonna enumerate what is, in my opinion, the best kind :

LinearLayout : it’s a basic layout which take everything you put inside like a list (vertical or horizontal), one after another.

Relative Layout : it’s a more complex layout, but you will use it a lot because you can relate one view to another, for example if you want to put a view below and to the right of something else you’ll be able to do it. But you will need identification.

ScrollViews : if you know that the layout you will use will be to big for the screen you use, you’ll need this, because thanks to this we can scroll to your extent of content.

And the widget are stuff that you can change just like that : text, images, buttons etc.

So let’s practice :

Let’s create an Activity (which is a class that extends from Activity)

public class Test extends Activity {

TextView tv_test;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.test);

tv_test = (TextView) findViewById(R.id.tv_test);

}

}

The oncreate method is the first step your activity call (and i have gotten used to only use this method even if it’s wrong) .

setContentView(ID), it’s the method to define which xml layout your activity will take. ID is the id of the layout.

TextView (like its name sounds like is a view, more precisely a widget) is used to display text on your app. You can change it, twist it, do whatever with it. To link it to the xml you will need to use the method “findViewById(ID)” and put your id inside of it.

Now it’s the xml part :

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >

<TextView
 android:id="@+id/tv_test"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 />

</LinearLayout>

So what does that mean ? Simply that there is a widget text inside a layout. You can either set some parameters inside the xml like android:text=”” or to put it in the java code with something like setText(Charsequence).

Feel free to comment to know more and more.

Android tutorial 2 : Java and xml

Today i’m gonna tell you how and android project is constructed.

When you click on the project, you will see some folders, let me explain :

– src : is where your java files are, if you already did some java project you know what a package is, or even what a java file is. Your main program are written in java code, Java is a OOP which means something like Object Oriented Programming, so you have parents and children classes, getters and setters etc.

– gen : don’t touch it, like never. Don’t even think about it. This is where ids are.

– Android x.x.x : don’t know, don’t really care, but it’s important because this is where all the android library is.

– Android depedencies : this is where your jar will be imported.

– bin : don’t really know.

– libs : it’s your own folders to put your jar in and import them later.

– res : it’s quite important, because it’s the graphical part of your application, you will see sub-folder like :

>drawable : like its name suggest is where you draw background or animation or stuff like that. OR you can upload your images but in png only.

>layout : it’s where you draw the part that you can see in your app, literally, this is where you construct the way your classes (view) will looks like.

>values : where you put your strings, colors, dimensions or style/theme.

And another one is a file : the AndroidManifest, where you declare what you will need or use in your app (classes, permission…)

Okay i’m out.

PS : feel free to comment if you need more information.

Android tutorial 1 : Hello World

This is your first android tutorial, i’m just going ahead and say : sometimes you will not see picture to see with the code because i’m not a graphist, and you’ll see some erros of grammar because english is not my native langage. But i will teach you all i know about Android.

I’m sure that if you wanted to learn, you already installed Eclipse or the Android SDK.

Let’s begin :

Click on File, New, Android Application Project.

Put a name for your application, a package name, click next do your configuration etc.

And done, now you just need to launch your emulator… Oh wait you don’t know how to do it.

Click on your AVD Manager, click new and create your own emulator with your specs. Don’t be a noob, you can manage.

Continue reading →

Swipe with Android / Android tutorial

You need first to create a class extended from gesturedetector.simpleongesturelistener . And inside create three variables .

private static final int SWIPE_MIN_DISTANCE = 150;

private static final int SWIPE_MAX_OFF_PATH = 100;

private static final int SWIPE_THRESHOLD_VELOCITY = 100;

SWIPE_MIN_DISTANCE is the minimal distance you need to make that will be receive by the Android device.

SWIPE_THRESHOLD_VELOCITY is the speed you need to do.

SWIPE_MAX_OFF_PATH is the error limit you can do . For example you swipe diagonally, the Android device will know if you’re trying to swipe up or right .

Continue reading →