ITI0011:Android praktikum 1
Lähtekood
23. praktikumis tehtud kood: Meedia:ITI0011-Android-FirstApp.zip
Põhilised koodifailid
MainActivity.java
<source lang="java"> package ee.ttu.firstapp;
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; 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 {
private EditText questionField; private Button askButton; private TextView responseText; private ImageView responseImage;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
Log.d("MainActivity", "ON CREATE");
setContentView(R.layout.activity_main);
questionField = (EditText) findViewById(R.id.questionField); askButton = (Button) findViewById(R.id.askButton); responseText = (TextView) findViewById(R.id.responseText); responseImage = (ImageView) findViewById(R.id.responseImage); }
public void ask(View view) { String question = questionField.getText().toString();
Log.d("MainActivity", "Question: " + question);
if (Math.random() < 0.5) { responseText.setText("Nope!"); responseImage.setImageResource(R.drawable.grumpycat_nope); } else { responseText.setText("Hell yeah!"); responseImage.setImageResource(R.drawable.thumbs_up); } }
public void showSecond(View view) { Intent intent = new Intent(this, SecondActivity.class);
String question = questionField.getText().toString();
intent.putExtra("küsimus", question);
startActivity(intent); }
}
</source>
SecondActivity.java
<source lang="java"> package ee.ttu.firstapp;
import android.app.Activity; import android.os.Bundle; import android.widget.TextView;
public class SecondActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView placeholder = (TextView) findViewById(R.id.placeholder); String question = getIntent().getStringExtra("küsimus");
placeholder.setText(question); } }
</source>
AndroidManifest.xml
<source lang="xml"> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ee.ttu.firstapp" >
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" 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=".SecondActivity"/> </application>
</manifest>
</source>
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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Ask a question" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignParentStart="true" android:layout_alignParentEnd="true"/>
<EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/questionField" android:layout_below="@+id/textView" android:layout_alignParentStart="true" android:layout_alignParentEnd="true" android:hint="Question"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ask" android:id="@+id/askButton" android:layout_below="@+id/questionField" android:layout_centerHorizontal="true" android:onClick="ask"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Response will be here" android:id="@+id/responseText" android:layout_below="@+id/askButton" android:layout_centerHorizontal="true"/>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/responseImage" android:layout_below="@+id/responseText" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" android:layout_alignParentStart="true" android:scaleType="fitCenter" android:onClick="showSecond"/>
</RelativeLayout>
</source>
res/layout/activity_second.xml
<source lang="xml"> <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffae5646">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Placeholder" android:id="@+id/placeholder" android:layout_centerVertical="true" android:layout_centerHorizontal="true"/>
</RelativeLayout> </source>
res/layout-land/activity_second.xml
<source lang="xml"> ´<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff5391ae">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Another placeholder" android:id="@+id/placeholder" android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/>
</RelativeLayout> </source>