Sei sulla pagina 1di 23

Tun 3-project 1

android:id="@+id/chkMau" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:onClick="fChangeText" /> </LinearLayout> <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="5" android:orientation="vertical" > <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Use some bold typeface" android:textAppearance="?android:attr/textAppear anceLarge" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check to output bold text" android:textAppearance="?android:attr/textAppear anceSmall" /> </LinearLayout> <CheckBox android:id="@+id/chkDam" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:onClick="fChangeText" /> </LinearLayout> </LinearLayout> </LinearLayout>

------xml-------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/lblDem" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5px" android:background="@android:color/white" android:textAppearance="?android:attr/textAppear anceLarge" android:textColor="@android:color/black" /> <Button android:id="@+id/btnClick" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5px" android:onClick="fClick" android:text="Click me !" /> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="wrap_content" <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="5" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Use colored text" android:textAppearance="?android:attr/textAppear anceLarge" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Check to output colored text" android:textAppearance="?android:attr/textAppear anceSmall" /> </LinearLayout> <CheckBox

-----activity--------------------------public class Tuan3_project1Activity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setTitle("Simple aplication"); } int count = 0; public void fClick(View v) {

AlertDialog.Builder b = new AlertDialog.Builder(Tuan3_project1Activity.this) ; b.setMessage("Is it clicked ?"); b.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Autogenerated method stub dialog.cancel(); } }); b.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { count ++; TextView lbl = (TextView)findViewById(R.id.lblDem); lbl.setText("You've clicked " + count + " times."); } }); b.show(); } public void fChangeText(View v) { TextView lbl = (TextView)findViewById(R.id.lblDem); if(v.getId()==R.id.chkMau) { CheckBox chkColor = (CheckBox)findViewById(R.id.chkMau); if(chkColor.isChecked()) { lbl.setTextColor(Color.RED); } else { lbl.setTextColor(Color.BLACK); } } else if(v.getId()==R.id.chkDam) { CheckBox chkBold = (CheckBox)findViewById(R.id.chkDam); if(chkBold.isChecked()) { lbl.setTypeface(null,Typeface.BOLD); } else { lbl.setTypeface(null,Typeface.NORMAL); } } } }

Tun 3-project2:

----------------------Xml-------------------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/text_pick" android:text="current_pick" android:textColor="#ffffff" android:background="#666666" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:layout_weight="1"> </TextView> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/colorred" android:text="color_red" android:textColor="#660000" android:background="#ff0000" android:layout_width="fill_parent" android:layout_height="20dp" android:layout_weight="1" android:gravity="center" /> <TextView android:id="@+id/colorgreen" android:text="color_green" android:textColor="#006600" android:background="#00ff00" android:layout_width="fill_parent" android:layout_height="20dp" android:layout_weight="1" android:gravity="center" /> <TextView android:id="@+id/colorblue" android:text="color_blue" android:textColor="#000066" android:background="#0000ff" android:layout_width="fill_parent" android:layout_height="20dp"

android:layout_weight="1" android:gravity="center" /> <TextView android:id="@+id/colorgray" android:text="color_gray" android:textColor="#cccccc" android:background="#666666" android:layout_width="fill_parent" android:layout_height="20dp" android:layout_weight="1" android:gravity="center" /> </LinearLayout> <RadioGroup android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#333333"> <RadioButton android:id="@+id/radio_red" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="24dp" /> <RadioButton android:id="@+id/radio_green" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="24dp" /> <RadioButton android:id="@+id/radio_blue" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="24dp" /> <RadioButton android:id="@+id/radio_gray" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="24dp" android:checked="true" /> </RadioGroup> </LinearLayout>

-----------------activity------------------------------------------public class Tuan3_project2Activity extends Activity { OnClickListener radioListener; TextView tvChon; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setTitle("Simple Aplication"); radioListener = new OnClickListener() { @Override public void onClick(View v) { RadioButton rb = (RadioButton)v; switch(rb.getId()) { case R.id.radio_red: tvChon.setBackgroundColor(Color.parseColor("#ff0 000")); break; case R.id.radio_green: tvChon.setBackgroundColor(Color.parseColor("#00f f00")); break; case R.id.radio_blue: tvChon.setBackgroundColor(Color.parseColor("#000 0ff")); break; case R.id.radio_gray: tvChon.setBackgroundColor(Color.parseColor("#666 666")); break; } } }; tvChon = (TextView)findViewById(R.id.text_pick); RadioButton rbRed = (RadioButton)findViewById(R.id.radio_red); rbRed.setOnClickListener(radioListener); RadioButton rbGreen = (RadioButton)findViewById(R.id.radio_green); rbGreen.setOnClickListener(radioListener); RadioButton rbBlue = (RadioButton)findViewById(R.id.radio_blue); rbBlue.setOnClickListener(radioListener); RadioButton rbGray = (RadioButton)findViewById(R.id.radio_gray); rbGray.setOnClickListener(radioListener); } }

Tuan 4-project 1

gridview.setAdapter(new ImageAdapter(this)); gridview.setOnItemClickListener(this); } public class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIDs.length; } public Object getItem(int arg0) { return null; } public long getItemId(int arg0) { return 0; } public View getView(int arg0, View arg1, ViewGroup arg2) { // TODO Auto-generated method stub ImageView imageView; if(arg1==null) { imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(120,120)); imageView.setScaleType(ImageView.ScaleTyp e.CENTER_CROP); imageView.setPadding(1, 1, 1, 1); } else { imageView = (ImageView) arg1; } imageView.setImageResource(mThumbIDs[arg0 ]); return imageView; } } public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { showScreen2(arg2); } private void showScreen2(int position){ setContentView(R.layout.solo_picture); ivSoloPicture = (ImageView) findViewById(R.id.ivSoloImage); ivSoloPicture.setImageResource(mThumbIDs[ position]); ivSoloPicture.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { onCreate(myMemoryBundle); } }); } }

-----------------------main.xml------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <GridView android:id="@+id/gridView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:verticalSpacing="15dp" android:horizontalSpacing="0dp" android:numColumns="auto_fit" android:columnWidth="100dp" android:stretchMode="columnWidth" android:gravity="center"> </GridView> </LinearLayout> -----------------solo_picture.xml-------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/ivSoloImage" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> ----------------activity--------------------public class Tuan4_project1Activity extends Activity implements OnItemClickListener{ Integer[] mThumbIDs; ImageView ivSoloPicture; GridView gridview; Bundle myMemoryBundle; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myMemoryBundle = savedInstanceState; mThumbIDs = new Integer[]{R.drawable.v20,R.drawable.v21,R.drawab le.v22,R.drawable.v23,R.drawable.v24,R.drawable. v25,R.drawable.v26}; gridview = (GridView) findViewById(R.id.gridView1);

Tuan4-project2

------layout_contact.xml--------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="5dp"> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview" /> <TextView android:id="@+id/tvPhone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <TextView android:id="@+id/tvEmail" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </LinearLayout> --------------------main.xml-------------<ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout> -----------------activity---------------public class Tuan4_project2Activity extends ListActivity { /** Called when the activity is first created. */ String cName [] = {"Van","Hue","Tam","Quan","Hoang"}; String cPhone [] = {"01236237999","0123456789","098767656","0980978 762","0913674654"}; String cEmail [] ={"duyvan.hh@gmail.com","honghue.dv@gmail.com"," tam@gmail.com","quan@gmail.com","hoang@gmail.com "};

@Override public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); this.setTitle("Simple Aplication"); ListView lis = getListView(); ContactAdapter ct = new ContactAdapter(this, 0); lis.setAdapter(ct); } class ContactAdapter extends ArrayAdapter { Context mContext; public ContactAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); mContext = context; } @Override public int getCount() { return cName.length; } @Override public View getView(int position, View convertView, ViewGroup parent) { TextView tvName =null; TextView tvPhone =null; TextView tvEmail =null; View v = convertView; if(v==null) { LayoutInflater l = LayoutInflater.from(mContext); v = l.inflate(R.layout.layout_contact, null); } tvName = (TextView) v.findViewById(R.id.tvName); tvPhone = (TextView) v.findViewById(R.id.tvPhone); tvEmail = (TextView) v.findViewById(R.id.tvEmail); tvName.setText(cName[position]); tvPhone.setText("Mobile: "+cPhone[position]); tvEmail.setText("Mail: "+cEmail[position]); return v; }

} }

Tuan4_project3
arg3) { method stub

int arg2, long // TODO Auto-generated if (!items[arg2].equals("Please choose book!")) { if (items[arg2].contains("android")) { AlertDialog.Builder a = new AlertDialog.Builder(

-----xml-------------------------------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> ----------------activity----------------public class Tuan4_project3Activity extends Activity { /** Called when the activity is first created. */ TextView selection; String[] items = { "Please choose book!", "android programing", "nghin le mot dem", "co gai xau xi", "co be quang khan do", "bach tuyet va bay chu lun" }; Spinner spin; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spin = (Spinner) findViewById(R.id.spinner1); ArrayAdapter<String> aa = new ArrayAdapter<String>(this,

Tuan4_project3Activity.this); a.setTitle("Android book"); a.setMessage("Thanks for choosing android subject"); a.setNegativeButton("Ok", null); a.create().show(); } else { AlertDialog.Builder a = new AlertDialog.Builder( Tuan4_project3Activity.this); a.setTitle("Not Android book"); a.setMessage("This is not Android Subject.\n Good bye"); a.setNegativeButton("Exit", new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0,int arg1) { finish(); } }); a.create().show(); } } } public void onNothingSelected(AdapterView<?> arg0) { } }); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); } }

android.R.layout.simple_spinner_item, items); aa.setDropDownViewResource(android.R.layo ut.simple_spinner_dropdown_item); spin.setAdapter(aa); spin.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1,

Tuan4-project4

-------------------activity-------------public class Tuan4_project4Activity extends Activity implements OnClickListener { SlidingDrawer myDraw; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setTitle("Simple Application"); myDraw = (SlidingDrawer) findViewById(R.id.slidingDrawer1); TextView txtRed = (TextView) findViewById(R.id.txtRed); TextView txtBlue = (TextView) findViewById(R.id.txtBlue); TextView txtGreen = (TextView) findViewById(R.id.txtGreen); TextView txtGray = (TextView) findViewById(R.id.txtGray); txtRed.setOnClickListener(this); txtBlue.setOnClickListener(this); txtGreen.setOnClickListener(this); txtGray.setOnClickListener(this); } public void onClick(View arg0) { TextView tv = (TextView) arg0; LinearLayout ln = (LinearLayout) findViewById(R.id.lnMain); switch (tv.getId()) { case R.id.txtRed: ln.setBackgroundColor(Color.RED); break; case R.id.txtBlue: ln.setBackgroundColor(Color.BLUE); break; case R.id.txtGreen: ln.setBackgroundColor(Color.GREEN); break; case R.id.txtGray: ln.setBackgroundColor(Color.GRAY); break; default: ln.setBackgroundColor(Color.BLACK); break; } } }

--------------xml------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:id="@+id/lnMain" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:gravity="center" > <SlidingDrawer android:id="@+id/slidingDrawer1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:content="@+id/content" android:handle="@+id/handle" > <Button android:id="@id/handle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Handle" /> <LinearLayout android:id="@id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/txtRed" android:layout_width="45dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_margin="1dp" android:background="#ff0000" /> <TextView android:id="@+id/txtGreen" android:layout_width="45dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_margin="1dp" android:background="#00ff00" /> <TextView android:id="@+id/txtBlue" android:layout_width="45dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_margin="1dp" android:background="#0000ff" /> <TextView android:id="@+id/txtGray" android:layout_width="45dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_margin="1dp" android:background="#888888" /> </LinearLayout> </SlidingDrawer> </LinearLayout>

Tuan4-project 6

------------------main.xml----------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" > <include layout="@layout/layout_name"/> </LinearLayout> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" > <include layout="@layout/layout_color"/> </LinearLayout> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" > <include layout="@layout/layout_animal"/> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/re s/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:layout_margin="10dp" android:text="Enter your pet:" /> <EditText android:id="@+id/edtAnimal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <requestFocus /> </EditText> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" > <Button android:id="@+id/btPreviousAnimal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Previous" /> <Button android:id="@+id/btFinish" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Finish" /> </LinearLayout> </LinearLayout> --------------layout_color--------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/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:layout_margin="8dp" android:text="Enter your favorite color: " /> <EditText android:id="@+id/edtColor" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="8dp" > <requestFocus /> </EditText> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btPreviousColor" android:layout_width="fill_parent"

-----------layout_animal-----------<?xml version="1.0" encoding="utf-8"?>

android:layout_height="wrap_content" android:layout_weight="1" android:text="Previous" /> <Button android:id="@+id/btNextColor" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="Next" /> </LinearLayout> </LinearLayout> --------------layout_name---------------

spec.setIndicator("Color"); tabs.addTab(spec); spec = tabs.newTabSpec("tag3"); spec.setContent(R.id.tab3); spec.setIndicator("AnimalS"); tabs.addTab(spec); tabs.setCurrentTab(0); OnClickListener myOnClick = new OnClickListener() { public void onClick(View v) if (v.getId() == R.id.btNextColor || v.getId() == R.id.btNextName) { tabs.setCurrentTab(tabs.getCurrentTab() +

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/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="Enter your name: " android:layout_margin="10dp"/> <EditText android:id="@+id/edtName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp"> <requestFocus /> </EditText> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" > <Button android:id="@+id/btNextName" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_margin="10dp" android:text="Next" /> </LinearLayout> </LinearLayout> -------------activity---------------public class Tuan4_project6Activity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setTitle("Simple Aplication"); final TabHost tabs = getTabHost(); tabs.setup(); TabHost.TabSpec spec; spec = tabs.newTabSpec("tag1"); spec.setContent(R.id.tab1); spec.setIndicator("Name"); tabs.addTab(spec); spec = tabs.newTabSpec("tag2"); spec.setContent(R.id.tab2);

1); } else if (v.getId() == R.id.btPreviousAnimal || v.getId() == R.id.btPreviousColor) { tabs.setCurrentTab(tabs.getCurrentTab() 1); } else { EditText edtName = (EditText) findViewById(R.id.edtName); EditText edtColor = (EditText) findViewById(R.id.edtColor); EditText edtAnimal = (EditText) findViewById(R.id.edtAnimal); AlertDialog.Builder al = new AlertDialog.Builder( Tuan4_project6Activity.this); al.setTitle("Your choosing"); al.setMessage("Name: " + edtName.getText().toString() + "\nColor: " + edtColor.getText().toString() + "\nAnimal: " + edtAnimal.getText().toString()); al.setPositiveButton("Ok", null); al.create().show(); } } }; Button btnNextName = (Button) findViewById(R.id.btNextName); Button btnNextColor = (Button) findViewById(R.id.btNextColor); Button btnFinish = (Button) findViewById(R.id.btFinish); Button btnPreviousColor = (Button) findViewById(R.id.btPreviousColor); Button btnPreviousAnimal = (Button) findViewById(R.id.btPreviousAnimal); btnNextName.setOnClickListener(myOnClick) ; btnNextColor.setOnClickListener(myOnClick ); btnFinish.setOnClickListener(myOnClick); btnPreviousAnimal.setOnClickListener(myOn Click); btnPreviousColor.setOnClickListener(myOnC lick); } }

Tuan5-project1

@Override public int getCount() { // TODO Auto-generated method stub return listNumber.size(); } @Override public void remove(Object object) { // TODO Auto-generated method stub super.remove(object); } @Override public View getView(int position,final View convertView, ViewGroup parent) { // TODO Auto-generated method stub final int pos = position; View v = convertView; ViewWrapper myw; if (v == null) { LayoutInflater lf = LayoutInflater.from(thisContext); v = lf.inflate(R.layout.item_layout, null); myw = new ViewWrapper(v); v.setTag(myw); } else myw = (ViewWrapper) v.getTag(); final TextView tvNumber = myw.getTvNumber(); final Button btnRemove = myw.getBtnRemove(); btnRemove.setText("X"); tvNumber.setText(listNumber.get(position) .toString()); btnRemove.setTextColor(pos % 2 == 0 ? Color.RED : Color.BLUE); tvNumber.setTextColor(pos % 2 == 0 ? Color.RED : Color.BLUE); btnRemove.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if (listNumber.get(pos) == timMax()) { listNumber.remove(pos); remove(convertView); return; } else { Toast.makeText(thisContext, "Not biggest number!\n(Max= " + timMax() + ")", 0) .show(); } } }); return v; } Integer timMax() { Integer max = listNumber.get(0);

-----------main.xml----------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <GridView android:id="@+id/gvMain" android:layout_width="fill_parent" android:layout_height="fill_parent" android:columnWidth="90dp" android:gravity="center" android:horizontalSpacing="10dp" android:numColumns="auto_fit" android:stretchMode="columnWidth" android:verticalSpacing="10dp" > </GridView> </LinearLayout> --------item_layout.xml--------------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/tvNumber" android:layout_width="40dp" android:layout_height="wrap_content" android:gravity="center" /> <Button android:id="@+id/btnRemove" android:layout_width="40dp" android:layout_height="50dp" /> </LinearLayout> ----------------CustomAdapter.java------------public class CustomAdapter extends ArrayAdapter { Context thisContext; ArrayList<Integer> listNumber; public CustomAdapter(Context context, ArrayList<Integer> listNumber) { super(context, R.layout.item_layout); thisContext = context; this.listNumber = listNumber; }

for (int i = 0; i < listNumber.size(); i++) { if (listNumber.get(i) > max) max = listNumber.get(i); } return max; } public class ViewWrapper { View base; TextView tvNumber = null; Button btnRemove = null; public ViewWrapper(View base) { this.base = base; } public TextView getTvNumber() { if (tvNumber == null) { tvNumber = (TextView) base.findViewById(R.id.tvNumber); } return tvNumber; } public Button getBtnRemove() { if (btnRemove == null) { btnRemove = (Button) base.findViewById(R.id.btnRemove); } return btnRemove; } } }

Tuan5_project2

----------------main.xml----------------------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="10dp" > <Spinner android:id="@+id/spnTemplate" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnNext" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Next" /> </LinearLayout> -----------------message_layout.xml------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="10dp" > <TextView android:id="@+id/tvMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dip" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Send SMS to phone number:" /> <EditText android:id="@+id/txtNumber"

-------------------activity-----------------public class Tuan5_project1Activity extends Activity { /** Called when the activity is first created. */ GridView gvMain; CustomAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setTitle("Simple Aplication"); // Create list values ArrayList<Integer> listNumber = new ArrayList<Integer>(); Random rand = new Random(); while (listNumber.size() < 40) { int num = rand.nextInt(200); if (listNumber.indexOf(num) < 0) listNumber.add(num); } adapter = new CustomAdapter(this, listNumber); gvMain = (GridView) findViewById(R.id.gvMain); gvMain.setAdapter(adapter); } }

android:layout_width="match_parent" android:layout_height="wrap_content" > <requestFocus /> </EditText>

TextView tvMessage=(TextView)findViewById(R.id.tvMessage) ; tvMessage.setText(message); SharedPreferences sp=getSharedPreferences("app", MODE_PRIVATE); String phoneNumber=sp.getString("phoneNumber", ""); txtNumber.setText(phoneNumber); btnSend.setOnClickListener(new OnClickListener() { public void onClick(View

<Button android:id="@+id/btnSend" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send SMS" /> </LinearLayout> ------------message_template_layout.xml------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="10dp" > <ImageView android:id="@+id/imgIcon" android:layout_width="50dp" android:layout_height="50dp" android:scaleType="centerCrop" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tvMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" /> </LinearLayout> ---------------SendMessage_Activity-----------public class SendMessage_Activity extends Activity { /** Called when the activity is first created. */ String message = ""; Intent myIntent; EditText txtNumber; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.message_layout); myIntent=getIntent(); message=myIntent.getExtras().getString("v Message"); Button btnSend=(Button)findViewById(R.id.btnSend); txtNumber=(EditText)findViewById(R.id.txt Number);

arg0) { // TODO Auto-generated method stub String phoneNumber=txtNumber.getText().toString(); myIntent = new Intent(Intent.ACTION_SENDTO); myIntent.setData(Uri.parse("sms:" + phoneNumber)); myIntent.putExtra("sms_body", message); startActivity(myIntent); } }); } @Override protected void onPause() { SharedPreferences sp=getSharedPreferences("app",MODE_PRIVATE); Editor ed=sp.edit(); ed.putString("phoneNumber",txtNumber.getT ext().toString() ); ed.commit(); super.onPause(); } } ---------------------activity------------public class Tuan5_project2Activity extends Activity { /** Called when the activity is first created. */ int[] arrIcon = { R.drawable.ic_launcher, R.drawable.blackberry, R.drawable.htc, R.drawable.iphone4s, R.drawable.samsung }; String[] arrMessage = { "Chn Template SMS...", "How are you", "Ti nay cafe nh", "Chc ng ngon", "Sao bun th ?" }; String message = ""; Intent myIntent; Spinner spnTemplate; Button btnNext; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

btnNext = (Button) findViewById(R.id.btnNext); spnTemplate = (Spinner) findViewById(R.id.spnTemplate); CustomMessageAdapter adapter = new CustomMessageAdapter(this); spnTemplate.setAdapter(adapter); spnTemplate.setOnItemSelectedListener(new OnItemSelectedListener() {

private Context mContext; public CustomMessageAdapter(Context context) { this.mContext = context; } public int getCount() { return arrMessage.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ViewWrapper myw; if (v == null) { LayoutInflater lf = LayoutInflater.from(mContext); v = lf.inflate(R.layout.message_template_layout, null); myw = new ViewWrapper(v); v.setTag(myw); } else myw = (ViewWrapper) v.getTag(); TextView tvMessage = myw.getTvMessage(); ImageView imgIcon = myw.getImgIcon(); tvMessage.setText(arrMessage[position]); tvMessage.setTextColor(Color.RED); imgIcon.setImageResource(arrIcon[position ]); return v; } } class ViewWrapper { View base; TextView tvMessage = null; ImageView imgIcon = null; public ViewWrapper(View base) { this.base = base; } public ImageView getImgIcon() { if (imgIcon == null) imgIcon = (ImageView) base.findViewById(R.id.imgIcon); return imgIcon; } public TextView getTvMessage() { if (tvMessage == null) tvMessage = (TextView) base.findViewById(R.id.tvMessage); return tvMessage; } } }

public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) { // TODO Auto-generated method stub if (pos > 0) { message = arrMessage[pos]; btnNext.setEnabled(true); } else btnNext.setEnabled(false); } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); btnNext.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub if (message.length() > 0) { myIntent = new Intent( Tuan5_project2Activity.this, SendMessage_Activity.class); myIntent.putExtra("vMessage", message); startActivity(myIntent); message = ""; } } }); } @Override protected void onResume() spnTemplate.setSelection(0); btnNext.setEnabled(true); super.onResume(); } class CustomMessageAdapter extends BaseAdapter {

Tuan5-project3

------------main.xml--<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="10dp" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="User Name" /> <EditText android:id="@+id/txtUserName" android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="true" android:text="Fit" /> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="password" /> <EditText android:id="@+id/txtPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:password="true" android:text="hui" /> <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" /> </LinearLayout> -----------choose_email_layout.xml------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="20dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:orientation="vertical" > <Button android:id="@+id/btnYahoo" android:layout_width="match_parent" android:layout_height="wrap_content"

android:text="Get password for Yahoo mail" /> <Button android:id="@+id/btnGmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Get password for Gmail mail" /> <Button android:id="@+id/btnHotMail" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Get password for Hot mail" /> </LinearLayout> -----------activity public class Tuan5_project3Activity extends Activity { Intent myIntent; EmailAccount account; EmailAccount emailPass; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.setTitle("Simple Aplication"); EditText txtUser = (EditText) findViewById(R.id.txtUserName); Button btnLogin = (Button) findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { EditText txtUserName = (EditText) findViewById(R.id.txtUserName); EditText txtpassword = (EditText) findViewById(R.id.txtPassword); account = new EmailAccount(txtUserName.getText().toString(),tx tpassword.getText().toString()); Bundle myBundle = new Bundle(); myBundle.putSerializable("vAccount", account); myIntent = new Intent( Tuan5_project3Activity.this, ChooseEmail_Activity.class); myIntent.putExtras(myBundle); startActivityForResult(myIntent, 1111); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); if ((requestCode == 1111) && (resultCode == Activity.RESULT_OK)) {

Bundle bundle = data.getExtras(); emailPass = (EmailAccount) bundle.getSerializable("vEmailPass"); Toast t = Toast.makeText(this, emailPass.getPassWord(), 0); t.show(); } } } } ----ChooseEmail_Activity.java--------------public class ChooseEmail_Activity extends Activity implements OnClickListener { /** Called when the activity is first created. */ Intent myIntent2; EmailAccount account; EmailAccount emailPass; String pass = ""; Bundle myBundle; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.choose_email_layo ut); Button btnYahoo = (Button) findViewById(R.id.btnYahoo); Button btnGmail = (Button) findViewById(R.id.btnGmail); Button btnHotMail = (Button) findViewById(R.id.btnHotMail); btnYahoo.setOnClickListener(this); btnGmail.setOnClickListener(this); btnHotMail.setOnClickListener(this); myIntent2 = getIntent(); myBundle = myIntent2.getExtras(); account = (EmailAccount) myBundle.getSerializable("vAccount"); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btnYahoo: pass = "Yahoo: " + account.getPassWord(); break; case R.id.btnGmail: pass = "Gmail: " + account.getPassWord(); break; case R.id.btnHotMail: pass = "HotMail: " + account.getPassWord(); break; default: break; } { this.userName = userName; } public String getPassWord() { return passWord; } public String getUserName() { return userName; } } } emailPass = new EmailAccount(account.getUserName(), pass); myBundle.putSerializable("vEmailPass", emailPass); myIntent2.putExtras(myBundle); setResult(Activity.RESULT_OK, myIntent2); super.finish();

-------------EmailAccount.java-----------mport java.io.Serializable; public class EmailAccount implements Serializable { private String userName; private String passWord; public EmailAccount(String user, String pass) { this.userName = user; this.passWord = pass; } public void setPassWord(String passWord) { this.passWord = passWord; } public void setUserName(String userName)

-----------------manifest------------<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".Tuan5_project3Activity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ChooseEmail_Activity" > </activity> </application>

Tuan5-project4

android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="7" > </Button>

--------------xml-------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <WebView android:id="@+id/webView1" android:layout_width="match_parent" android:layout_height="60dp" android:layout_weight="0.00" > </WebView> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="(" > </Button> <Button android:id="@+id/btn11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text=")" > </Button> <Button android:id="@+id/btnB" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="B" > </Button> <Button android:id="@+id/btnC" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="C" > </Button> </LinearLayout> <LinearLayout android:id="@+id/linearLayout1"

<Button android:id="@+id/btn8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="8" > </Button> <Button android:id="@+id/btn9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="9" > </Button> <Button android:id="@+id/btnCong" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="+" > </Button> </LinearLayout> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" > </Button> <Button android:id="@+id/btn5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" > </Button> <Button android:id="@+id/btn6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="6" > </Button> <Button android:id="@+id/btnTru" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:layout_weight="1" android:text="-" > </Button> </LinearLayout> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" > </Button> <Button android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" > </Button> <Button android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" > </Button> <Button android:id="@+id/btnNhan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="*" > </Button> </LinearLayout> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/btn0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="0" > </Button> <Button android:id="@+id/btnNhan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="." > </Button> <Button android:id="@+id/btnKetQua"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="=" > </Button> <Button android:id="@+id/btnChia" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="/" > </Button> </LinearLayout> </LinearLayout> -------------activity-------public class Tuan5_project4Activity extends Activity implements OnClickListener{ /** Called when the activity is first created. */ StringBuilder sd; WebView vv; MyObject oj =new MyObject();

int[] btnS = { R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9, R.id.btnB, R.id.btnC, R.id.btnChia, R.id.btnKetQua, R.id.btnNhan, R.id.btnTru, R.id.btnCong, R.id.btn10, R.id.btn11 }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); vv = (WebView) findViewById(R.id.webView1); vv.getSettings().setJavaScriptEnabled(tru e); sd = new StringBuilder(); for (int btnID : btnS) { Button mybtn = (Button) findViewById(btnID); mybtn.setOnClickListener(this); } } public void onClick(View v) { // TODO Auto-generated method stub Button mybtn = (Button) (v); if (mybtn.getText().toString().equals("C")) { sd.delete(0, sd.length()); hienthi(); } else if (mybtn.getText().toString().equals("B")) { sd.deleteCharAt(sd.length() - 1); hienthi(); } else if (mybtn.getText().toString().equals("=")) {

hienthiketqua(); } else { sd.append(mybtn.getText().toString()); hienthi(); } } void hienthi() { String myhtml = "<html><body>" + sd.toString() + "</body></html>"; vv.loadData(myhtml, "text/html", "uft-8"); } void hienthiketqua() { vv.addJavascriptInterface(oj,"myObject"); String shtml = "<html><body><script "; shtml += " type=\"text/javascript\">"; shtml += "document.write(\"" + sd.toString() + "\");"; shtml += "document.write(\"<br/>\");document.write(\"=\") ;"; shtml += "document.write(eval(\"" + sd.toString() + "\"));"; shtml += "</script></body></html>"; vv.loadData(shtml, "text/html", "uft-8"); } class MyObject { public void dislayResult(String result) { Toast.makeText(Tuan5_project4Activity.thi s, result, Toast.LENGTH_SHORT).show(); } } }

Tun6-project1

-------------xml---------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/lbl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="How are you today ? This is a long text ." android:textAppearance="?android:attr/textAppear anceMedium" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/btnPre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" /> <Button android:id="@+id/btnPause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" /> <Button android:id="@+id/btnNext" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" /> </LinearLayout>

</LinearLayout> --------------activity--------public class W6_project1Activity extends Activity {

/** Called when the activity is first created. */ String str; Handler h; TextView lbl; int for_or_back = 1; boolean isRunning = true; int position; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); h = new Handler(); lbl = (TextView) findViewById(R.id.lbl); str = lbl.getText().toString(); Button btnPre = (Button) findViewById(R.id.btnPre); btnPre.setText("<<"); btnPre.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub for_or_back = -1; } }); Button btnNext = (Button) findViewById(R.id.btnNext); btnNext.setText(">>"); btnNext.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub for_or_back = 1; } }); Button btnPause = (Button) findViewById(R.id.btnPause); btnPause.setText(">"); btnPause.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Button btn = (Button) v; if (btn.getText().equals(">")) { btn.setText("||"); isRunning = true; } else {

btn.setText(">"); isRunning = false; } runThread(); } }); } void runThread() { Thread th = new Thread(new Runnable() { public void run() { // TODO Auto-generated method stub while (isRunning) { try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } h.post(new Runnable() { public void run() { // TODO Auto-generated method stub lbl.setText(scrollText()); } }); } } }); th.start(); } String scrollText() { String s1 = str.substring(0, position); String s2 = str.substring(position + 1); position = position + for_or_back; position = position % str.length(); if (position < 0) { position = str.length() - 1; } return s2 + s1; } static final int DIALOG_REALLY_EXIT_ID = 1; @Override protected Dialog onCreateDialog(int id) { // TODO Auto-generated method stub Dialog d; switch (id) { case DIALOG_REALLY_EXIT_ID:

{ AlertDialog.Builder alert = new AlertDialog.Builder( W6_project1Activity.this); alert.setMessage("Do you really want to quit ?"); alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub W6_project1Activity.this.finish(); } });

Tuan6-project 2

---------------xml-----<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Start list" /> <ListView android:id="@+id/lsv" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout> ----------------item.xml----------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Medium Text" android:layout_gravity="center_vertical" android:textAppearance="?android:attr/textAppear anceMedium" /> </LinearLayout>

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); } }); d = alert.create(); } break; default: d = null; break; } return d; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { showDialog(DIALOG_REALLY_EXIT_ID); } return true; }

----------activity---------public class W6_project2Activity extends Activity { ProgressDialog dialog; ListView lsv; myListAdapter myAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lsv = (ListView) findViewById(R.id.lsv); myAdapter = new myListAdapter(this, R.layout.item); lsv.setAdapter(myAdapter); dialog = new ProgressDialog(this); Button btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { W6_project2Activity.this.RunThread(); } }); } Handler h = new Handler() { public void handleMessage(Message msg) { if (msg.what == 1) { Bundle b = msg.getData(); int i = b.getInt("key"); myAdapter.add(String.valueOf(i)); dialog.setMessage(String.valueOf(i)+" %"); } else { dialog.dismiss(); } }; }; class myThread implements Runnable { public void run() { for (int i = 0; i < 100; i++) { try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } Message m = h.obtainMessage(); Bundle b = new Bundle(); b.putInt("key", i + 1); m.setData(b); m.what = 1; h.sendMessage(m); } h.sendEmptyMessage(0); } } void RunThread() { dialog.setTitle("PLease wait...");

dialog.show(); Thread th = new Thread(new myThread()); th.start(); } class myListAdapter extends ArrayAdapter { public myListAdapter(Context context, int textViewResourceId) { super(context, textViewResourceId); } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; ViewWrapper myWrap; if (v == null) { LayoutInflater l = LayoutInflater .from(W6_project2Activity.this); v = l.inflate(R.layout.item, null); myWrap = new ViewWrapper(v); v.setTag(myWrap); } else myWrap = (ViewWrapper) convertView.getTag(); ImageView img = myWrap.getImg(); TextView lbl = myWrap.getlbl(); img.setImageResource(R.drawable.ic_launch er); lbl.setText(this.getItem(position).toStri ng()); return v; } } class ViewWrapper { View base; TextView lbl = null; ImageView img = null; public ViewWrapper(View base) { this.base = base; } public TextView getlbl() { if (lbl == null) lbl = (TextView) base.findViewById(R.id.textView1); return lbl; } public void setlbl(TextView lbl) { this.lbl = lbl; } public ImageView getImg() { if (img == null) img = (ImageView) base.findViewById(R.id.imageView1); return img; } public void setImg(ImageView img) { this.img = img; } } }

Tuan6-project3

<LinearLayout android:id="@+id/linearLayout2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="0.30" android:orientation="vertical" > </LinearLayout> </LinearLayout> ------acivity-----public class BaiTapTuan6_Projcet3Activity extends Activity { EditText edt_so; LinearLayout l; Button bt_new; int so_bt = 0; int diem = 0; Boolean chay = true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); l = (LinearLayout) findViewById(R.id.linearLayout2); edt_so = (EditText) findViewById(R.id.editText1); bt_new = (Button) findViewById(R.id.button1); bt_new.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (edt_so.getText().length() > 0) { so_bt = (Integer.parseInt(edt_so.getText().toString())); if (so_bt < 8) { l.removeAllViews(); chay = true; diem = 0; for (int i = 0; i < so_bt; i++) { run_bt run_ = new run_bt(); run_.execute(); } } else Toast.makeText(BaiTapTuan6_Projcet3Activi ty.this, "Max lever is 7", Toast.LENGTH_LONG).show(); }

Xml----------<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/re s/android" android:id="@+id/main_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" >

<TextView android:id="@+id/textView1" android:layout_width="43dp" android:layout_height="wrap_content" android:layout_weight="0.09" android:text="Level" android:textAppearance="?android:attr/textAppear anceMedium" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="number" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start game" /> </LinearLayout>

} }); } class run_bt extends AsyncTask<Button, Button, Button> { Random r = new Random(); int weght = 100; Button btn; LayoutParams l_para = new LayoutParams(100, LayoutParams.WRAP_CONTENT); @Override protected void onCancelled() { // TODO Auto-generated method stub super.onCancelled(); } @Override protected void onPostExecute(Button result) { // TODO Auto-generated method stub super.onPostExecute(result); } SystemClock.sleep(0); publishProgress(null); i < 15; i++) {

for (int i = 0; weght--; //

} btn.setTag(true); } } diem++; chay = false; if (diem == 1) publishProgress(null); return null;

@Override protected void onProgressUpdate(Button... values) { // TODO Auto-generated method stub super.onProgressUpdate(values);

} @Override protected void onPreExecute() { // TODO Auto-generated method stub super.onPreExecute(); btn = new Button(BaiTapTuan6_Projcet3Activity.this); btn.setLayoutParams(l_para); btn.setTag(true); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { btn.setText(btn.getWidth() + ""); btn.setTag(false); } }); l.addView(btn); } @Override protected Button doInBackground(Button... params) { // TODO Auto-generated method stub while (btn.getWidth() < 480 && chay) { if ((Boolean) btn.getTag()) { weght += r.nextInt(15); SystemClock.sleep(20); publishProgress(null); } else { Toast.makeText(BaiTapTuan6_Projcet3Activi ty.this, "I win", Toast.LENGTH_LONG).show(); } } } } if (chay) { l_para.width = weght; btn.setWidth(weght); } else if (diem == 1) {

Potrebbero piacerti anche