Below is an Android Activity
and corresponding XML layout
. The function of the class is to search an SQLite database
to show results to the user.
Can I get a review of the code quality? This is my first Android app so feedback would be greatly appreciated as I do not know standard Android/Java practices to aim for.
Activity Code:
package com.example.briapp;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* Activity that contains functionality for searching by average attention value
*
* @author Ross
*
*/
public class AttentionAverageSearch extends ActionBarActivity implements
View.OnClickListener {
// declare variables
TextView instruction;
EditText avgEntered;
TextView tvResults;
DatabaseHelper db = new DatabaseHelper(this);
String log;
Button sub;
List<Session> results;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.attentionaveragesearch);
initialiseVars();
}
/**
* Method that initialises variables for the activty and sets onclick
* listener for button
*/
public void initialiseVars() {
instruction = (TextView) findViewById(R.id.tvAverageAttSearch1);
avgEntered = (EditText) findViewById(R.id.etEnterSpecificAttAvgVal);
tvResults = (TextView) findViewById(R.id.tvAvgAttSearchResults1);
tvResults.setMovementMethod(new ScrollingMovementMethod());
sub = (Button) findViewById(R.id.btnAvgAttSearch);
sub.setOnClickListener(this);
}
/**
* On click listener for button(s)
*/
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnAvgAttSearch:
// validation to ensure a blank entry isn't given
if (avgEntered.getText().toString().length() < 1) {
Toast.makeText(getApplicationContext(),
"Please enter something !", Toast.LENGTH_LONG).show();
}
else {
// if valid
final String entry = avgEntered.getText().toString();
int avgValToSearch = Integer.parseInt(entry);
// /setting results equal to return value of search
results = db.getSpecificAverageAtt(avgValToSearch);
// hides keyboard when btn pressed
InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus()
.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
tvResults.setText(listToString(results));
}
break;
}
}
/**
* Method used to format the output of a List
*
* @param list
* @return String
*/
public static String listToString(List<?> list) {
String result = "";
for (int i = 0; i < list.size(); i++) {
result += "" + list.get(i) + "\n\n";
}
return result;
}
}
Corresponding XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/menubackground"
android:orientation="vertical" >
<TextView
android:id="@+id/tvAverageAttSearch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:layout_marginTop="7dp"
android:text="Enter Minimum Average value..."
android:textSize="25dp" />
<EditText
android:id="@+id/etEnterSpecificAttAvgVal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter value..."
android:inputType="number" >
</EditText>
<Button
android:id="@+id/btnAvgAttSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="6dp"
android:background="@drawable/circle2"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:text="Submit"
android:textSize="20sp" >
</Button>
<TextView
android:id="@+id/tvAvgAttSearchResults1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:maxLines="30"
android:minLines="5"
android:scrollbars="vertical"
android:text=" Results..."
android:textSize="17dp" />
</LinearLayout>