A. A Quoi Sert L'attribut Android:layout - Gravity 'Center'' ?
A. A Quoi Sert L'attribut Android:layout - Gravity 'Center'' ?
A. A Quoi Sert L'attribut Android:layout - Gravity 'Center'' ?
2- Si on veut qu’on exécute des instructions après que l’activité est fermée par
l’utilisateur comment peut-on procédé ?
@Override
public void onDestroy() {
super.onDestroy();
//Instruction
}
a- Donnez le code du layout.xml
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/customername"
android:hint="Customer name"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/phone"
android:inputType="phone"
android:hint="Telephone"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email"
android:inputType="textEmailAddress"
android:hint="E-mail address"
android:layout_marginBottom="10dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Small"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bacon"
android:id="@+id/bacon"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Extra Cheese"
android:id="@+id/extraCheese"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Onion"
android:id="@+id/onion"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mushroom"
android:id="@+id/mashroom"/>
<EditText
android:id="@+id/ref"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Preferred Delivery Time"/>
<EditText
android:id="@+id/del"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Delivery instructions"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/submit"
android:layout_gravity="bottom"
android:text="SUBMIT ORDER"
/>
</LinearLayout>
b- Codez la classe .java qui permet de collecter les informations saisies par
l’utilisateur et l’enregistrer dans une base de données nommé order via le button
submit order
public class MainActivity extends AppCompatActivity {
EditText name ,phone ,email ,Del ,Ref;
CheckBox Bacon ,Extra_Cheese ,Mushroom ,Onion;
Button submit ;
SQLiteDatabase DB;
String Small ="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText)findViewById(R.id.customername);
phone=(EditText)findViewById(R.id.phone);
email=(EditText)findViewById(R.id.email);
Del=(EditText)findViewById(R.id.del);
Ref=(EditText)findViewById(R.id.ref);
submit = (Button)findViewById(R.id.submit);
Bacon = (CheckBox)findViewById(R.id.bacon) ;
Extra_Cheese = (CheckBox)findViewById(R.id.extraCheese)
;
Onion = (CheckBox)findViewById(R.id.onion) ;
Mushroom = (CheckBox)findViewById(R.id.mashroom) ;
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DB=openOrCreateDatabase("order",MODE_PRIVATE,null);
DB.execSQL("CREATE TABLE IF NOT EXISTS
CUSTOMER(num integer primary key autoincrement,name VARCHAR
,num_tele VARCHAR ,email VARCHAR ,small VARCHAR ,Pref VARCHAR
,del VARCHAR);");
if(Bacon.isChecked())
Small+="Bacon ";
if(Extra_Cheese.isChecked())
Small+="Extra_Cheese ";
if(Onion.isChecked())
Small+="Onion ";
if(Mushroom.isChecked())
Small+="Mushroom ";
if (name.getText().toString().trim().length()!=0
&&
phone.getText().toString().trim().length()!=0 &&
email.getText().toString().trim().length()!=0 &&
Del.getText().toString().trim().length()!=0 &&
Ref.getText().toString().trim().length()!=0 &&
Small.length()!=0) {
});
. c- Créer un seconde class.java qui n’affiche aucun layout mais qui permet
d’afficher un Toast contenant les informations du dernier ordre effectué ?
public class secondAct extends AppCompatActivity {
SQLiteDatabase DB ;
Cursor c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
try {
DB = openOrCreateDatabase("order", MODE_PRIVATE,
null);
c = DB.rawQuery("SELECT * FROM CUSTOMER", null);
StringBuffer buffer = new StringBuffer();
while (c.moveToNext()) {
buffer.append("name: " + c.getString(1) + "\n");
buffer.append("tele: " + c.getString(2) + "\n");
buffer.append("Prénom: " + c.getString(3) +
"\n");
buffer.append("small: " + c.getString(4) +
"\n");
buffer.append("pref: " + c.getString(5) + "\n");
buffer.append("del: " + c.getString(6) + "\n");
}
c.close();
AfficheMessage("Customers", buffer.toString());
}catch(Exception ex) {
Toast.makeText(this, ex.toString(),
Toast.LENGTH_LONG).show();
}
}
public void AfficheMessage(String titre,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(titre);
builder.setMessage(message);
builder.show();
}
}
d- Créer un intent qui permet de passer de la première activité vers la deuxième,
le lancement sera effectué directement après l’insertion des informations dans la
base de données.
4- On suppose qu’on a un layout contenant un spinner dont l’id est ‘’sp’’ créer une
classe java permettant d’afficher les éléments suivants : java, c++, .net, visual
basic, prolog dans le spinner.
Spinner sp =null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner) findViewById(R.id.sp);
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrer quelque chose ci-dessous :"
android:paddingBottom="10dip" />
<EditText
android:id="@+id/Edchamp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/txt" />
</RelativeLayout>
b- Donnez le code de la classe .java qui permet d’afficher cette boite de dialogue,
le boutton ok permet d’afficher un toast contenant le texte tappé dans l’edit text
le bouton annuler permet de fermer la boite de dialogue.
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater li = LayoutInflater.from(this);
final View alertDialog =
li.inflate(R.layout.activity_main, null);
builder.setView(alertDialog);
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("OK", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
which) {
EditText Edchamp =
(EditText)alertDialog.findViewById(R.id.Edchamp);
Toast.makeText(MainActivity.this,
Edchamp.getText(), Toast.LENGTH_SHORT).show();
} });
builder.setNegativeButton("Annuler", new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int
which) {
} });
builder.show();
}
}
7- On suppose qu’on a une interface qui contient un edittext et trois radiobutton :
a- Donnez le code du layout.xml.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text"
android:layout_marginTop="20dp"
android:hint="Enter un text "/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rouge"
android:text="Rouge"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/vert"
android:text="Vert"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/jaune"
android:text="Jaune"/>
</RadioGroup>
</LinearLayout>
b- Donnez le code de la classe.java.
RadioGroup radioColor ;
EditText txt ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioColor = (RadioGroup) findViewById(R.id.radioColor);
txt = (EditText) findViewById(R.id.txt);
radioColor.setOnCheckedChangeListener((OnCheckedChangeListener)t
his);
@Override
public void onCheckedChanged(RadioGroup group, int
checkedId) {
switch (checkedId)
{
case R.id.rouge:
txt.setTextColor(Color.parseColor("#ff0000"));
break;
case R.id.vert:
txt.setTextColor(Color.parseColor("#00ff00"));
break;
case R.id.jaune:
txt.setTextColor(Color.parseColor("#ffff00"));
break;
default:
break;
}
}
}
Ktee……..eeeb
3la ……
9albeeeek (:
Best wiches xD