so this app is for me to track my bicycle rides. It is my first project using fragments. I want to see on fragment “gesamt” all bikerides i did over the year (kilometer and altitude).
So i created two progressbars (the max of the progressbars are the data from last year), and a surface to add racing bicycle and a surface to add mountainbike rides.
Once you enter your kilometer and altitude for, let´s say racing bicycle 500/5000 and press the add button
a TextView under the progressbar should display 500 / max and 5000 / max.
Code:
package com.example.fahrradapp;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Gesamt extends Fragment {
ProgressBar progressBarkm, progressBarhm;
Button btnrradd, btnmtbadd, btnsave;
EditText kmrr, hmrr, kmmtb, hmmtb;
TextView percentkm, percenthm;
//This app is to track the kilometer and altitude difference i made over the year.
@SuppressLint("SetTextI18n")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.gesamt, container, false);
progressBarkm = v.findViewById(R.id.progressbarkm);
progressBarhm = v.findViewById(R.id.progressbarhm);
btnrradd = v.findViewById(R.id.btnrradd);
btnmtbadd = v.findViewById(R.id.btnmtbadd);
btnsave = v.findViewById(R.id.btnsave);
kmrr = v.findViewById(R.id.kmrr);
hmrr = v.findViewById(R.id.hmrr);
kmmtb = v.findViewById(R.id.kmmtb);
hmmtb = v.findViewById(R.id.hmmtb);
percentkm = v.findViewById(R.id.percentkm);
percenthm = v.findViewById(R.id.percenthm);
//Shows the progress of both progressbars (for kilometer and altitude)
//At the start of the app it should display 0 /2492 and 0 / 37557
int progressgesamtkm = progressBarkm.getProgress();
percentkm.setText(progressgesamtkm + " / " + progressBarkm.getMax());
int progressgesamthm = progressBarhm.getProgress();
percenthm.setText(progressgesamthm + " / " + progressBarhm.getMax());
//Button for the bike type racing bicycle
btnrradd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int rrkmz1 = Integer.parseInt(kmrr.getText().toString()); //get the entered KM to string
int rrkmz2 = progressBarkm.getProgress(); //get the progress of KM bar
int rrkmz3 = rrkmz1 + rrkmz2; //add both
try {
percentkm.setText(rrkmz3 + " / " + progressBarkm.getMax()); //change the TextView under progressbar from 0 /2492 to the new value
} catch (NumberFormatException e){
Toast.makeText(getActivity(), "Fail", Toast.LENGTH_SHORT).show();
}
int rrhmz1 = Integer.parseInt(hmrr.getText().toString()); //same code like above but for altitude
int rrhmz2 = progressBarhm.getProgress();
int rrhmz3 = rrhmz1 + rrhmz2;
try {
percenthm.setText(rrhmz3 + " / " + progressBarhm.getMax());
} catch (NumberFormatException e){
Toast.makeText(getActivity(), "Fail", Toast.LENGTH_SHORT).show();
}
progressBarkm.setProgress(rrkmz3);
progressBarhm.setProgress(rrhmz3);
kmrr.setText(null);
hmrr.setText(null);
}
});
//this is exactly the same button like "btnrradd" but for mountainbikes
//also, this button crashes the app
btnmtbadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int mtbkmz1 = Integer.parseInt(kmmtb.getText().toString());
int mtbkmz2 = progressBarkm.getProgress();
int mtbkmz3 = mtbkmz1 + mtbkmz2;
try {
percentkm.setText(mtbkmz3 + " / " + progressBarkm.getMax());
} catch (NumberFormatException e){
Toast.makeText(getActivity(), "Fail", Toast.LENGTH_SHORT).show();
}
int mtbhmz1 = Integer.parseInt(hmrr.getText().toString());
int mtbhmz2 = progressBarhm.getProgress();
int mtbhmz3 = mtbhmz1 + mtbhmz2;
try {
percenthm.setText(mtbhmz3 + " / " + progressBarhm.getMax());
} catch (NumberFormatException e){
Toast.makeText(getActivity(), "Fail", Toast.LENGTH_SHORT).show();
}
progressBarkm.setProgress(mtbkmz3);
progressBarhm.setProgress(mtbhmz3);
kmmtb.setText(null);
hmmtb.setText(null);
}
});
//not implemented yet, i want to safe the values of both TextViews under the progressbars.
btnsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
return v;
}
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:background="#EEf5DB"
>
<TextView
android:id="@+id/kmgesamt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#FE5F55"
android:gravity="center"
android:text=" Kilometer gesamt "
android:textColor="@color/black"
android:textSize="25sp"
android:textStyle="italic"></TextView>
<ProgressBar
android:id="@+id/progressbarkm"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleY="1.5"
android:progress="0"
android:max="2492"
android:layout_marginTop="10dp"
android:progressTint="#4F6367"
android:background="#B8D8D8"
android:progressDrawable="@drawable/progressbarkm"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textStyle="italic"
android:layout_gravity="center"
android:textColor="@color/black"
android:id="@+id/percentkm"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/hmgesamt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Höhenmeter gesamt "
android:textSize="25sp"
android:textColor="@color/black"
android:layout_gravity="center"
android:gravity="center"
android:background="#FE5F55"
android:textStyle="italic"
android:layout_marginTop="20dp">
</TextView>
<ProgressBar
android:id="@+id/progressbarhm"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleY="1.5"
android:progress="0"
android:max="37557"
android:layout_marginTop="10dp"
android:progressTint="#4F6367"
android:background="#B8D8D8"
android:progressDrawable="@drawable/progressbarkm"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22sp"
android:textStyle="italic"
android:layout_gravity="center"
android:textColor="@color/black"
android:id="@+id/percenthm"
android:layout_marginTop="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#7A9E9F"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="KM Rennrad"
android:layout_marginLeft="20dp"
android:textColor="@color/black"
android:id="@+id/kmrr"
android:inputType="number">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="HM Rennrad"
android:layout_marginLeft="20dp"
android:textColor="@color/black"
android:id="@+id/hmrr"
android:inputType="number">
</EditText>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Add"
android:textColor="@color/black"
android:layout_marginLeft="40dp"
android:layout_marginRight="50dp"
android:backgroundTint="#7A9E9F"
android:textSize="18sp"
android:id="@+id/btnrradd"
>
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#7A9E9F"
android:layout_marginTop="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="KM MTB"
android:layout_marginLeft="20dp"
android:textColor="@color/black"
android:id="@+id/kmmtb"
android:inputType="number">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:hint="HM MTB"
android:layout_marginLeft="20dp"
android:textColor="@color/black"
android:id="@+id/hmmtb"
android:inputType="number">
</EditText>
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Add"
android:textColor="@color/black"
android:layout_marginLeft="40dp"
android:layout_marginRight="50dp"
android:backgroundTint="#7A9E9F"
android:textSize="18sp"
android:id="@+id/btnmtbadd"
>
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="#7A9E9F"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="Save"
android:textColor="@color/black"
android:backgroundTint="#7A9E9F"
android:textSize="20sp"
android:id="@+id/btnsave"
android:layout_gravity="center"
android:layout_marginTop="20dp">
</Button>
</LinearLayout>
In my mind the btnmtbadd should work perfectly fine since the btnrradd has basically the same code and works.
But if i enter values for KM and HM for my mountainbike and press the add button the app crashes.
If i do the same for racing bicycle it shows exactly what it should show.