Clickable view / onclick: Correct approach to make View clickable?

The following code will make the View clickable, but I am wondering if this is the correct approach to make a custom view clickable?


public class NodePickup extends LinearLayout
 public NodePickup(Context context, AttributeSet attributeSet)
  super(context, attributeSet);

  LayoutInflater inflater = (LayoutInflater)     context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.nodepickup, this);

        this.setOnClickListener(new OnClickListener() 
   public void onClick(View v)
          AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
          .setPositiveButton("JA!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) 

Asked by: Agata332 | Posted: 24-01-2022

Answer 1

The code in onClick() is simply creating the dialog - there's nothing there that would cause it to get displayed on screen. To make this work, call showDialog(int) in your click handler and implement onCreateDialog(int) in your activity.

Check out the Creating Dialogs section of the Android docs for more information.

Answered by: Lyndon864 | Posted: 25-02-2022

Answer 2

Calling setOnClickListener() is the appropriate way of making a view clickable.

Answered by: Sophia174 | Posted: 25-02-2022

Similar questions

android - I need a clickable UI element instead of Toast (can i create custom toast with clickable feature?)

I am developing a RSS news feed app for Android, and using Toast.makeText() to show the news or current update. It must go on web on the click of that message/news. The problem is that the Toast has no API/features that I can click on that and go on web to read the full article. The prime requirement is that I need to display the news one by one so I can not use list or any other widget.

html - Android TextView - Clickable Links and Clickable View

I have a TextView, in which I am putting some HTML content using Html.fromHtml(). The TextView has an onClick() method, which is used to perform several actions. Sometimes the HTML that is in the TextView contains links, which are recognised as links, but are unclickable. I can make them clickable, but then that stops the onClick method. My question is, is there a way that, if the user clicks on a link then the lin...

android - How do I add a clickable button to a textview that is clickable currently

New to android trying to figure something out, any help appreciated.I have a textview which is currently clickable. I just need to put a clickable button background to it. This is my xml file: mainscreen.xml under layout <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:divider="@android:drawable/divider_horizontal" ...

android - How to make markers clickable and clusters not clickable?

This question already has answers here:

android - How to have a clickable link in a textview, within a custom listview and still have clickable listview

I have a "setOnItemClickListener" for a custom listview that I made. When the row is clicked, a method runs. That works. I am able to get a link to be clickable in the textview from within the listview by using this... android:autoLink="web" But then the row becomes unclickable. Any row that does not have a link in it can be clicked. The autoLink is overriding the "setOnItemClickListener" ...

java - Make Layout Non Clickable But Children Clickable

I've been working on a system overlay and I found that moving my imageview around the screen is a hassle using WindowManager.LayoutParams x and y, so I've decided to make a RelativeLayout with the height of the device to hold my imageview. The only problem is I want to make the Layout non clickable so that the activity below it can be clicked, but not the imageview itself as it launches an activity. Is there anyway to do t...

android - How to make a child view not clickable inside a clickable container

I have a layout container like described below: <ConstraintLayout android:id="@+id/container" ...> <child1/> <child2/> <child3/> </ConstraintLayout> In my code, I have to attach a click listener to the container to be able to execute an action when the user clicks on any area of the container. What is the best solution to exclude the child2 fr...

python - How can I find an element with a class that has a text attrb but is not clickable within a main class that is clickable but has no text attrb

I was trying to find a way to click on an element from a class/ID that has the clickable attribute TRUE but has no other attribute to be differentiated from the others. The said element has another class/ID with the attribute text but has clickable is FALSE. How can I do this using Selenium with Python? How can I use 2 different classes to iterate based on the attribute text? <Class/I...

Android: How to do create custom EditText with a clickable arrow on the right

I would like to have an EditText with one modification: on the right but still inside the EditText there should an arrow pointing downwards that I can set OnClickListener to so that when the user clicks on the arrow it displays a menu. What is the best way to do this?

java - Clickable widgets in android

The developer documentation has seemed to have failed me here. I can create a static widget without thinking, I can even create a widget like the analogue clock widget that will update itself, however, I can not for the life of me figure out how to create a widget that reacts to when a user clicks on it. Here is the best code sample that the developer documentation gives to what a widget activity should contain (the only...

css - Hidden links are still clickable on the Android browser

I've got a contextual menu that appears when a button is clicked. This menu has some links and after some seconds is hidden again. The problem is that after the menu has disappeared the links are still there. They are not shown but are clickable and the green border appears. And after some seconds they disappear. I've tryied many ways of hiding the menu: display: none, visibility: hidden, width: 0, height: 0, jque...

Android - I can't make a widget clickable to launch an intent

I am new to Android development. I have developed a very simple widget that was meant to interact with the user via an ImageButton. What I am trying to do now is as follows. When a user taps the button (after adding the widget to their home screen), I want the phone to dial a certain telephone number. A sort of speed dial for your home screen. Unfortunately when I tap the button nothing happens....

Android: In Gridview how do I make the images clickable

I am trying to make the images clickable so that when they are pressed it will send the user to another page or link. How can I achive this? Currently each row in the GridView has 2 Buttons. How will it know which item in the GridView is clicked so that it performs a certain action, specific to the item that was clicked. Thanks for any help! This is in my imag...

android - Make an image clickable

I'm kind of new to this whole thing so I need some help. I have an application that creates an ImageView OnCreate. I want to make this image clickable and have it open a browser to a specific link. How would I do this? I'm having trouble specifically with the setOnClickListener because the parameters are not accepting an OnClickListener. I'm developing for Android 1.6

android - How to set a clickable region for an ImageView?

I have 2 ImageViews which have AnimationDrawable backgrounds to animate a box opening and closing when you click on it. The 2 images are slightly overlapping (the transparent area) so they can be closer together. The problem this is causing is that I am unable to click on regions of the 1st image where the 2nd image (transparent area) is overlapping. Is there a way to set a clickable region of an ImageView?...

java - Marker icon not showing up but is clickable

I've got the following code which gets information from a database and plots it on a map. The information is there and clickable but the actual icon androidmarker is not visable. Why? How do I fix this? public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tweets = new LocationData(this); mapView = (MapView) findVi...

clickable imageview location change with animation - android

I have an imageview that's included inside a RelativeLayout. When the imageview is clicked, I animate the entire RelativeLayout with a translate animation to move it down. when I click the imageview again (in it's new location) it's supposed to move it back up, but it does not. However, if I click where the imageview started, it does move the entire "panel" back up. Why is the imageview not moving with the relat...

android - GridView is not clickable

I have the following layout: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="" android:layout_width="fill_parent" android:layout_height="fill_parent"> <GridView android:id="@+id/grid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" ...

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android