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.
Related
I am trying to set the visibility of a Textview depending on the response returned by Retrofit . onFailure, I am setting the visibility to visible tv_no_cat.setVisibility(View.VISIBLE); However, this does not work. I am doing the same with a ContentLoadingProgressBar and it's working appropriately. Here is my code
public void onFailure(Call<List<MoviesCategory>> call, Throwable t) {
tv_no_cat.setVisibility(View.VISIBLE); ////This does not work
Boolean x = tv_no_cat.getVisibility() == View.VISIBLE;
Toast.makeText(getContext(), "Network Error "+x, Toast.LENGTH_LONG).show(); //This shows true
tv_no_cat.setTextColor(Color.BLUE);
videoLoadingPb.setVisibility(View.GONE); //This works
}
My guess is that am trying to set the textview which is on UI thread from another thread. If so, why is it working for the ContentLoadingProgressBar?
Here is my xml for the 2 views
<android.support.v4.widget.ContentLoadingProgressBar
android:id="#+id/loading_categories"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:visibility="visible" />
<TextView
android:layout_below="#id/loading_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No Movie Category Found"
android:id="#+id/tv_no_cat"
android:textAlignment="center"
android:layout_gravity="bottom"
android:visibility="gone"
/>
Any help on how I can set the textview Visibility to VISIBLE is welcome.
From the docs:
On Android, callbacks will be executed on the main thread...
So this is not your issue, and I'm doubtful that it's related to Retrofit, especially when it works for another view. Please make sure that there are no problem in your xml design, and that there are no other methods that affect this view's visibility.
are registering Textview inside Oncreate or some where else ?
And this code you are using inside activity of fragment ?
Please check this ...
tv_no_cat=(TextView)findViewById(R.id.tv_no_cat);(Inside Activity)
tv_no_cat=(TextView)rootView.findViewById(R.id.tv_no_cat);(Inside Fragment )
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.
I am developing an Android app that has an ImageButton in it, and I need to add an event handler to it. In my attempt to do this, I have added a new function to the .java file that Android Studio creates when you make new project that looks like this:
public void tapImageButton(ImageButton myImgBtn) {
// Code that does stuff will come later on.
}
After doing this, trying to set the onClick proerty does not show the function, and trying to enter it manually causes the app to crash when I test it.
You must have View as a parameter of method that you wish to call, while bind click event through layout file.
Try this:
public void tapImageButton(View view) {
// Code that does stuff will come later on.
Toast.makeText(this, "clicked !!", Toast.LENGTH_SHORT).show();
}
In xml:
...
android:onClick="tapImageButton"
...
I am not very familiar with android studio but when i tried making a button and having it do something on click i first added the android:onClick = "some_method_name" to the XML that makes the design of the app. I first added the name in the onClick and then i clicked on the name. When the little light bulb icon shows up the arrow next to it give the option of creating a method with that method name that you just wrote. once you create it then you add the code of whatever you want to happen when you click. Hope it works.
For Example, This is the XML:
<Button
android:id="#+id/playButton"
android:text="#string/play"
android:textSize="25sp"
android:background="#0099FF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:onClick="startGame" />
This is the Java:
public void startGame(View view) {
Intent intent = new Intent(this,GameActivity.class);
startActivity(intent);
}
I am new to android development, and have been trying to use the beginner's tutorial as my starting point for developing a simple app. There is one screen, with an image, a row of four buttons, a textbox for the user to enter a pin, and a textview to display results.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/skytrek"
/>
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_today"
android:onClick="today_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_tomorrow"
android:onClick="tomorrow_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_this_week"
android:onClick="this_week_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_next_week"
android:onClick="next_week_click" />
</LinearLayout>
<EditText android:id="#+id/editText1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/edit_message" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/init_message"/>
</LinearLayout>
I have code to deliver the results I want based on the button pressed - all fine. But then I wanted the user to enter a PIN as well as pressing a button, so I added the EditText control. This threw the following error:
E/AndroidRuntime(28578): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
My Java class:
private String content = null;
private TextView textView1;
private EditText editText1;
public void today_click(View view) {
getPage("today");
}
public void tomorrow_click(View view) {
getPage("tomorrow");
}
public void this_week_click(View view) {
getPage("thisweek");
}
public void next_week_click(View view) {
getPage("nextweek");
}
public void getPage(String strParam) {
editText1 = (EditText) findViewById(R.id.editText1);
String message = editText1.getText().toString();
if (message.equals("4567")) {
content = "PIN recognised";
} else {
content = "PIN not recognised";
}
textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(content);
}
I thought I had done something silly, using the name of a TextView instead of an EditText control, but I can't find it if I have.
The error is being thrown at the line
getPage("thisweek");
I didn't understand how this line involved views of any sort, but of course the function heading
this_week_click(View view)
does, and when I changed the order of the TextView and the EditText in the XML file (so that the TextView comes first), the error disappeared. It is as if the "view" being passed is not the button, but the nearest widget to the button. I have read
existence of parameter (View view)
but it only seems to confirm my (mis)understanding that a button should be passed as the view parameter. I have also tried cleaning the project, and building a completely new project. What on earth is causing the casting error?
If you're using Eclipse, go to the menu voice "Project" and select "Clean"
Sometimes Eclipse has some problem with ids, by cleaning the project you regenerate them..
Everytime if you make any changes in xml or any interchange of position of views in xml or change of id's,you need to clean build your project.If not you will get this exception.
Hello I have tested your code, your code is fine. Please clean your project from
select your project then click on Project and then Clean and also check the Build Automatically . Your auto generated class R is not generated properly.
Try cleaning your project and run it again..
Eclipse -> Project menu ->Clean
It is because eclipse gets confused when we play around in the xml file ;)
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