Lab4 #4 ImageView and onClickListener

To be familiarized with

  • Button and setOnClickListener
  • RatingBar
  • ImageView


Step 1: Download 2 pictures from
https://www.dropbox.com/s/20l9y6tihmwiwi5/imagesForRatingBarExample.zip?dl=0


Step 2: Copy and paste the 2 pictures into res>drawable folder



Step 3: Remember to rename the image file  name to lowercase (only jpg and png format are accepted and file size of image should not be too big.

Reducing image download sizes
Most download traffic consists of images. As a result, the smaller you can make your downloadable images, the better a network experience your app can provide for users. This page provides guidance on making image files smaller and more network-friendly.


Please Note the image name can only include lowercase a-z, 0-9, or underscore, otherwise there will have an error when you rebuild the android project. The error message like ” Error: ‘-‘ is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore ”
Reference

Step 4: Add a submit button
Step5 : Add java coding to MainActivity.java


Use Intellisense to to import classes for those code in Red
Just move your cursor near the red coloured text, and press ALT+Enter > Import class
as shown above.

Here are the partial codes given to you


//#1 Declare a ratingBar object of RatingBar class

RatingBar ratingBar; 

 

//#2 binding the xml view with java ratingBar using its id, R.id.ratingBar1
ratingBar = (RatingBar) findViewById(R.id.ratingBar1); 
 

Bind Java Button with xml GUI component btnSubmit
//#3 binding the xml view with java button using its id, R.id.btnSubmit
Button button = (Button)findViewById(R.id.btnSubmit);


This is a OnClickListener for button btnSubmit
 //#4 Button click Listener
button.setOnClickListener(new View.OnClickListener(){
     public void onClick(View v){



            }

        });

Toast is to show/display text on the screen for a short duration of time Toast.LENGTH_SHORT

//#5 Show the rating value on Toast
               Toast.makeText(MainActivity.this,"Movie Rating is "+ String.valueOf(ratingBar.getRating()),
Toast.LENGTH_SHORT).show(); 

Step 6 : Run the App to see the output results



Demo and explanation of codes

No comments:

Post a Comment

Note: only a member of this blog may post a comment.