Erinevus lehekülje "ITI0011-2014:Android" redaktsioonide vahel
P (Ago teisaldas lehekülje ITI0011:Android pealkirja ITI0011-2014:Android alla) |
|||
| (ei näidata sama kasutaja 3 vahepealset redaktsiooni) | |||
| 10. rida: | 10. rida: | ||
* Window | * Window | ||
* Preferences | * Preferences | ||
| + | * Vasakult nimekirjast valida "Android" | ||
* Valida kaust, kuhu android sdk pakiti | * Valida kaust, kuhu android sdk pakiti | ||
. | . | ||
| 41. rida: | 42. rida: | ||
* Android studio, IntelliJ IDEA jaoks: https://developer.android.com/sdk/installing/studio.html | * Android studio, IntelliJ IDEA jaoks: https://developer.android.com/sdk/installing/studio.html | ||
| + | |||
| + | == Telefoni peal arendamine == | ||
| + | |||
| + | Kui soovid oma telefoni peal katsetada app-e (tugevalt soovitatav, kuna palju kiirem), siis see on juhend -> http://www.phonearena.com/news/How-to-enable-USB-debugging-on-Android_id53909 | ||
== Arendamine == | == Arendamine == | ||
| 65. rida: | 70. rida: | ||
Failid (projekti all): | Failid (projekti all): | ||
| + | * src/ - lähtekood, java-failid | ||
| + | * res/ - ressursid | ||
* res/layout/activity_main.xml - peamine layout | * res/layout/activity_main.xml - peamine layout | ||
* res/values/strings.xml - sisaldab kõiki sõnesid, mis rakenduses kasutada saab | * res/values/strings.xml - sisaldab kõiki sõnesid, mis rakenduses kasutada saab | ||
| + | * AndroidManifest.xml - rakenduse seaded | ||
XML-failide puhul saab sisuakna all olevate tab'idega valida kas lähtekood (tekstiline näitamine) või graafiline näitamine. | XML-failide puhul saab sisuakna all olevate tab'idega valida kas lähtekood (tekstiline näitamine) või graafiline näitamine. | ||
| + | |||
| + | == Koodinäide loengust == | ||
| + | |||
| + | '''src/com/example/smartguy/MainActivity.java:''' | ||
| + | <source lang="java"> | ||
| + | package com.example.smartguy; | ||
| + | |||
| + | import android.app.Activity; | ||
| + | import android.os.Bundle; | ||
| + | import android.view.Menu; | ||
| + | import android.view.MenuItem; | ||
| + | import android.view.View; | ||
| + | import android.widget.Button; | ||
| + | import android.widget.EditText; | ||
| + | import android.widget.ImageView; | ||
| + | import android.widget.TextView; | ||
| + | |||
| + | |||
| + | public class MainActivity extends Activity { | ||
| + | |||
| + | @Override | ||
| + | protected void onCreate(Bundle savedInstanceState) { | ||
| + | super.onCreate(savedInstanceState); | ||
| + | setContentView(R.layout.activity_main); | ||
| + | |||
| + | Button askButton = (Button) findViewById(R.id.ask_button); | ||
| + | |||
| + | askButton.setOnClickListener(new View.OnClickListener() { | ||
| + | @Override | ||
| + | public void onClick(View v) { | ||
| + | EditText questionField = (EditText) findViewById(R.id.question_field); | ||
| + | String question = questionField.getText().toString(); | ||
| + | TextView answerText = (TextView) findViewById(R.id.answer_text); | ||
| + | ImageView answerImage = (ImageView) findViewById(R.id.answer_image); | ||
| + | double random = Math.random(); | ||
| + | if (random > 0.6) { | ||
| + | answerText.setText("Probably Yes!"); | ||
| + | answerImage.setImageResource(R.drawable.thumbs_down); | ||
| + | } else { | ||
| + | answerText.setText("No!"); | ||
| + | answerImage.setImageResource(R.drawable.borat_thumbs_up); | ||
| + | } | ||
| + | } | ||
| + | }); | ||
| + | } | ||
| + | |||
| + | |||
| + | @Override | ||
| + | public boolean onCreateOptionsMenu(Menu menu) { | ||
| + | // Inflate the menu; this adds items to the action bar if it is present. | ||
| + | getMenuInflater().inflate(R.menu.main, menu); | ||
| + | return true; | ||
| + | } | ||
| + | |||
| + | @Override | ||
| + | public boolean onOptionsItemSelected(MenuItem item) { | ||
| + | // Handle action bar item clicks here. The action bar will | ||
| + | // automatically handle clicks on the Home/Up button, so long | ||
| + | // as you specify a parent activity in AndroidManifest.xml. | ||
| + | EditText questionField = (EditText) findViewById(R.id.question_field); | ||
| + | |||
| + | int id = item.getItemId(); | ||
| + | if (id == R.id.action_settings) { | ||
| + | questionField.setText("Are settings useful?"); | ||
| + | return true; | ||
| + | } else if (id == R.id.weather_question) { | ||
| + | questionField.setText("Is it going to rain today?"); | ||
| + | return true; | ||
| + | } | ||
| + | return super.onOptionsItemSelected(item); | ||
| + | } | ||
| + | } | ||
| + | </source> | ||
| + | |||
| + | '''res/drawable/''': | ||
| + | siin on 2 pilti nimega: borat_thumbs_up.jpg ja thumbs_down.jpg (täiesti suvalised pildid sobivad) | ||
| + | |||
| + | '''res/layout/activity_main.xml:''' | ||
| + | <source lang="xml"> | ||
| + | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
| + | xmlns:tools="http://schemas.android.com/tools" | ||
| + | android:layout_width="match_parent" | ||
| + | android:layout_height="match_parent" | ||
| + | android:paddingBottom="@dimen/activity_vertical_margin" | ||
| + | android:paddingLeft="@dimen/activity_horizontal_margin" | ||
| + | android:paddingRight="@dimen/activity_horizontal_margin" | ||
| + | android:paddingTop="@dimen/activity_vertical_margin" | ||
| + | tools:context="com.example.smartguy.MainActivity" > | ||
| + | |||
| + | <TextView | ||
| + | android:id="@+id/textView2" | ||
| + | android:layout_width="wrap_content" | ||
| + | android:layout_height="wrap_content" | ||
| + | android:layout_alignParentLeft="true" | ||
| + | android:text="@string/question" | ||
| + | android:textAppearance="?android:attr/textAppearanceLarge" /> | ||
| + | |||
| + | <EditText | ||
| + | android:id="@+id/question_field" | ||
| + | android:layout_width="wrap_content" | ||
| + | android:layout_height="wrap_content" | ||
| + | android:layout_alignParentLeft="true" | ||
| + | android:layout_alignParentRight="true" | ||
| + | android:layout_below="@+id/textView2" | ||
| + | android:ems="10" | ||
| + | android:inputType="text" > | ||
| + | |||
| + | <requestFocus /> | ||
| + | </EditText> | ||
| + | |||
| + | <Button | ||
| + | android:id="@+id/ask_button" | ||
| + | android:layout_width="wrap_content" | ||
| + | android:layout_height="wrap_content" | ||
| + | android:layout_below="@+id/question_field" | ||
| + | android:layout_centerHorizontal="true" | ||
| + | android:text="@string/ask_button" /> | ||
| + | |||
| + | <TextView | ||
| + | android:id="@+id/answer_text" | ||
| + | android:layout_width="wrap_content" | ||
| + | android:layout_height="wrap_content" | ||
| + | android:layout_below="@+id/ask_button" | ||
| + | android:layout_centerHorizontal="true" | ||
| + | android:textAppearance="?android:attr/textAppearanceLarge" /> | ||
| + | |||
| + | <ImageView | ||
| + | android:id="@+id/answer_image" | ||
| + | android:layout_width="200dp" | ||
| + | android:layout_height="200dp" | ||
| + | android:layout_below="@+id/answer_text" | ||
| + | android:layout_centerHorizontal="true" /> | ||
| + | |||
| + | </RelativeLayout> | ||
| + | </source> | ||
| + | |||
| + | '''res/menu/menu.xml''': | ||
| + | <source lang="xml"> | ||
| + | <menu xmlns:android="http://schemas.android.com/apk/res/android" | ||
| + | xmlns:tools="http://schemas.android.com/tools" | ||
| + | tools:context="com.example.smartguy.MainActivity" > | ||
| + | |||
| + | <item | ||
| + | android:id="@+id/action_settings" | ||
| + | android:orderInCategory="100" | ||
| + | android:showAsAction="ifRoom" | ||
| + | android:title="@string/action_settings"/> | ||
| + | <item | ||
| + | android:id="@+id/weather_question" | ||
| + | android:title="@string/action_weather" | ||
| + | android:showAsAction="ifRoom"> | ||
| + | </item> | ||
| + | |||
| + | </menu> | ||
| + | </source> | ||
| + | |||
| + | '''res/values/strings.xml''': | ||
| + | <source lang="xml"> | ||
| + | <?xml version="1.0" encoding="utf-8"?> | ||
| + | <resources> | ||
| + | |||
| + | <string name="app_name">SmartGuy</string> | ||
| + | <string name="hello_world">Hello world!</string> | ||
| + | <string name="action_settings">Settings</string> | ||
| + | <string name="question">Ask me anything?</string> | ||
| + | <string name="ask_button">Ask</string> | ||
| + | <string name="action_weather">Weather</string> | ||
| + | |||
| + | </resources> | ||
| + | </source> | ||
Viimane redaktsioon: 20. aprill 2015, kell 10:48
Tagasi ITI0011 lehele.
Eclilpse'i seadistamine
- Help -> Eclipse Marketplace ...
- Otsida "android"
- Valida tuleb: Android Development Tools for Eclipse by Google (nimekirjas 5. kohal)
- Installeerides küsitakse kinnitust (nagu ikka, "Accept", "OK" jne)
.
- Window
- Preferences
- Vasakult nimekirjast valida "Android"
- Valida kaust, kuhu android sdk pakiti
.
- Windows
- SDK manager
- Android 5.0 eest linnukesed ära (kui muidugi soovi ja aega, võib ka versioon 5 installeerida)
- Android 4.4 valida (võib välja jätta Glass, Google API ja näiteks Intel x86 Atom System Image).
- Install packages
- tõmbab alla vajalikud pakid
Android SDK
https://developer.android.com/sdk
GET THE SDK FOR AN EXISTING IDE (kui Eclipse või muu IDE on juba olemas).
Alternatiivne emulaator
Genymotion: http://www.genymotion.com/
Allalaadimiseks tuleb tasuta konto teha.
Mitte Windowsi kasutajad peavad VirtualBox'i lisaks installeerima.
Linke
- Android studio, IntelliJ IDEA jaoks: https://developer.android.com/sdk/installing/studio.html
Telefoni peal arendamine
Kui soovid oma telefoni peal katsetada app-e (tugevalt soovitatav, kuna palju kiirem), siis see on juhend -> http://www.phonearena.com/news/How-to-enable-USB-debugging-on-Android_id53909
Arendamine
- New ..
- Android
- New Android Application
- erinevad sammud, kus saab määrata nime, minimaalset androidi versiooni, kus programm töötab, ikooni jne.
- nimekirjast valida: Blank activity
- MainActivity ja activity_main sobivad
Käivitamine:
- Android Virtual Device tuleb luua: nimi, valida device jne.
- Võtab kaua aega
- run
- Android application
- Valik seadmetest (kui eelnevalt on loodud ja töötab), võib ka uue luua
- Kui device käib, ei ole vaja seda kinni panna. Hiljem saab muudetud rakendust käivitada juba käimas olevas seadmes.
Projekt genymotionis käima
- Kui genymotion töötab, siis saab käivitamise ajal valida käimas olevate seadmete seast genymotioni oma.
Failid (projekti all):
- src/ - lähtekood, java-failid
- res/ - ressursid
- res/layout/activity_main.xml - peamine layout
- res/values/strings.xml - sisaldab kõiki sõnesid, mis rakenduses kasutada saab
- AndroidManifest.xml - rakenduse seaded
XML-failide puhul saab sisuakna all olevate tab'idega valida kas lähtekood (tekstiline näitamine) või graafiline näitamine.
Koodinäide loengust
src/com/example/smartguy/MainActivity.java: <source lang="java"> package com.example.smartguy;
import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button askButton = (Button) findViewById(R.id.ask_button);
askButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { EditText questionField = (EditText) findViewById(R.id.question_field); String question = questionField.getText().toString(); TextView answerText = (TextView) findViewById(R.id.answer_text); ImageView answerImage = (ImageView) findViewById(R.id.answer_image); double random = Math.random(); if (random > 0.6) { answerText.setText("Probably Yes!"); answerImage.setImageResource(R.drawable.thumbs_down); } else { answerText.setText("No!"); answerImage.setImageResource(R.drawable.borat_thumbs_up); } } });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
EditText questionField = (EditText) findViewById(R.id.question_field);
int id = item.getItemId();
if (id == R.id.action_settings) {
questionField.setText("Are settings useful?");
return true;
} else if (id == R.id.weather_question) {
questionField.setText("Is it going to rain today?");
return true;
}
return super.onOptionsItemSelected(item);
}
} </source>
res/drawable/: siin on 2 pilti nimega: borat_thumbs_up.jpg ja thumbs_down.jpg (täiesti suvalised pildid sobivad)
res/layout/activity_main.xml: <source lang="xml"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.smartguy.MainActivity" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/question"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/question_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="text" >
<requestFocus /> </EditText>
<Button
android:id="@+id/ask_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/question_field"
android:layout_centerHorizontal="true"
android:text="@string/ask_button" />
<TextView
android:id="@+id/answer_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ask_button"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="@+id/answer_image"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@+id/answer_text"
android:layout_centerHorizontal="true" />
</RelativeLayout> </source>
res/menu/menu.xml: <source lang="xml"> <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" tools:context="com.example.smartguy.MainActivity" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="ifRoom"
android:title="@string/action_settings"/>
<item
android:id="@+id/weather_question"
android:title="@string/action_weather"
android:showAsAction="ifRoom">
</item>
</menu> </source>
res/values/strings.xml: <source lang="xml"> <?xml version="1.0" encoding="utf-8"?> <resources>
<string name="app_name">SmartGuy</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="question">Ask me anything?</string> <string name="ask_button">Ask</string> <string name="action_weather">Weather</string>
</resources> </source>