Sei sulla pagina 1di 39

Practical Works:

Basic Elements of Android


Part 1
Design with Palettes
Create New Project
Filename: ViewElements
• Select small size picture
• Approximately size 500x300, not too big
• Check the size when you google them.
• Copy the picture files into the drawable folder
Activity_main.xml
MainActivity.Java
Create New Project
Filename: ToastElement
Create New Project
Filename: CheckBoxToast
Create New Project
Filename: RadioButton
Radio Button & Radio Group Button
In Android, RadioButton are mainly used together in a 
RadioGroup. In RadioGroup checking the one
radio button out of several radio button added in it will
automatically unchecked all the others. It means at
one time we can checked only one radio button from a
group of radio buttons which belong to same radio
group.
Create New Project
Filename:
RatingBar
Copy Paste this XML codes into
activity_main.xml
<TimePicker
android:id="@+id/simpleTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#090"
android:padding="20dp"
android:timePickerMode="spinner"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.253" />
</androidx.constraintlayout.widget.ConstraintLayout>
Add TextView and
Adjust position
Copy Paste this fragment of code if
you have difficulties in doing the
codes of timepicker.
myTimePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// display a toast with changed values of time picker
Toast.makeText(getApplicationContext(), hourOfDay + " " + minute, Toast.LENGTH_SHORT).show();
mytime.setText("Time is :: " + hourOfDay + " : " + minute); // set the current time in text view
}
});
EXERCISE
• Explore ProgressBar widget.
• Create small program that show at least ONE element
ProgressBar
• Run your program and show to your lecturer
• Need to explain how the program / codes work.
Template Coding in Java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
Exercise : Write the Java codes for the following design

ImageView
ImageView1
TextView
Button1

TextView1
Button1 Button1
ImageView2
Button2 Button2 Button2

TextView2

Click Button1 show in TextView INTI Button1 text = my picture Button1 text = Rate This Picture
Click Button2 show in TExtView FIT Button2 text = my pet Button2 text = Rate This Picture
Click Button1 show your picture in imageView Click Button1 show rating in textview1
Click Button2 show your pet in imageView Click Button2 show rating in textview2
The End

Potrebbero piacerti anche