Sie sind auf Seite 1von 16

Tutorial 4: (Setting up an Android Project)

New -> Android -> Android Project -> Give a name -> Choose build Target (2.2 or 2.3) -> give package name (Imp: make it unique) -> Project is set up Tutorial 5: (Overview of Project and Adding Folders) (http://www.youtube.com/watch?v=sPFUTJgvVpQ&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=1) Two new folders required in the resource section - raw (to save sounds) - layout-land (for landscape view) Manifest.xml - We can mention all the permissions here, like accessing internet, camera, microphone Tutorial 6: (Introduction to Layouts in xml) (http://www.youtube.com/watch?v=maYFI5O6P-8&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=2) <Linearlayout> Contains all the contents, text-views and buttons Use of android: orientation as horizontal and vertical

Tutorial 7: (Creating a button in xml and adding an ID) (http://www.youtube.com/watch?v=6moe-rLZKCk&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=3) Use dp/dip (density pixel) for text size as it sizes it based on density and suits any android device Layout_gravity is to center the layout of the text,button Gravity is to put the content in the text view or the button to center

Tutorial 8: (Setting up variables and referencing xml IDs) (http://www.youtube.com/watch?v=eKXnQ83RU3I&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=4) Java Code // Defining Variables put it above the increate method int couter; Button add, sub; // put ids in xml as bAdd, bSub TextView display; // put id as tvDisplay // inside onCreate method Counter = 0; add = (Button) findViewById(R.id.bAdd); sub = (Button) findViewById(R.id.bSub); display = (TextVie) findViewById(R.id.tvDisplay);

Tutorial 9: (Set up a Button with OnClickListener) (http://www.youtube.com/watch?v=WjE-pWYElsE&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=5) Java Code //below the code written in last tutorial under the onCreate Methodl add.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Auto-generated method stub } }); sub.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Auto-generated method stub } });

Tutorial 10: (Using setText method for our button) (http://www.youtube.com/watch?v=hUA_isgpTHI&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=6) Java Code //below/ modifying the code written in last tutorial under the onCreate Methodl add.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Auto-generated method stub counter = counter + 1; // can use couter +=1 (or) counter++ display.setText(Your Total is + counter); } }); sub.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Auto-generated method stub Counter-- ; display.setText(Your Total is + counter); } }); Note: Display.setText(counter) wont work as it needs string input

Tutorial 11: (adding resources and setting background using sound too) (http://www.youtube.com/watch?v=IHg_0HJ5iQo&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=7) - Add a image file in drawable (background.png) - Add a sound file in raw (splash sound.mp3) Note: make sure the name is all in small letters or else you will get error Java Code //Making a new splash screen // Make a new splash xml file in layout section to set up the view of the layout // set the background as the image just put into the drawable

Tutorial 12: (Setting up an activity and using SetContentView) (www.youtube.com/watch?v=H92G3CpSQf4&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=8) Java Code // Make a new Class Splash //Make the splash class to be an activity by extending it to Activity // In Splash. Java Package com. Import .. Public class Splash extends Activity { // how to call the unimplemented method // right click within brackets -> source -> override /implement methods -> Activity (contains various methods) -> onCreate(Bundle) -> OK // sets up the default method Protected void onCreate(Bundle savedInstanceState) { // savedInstanceState is the variable sent inside // TODO Auto-generated methodState; super.onCreate(sacedInstanceState); //we want to setup the content view, we want this activity to have the splash layout setContentView(R.layout.splash); }

Tutorial 13: (Introduction to the Android Manifest) (http://www.youtube.com/watch?v=B5uJeno3xg8&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=9) // go to androidmanifest.xml // look at the intent-filter, I already know it so cool :-P // we can decide here which application page do we want to launch first as the application starts

Tutorial 14: ( The frame work of a thread) (www.youtube.com/watch?v=hy0mRoT1ZlM&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=10) // Working within the splash activity // Thread allows us to do multiple things pretty much at the same time (parallel processing // editing the last time code Java Code (Splash.java) Protected void onCreate(Bundle savedInstanceState) { // savedInstanceState is the variable sent inside // TODO Auto-generated methodState; super.onCreate(sacedInstanceState); //we want to setup the content view, we want this activity to have the splash layout setContentView(R.layout.splash); Thread timer = new Thread(){ // wat thread is looking for is a method called run(), so we need to setup this method Public void run() { // within our run method we want to keep a check try/catch for exceptions Try{ } catch (InterrupterException e){ /* InterrupterException will show error as we have nothing to do in try*/ } finally { } } }; Timer.start(); // this is to start the thread }

Tutorial 15: How to start a new activity using intent (www.youtube.com/watch?v=Xpkbu2GrJpE&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=11) Java Code (Splash.java) Protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated methodState; super.onCreate(sacedInstanceState); setContentView(R.layout.splash); Thread timer = new Thread(){ Public void run() { Try{ sleep(5000); // for 5000 milli secs :-D } catch (InterrupterException e){ e.pritnStaceTrace(); } finally { //so where to start eh application, lets say here // we could also do it under try just below sleep // there are many methods, this is a simple way // StartingPoint is the name of the main activity Intent openStartingPoint = new Intent(com.www.STARTINGPOINT) // action name in quotation is as set in the android manifest file startActivity(); //is the method to start } } }; Timer.start(); }

Tutorial 16: Activity Life Cycle (www.youtube.com/watch?v=-G91Hp3t6sg&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=12) // we will talk about the life cycle of the activity class // coz wat happens now is we had our application run but still our splash screen is thr, if hit the back button it will go back to the splash activity and we dont want that as it will potentially take some memormy hence we want to kill it once the page is closed // Watch this tutorial to understand the killing of the application when a call or a message comes on the mobile // Java Code (Splash.java) Protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated methodState; super.onCreate(sacedInstanceState); setContentView(R.layout.splash); Thread timer = new Thread(){ Public void run() { Try{ sleep(5000); // for 5000 milli secs :-D } catch (InterrupterException e){ e.pritnStaceTrace(); } finally { Intent openStartingPoint = new Intent(com.www.STARTINGPOINT) startActivity(); //is the method to start } } }; Timer.start(); } // override/implement methods onPause // Important: to destroy the other class activity ;-) Protected void onPause(){ // TODO Auto-generated method stud Super.onPause(); Finish(); }

Tutorial 17: (Adding Music with MediaPlayer) (http://www.youtube.com/watch?v=-zGS_zrL0rY&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=13) // should have splashsound.mp3 or .ogg in your raw folder // we have to set up the sound variable, thr are 2 classes that we can use for sound: Soundpool & Mediaplayer // soundpool is for small clips like explosion or gunshots 1-2 sec clips // mediaplayer is for almost everything and its easy to implement like background music for application Java Code (Splash.java) MediaPlayer ourSong; Protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated methodState; super.onCreate(sacedInstanceState); setContentView(R.layout.splash); // Media Player object // Lets say our song is 20 seconds long we dont want it to carry over into the next activity // when we are defining the variable in onCreate class we cannot use the ourSong.stop() in the onPause() method defined out of the onCreate class, so the thing we can do is to gloabalise the variable by calling it outside the onCreate class // now change this MediaPlayer ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); to ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); ourSong.start(); Thread timer = new Thread(){ Public void run() { Try{ sleep(5000); // for 5000 milli secs :-D } catch (InterrupterException e){ e.pritnStaceTrace(); } finally { Intent openStartingPoint = new Intent(com.www.STARTINGPOINT) startActivity(); //is the method to start } } }; Timer.start(); } Protected void onPause(){ // TODO Auto-generated method stud Super.onPause(); ourSong.release(); // this will release the music and say we are done with you Finish(); }

Tutorial 18: (Create a List Menu from the ListActivity class) (www.youtube.com/watch?v=4LHIESO0NGk&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=14) Here we are going to set-up a menu, as we will be creating lots of small application so that we can go to each activity and see what that application does Create a new class in src file calling it Menu . Menu.java

Java Code (Menu.java) Public class Menu extends ListActivity { // create the two unimplemented methods // now we will set-up a string array variable, basically variable depending upon wat position it is // as we want both methods to access this we define gloabally // name the first activity startingActivity make sure its of exact case sensitive nature String classes*+ = , startingPoint, example1, example2, exapmle3, example4, example5-; // now we can access this string array and give it a position and it will find at what position it is classes[4]; // it refers to example4 int eh string array @Override protected void onCreate(Bundle savedInstanceState) { // TODO Super.onCreate(savedInstanceState); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated Super.onListItemClick(l, v, position, id); } }

Tutorial 19: (Setting up an Array Adapter) (http://www.youtube.com/watch?v=8kybpxIixRk&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=15) We are not going to reference a layout and we are going to develop everything (layout) in Java Instead of saying setContentView we will use ListArrayAdapter

Java Code (Menu.java) Public class Menu extends ListActivity { String classes*+ = , startingPoint, example1, example2, exapmle3, example4, example5-; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Super.onCreate(savedInstanceState); setListAdapter(); // this takes a list adapter or array adapter within the perimeters // as we are usgin array adapter we call array adapter setLsitAdapter(new ArrayAdapter<String>(Menu.this, 0, classes)); // the parameter showing int 0 will be a single parameter of the item in the list if that makes sense // So change 0 -> android.R.layout.simple_list_item_1 // setLsitAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1 , classes)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated Super.onListItemClick(l, v, position, id); } }

Tutorial 20: (Starting an Activity with a Class Object) (www.youtube.com/watch?v=eHh2Yib7u-A&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=16) Java Code (Menu.java) Public class Menu extends ListActivity { String classes*+ = , startingPoint, example1, example2, exapmle3, example4, example5-; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Super.onCreate(savedInstanceState); setListAdapter(); // this takes a list adapter or array adapter within the perimeters setLsitAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1 , classes)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated Super.onListItemClick(l, v, position, id); // starting a new activity, different form of setting intent Class ourClass = Class.forName(com..www.startingPoint); Intent ourIntent = new Intent(Menu.this, ourClass); // or can write the content of ourClass directly here startActivity(ourIntent); } }

Tutorial 21: (Finishng List Activity) (www.youtube.com/watch?v=zjHYyAJQ7Vw&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=17) Java Code (Menu.java) Public class Menu extends ListActivity { String classes*+ = , startingPoint, example1, example2, exapmle3, example4, example5-; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Super.onCreate(savedInstanceState); setListAdapter(); // this takes a list adapter or array adapter within the perimeters setLsitAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1 , classes)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated Super.onListItemClick(l, v, position, id); // Catching Exception if existing Try{ Class ourClass = Class.forName(com..www.startingPoint); Intent ourIntent = new Intent(Menu.this, ourClass); startActivity(ourIntent); } catch(ClassNotFoundException e){ e.printStackTrace(); } } }

/* now we observe that when we click an item each time it will only open ourClass, and we want to have it such that it opens the class we want it to open according to the item in the string array */

Modofying the above code (Menu.java) Public class Menu extends ListActivity { String classes*+ = , startingPoint, example1, example2, exapmle3, example4, example5-; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Super.onCreate(savedInstanceState); setListAdapter(); // this takes a list adapter or array adapter within the perimeters setLsitAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1 , classes)); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); // Lets set up a local string here below super. .. , call It cheese String cheese = classes[position];

try{ Class ourClass = Class.forName(com..www. + cheese); // oh now I see why separately called Intent ourIntent = new Intent(Menu.this, ourClass); startActivity(ourIntent); } catch(ClassNotFoundException e){ e.printStackTrace(); } } } // change the manifest settings ;-)

Tutorial 22: (XML Introducing the EditText) (www.youtube.com/watch?v=ma8aUC-Mf5M&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=18) Here we will make a small application to mess with some text and learn a little about layout Create a new xml file under layout naming it text.xml

Java Code (text.xml) Blah Blah <EditText android:layout_width =fill_parent android:layout_height=wrap_content android:id = @+id/etCommands android:hint=Type a Command android:password=true /> <Button android:layout_width=fill_parent android:id=@+id/bResults android:layout_text=Button android:layout_height= /> <TextView android:id=@+id/tvResults android:layout_width = android:layout_height= android:layout_text = invalid android:gravity=center /> Blah

Tutorial 23: (XML ToggleButton, WeightSum and LayoutWeight) (www.youtube.com/watch?v=4MnuiIKCqsQ&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=19) Java Code (text.xml) .. <EditText android:layout_width =fill_parent android:id = @+id/etCommands android:password=true/>

android:layout_height=wrap_content android:hint=Type a Command

// I want these two buttons to be in horizontal fashion linear layout orientation so we have to set up a new <LinearLayout android:layout_width=fill_paret android:layout_height=wrap_content android:orientation=hortizontal // so the problem is Buttons are in fill parent and hence weights are needed, so 1st we need to set the width of both the buttons to fill_parent Android:weightSum=100 //100 is simple number to play with > <Button android:layout_width=fill_parent android:layout_text=Button android:layout_weight=50/> <ToggleButton android:text = ToggleButton android:layout_width=fill_parent android:layout_weight=50/> </LinearLayout>

android:id=@+id/bResults android:layout_height=

android:id=+id/toggleButton1 android:layout_height=wrap_content

<TextView android:id=@+id/tvResults android:layout_height= android:gravity=center />

android:layout_width = android:layout_text = invalid

problems when we use weight to be 20:80 between the buttons but in reality it takes shape (width) in just the opposite way

Tutorial 24: (XML Padding and setting Toggle to On) (http://www.youtube.com/watch?v=gz6P2E9lkfo&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=20)

Java Code (text.xml) // In the main Layout add this setting .. android:padding=25dip .. <EditText android:layout_width =fill_parent android:id = @+id/etCommands android:password=true/> <LinearLayout android:layout_width=fill_paret android:layout_height=wrap_content android:orientation=hortizontal Android:weightSum=100 > <Button android:layout_width=fill_parent android:layout_text=Try Command android:layout_weight=20/>

android:layout_height=wrap_content android:hint=Type a Command

android:id=@+id/bResults android:layout_height=

<ToggleButton android:text = ToggleButton android:id=+id/tbPassword android:layout_width=fill_parent android:layout_height=wrap_content android:layout_weight=80 android:paddingBottom=10dip android:checked=true/> // true means on button </LinearLayout>

<TextView android:id=@+id/tvResults android:layout_height= android:gravity=center />

android:layout_width = android:layout_text = invalid

Tutorial 25: (Quick Review by setting up a new Activity) (http://www.youtube.com/watch?v=roulejuE6B8&feature=autoplay&list=SP2F07DBCDCC01493A&playnext=21) Lets create a new java class in src naming it TextPlay

Java Code (TextPlay.java) public class TextPlay extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { //TODO Super.onCreate(savedInstanceState); setContentView(R.layout.text); Button chkCmd = (Button) findViewById(R.id.bResults); ToggleButton passTog = (ToggleButton) findViewById(R.id.tbPassword); EditText input = (EditText) findViewByID(R.id.etCommands); TextView display = (TextView) findViewById(R.id.tvResults); } } // Change example1 to TextPlay in Menu.java

Das könnte Ihnen auch gefallen