What could be responsible for an ImageView not being clickable - android

I am using an ImageView as a NEXT button in my Android app which is responsible for loading the next page of results into the current activity. However, despite that I bind a click listener to it, I cannot seem to capture click events on this ImageView. Here is the XML:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="left"
android:orientation="horizontal">
<ImageView
android:id="#+id/listBackIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/back_icon"/>
<TextView
android:id="#+id/listBackLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:textSize="16dip"/>
</LinearLayout>
And here is the relevant Java code:
ImageView forwardIconView = (ImageView)findViewById(R.id.listBackIcon);
// not sure if necessary; doesn't fix it anyway
forwardIconView.setClickable(true);
forwardIconView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
++pageNumber;
try {
params.put("page", pageNumber);
} catch (JSONException e) {
// do something
}
ConnectionTask task = new ConnectionTask();
task.execute(new String[0]);
}
});
I spent about an hour researching this on Stack Overflow. I found a few places which claimed that ImageView could directly be made clickable, but most things recommended workarounds using other types of widgets.
Does anything about my layout/code stand out as being a culprit for this behavior?
Update:
I also tried binding a click listener to the TextView at the same level as the ImageView and this too failed to capture clicks. So now I am suspecting that the views are being masked by something. Perhaps something is capturing the clicks instead of the views.

I would set it up like this:
private ImageView nextButton;
nextButton = (ImageView)view.findViewById(R.id.back_button);
Util.loadImage(getActivity(),R.drawable.some_image,nextButton); //Here i would load the image, but i see you do it in XML.
nextButton.setOnClickListener(nextButtonListener);
nextButton.setEnabled(true);
View.OnClickListener nextButtonListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.v(TAG, "ImageView has been clicked. do something.");
}
};
This works for me.

Why not use android:drawableLeft attribute for the textview instead of using imageView​ and textview both in a linearlayout .

<ImageView
android:id="#+id/listBackIcon"
...
android:clickable="true"
Or you can try overriding onTouchListener with ACTION_DOWN event filter, not onClickListener. Also check for parrents with android:clickable="false", they could block childs for click events.

What seemed to work for me was the accepted answer from this SO question, which suggests adding the following the every child element of the LinearLayout which I pasted in my question:
android:duplicateParentState="true"
I don't know exactly what was happening, but it appears the click events were not making it down to the TextView and ImageView. Strangely, the click events were reaching a Button, when I added one for debugging purposes. If someone has more insight into what actually happened, leave a comment and this answer can be updated.

Related

Two buttons but wrong event is triggered when pushing them

I have a very strange behaviour of two buttons:
<Button
android:id="#+id/main_button_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/main_button_measure"
android:layout_centerHorizontal="true"
android:layout_marginTop="76dp"
android:text="#string/button_logout" />
<Button
android:id="#+id/main_button_measure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/main_button_logout"
android:layout_alignStart="#+id/main_button_logout"
android:layout_alignParentTop="true"
android:layout_marginTop="194dp"
android:text="#string/button_measure" />
I initialize them in my main activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.sessionManager = new SessionManager(this);
this.sessionManager.login();
this.setContentView(R.layout.activity_main);
this.buttonMeasure = (Button)this.findViewById(R.id.main_button_measure);
this.buttonMeasure.setOnClickListener(this);
this.buttonLogout = (Button)this.findViewById(R.id.main_button_logout);
this.buttonLogout.setOnClickListener(this);
}
My click listener:
public void onClick(View view) {
switch(view.getId()) {
case R.id.main_button_measure : this.readNFC(view); break;
case R.id.main_button_logout : this.sessionManager.logout(); break;
}
}
Now to the problem: Everytime i push my measure button the logout is called and everytime
i call my logout button the readNFC is called. Whats wrong there?
It happens when you change a layout for example that only part of the application is rebuilt (the rest, e.g. the sources will use the previous build artifacts if they haven't changed) and a resource ID with the same name in the xml layout and the java sources will actually translate to different int IDs.
Try to put a breakpoint in your onCreate() and check the details of the 2 Buttons you set the on click listener of, most likely this.buttonMeasure will reference the logout button
(i.e. will have a top margin of 76dp for example), and vice-versa.
So just clean and rebuild, it should solve your problem.
You can blame it on the build tools ;)

gridview and 52 buttons want compress button capacity

I have got one simple problem, my app contains 52 buttons but i cant display all buttons. I need suggest have can i solve this trouble the best as I can.
At the moment i have buttons like that:
http://prntscr.com/482kjw
but this solve is untransparent and i want one button make two functionaly one on click and second on long clik or something that, but i havent got idea how..
and now my question is: I need recommendations for the solution of my problem or some tutorial how to display two images on one button, Gridview.
you can make a layout of two images in one on a linearLayout with orientation:Horizontal or
To add two pics on one button, try this
android:drawableEnd="#drawable/ic_action_done1"
android:drawableTop="#drawable/ic_action_done2"
and so forth.. this puts two images-(#drawable/ic_action_done1 & #drawable/ic_action_done2)- on one button
and speaking of the functionality of the buttons, from my understanding to your question, you need a click function and a long press function, well you can use one button for to achieve these..
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View arg0) {
// TODO Auto-generated method stub
return false;
}
});
maybe it might help you.
SAMPLE OR TUTORIAL ON WHAT YOU ASKED
The name is button_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="#+id/registerButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="8dp"
android:drawableLeft="#drawable/checkmark"
android:drawableRight="#drawable/ic_launcher"
android:text="#string/register"
android:textColor="#ffffff"
android:textSize="22sp" />
button_layout is the xml_file you will inflate..in the link you gave me the button_xml was written programatically in the baseAdapter which was
imageView imageView;
imageView = new ImageView(mContext);
you can also do yours programmatically in this way
Button button = (Button)getLayoutInflater().inflate(R.layout.button_layout, null);
it seems you are new in android. so check these sites, it might give you a lucid idea of what i mean..
http://www.learn2crack.com/2014/01/android-custom-gridview.html
http://www.mkyong.com/android/android-gridview-example/
http://www.tutorialspoint.com/android/android_grid_view.htm
http://www.androidhub4you.com/2013/07/custom-grid-view-example-in-android.html
http://androidexample.com/Custom_Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=76&aaid=100
http://javatechig.com/android/android-gridview-example-building-image-gallery-in-android

Sample code for homescreen widget which has a button and is updated via service

I have read a number of tutorials on this topic but not able to make it working. Can anyone please help me with the following
a) A home screen widget which has a button, Imageview and a TextView
b) The textview updates periodically via a service
c) On Click of the button the Image Changes.
Can anyone please help with a sample code or point to codes which do this functionality
check out this book, it explains very clear how to make a widget "silent mode toggle"
http://iit.qau.edu.pk/books/Android.Application.Development.for.For.Dummies.pdf
a) In your xml, the following will be the code.
<Button
android:id="#+id/b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/go" />
<ImageView
android:id="#+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image" />
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#string/welcome" />
Of course, you need to arrange them according to your fancy.
b) Updating the text view is no big deal. Getting the text from the source is what is important. If it is from a web page, you could refer to usage of JSON. If it is from a database, then try SQLite. It all depends on your necessity.
Lets assume that you get your text. Your code for updation will be:
TextView t =(TextView) findViewById(R.id.tv);
t.setText(your_string);
To do it periodically you can use a parallel threading concept such as a runnable.
c) Set up an on Click Listener for this purpose.
Button b1;
b1 = (Button) findViewById(R.id.b);
b1.setOnClickListener(button_func);
This comes in your onCreate method.
View.OnClickListener button_func = new View.OnClickListener() {
#Override
public void onClick(View arg0) {
image = (ImageView) findViewById(R.id.iv);
iv.setImageResource(R.id.new_image);
}
};
Hope that you find this useful

Find and return the ID of the current view

I'm trying to set up a "close to start" button in a game. The game takes the user from view to view like a maze. The user could be on any view, but I want a way to get them back to the start screen. Is there a way to find and return the ID of the current view for my findViewByID? I've found a I've tried the following code in various forms:
OnClickListener closetomain = new OnClickListener() {
#Override
public void onClick(View v) {
int currentid = v.findFocus().getId();
RelativeLayout hideme = (RelativeLayout)findViewById(currentid);
hideme.setVisibility(View.GONE);
setContentView(R.layout.main);
// RelativeLayout showme = (RelativeLayout)findViewById(R.id.startLayout);
// showme.setVisibility(View.VISIBLE);
}
};
Okay, it turns out I should have given each close button the same ID and used theisenp's suggestion for the simplest code (well, that I could understand). It also turns out that I should have started by putting each level/layout in its own activity. You live and you learn I guess. Below is my XML and java. It may not be the elegant solution but I'm not sure I care all that much as long as it works.
<ImageButton android:id="#+id/closeButton"
android:layout_height="wrap_content"
android:background="#drawable/close"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_marginBottom="10dp"
android:onClick="closetomain">
</ImageButton>
And here's my Java:
public void closetomain(View view) {
switch(view.getId()) {
case R.id.closeButton:
setContentView(R.layout.main);
break;
}
}
Why do you need to retrieve the id of the current view? Is it just so that you can set it's visibility to GONE? If so, there are probably better ways of implementing that same functionality.
Instead of just changing the layout with setContentView(), it would probably be a better idea to have the Start Screen be its own separate activity. When you are in any of the "maze views" you could simply use an intent to start your home screen activity like this
Intent startScreenIntent = new Intent(this, StartScreen.Class);
startActivity(startScreenIntent);
Then you won't have to worry about finding id's or changing visibilities, plus the code is cleaner, because it separates the code for your Maze levels and your Start Menu.

Clickable image - android

How do i make a image clickable? I have tried some ways, but without success.
Here's the last code i tried (it's clickable but gets error):
ImageView btnNew = (ImageView) findViewById(R.id.newbutton);
btnNew.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do stuff
}
});
and here's the part from xml:
<ImageView
android:src="#drawable/tbnewbutton"
android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/newbutton"
android:clickable="true"
android:onClick="clickImage"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
When running this code, and clicking the image i get this error:
01-24 19:14:09.534: ERROR/AndroidRuntime(1461): java.lang.IllegalStateException: Could not find a method clickImage(View) in the activity
HERE'S THE SOLUTION:
The XML:
<ImageButton
android:src="#drawable/tbnewbutton"
android:text="#string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/newbutton"
android:clickable="true"
android:onClick="clickNew"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#null" />
The code :
public void clickNew(View v)
{
Toast.makeText(this, "Show some text on the screen.", Toast.LENGTH_LONG).show();
}
As other said: make this an ImageButton and define its onClick attribute
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left"
android:onClick="scrollToTop"
android:src="#drawable/to_top_button"
/>
The image is here encoded in a file res/drawable/to_top_button.png. If the user clicks on the button, the method scrollToTop() is called. This method needs to be declared in the class that sets the Layout with the ImageButton as its content layout.
public void scrollToTop(View v) {
...
}
Defining the OnClick handler this way saves you a lot of typing and also prevents the need to anonymous inner classes, which is beneficial for the memory footprint.
Does an ImageButton do what you want?
The error message you get implies that you do not have a method in your activity that matches your onClick handler.
You should have something like clickImage(View view) in your activity with the click handling implementation.
You could just use the ImageButton class...
http://developer.android.com/reference/android/widget/ImageButton.html
Use a ImageButton ;)
You've set the onclick method to call "clickImage" when the image is clicked in your XML, but you haven't created a clickImage method in your code. You shouldn't need to set the onclick listener at all. Just implement the method from your XML and you should be set.

Categories

Resources