Difference between revisions of "Basic Android"

esse quam videri
Jump to: navigation, search
(Basics)
(Dog Class in java)
Line 82: Line 82:
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
public class Dog {
 
public class Dog {
 +
    public String Name; //Java uses capital S String
 +
    public int Weight;
 +
    public String BarkSound;
  
public String Name;
+
    //Constructor
public int Weight;
+
    public Dog()
public String BarkSound;
+
    {
+
        this.Name = "fido";
//Constructor
+
        this.Weight = 1;
public Dog()
+
        this.BarkSound = "woof";
{
+
    }
this.Name = "fido";
+
 
this.Weight = 1;
+
    public void Eat()
this.BarkSound = "woof";
+
    {
}
+
        this.Weight++;
+
    }
public void Eat()
+
    public void Poop()
{
+
    {
this.Weight++;
+
        this.Weight--;
}
+
    }
public void Poop()
+
 
{
+
    public String About()
this.Weight--;
+
    {
}
+
        //Java string.format is similar to c# but used %s for string substitution
+
        return String.format("Hello my name is %s. I weigh %s lbs",
public String About()
+
                this.Name, this.Weight);
{
+
    }
return String.format("Hello my name is %s. I weigh %s lbs",
 
this.Name, this.Weight);
 
}
 
 
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 03:38, 8 November 2016


Android Basics

Basics

  • src folder containing all the source
  • res folder containing all the resources

folder hierarchy explained http://developer.android.com/tools/projects/index.html

First app demo http://developer.android.com/training/basics/firstapp/running-app.html

App lifecycle http://developer.android.com/guide/components/activities.html

activity_lifecycle.png

Resources

No DPI or PPI Andoid uses dp density independent pixels or sp scale interdependent pixels http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

also see supporting multiple screens http://developer.android.com/guide/practices/screens_support.html

Activities

Activity life cycle http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle

Get an instance of an object from the R class

/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Get the Button and the TextView from the Resource class
        Button btn = (Button)this.findViewById(R.id.button1);
        final TextView tv = (TextView)this.findViewById(R.id.textView1);  
        String s = "hello";
        tv.setText(s);
    }
notice the cast to the type also notice that the TextView is marked as final 
this is so that we can use it in an inner method

handle a click event

btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				tv.setText(d.About());
			}
		});

Events

Lots more good reading http://developer.android.com/guide/topics/fundamentals.html


AVD

Dog Class in java

public class Dog {
    public String Name; //Java uses capital S String
    public int Weight;
    public String BarkSound;

    //Constructor
    public Dog()
    {
        this.Name = "fido";
        this.Weight = 1;
        this.BarkSound = "woof";
    }

    public void Eat()
    {
        this.Weight++;
    }
    public void Poop()
    {
        this.Weight--;
    }

    public String About()
    {
        //Java string.format is similar to c# but used %s for string substitution
        return String.format("Hello my name is %s. I weigh %s lbs",
                this.Name, this.Weight);
    }
}

In Class Demo

AndroidStudioConfigure.png

  • Select Android SDK Manager

AndroidStudioSDKManager.png

  • Make sure you have a current SDK for this Demo we will use 23.05

Alt

  • Go back to the main menu and select New Project

Alt

  • Name the Project OOPDog and select Next

Alt

  • Select a Minimum SDK for this demo we will use API15 : IceCreamSandwich

Alt

  • Select Blank Activity for the Project template

Alt

  • The defaults are fine for the activity name select next

Alt

  • After a minute or so you can open the Project window if it isn't already open Att-1 or View/Tool Windows/Project

Alt

  • Add Dog class

Alt

  • Name the class Dog

Alt

  • Here is the c# dog class we have been using we need to change it a bit

Android Studio CSharpDog Code

Alt

Alt