Android : Add a listener to an icon in a text field

Viewed 4805

i am a beginner in application development and i like to ask a question that i tried my best to get an answer in google but i failed. So,

in my application (in java) i am using too text fields :

  <com.google.android.material.textfield.TextInputLayout

        android:layout_marginBottom="20dp"
        android:id="@+id/start_date_Layout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="20dp"
        app:helperText="Start Date"
        app:endIconMode="custom"
        app:endIconDrawable="@drawable/ic_date"

        >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/start_date"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="dd/mm/yy"

            />

    </com.google.android.material.textfield.TextInputLayout>

I added a listener to my end icon like that :

this.startDateLayout = v.findViewById(R.id.start_date_Layout);
this.startDateLayout.setEndIconOnClickListener(this);

My problem is when i try to get the view that the user clicked :

 @Override
public void onClick(View v) {
    
    if (v.getParent() == this.startDateLayout){
       
        Toast.makeText(this.context, "click", Toast.LENGTH_LONG).show();
    }
    
}

the toast never appear and the if condition is never true, my question is how can i get the view on witch the user clicked. thanks for reading i hope i was clear as much as possible.

6 Answers

For me only doing following works:

final TextInputLayout textInputLayout = findViewById(R.id.start_date_Layout);
textInputLayout.setEndIconOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // DO STUFF
    }
});

As you're explicitly setting listener on End icon only you shouldn't be worried about verifying viewId.

Just use:

startDateLayout = v.findViewById(R.id.start_date_Layout);
startDateLayout.setEndIconOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // DO something....
    }
});

First what I can notice in the above code, you're trying to listen to click events on your View

A simple way to do so is change your onClick() method to below

public void onClick(View v) {
    
    if (v.getId() == R.id.start_date_Layout){
       
        Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG).show();
    }
    
}

I believe I could help

If you are using EditText, then try setting an onFocusChangeListner to the editText instead of onClickListner.

startDateLayout.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus) {
       // Show your code when has focus
    } else {
       // when it does not have focus
    }
}});

The question is simple, You can you a FragmeLayout or LinearLayout, EditText and an ImageView for achieving the solution, it will be similar to the TextInputEditText but you will have much more control over View Hierarchy, let me show you how you can achieve it, for the sake of simplicity as you are a beginner I'll use linear Layout

Create A FrameLayout or LinearLayout and then create an EditText and an ImageView like

XML:

<LinearLayout
    android:layout_margin="10dp"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="#DCDFDF"
    android:orientation="horizontal"
    tools:ignore="MissingConstraints">


    <ImageView
        android:layout_gravity="center_vertical"
        android:id="@+id/imageViewTitle"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:src="@drawable/android"
        tools:ignore="ContentDescription" />

    <EditText
        android:layout_gravity="center_vertical"
        android:id="@+id/editTextTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:background="#DCDFDF"
        android:gravity="top"
        android:hint="Start Date"
        android:paddingTop="10dp"
        android:textAlignment="gravity"
        android:importantForAutofill="no" />

</LinearLayout>

Java

public class MainActivity extends AppCompatActivity {
    EditText editTextTitle;
    ImageView imageViewTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editTextTitle = findViewById(R.id.editTextTitle);
        imageViewTitle = findViewById(R.id.imageViewTitle);

          imageViewTitle.setOnClickListener(new View.OnClickListener() {
          @Override
           public void onClick(View view) {
             //Do your work here
             Toast.makeText(MainActivity.this, "ImageView Pressed", Toast.LENGTH_SHORT).show();
           }
       });
    }
}

Simple kotlin:

    b.dateLabel.setEndIconOnClickListener {
        Toast.makeText(requireContext(), "date", Toast.LENGTH_SHORT).show()
    }
Related