0

Here is my code. On line 111 (machineOD_input.setOnClickListener) you can see where I am trying to clear the string when the user clicks on it to change the data, but once it's cleared, the "NEXT" option in the keyboard doesn't work anymore, it gets stuck at first input.

At the moment I'm just working on clearing 1st input, once it's working I will do the same for all input.

What am I doing wrong, please?

package com.example.beno;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.view.View;
import android.view.Window;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.text.InputType;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {

private EditText machineOD_input, pipeOD_input, pipe_length_input,     drive_length_input, muck_up_input, jacking_speed_input;

private TextView lub_result, litre_per_meter_result, volume_per_pipe_result, volume_for_drive_result;

public static final String SHARED_PREFS = "sharedPrefs";

public static final String TBMODINPUT = "machineOD_input";
public static final String PIPEODINPUT = "pipeOD_input";
public static final String PIPELENGTHINPUT = "pipe_length_input";
public static final String DRIVELENGTHINPUT = "drive_length_input";
public static final String MUCKUPINPUT = "muck_up_input";
public static final String JACKINGSPEEDINPUT = "jacking_speed_input";

private String tbmODinputText, pipeODinputText, pipeLengthinputText, driveLengthinputText, muckupinputText, jackingSpeedinputText;

@SuppressLint("DefaultLocale")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Objects.requireNonNull(getSupportActionBar()).setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.toolbar_title_layout);
    setContentView(R.layout.activity_main);

    machineOD_input = findViewById(R.id.machineOD_input);
    pipeOD_input = findViewById(R.id.pipeOD_input);
    pipe_length_input = findViewById(R.id.pipe_length_input);
    drive_length_input = findViewById(R.id.drive_length_input);
    muck_up_input = findViewById(R.id.muck_up_input);
    jacking_speed_input = findViewById(R.id.jacking_speed_input);
    lub_result = findViewById(R.id.lub_result);
    litre_per_meter_result = findViewById(R.id.litre_per_meter_result);
    volume_per_pipe_result = findViewById(R.id.volume_per_pipe_result);
    volume_for_drive_result = findViewById(R.id.volume_for_drive_result);
    Button calculate = findViewById(R.id.calculate);

    calculate.setOnClickListener(v -> {
        if (machineOD_input.getText().toString().length() == 0) {
            machineOD_input.setText("1");
        }
        if (pipeOD_input.getText().toString().length() == 0) {
            pipeOD_input.setText("1");
        }
        if (pipe_length_input.getText().toString().length() == 0) {
            pipe_length_input.setText("1");
        }
        if (drive_length_input.getText().toString().length() == 0) {
            drive_length_input.setText("1");
        }
        if (muck_up_input.getText().toString().length() == 0) {
            muck_up_input.setText("2.5");
        }
        if (jacking_speed_input.getText().toString().length() == 0) {
            jacking_speed_input.setText("1");
        }


        double pi = Math.PI / 4;

        double tbmODInput = Double.parseDouble(machineOD_input.getText().toString()) / 1000.0;
        double pipeODInput = Double.parseDouble(pipeOD_input.getText().toString()) / 1000.0;
        double muckUpInput = Double.parseDouble(muck_up_input.getText().toString());
        double jackingSpeedInput = Double.parseDouble(jacking_speed_input.getText().toString());
        double pipeLengthInput = Double.parseDouble(pipe_length_input.getText().toString());
        double driveLengthInput = Double.parseDouble(drive_length_input.getText().toString());

        // Calculate Volume per Meter (Ltr per meter)
        double sum = ((pi * (tbmODInput) * (tbmODInput)) - (pi * (pipeODInput) * (pipeODInput))) * muckUpInput * 1000;
        litre_per_meter_result.setText(String.format("%.0f", sum));

        // Calculate Lubrication Pump Capacity (Ltr per min)
        double sum2 = (jackingSpeedInput / 1000) * sum;
        lub_result.setText(String.format("%.0f", sum2));

        // Calculate Volume per pipe (M³ per pipe)
        double sum3 = (sum / 1000) * pipeLengthInput;
        volume_per_pipe_result.setText(String.format("%.3f", sum3));

        // Calculate Volume for drive (M³ for drive)
        double sum4 = (sum / 1000) * driveLengthInput;
        volume_for_drive_result.setText(String.format("%.0f", sum4));

        saveData();

    });

    machineOD_input.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (machineOD_input.getText().toString().length() > 0) {
                machineOD_input.setText("");
            }

        }
    });

    loadData();
    updateViews();

}

public void saveData(){
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();

    editor.putString(TBMODINPUT, machineOD_input.getText().toString());
    editor.putString(PIPEODINPUT, pipeOD_input.getText().toString());
    editor.putString(PIPELENGTHINPUT, pipe_length_input.getText().toString());
    editor.putString(DRIVELENGTHINPUT, drive_length_input.getText().toString());
    editor.putString(MUCKUPINPUT, muck_up_input.getText().toString());
    editor.putString(JACKINGSPEEDINPUT, jacking_speed_input.getText().toString());

    editor.apply();
}

public void loadData(){
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);

    tbmODinputText = sharedPreferences.getString(TBMODINPUT, "");
    pipeODinputText = sharedPreferences.getString(PIPEODINPUT, "");
    pipeLengthinputText = sharedPreferences.getString(PIPELENGTHINPUT, "");
    driveLengthinputText = sharedPreferences.getString(DRIVELENGTHINPUT, "");
    muckupinputText = sharedPreferences.getString(MUCKUPINPUT, "");
    jackingSpeedinputText = sharedPreferences.getString(JACKINGSPEEDINPUT, "");
}

public void updateViews(){
    machineOD_input.setText(tbmODinputText);
    pipeOD_input.setText(pipeODinputText);
    pipe_length_input.setText(pipeLengthinputText);
    drive_length_input.setText(driveLengthinputText);
    muck_up_input.setText(muckupinputText);
    jacking_speed_input.setText(jackingSpeedinputText);
}

}

Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • add `android:focusableInTouchMode="false"` to your `machineOD_input` in xml file – Sniffer Oct 26 '21 at 06:46
  • Then the keyboard doesn't show when user clicks on machineOD_input. – user2253720 Oct 26 '21 at 06:54
  • you have to do it programmatically or use button separately to clear Edittext – Sniffer Oct 26 '21 at 07:04
  • I'm doing it seperately like this: machineOD_input.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (machineOD_input.getText().toString().length() > 0) { machineOD_input.setText(""); } } }); – user2253720 Oct 26 '21 at 07:28
  • machineOD_input is your EditText, not a button. I suggest you, use a separate delete button to clear your edittext. If you still want to use EditText click listener then disable the Edittext focus after text-change then set click listener – Sniffer Oct 26 '21 at 07:30
  • Yes it's EditText, inputType="numberDecimal". I have 6 of them. User inputs numbers then app calculates the value. I save all inputs to sharedPreferences. If user wants to change one of the inputs Id'e like the previous input to disappear when they click on it, ready to input new value, rather than having to manoeuvre the cursor & delete one by one digit. I cannot have a clear button for every value. – user2253720 Oct 26 '21 at 07:42
  • Check [this](https://stackoverflow.com/a/63402813/5978440) – Sniffer Oct 26 '21 at 08:33
  • maybe i should use OnTouchListener instead of OnClickListener? – user2253720 Oct 26 '21 at 09:06

0 Answers0