I've created this screen using the design mode screen design.

enter image description here

The XML for this is

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ftpDetails">

    <Button
        android:id="@+id/btnTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="11dp"
        android:layout_marginLeft="11dp"
        android:layout_marginBottom="16dp"
        android:text="@string/test"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <EditText
        android:id="@+id/txtPassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="96dp"
        android:layout_marginLeft="96dp"
        android:layout_marginTop="292dp"
        android:width="250dp"
        android:ems="10"
        android:importantForAutofill="no"
        android:inputType="textPassword"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:targetApi="o" />

    <EditText
        android:id="@+id/txtUsername"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="100dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="216dp"
        android:width="250dp"
        android:ems="10"
        android:importantForAutofill="no"
        android:inputType="text"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:targetApi="o" />

    <EditText
        android:id="@+id/txtFolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="96dp"
        android:layout_marginLeft="96dp"
        android:layout_marginTop="140dp"
        android:width="250dp"
        android:ems="10"
        android:importantForAutofill="no"
        android:inputType="text"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:targetApi="o" />

    <EditText
        android:id="@+id/txtServer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="96dp"
        android:layout_marginLeft="96dp"
        android:layout_marginTop="68dp"
        android:width="250dp"
        android:ems="10"
        android:importantForAutofill="no"
        android:inputType="text"
        android:textSize="12sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:targetApi="o" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="225dp"
        android:text="@string/username"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="300dp"
        android:text="@string/password"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintStart_toStartOf="@+id/textView4"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="17dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="150dp"
        android:text="@string/ftp_folder"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="75dp"
        android:text="@string/ftp_server"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/login_credentials"
        android:textAppearance="@style/TextAppearance.AppCompat.Body2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginBottom="16dp"
        android:text="@string/save"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</android.support.constraint.ConstraintLayout>

This question is based around the bottom part of the XML, with the button btnSave.

In my Java class for this activity, I have tried to create an onClickListener like so

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ftpDetails extends AppCompatActivity{
private TextView txtServer;
private TextView txtFolder;
private TextView txtUsername;
private TextView txtPassword;

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

    setContentView(R.layout.activity_ftp_details);
    setTitle(R.string.ftpTitle);

    txtServer = (TextView) findViewById(R.id.txtServer);
    txtFolder = (TextView) findViewById(R.id.txtFolder);
    txtUsername = (TextView) findViewById(R.id.txtUsername);
    txtPassword = (TextView) findViewById(R.id.txtPassword);

findViewById(R.id.btnSave).setOnClickListener(null);

    // Check that all text boxes have a value in them
    if (txtServer.getText().length() == 0)
    {
        // MESSAGE BOX
        AlertDialog.Builder msg = new AlertDialog.Builder(this);
        msg.setTitle("Enter Server");
        msg.setMessage("Please enter a server address.");
        msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Leave this blank, this will mean nothing happens, the msg just disappears
            }
        });
    }

    if (txtFolder.getText().length() == 0)
    {
        // MESSAGE BOX
        AlertDialog.Builder msg = new AlertDialog.Builder(this);
        msg.setTitle("Enter Folder");
        msg.setMessage("Please enter a folder to use.");
        msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Leave this blank, this will mean nothing happens, the msg just disappears
            }
        });
    }

    if (txtUsername.getText().length() == 0)
    {
        // MESSAGE BOX
        AlertDialog.Builder msg = new AlertDialog.Builder(this);
        msg.setTitle("Enter Username");
        msg.setMessage("Please enter your username.");
        msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Leave this blank, this will mean nothing happens, the msg just disappears
            }
        });
    }

    if (txtPassword.getText().length() == 0)
    {
        // MESSAGE BOX
        AlertDialog.Builder msg = new AlertDialog.Builder(this);
        msg.setTitle("Enter Server");
        msg.setMessage("Please enter a your password.");
        msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // Leave this blank, this will mean nothing happens, the msg just disappears
            }
        });
    }
  }
}

I have breakpoints on the lines

super.onCreate(savedInstanceState);

findViewById(R.id.btnSave).setOnClickListener(null);

and

if (txtServer.getText().length() == 0)

However, when I debug the app (Shift + F9) to my connected device (OnePlus 6T), none of the breakpoints are hit. Also, when I press the button and the textboxes are blank, the message alerts are not displayed.

Have I done something wrong? I've tried using the XML onClick: attribute but had the same result, as well as setting the same attribute in design mode, but again there was no change.

EDIT

Following the suggestions in the answers, I currently have this

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

    setContentView(R.layout.activity_ftp_details);
    setTitle(R.string.ftpTitle);

    txtServer = (TextView) findViewById(R.id.txtServer);
    txtFolder = (TextView) findViewById(R.id.txtFolder);
    txtUsername = (TextView) findViewById(R.id.txtUsername);
    txtPassword = (TextView) findViewById(R.id.txtPassword);

    findViewById(R.id.btnSave).setOnClickListener(btnSaveListener);
}

private View.OnClickListener btnSaveListener =new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // my code in here
        }
    }

But still I get no result.

EDIT 2

After further investigation, when I press the button, the logcat window displays the following information

2019-04-24 14:25:57.866 28031-28031/com.example.my_test_app W/RenderThread: type=1400 audit(0.0:425186): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=22761 scontext=u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0

2019-04-24 14:25:57.875 28031-28057/com.example.my_test_app E/libc: Access denied finding property "vendor.debug.egl.swapinterval"

2019-04-24 14:25:57.886 28031-28057/com.example.my_test_app E/libc: Access denied finding property "vendor.debug.egl.swapinterval"

2019-04-24 14:25:57.876 28031-28031/com.example.my_test_app W/RenderThread: type=1400 audit(0.0:425187): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=22761 scontext=u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0

2019-04-24 14:25:57.896 28031-28031/com.example.my_test_app W/RenderThread: type=1400 audit(0.0:425188): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=22761 scontext=u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0

2019-04-24 14:25:57.903 28031-28057/com.example.my_test_app E/libc: Access denied finding property "vendor.debug.egl.swapinterval"

2019-04-24 14:25:57.916 28031-28031/com.example.my_test_app W/RenderThread: type=1400 audit(0.0:425189): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=22761 scontext=u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0

2019-04-24 14:25:57.923 28031-28057/com.example.my_test_app E/libc: Access denied finding property "vendor.debug.egl.swapinterval"

2019-04-24 14:25:57.942 28031-28057/com.example.my_test_app E/libc: Access denied finding property "vendor.debug.egl.swapinterval"

2019-04-24 14:25:57.926 28031-28031/com.example.my_test_app W/RenderThread: type=1400 audit(0.0:425190): avc: denied { read } for name="u:object_r:vendor_default_prop:s0" dev="tmpfs" ino=22761 scontext=u:r:untrusted_app:s0:c129,c256,c512,c768 tcontext=u:object_r:vendor_default_prop:s0 tclass=file permissive=0

2019-04-24 14:25:57.954 28031-28057/com.example.my_test_app E/libc: Access denied finding property "vendor.debug.egl.swapinterval"

--------- beginning of system

2019-04-24 14:25:58.237 28031-28057/com.example.my_test_app I/chatty: uid=10129(com.example.my_test_app) RenderThread identical 17 lines

2019-04-24 14:25:58.252 28031-28057/com.example.my_test_app E/libc: Access denied finding property "vendor.debug.egl.swapinterval"

6 Answers

0
David On Best Solutions

In the end, it wasn't an issue with how I was setting my listenener.

Changing the activity call from my MainActivity startup class to StartActivity meant that the OnCreate code was called correctly.

0
Martin Zeitler On

.setOnClickListener(null) won't assign any OnClickListener; so how are you going to debug that event? and that .getText().length() probably should be .getText().toString().length() ... so that the condition could be met.

0
Nikos Hidalgo On

You're not setting the listener correctly. Add the following code:

Button btnSave = (Button) findViewById(R.id.btnSave);

btnSave.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //add your conditions here
    }
0
Luca Nicoletti On

Change this line:

findViewById(R.id.btnSave).setOnClickListener(null);

into

findViewById(R.id.btnSave).setOnClickListener(btnSaveListener);

and remove the followings lines from the onCreate method.

Then create btnSaveListener as follow:

private View.OnClickListener btnSaveListener =new View.OnClickListener() {

   @Override
   public void onClick(View v) {
      // here goes all the code belove the line you change in the method `onCreate`
   }
};
0
Tobibur Rahman On

why are you using click listener like this: findViewById(R.id.btnSave).setOnClickListener(null);

do this instead:

findViewById(R.id.btnSave).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //put all your textView logic here
            }
        });
0
Asif Rahman On

Your button click listener implementation is wrong, try like this:

 findViewById(R.id.action_ask).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // Check that all text boxes have a value in them
                if (txtServer.getText().length() == 0)
                {
                    // MESSAGE BOX
                    AlertDialog.Builder msg = new AlertDialog.Builder(this);
                    msg.setTitle("Enter Server");
                    msg.setMessage("Please enter a server address.");
                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Leave this blank, this will mean nothing happens, the msg just disappears
                        }
                    });
                }

                if (txtFolder.getText().length() == 0)
                {
                    // MESSAGE BOX
                    AlertDialog.Builder msg = new AlertDialog.Builder(this);
                    msg.setTitle("Enter Folder");
                    msg.setMessage("Please enter a folder to use.");
                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Leave this blank, this will mean nothing happens, the msg just disappears
                        }
                    });
                }

                if (txtUsername.getText().length() == 0)
                {
                    // MESSAGE BOX
                    AlertDialog.Builder msg = new AlertDialog.Builder(this);
                    msg.setTitle("Enter Username");
                    msg.setMessage("Please enter your username.");
                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Leave this blank, this will mean nothing happens, the msg just disappears
                        }
                    });
                }

                if (txtPassword.getText().length() == 0)
                {
                    // MESSAGE BOX
                    AlertDialog.Builder msg = new AlertDialog.Builder(this);
                    msg.setTitle("Enter Server");
                    msg.setMessage("Please enter a your password.");
                    msg.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Leave this blank, this will mean nothing happens, the msg just disappears
                        }
                    });
                }
            }
        });