Basic GUI elements - exercises
https://developer.android.com/studio/index.html
LIVE DEMO
Please create a simple application, which will be used to calculate the area of basic geometric figures.
To add images to the drawable forlder, right click on it, and select the Show in Explorer option. Copy images to the opened drawable folder.
<?xml version="1.0" encoding="utf-8"?> <TableLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TableRow> <ImageView android:id="@+id/img" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/square" /> <TextView android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="square area" android:layout_marginleft="20dp" android:layout_margintop="5dp" android:textsize="20dp" /> </TableRow> </TableLayout>
Create a new activity by chosing the right option from the context menu.
Name the new activity and click finish
package pl.tomaszx.geometry; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.textview; public class MainActivity extends AppCompatActivity { TextView t1; @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } } t1 = (TextView)findViewById(R.id.textView1); View.OnClickListener listener = new View.OnClickListener() { @Override public void onclick(view v) { Intent i = new Intent(getApplicationContext(), SquareActivity.class); startactivity(i); } }; t1.setonclicklistener(listener);
Add some components (widgets) to the Square Activity to recreate the view shown on the left. Usefull ones are: TextView EditText Button
package pl.tomaszx.geometry; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; public class SquareActivity extends AppCompatActivity { Button calculate; TextView result; EditText side; @Override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_square); calculate = (Button) findviewbyid(r.id.button1); side = (EditText) findviewbyid(r.id.edittext1); result = (TextView) findviewbyid(r.id.textview1); } } calculate.setonclicklistener(new View.OnClickListener() { @Override public void onclick(view v) { result.settext((double.parsedouble(side.gettext().tostring()) * Double.parseDouble(side.getText().toString())) + ""); } });
Write the following applications using the basic GUI elements (Button, TextView): To simulate a flashlight (by changing the background color of the activity into one of 4 colors). To convert the temperature between Celsius and Kelvin and Fahrenheit To calculate average fuel combustion per 100 km and its cost.
Remember to add: implements OnClickListener
Log hierarchy: Log.v(String, String) - verbose Log.d(String, String) - debug Log.i(String, String) - information Log.w(String, String) - warning Log.e(String, String) - error
Write the following applications: "Guess my number" game. The program draws a number from the range of 2000 to 2000 and the user's task is to guess it (based on "too much", "too little"). The program also displays the number of attempts. Program to calculate the roots of a square equation. Try to secure your applications using: http://developer.android.com/reference/android/text/t extutils.html (isempty method) Integer.parseInt or other standard java methods
Write an application that calculates the BMI. Use the following formula: BMI = weight height 2 [ kg m 2] For example, for 80 kg weight and 1.76 m height the BMI is: BMI = 80 1,76 2 = 80 3,0976 = 25,82 After clicking on a "Calculate" button, display the result to the user.
Write a simple calculator that adds two numbers. Display the result in 3 couting systems: Decimal Binary Hexadecimal