Sunday, February 15, 2015

hello world android

Dans cet exemple, on va montrer comment developper mon premier projet android.


voici le vidéo qui montre la création de premier projet:






Voici le code de notre activity MainActivity.java
package com.formation.helloworldproject;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btn;
EditText editext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
btn=(Button) findViewById(R.id.button2);
editext=(EditText) findViewById(R.id.editText1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String nom=editext.getText().toString();
Toast.makeText(MainActivity.this, "hello "+nom, Toast.LENGTH_LONG).show();
}
});
}
}

Toast:est un composant pour afficher le message lors de clique sur le bouton
Par la suite on doit créer le layout test.xml suivant:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nom" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
>
<requestFocus />
</EditText>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
</LinearLayout>
view raw test.xml hosted with ❤ by GitHub

No comments:

Post a Comment