• Save
Android crashcourse
Upcoming SlideShare
Loading in...5
×
 

Android crashcourse

on

  • 1,052 views

 

Statistics

Views

Total Views
1,052
Views on SlideShare
1,052
Embed Views
0

Actions

Likes
1
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Android crashcourse Android crashcourse Presentation Transcript

  • Android Crashcourse@AlexeyBuzdin 15.06.2013
  • AndroidJava SE + Android.jarDalvik runtimeComponent based MVC
  • Project Structure
  • Project StructureSource code in JavaImages, gradients, etcUI written in XMLReference Data (Strings, colors, arrays)App config file
  • Activity
  • Activitypublic class TestActivity extends Activity {@Overrideprotected void onCreate(Bundle savedState) {super.onCreate(savedState);setContentView(R.layout.main);}}
  • Activity states
  • ContextInterface to global information about anapplication environment.Up-calls for application-level operations suchas launching activities, broadcasting andreceiving intents, etc.
  • Context examplesTextView text = new TextView(context);new ArrayAdapter(context, R.layout.item, myList)context.getExternalCacheDir()context.getResources()context.startActivity(navigationIntent)
  • R.javapublic class TestActivity extends Activity {@Overrideprotected void onCreate(Bundle savedState) {super.onCreate(savedState);setContentView(R.layout.main);}}
  • R.java (resource)Auto generated interface between src and androidresources. Used only for compilation time.
  • R.java
  • Project Structure
  • Views
  • ViewsViews - TextView, EditText, Button ...ViewGroup - LinearLayout, RelativeLayout ...AdapterView - Spinner, Gallery, ListView ...
  • res/layout/<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"></LinearLayout>
  • res/layout/<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:id="@+id/textView"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="@string/app_name"/></LinearLayout>
  • res/layout/
  • Views. Java part@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);TextView text = (TextView) findViewById(R.id.textView);}
  • Views. Java part@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);TextView text = (TextView) findViewById(R.id.textView);text.findViewById(0); // same method invocation}
  • Under the hood. LayoutInflaterLayoutInflater inflater = context.getLayoutInflater();View view = inflater.inflate(R.layout.main, null);
  • Under the hood. LayoutInflater@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);LayoutInflater inflater = context.getLayoutInflater();View view = inflater.inflate(R.layout.main, null);setContentView(view);TextView text = (TextView) view.findViewById(R.id.textView);}
  • Event HandlingButton button = new Button(context);button.setOnClickListener(new OnClickListener(){@Overridepublic void onClick(View v){Toast.makeText(MyActivity.this, "HelloWorld", Toast.LENGTH_SHORT).show();}});
  • Event HandlingFor ViewsOnLongClickListenerOnSelectListenerOnHoverListener.....For AdapterViewOnItemClickListenerOnItemLongClickListener
  • TextView
  • ListView?
  • Adapter
  • AdapterViewGallery gallery = (Gallery) findViewById(R.id.gallery);ArrayList<String> list = new ArrayList<String>();gallery.setAdapter(new ArrayAdapter<String>(this, 0, list){@Overridepublic View getView(int position, View convertView,ViewGroup parent) {// 1. Check if view already exists// 2. If not - Inflate view// 3. Populate viewreturn view;}});
  • IntentAbstract description of an operation to be performed.External IntentsOpen URLCall a PersonShare an ImageInternal IntentsStart Activity
  • ExampleIntent intent = new Intent(context, SecondActivity.class);startActivity(intent);Intent intent = new Intent(context, SecondActivity.class);intent.putExtra("url", url);intent.putExtra("bean", serializable);startActivity(intent);
  • Exampleprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.article_detail);String s = getIntent().getStringExtra("url");Bean b = (Bean)getIntent().getSerializableExtra("bean");}
  • AndroidManifest<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="lv.ldn.rssreader"android:versionCode="1"android:versionName="1.0" ><uses-sdk android:minSdkVersion="7" android:targetSdkVersion="16" /><uses-permission android:name="android.permission.INTERNET" /><application android:icon="@drawable/starred" android:label="@string/app_name"><activity android:name=".MyActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>
  • AndroidManifest<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="lv.ldn.rssreader"android:versionCode="1"android:versionName="1.0" ><uses-sdk android:minSdkVersion="7" android:targetSdkVersion="16" /><uses-permission android:name="android.permission.INTERNET" /><application android:icon="@drawable/starred" android:label="@string/app_name"><activity android:name=".MyActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".SecondActivity"/></application></manifest>
  • AsyncTaskpublic class MyTask extends AsyncTask<Integer, Void, String>{protected void onPreExecute() {}protected String doInBackground(Integer... integers) {return null;}protected void onPostExecute(String result) {}}
  • AsyncTaskMyTask task = new MyTask();task.execute(value)