This is the second tutorial for the UAT Tester app, in case you missed the first Tutorial: the UAT Tester app is an application which retrieves data from the device to help test managers or developers more easily to debug issues raised by people outside the project team. To read more about it, please see Part I of this tutorial serie.
In the first tutorial we have set the layout of our UAT Tester application with several android widgets and we build the main.xml file (in the res folder). In the second tutorial we will build through Java the references to these layout items. Also we will set the text in these views based on the Android API’s; meaning we will set information from the device in these widgets (TextViews).
Open you Java.class via src folder in your Android project. In the code block below your find the references to the widgets we have set in our main.xml. As explaining each of them will not add much value, I will discuss one reference as an example:
final TextView tvsoftwarebrand = (TextView)findViewById(R.id.tv_softwarebrand);
As you remember we have one output field in the application which retrieves the softwarebrand of the device, we called this TextView tv_softwarbrand. (see tutorial part 1). So we need to refer to TextView (an existing class in Android) and give it a variable name (in this case tvsoftwarebrand) subsequently we need to refer it to the place in our project where we have defined the TextView findViewById(R.id.tv_softwarebrand).
As you can see in the code-block below I did the same for the EditText, Spinner, Buttons and the ImageView.
public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView tvsoftwarebrand = (TextView)findViewById(R.id.tv_softwarebrand); final TextView tvdevicebrand = (TextView)findViewById(R.id.tv_devicebrand); final TextView tvmodeltype = (TextView)findViewById(R.id.tv_modeltype); final TextView tvandroidversion = (TextView)findViewById(R.id.tv_androidversion); final TextView tvscreensize = (TextView)findViewById(R.id.tv_screensize); final TextView tvtimestamp = (TextView)findViewById(R.id.tv_timestamp); final EditText etexpected = (EditText)findViewById(R.id.et_expected); final EditText etactual = (EditText)findViewById(R.id.et_actual); final EditText etcomments = (EditText)findViewById(R.id.et_comments); final Spinner priolist = (Spinner)findViewById(R.id.sp_priority); thumbnailscr = (ImageView)findViewById(R.id.iv_thumbscr); Button btnscreenshot = (Button)findViewById(R.id.btn_screenshot); Button btnsend = (Button)findViewById(R.id.btn_email);
The next step is that we retrieve the proper information from the device API and set this to the proper TextViews. As you can see below we use the Build class within Android to retrieve Brand software, Manufacturer, Model and Version Release. For example this would mean Vodafone Netherlands software, Samsung, Galaxy Nexus, Android 4.1.1.
We simple use the variable we created above for each of the TextViews and use .setText to create a label + the data from the Build class; and then set it to a String value.
tvsoftwarebrand.setText("Branded software name: " + Build.BRAND.toString()); tvdevicebrand.setText("Device Brand: " + Build.MANUFACTURER.toString()); tvmodeltype.setText("Model type " + Build.MODEL.toString()); tvandroidversion.setText("Android Version: " + Build.VERSION.RELEASE.toString());
The next step is that we set the timestamp, set the list for selecting the priority and retrieving the screensize of the device.
We can create a new Time object through Time.getCurrentTimezone() and set it to the TextView tvtimestamp.
- The format can be set in different ways I used: %H:%M:%S for Hour, Minutes, Seconds
- Another thing you probably noticed is that I created a separate variable Month which adds 1 to the Time variable. The reason is that somehow the class returns a 0 for January instead of 1. I’m not sure if this is a mistake in Java, or that I’m making a mistake. Anyhow I solved it by incrementing the month value with +1 (lol).
The list with priorities can be set via an ArrayAdapter with a build-in view simple_list_item_1. As you can see it refers to another variable “priorities” which we didn’t defined yet. Therefore, add the following Array variable to your variables on the top of your class.
String [] priorities = {"No priority", "Cosmetical", "Minor", "Major", "Urgent" };
Finally you can retrieve the screensize via the method DisplayMetrics() and call withPixels and heightPixels
Time time1 = new Time(Time.getCurrentTimezone()); time1.setToNow(); int month = time1.month + 1; tvtimestamp.setText("Timestamp: " + time1.format("%H:%M:%S") + " "+ time1.monthDay + "/"+ month + "/"+ time1.year); priolist.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1 ,priorities)); DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); tvscreensize.setText("The screensize is: " + (displaymetrics.widthPixels + " x " + displaymetrics.heightPixels).toString());
At this stage we have made our layout of the app via the main.xml (see tutorial part I) and created our reference variables in Java plus set data to our TextViews via various Android and Java classes.
In the next tutorial we will start creating the OnClickListener for getting the screenshot from the Gallery. Click here for the part III.