I am loading list view by listview adapter class.
Within that list view I have button called favorite.
ImageButton mFavorite = (ImageButton) convertView.findViewById(R.id.method_fav_btn);
There are multiple image buttons under the same id. I want to identify which button was pressed by means of setting some extra parameters to it. I am doing this for that:
mFavorite.setId(pm.getId());
And on click:
mFavorite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Id is: "+mFavorite.getId());
}
});
But, the problem is, I have 3 items in list view. Every time I am getting same id. How to get different ids on different clicks?
Thanks.
You can use the setTag() + getTag() methods.
See here for a similar question and here for the official documentation.
Related
I am designing a password system based on images which are displayed in the grid view. Since I am not so much familiar with android I want your help.My images are displayed dynamically on the grid i.e they keep on changing their position.
i am not to figure out how to define image as password.I want four images to be clicked in order and save them at the time of registration to the user.I am having difficulty in the saving part like which method to use and where to save.
what i want to do is this.
I want phone to display 15 images dynamically in grid view. then the user clicks 4 images in sequence to set the pass code for registration and it is saved offline. so the user next time clicks 4 images in same order to get unlocked. I am having difficulty in storing the corresponding image displayed on the particular grid because in need images to save the pass code and how to store the data of the user like username, name , and the images which he has clicked.
You can use ImageButton's in your Gridlayout or use a button and set the image as background.Further write a adapter class(I am assuming that you have written one).
Set OnClick listener inside adapter and listen to the same click..
In your adapter class
holder.buttonone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((GridView)parent).performItemClick(v,position,0);
}
});
holder.buttontwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((GridView)parent).performItemClick(v,position,0);
}
});//further for your 15 buttons
And in your activity:
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(view.getId)
{
case R.id.buttonone:
//set the code in your edittext
break;
case R.id.buttontwo:
//do your stuff
break;
}
});
I am assuming you have a textview/edittext that will print the passcode or atleast xxxx ..So you can display it there and further handle that data.
Use edittext.getText().toString() and store it.
Hope that helps
I wanted to know if its possible to have two onclick methods for one buttton..Im trying to have a button that can open a new activity and send a id token to the server for firebase purposes, if possible how do i go about it on android studio
I think you are getting the underlying concept wrong.
Buttons react to clicks.
The "ActionListener" that gets triggered on that click ... can do whatever it wants. There is nothing (conceptually) that prevents you in your code to just trigger various things. Of course, you have to understand what you are doing (things like: not blocking the UI thread for too long; or how to kick of things in background threads, and so on).
No. There is only one onClick method for a Button. But you can still perform two different purposes by one button.
I am using a button to hide and show a linear layout. The code is given below :
final int[] count = {2};
//here startTopics is the button....
startTopics.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(count[0] %2==0)
{
topicLin.setVisibility(View.VISIBLE);
count[0]++;
}
else
{
topicLin.setVisibility(View.GONE);
//here topicLin is the linear layout
count[0]++;
}
}
});
It is one button and so you should apply only one onClick listener which performs the buttons job.
In your onClick-method you can just call another (private) method if you want to do multiple things without sacrificing code management.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendTokenToServer();
// Include your code to open the activity here or outsource it again into another private method
}
});
And your method to send the token to the server:
private void sendTokenToServer() {
// Your code here.
}
I've found numerous tutorials on how to accomplish this when you can grab the textview from the xml but nothing on how to implement such a function when you have multiple edit texts created programmatically that all need the same functionality.
After you create the EditText, you would add a click listener the same way you would to any other view. You may also need to disable the focusable attribute to prevent clicks from activating the keyboard:
editText.setFocusable(false);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Open dialog here
}
});
If you have all EditText's, simply create one OnClickListener, call setOnClickListener on every EditText and pass the OnClickListener Object.
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
//open dialog here;
}
};
editText1.setOnClickListener(listener);
editText2.setOnClickListener(listener);
You should maybe store the EditText's in an array, so that you can just loop through all of them.
I'm trying to make a method that is activated when another method gives it an int, and at the same time, the method can also be activated by a view.
Here is the top line of the method and where in Java the method is called:
checkNum(theNumber, null);
public void checkNum (int num, View view){
I tried using "onClick" in the xml for a button, but checkNum did not appear as a suggestion and the app crashed when I ran it. How can I fix this?
Thanks so much!
When using the onClick attribute in XML, the correct signature to use is
public void checkNum (View view)
If you want to pass in a other parameters, I suggest that you set it in Java code.
Add a click listener to your button this way:
Button button = (Button) findViewById(R.id.your_button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something when the button is clicked
}
});
I want to use the same xml file for displaying depending upon which button was clicked in the previous page. There is an xml template and depending upon the user's input the output will be shown.
Let's say there are 5 buttons and the layout of the output will be same for all but there will be difference in output data.
How can I get the id of the clicked button in the java file?
Can I use ImageView instead of buttons for the same purpose?
Thanks in advance!
1: You can create an onClickListener in your java class:
Button myButton = (Button) findViewById(R.id.mybutton);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
Or you can use set the click event in your layout-xml and ask the Id of the clicked view, see: How exactly does the android:onClick XML attribute differ from setOnClickListener?
2: You can use an ImageButton instead of a Button/Imageview
http://developer.android.com/reference/android/widget/ImageButton.html
Extra: sending info to next Activity
Bundle bundle = new Bundle();
bundle.putString("clickedTag", v.getTag());
intent.putExtras(bundle);
Tip: don't use the Id, but set the android:tag="ABC" to all your buttons, that's better to read than an integer Id.
To read the clickedTag use this inside your next Activity:
String tag = getIntent().getExtras().getString("clickedTag");