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");
Related
Thank you for helping me.
In the ViewPager + Fragment contains four pages, including Fragment contains listview Lane in the listview button, listview own click event is also need to use. In the listview item in the button click event, the trigger is not correct. Sometimes click no response, but in the switch Fragment with a button click event response and Fragment switch success
Thanks
Description:
That is, in the custom adapter to add the imageview click event,
click response response from time to time response.
A total of four Fragment fourth page also has the same item in the
imageview click event, but no problem arises. This problem occurs
in the first page. The two page codes are almost the same.
holder.operateInstanceImg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String InstanceId = mInstanceModelList.get(position).getInstanceid().toString();
String instanceCurStatus = mInstanceModelList.get(position).getStatus();
final MenuDialog mdlgClickMenu = new MenuDialog(mActivity,R.style.menu_dialog,InstanceId,instanceCurStatus);
mdlgClickMenu.setContentView(R.layout.dialog_mdlgclickmenu);
mdlgClickMenu.show();
}
});
change new OnClickListener() to new View.OnClickListener()
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 have morethan 100 button in my MainLayout.xml and i want to handle click on each. I want to group all the button and then listen to click of the group button, get the ID and put it into String and then use it as my base id to query from my Sqlite Databse Table. How can i do this? so far i have this on my MainClass.java.
MainClass.java
public class MainClass extends AppCompatActivity implements View.OnClickListener {
Button B0,B1,B2,B3....;
String baseId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
buttonInitializer();
}
public void buttonInitializer(){
B0 = (Button)findViewById(R.id.B0); B0.setOnClickListener(this);
B1 = (Button)findViewById(R.id.B1); B1.setOnClickListener(this);
...
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.B0:
baseId = "B0";
break;
case R.id.B1:
baseId = "B1";
break;
..........
default:
break;
}
QueryFire(); // Fire a query
}
Public void QueryFire(){
//Fire the query with id of the clicked button
}
}
As you can see if i continue doing this, i will have Long Code, i want to shorten my code, instead of the above code, i'd like to group the button and then listen of each click just like RadioButton, but NOTE. i dont want to use RadioButton. I'm trying to implement this seudo code.
// Put the button into RelativeLayout or something and use it to group the button
// OnClick of Each Button inside the GroupButton:
// Get the ID of the Clicked Button:
// Put the ID of Button into String:
// and FIRE my query with ID of Button
Any help woul be appreciated! Thank You Very Much!!!
You should be able to specify a String for the "android:tag" attribute for each button in your xml layout, as well as an "android:onclick" attribute. This should eliminate the need for all of the findViewById() and setOnClickListener() calls.
Then, in your onClick method, you can just do something like baseId = (String) view.getTag()
Might be a bit of a stretch but why not use a ListView with a custom adapter? each adapter will hold a button (or whatever you want) and with every click on button you will get the id (the position of clicked element). Its far more easy on the memory as well :)
I mean is like this
button = (Button)findViewById(R.id.button1);
button = (Button)findViewById(R.id.button2);
button = (Button)findViewById(R.id.button3);
button = (Button)findViewById(R.id.button4);
button = (Button)findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//Do same logic
}
});
}
it is possible to define like that? or it can causes force close?
and one question again, it is okay to use copying ID from current xml to another xml? i mean the xml was different but the Widget and ID is same, and define the ID in diferrent class. Because it is more simple to copying than make the Same widget with new Id, it is okay?
example :
so in activityone.xml
i had this
so i was copy the widget to activitytwo.xml so they have a same Widget and ID
it is okay if i do like that?
That will only set the listener for button5. Every time you assign button you lose the previous assignment, so when you set the listener the variable button doesn't know that it used to be pointing to button1 to 4.
So no, it will not work.
I put a comment about your second question.
For that you should have single listener but five different buttons (Objects). Currently you are assigning every button one by one to same button reference. In that way your button will have R.id.button5 and in further code you won't have reference to your previous buttons butons1-4. So, while you add listener to the button you are actually adding listener to only button 5 and any other buttons will not have that listener.
First of all you need five different buttons and you should create class which implement the listener and add instance of that listener to your setOnClicklistener method or in a better way you can just specify android:onClick="onClick" in your xml and add onClick method in your activity.
FOR EXAMPLE :
//.. on create
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
Button button5 = (Button)findViewById(R.id.button5);
ListenerClass listener = new ListenerClass();
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
//..and so on
//Create class
public class ListenerClass implements View.OnClickListener {
//...
#Override
public void onClick(View view) {
//You can determine which button is clicked by view.getId()
//Add logic here
}
}
OR
public class YourActivity implements View.OnClickListener {
//...on create
button1.setOnClickListener(this);
button2.setOnClickListener(this);
//..and so on
//...
#Override
public void onClick(View view) {
//You can determine which button is clicked by view.getId()
//Add logic here
}
}
it is okay to use copying ID from current xml to another xml?
Yes it is fine but you should avoid using that as it may create confusion in a long run. In a wide project working with a team can create problem in understanding that. Other than that you can have same id because when you call findViewById it will refer to the current view and does not get influenced by ids of other views.
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.