How to insert control like buttonOnClickListener ImageOnClickListener in Fragment in android
You can place it in the xml for the fragment as any other view
I guess you might be having trouble with context, use 'getActivity()' to get tht context of the Activity or you can also use 'ParentActivity.class'.
You night be used to used to using 'this' as context when you are using buttons in 'Activity' but 'this' can't be used when you want to use button in fragment as 'this' would refer to the 'Fragment' and not to the 'Activity'
The following code can be used in 'onCreate()' of 'Fragment'
Button btn = new Button(getActivity());
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Please feel free to ask any further doubts and do let me know whether this solves your problem
Related
I want to add google authentication, I am new to android studio and I have the error "Cannot find symbol variable google_btn". Here is my code.
findViewById(R.id.google_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sign_in_with_gmail();
}
});
Here is a screenshot.
https://i.stack.imgur.com/QsN4H.png
It seems like an issue with not finding the link in the xml. Which can be down to a few factors. The first is you have not declared the id in the xml. Second you are referencing the xml content when you view it.
You need to firstly identify where you are accessing your google_btn from.
If it is inside your activity_main.xml check it is created with:
android:id="#+id/google_btn"
//After setting content view
findViewById(R.id.google_btn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Sign_in_with_gmail();
}
});
Then in your java code for the activity you need to reference that layout to link it.
So in your java code set the view:
setContentView(R.layout.activity_mainmenu);
This activity should also be inside of your manifest.
After you have gotten the content view you can get the reference to the button
First of all, make sure that you have this button in your layout file (probably res->layout->activity_main.xml). If you have it set its id to google_bnt. Finally, the piece of your code, that sets the OnClickListener should be in onCreate() method in your activity class (probably MainActivity.java in your case)
I have an activity which lists objects from an array objects through a custom adapter. The row of this adapter contains several EditText's and a layout which is clickable and does the deleting of that object selected. My intention is the object can be updated by clicking on the item (which shows another activity) and deleting by clicking on the layout. So that, I have to implement the updating and the deleting by differents setOnItemClickListener's.
I have done the updating just setting an setOnItemClickListener to the listView of objects and sending the whole object to a new activity through putExtra and getIntent.
The problem is with the deleting. I have implemented an OnClickListener directly on the adapter, like this:
holder.layoutEliminar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Here call to an Async Task to delete the object but, what about t the id object???
}
That code goes fine when I click on the layout of the row but I don't know the way to obtain the id of the object selected in the listView. Does anybody know how??
Do not hesitate to ask me for more code or details.
Please excuse my English, not native.
You can set a tag for the view on your getView:
holder.layoutEliminar.setTag(theIdOfYourObject);
Note that View.setTag(Object tag) takes an Object as parameter (documentation). I will assume that you want to set the id of the object to delete as String for the tag.
And then, on your onClick
holder.layoutEliminar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout layoutEliminar;
// Retrieve your layoutEliminar from v
// ...
// Get the id of the object to delete from the tag
String id = (String) layoutEliminar.getTag();
}
};
I already did with the help of #Antonio. I didn't use Tag's, I have used the instruction getItem(position).getId() into the method onClick to refer the id of the object (don't know if it's the best and more efficient way to do). Like this:
holder.btnEliminar.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.i("PedidosAdapter dd: ",String.valueOf(getItem(position).getId()));
//Async Task for deleting the object with that ID
}
});
i am using page view in android and i view in this view an images. so i want to make event for on click. however, i call the image from another class like root view and i want the response of event from main class openoptionmenu() so i is there any way to call the event from second class when click the image?
((ImageView) rootView.findViewById(R.id.imageView1)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Object oo = ScreenSlideActivity.class;
}
});
Hi user3811293 I think you have 2 options, the first one pass Image view for call other time the event and the second one is create listener method in the other classes and call this. You can a simple sample here:
https://www.daniweb.com/software-development/mobile-development/threads/483268/can-i-set-onclicklistener-to-another-class-android-java
I think this is that you want, if you need more help or same advice me, good luck!
Here's the situation: I have an activity that dynamically generates a bunch of randomized custom imagebuttons and adds them to TableRows, in a TableView, in my xml. This activity also has a method that I want to call when one/any of these buttons is clicked. The buttons have variables inside them; the method gets these variables and sets them into a TextView (in the same activity) so I figure all the buttons can use this one method. If these buttons were defined in the XML I would just use android:onClick="displayCell" to specify the method, but they aren't. Is there a way to just set onClick for these buttons as I'm generating them in the activity or do I have to use
button.setOnClickListener(new OnClickListener(){....});
and go through a bunch of hassle as I've seen in some of the answers around here? The problem I have with that is that I can't seem to call my method from inside onClick because the argument of the method (the button) is not final (I'm making a bunch of 'button' in a loop so I don't think it can be):
button.setOnClickListener(new OnClickListener(){
public void onClick(View q){
button.getActivity().displayCell(button);//I want to do something like this but this obviously doesn't work
}
});
You can have the Activity implement OnClickListener and then (assuming you are in the activity):
button.setOnClickListener(this);
Yes as comodoro states, or make your onClickLIstener a member variable of your class, don't do a "new" on each button.
private OnClickListener mOnClickListener = new OnClickListener() {...};
and when creating your buttons:
button.setOnClickListener(mOnClickListener);
The onClick() function in your listener will be passed the View of the button itself. You can access the buttons variables, etc, from this function.
public void onClick(View v)
{
ImageButton button = (ImageButton)v;
// and access your button data via button object...
}
A solution to this could be :
Create different instances of buttons .(So you can make them final)
Use setId() method to give them an integer ID (to refer to them later).You can store the ID's in an a Listto refer them later on.
Define their onClickListeners right after you create it.
Try using a class that inherits from button and add there the OnClickListener. Like this:
class MyButton extends Button {
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
displayCell(v);
}
};
}
There are quite a few questions about this subject, but could not find any with the specific problem I have...
In my layout.xml, I use the android:onClick tag for a Button to call the right onClickListener. I get the error :
java.lang.IllegalStateException: Could not find a method handle_cancel(View) in the activity class com.matthieu.HelloWorldApplication for onClick handler on view class android.widget.Button with id 'button_cancel'
I have that method implemented in the Activity, but it is looking for it in the class that extends Application... I don't understand why. The View and all that is setup only in the Activity.
If anyone needs, here is the declaration of that method (in my activity, NOT in HelloWorldApplication):
public void handle_cancel(View v) {
// do something useful here
}
Edit (from adamp request)... and probably answering my own question :
Here is part of the code where that layout is used...
public class AddVocabularyActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.top); // that layout contains an empty LinearLayout id/main_content
}
private some_other_function() {
LinearLayout main_content = (LinearLayout) findViewById(R.id.main_content);
main_content.removeAllViews();
View.inflate(getApplicationContext(), R.layout.hello, main_content); // layout.hello is the one containing the button
}
// some other stuff
}
While copy/paste this code, I am guessing the problem is that I used getApplicationContext to inflate the View with that Button...
As mentioned in my edit, changing the getApplicationContext() with the Activity context fixes it...
The convention works like this:
In the layout xml file, you give this attribute:
android:onClick:"methodname"
Then, inside a class, you define a method like this:
public void methodname(View v){
//your method code
}
Any other way of doing this is not documented. If you need parameters, just call another method inside that method.