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!
Related
This might sound a bit convoluted.
I have roughly 15 Spinners in one activity and made a distinct method for each of these spinners. I then initiate the methods in the onCreate method.
Method example:
//Relative Position Spinner
public void relativePositionSpinner() {
Spinner relativePositionSpinner = (Spinner) findViewById(R.id.spinner_relativePosition);
ArrayAdapter relativePositionAdapter = ArrayAdapter.createFromResource(this, R.array.relativePosition, R.layout.spinner_item);
relativePositionSpinner.setAdapter(relativePositionAdapter);
//what happens when selected
relativePositionSpinner.setOnItemSelectedListener(this);
}
OnCreate Method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_hand);
//initiate all Spinners
relativePositionSpinner();
absolutePositionSpinner();
etc.
Now what I want is to send the data of each Spinner to another Activity with the click of a Button. I know that I can do this with an intent and using putExtra in the Button method like this:
public void openHandSummary() {
//Find the Button that gives option to enter new hand
Button handInputButton = (Button) findViewById(R.id.hand_input_button);
//set a click listener on Hand Analyzer Button
handInputButton.setOnClickListener(new View.OnClickListener() {
//below code will be executed when the new Hand Button is clicked
#Override
public void onClick(View view) {
Intent handSummaryIntent = new Intent(NewHandActivity.this, HandSummaryActivity.class);
handSummaryIntent.putExtra("RelPosString", WHATTOENTERHERE??)
startActivity(handSummaryIntent);
}
});
}
However I do not know how to retrieve the value/variable out of my Spinners to put them into the Button/intent method? Because if I make a String in the Spinner method, then I can't access this in the Button method.
So I feel like I have too many methods? So is there a way to pass data from one method to another method, or do I have to cancel some methods? What would be the easiest way to set this up?
I also made an onItemSelected to make some toasts, which worked. Can I use OnItemSelected somehow to create variables or initiate a data transfer to another Activity?
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TextView myText = (TextView) view;
switch (parent.getId()) {
case R.id.spinner_relativePosition:
makeText(NewHandActivity.this, "Relative Position is " + myText.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.spinner_absolutePosition:
makeText(NewHandActivity.this, "Absolute Position is " + myText.getText(), Toast.LENGTH_SHORT).show();
break;
I'm very new to coding, and I just can't figure out the logic how I get the Spinner methods, Button/iniate method and OnItemSelected method to work together and exchange variables. Would appreciate if someone can point me in the right direction. Have already browsed the internet a day or so to find an answer, with no success.
I like your idea of separating out the code to create each spinner into its own method. However, if you are copying and pasting the whole method and making a few changes, you should step back and think about how you can do it even more easily. Often the changes you make after copy and paste give a hint that you should add some parameters to the method. If you do it correctly, you can write just a single method and then copy and paste the method call instead of the entire method and then make appropriate changes to the arguments.
As for your actual question, you are making this much more complicated than necessary. Specifically, you do not need to setOnItemSelectedListener() on any of the Spinners. Instead, the OnClickListener for the button should just get the selected item from each spinner and send it to the new activity in the Intent.
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
}
});
This sounds like the other questions, but it actually is a little different. So my scenario is, I have six buttons and the user can click any of them => but I want to keep track of the ORDER by which the buttons were clicked. So here's what I did. I saved a record in SQLite and using the date, I can determine the order the buttons were clicked. Is there another way to do this?
public class ButtonClickListener implements OnClickListener {
#Override
public void onClick(View v) {
// I want to be able to pass data in this method.
try {
v.setVisibility(View.INVISIBLE);
} catch (Exception ex) {
}
saveClickDAO(); // has Date field to sort later
}
Is there a way to actually pass data to this onClick event? I know I can add data using a parameter to the constructor here, but that would be data that is static already.
You have a View v in your onClick(View v), so you can use data on the View. v.getId() or v.getTag() will give you some view related data.
v.getId() will give you the android:id you set in your layout xml, such as R.id.some_btn
v.getTag() will give you the object you set with v.setTag(Object o).
How to use Multiple spinners in a single layout i.e., setting set On Item Selected Listener in on create
Welcome to SO. You definitely should read the FAQ to learn what kind of questions to ask and how. If you want good answers and don't want your privileges to be revoked you need to be more specific about your questions and add relevant code and error messages showing that you have tried. But since you are new I'm going to give you this one. The easiest way to do this is attach the listener to each one in onCreate() after getting a reference to each spinner through your xml
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this;
then put the methods outside of onCreate()
#Override
public void onItemSelected(AdapterView<?> parent, View view, int arg2,
long arg3)
{
// do whatever you need here depending on what is selected
// you can use a case statement and switch on the id to get the spinner (parent) or view
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
and add Implements OnItemSelectedListener in your Activity signature. Good luck and please ask more specific questions in the future and add code showing that you have made an attempt.
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