Android App ep.101

My Network Reporter

For this project we are going to be creating a simple application using TextFields, EditText, and a TextWatcher.

This tutorial assumes you know basic information about widgets and Java programming

Application Purpose:

We want to discern if transmitting a file of a size A across a network with a speed B gives us a reasonable time in seconds.

Our size of the file size is measured in MiB and our network speed is measured in Mbps.

We know that 1 B = 8 b; 1 MiB = 220 B; 1 Mbps = 106 bps.

activity_main.xml Layout


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
    <TextView
android:id="@+id/textView"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingHorizontal="10dp"
android:text="@string/file_size_label"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="14sp" />
    <EditText
android:id="@+id/fileSizeInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/file_input_hint"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
    <TextView
android:id="@+id/networkSpeedLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/network_label"
android:textAlignment="center"
android:textSize="14sp" />
    <EditText
android:id="@+id/networkSpeedInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/network_hint"
android:inputType="number" />
</LinearLayout>
<TextView
android:id="@+id/resultDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Input is needed for both File Size and Network Speed to estimate time"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="18sp" />

Here we kept it as simple as possible using two linear layouts to block each input and label and a separate TextView to display a result.

On our Java side…


public class MainActivity extends AppCompatActivity {
EditText fileSize;
EditText networkSpeed;
TextView result;
     // Here we use a TextWatcher to see if there are any changes.
TextWatcher watchChange = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
        @Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(fileSize.getText().toString().equals("") || networkSpeed.getText().toString().equals("")){
result.setText("Input is needed for both File Size and Network Speed to estimate time");
}
else {
// this section calculates the time to send.
Double myb = Integer.parseInt(fileSize.getText().toString()) * Math.pow(2, 20) * 8;
Double mybps = Integer.parseInt(networkSpeed.getText().toString()) * Math.pow(10, 6);
result.setText(String.format("%.1f", myb / mybps) + " seconds");
}
}
@Override
public void afterTextChanged(Editable editable) {}
};
    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
         // this section assigns our java code to the xml android widgets
fileSize = (EditText) findViewById(R.id.fileSizeInput);
networkSpeed = (EditText) findViewById(R.id.networkSpeedInput);
result = (TextView) findViewById(R.id.resultDisplay);

//this assigns the TextWatcher to both entry fields
fileSize.addTextChangedListener(watchChange);
networkSpeed.addTextChangedListener(watchChange);
}
}

For more on TextWatchers visit Android documentation here : https://developer.android.com/reference/android/text/TextWatcher

Medium: Article: