9

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.

Khalil Ktiri
  • 181
  • 1
  • 6

6 Answers6

23

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.

Try this
  • 511
  • 3
  • 8
2

Just use:

startDateLayout = v.findViewById(R.id.start_date_Layout);
startDateLayout.setEndIconOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // DO something....
    }
});
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
1

Simple kotlin:

    b.dateLabel.setEndIconOnClickListener {
        Toast.makeText(requireContext(), "date", Toast.LENGTH_SHORT).show()
    }
Fortran
  • 2,218
  • 2
  • 27
  • 33
0

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

Mab
  • 412
  • 3
  • 12
  • Still don't work, i think because the icon in the text field layout is another view, i tried to get the string value of the view and i get that : com.google.android.material.internal.CheckableImageButton{6bb58e VFED..C.. ...P.... 8,3-104,99 #7f090125 app:id/text_input_end_icon} – Khalil Ktiri Sep 04 '20 at 12:30
0

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
    }
}});
0

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();
           }
       });
    }
}
Ali
  • 519
  • 4
  • 13
  • You can obviously and to be honest with a minor effort you can achieve more from this than TextInputEditText. – Ali Sep 04 '20 at 12:25
  • Thank you so much for taking your time to answer me, but i prefer to use a text field due to all his functionalities that a simple Edit text dont. – Khalil Ktiri Sep 04 '20 at 12:26