I have an ImageView for which I wanted to implement the onClickListener. But when I click on the image, nothing happens. Event the Logcat does not show any errors. Something is happening with the clickability attribute.
My XML code:
<ImageView
android:id="@+id/img2"
android:layout_width="104dp"
android:layout_height="126dp"
android:layout_weight="1"
android:padding="8dp"
android:scaleType="centerCrop"
android:clickable="true"
android:src="@drawable/ic_person_black_24dp"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="-3dp"
tools:layout_editor_absoluteY="14dp"
android:focusable="true" />
Code in Fragment:
public class DialogFrag extends DialogFragment {
private ImageView img2;
private static final int IMAGE_PICK_CODE = 1000;
private final int CODE_MULTIPLE_IMG_GALLERY = 2;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@NonNull
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_layout, container, false);
img2 = (ImageView) view.findViewById(R.id.img2);
img2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, IMAGE_PICK_CODE);
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int CODE_IMG_GALLERY = 1;
if (requestCode == CODE_IMG_GALLERY && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
if(imageUri!=null){
img2.setImageURI(imageUri);
}
}
if (requestCode == CODE_IMG_GALLERY && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
if(imageUri!=null){
img2.setImageURI(imageUri);
}
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
return builder
.setTitle("Заполните анкету")
.setView(R.layout.dialog_layout)
.setPositiveButton("OK", null)
.setNegativeButton("Отмена", null)
.create();
}
}
Where is my mistake? Help please)
Please override the onViewCreated() and use the view object
kotlin code:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
img2 = view.findViewById(R.id.img2);
// Implement onclicklistener
}
This should be of any help I guess.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.