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.
Related
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 :)
First off this question has been asked multiple times, however, none of these questions have been answered to any extent. I have one example that works in the main activity class:
final Button button = (Button) findViewById(R.id.viewcatalog);
button.setFocusable(true);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
setContentView(R.layout.find_item);
}
});
But all of my other attempts to replicate this in sequential pages has resulted in failure. I know the reason that they won't work the same way is that my buttons are instantiated in other classes and not in the host class. What is the correct way to fix this error?
The method that doesn't work for reference:
public void OnClickSearch(View view) {
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText text = (EditText)findViewById(R.id.editText);
String value = text.getText().toString();
setContentView(R.layout.search_results);
}
});
}
It sounds like you are mis-understanding how the UI works in Android.
It is not normally expected that you will change an Activity's view on the fly as your are doing in your OnClickListener.
Instead, you should do one of two things. Either switch to a new Activity, using an Intent and the Activity's startActivity method, or use Fragments, and replace a Fragment in your Activity with a new Fragment.
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");
Forgive me if this has been asked before. I have poured through this site and many others and can't seem to find an answer. I have a working Android app but I'm trying to clean the code up. I have 3 buttons:
public Button button1, button2, button3;
Then, I have a method that "grabs" the clicks:
public void getButtonClick(View view)
{
switch(view.getId())
{
case R.id.button1:
// Do something button1 related here
break;
case R.id.button2:
// Do something button2 related here
break;
case R.id.button3:
// Do something button3 related here
}
}
What I'd like to know is if there is any way I can have a generic method that will just wait for a button click and grab that variable so that I can avoid the multiple switch-case statements. Something on the line of:
public void oneMethodForAll(View view)
{
clkdBtn = view.getButtonThatWasClicked();
// Do stuff
}
Any help is greatly appreciated. TIA
One thing you can do is to have the activity implement View.OnClickListener:
public class FooActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle icicle) {
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(this);
Button btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// Button 1 was clicked
break;
case R.id.button2:
// Button 2 was clicked
break;
}
}
}
That will save you a little bit of typing since you're not creating a bunch of anonymous click handlers, but you will still have to set the onClickListener of each button individually.
Typically you implement different handlers for different buttons since the action the button will signal is usually different.
You can cast your View to a Button by using the View which is being passed as the sole parameter into your handler.
Button clickedButton = (Button)view;
If all you care about is the id of the button and your action will be based on that value then create separate handlers.
You can just do:
clkdBtn = (Button)view;
Usually the Button itself isn't used though. getButtonClick() should respond to the button presses with their corresponding action... I'm not sure why you'd need the actual button view itself.
If you want the text that the Button is showing, you could use:
String text = ((Button)view).getText();
hi i am a new developer. i am trying to design an app. In my app i want to calculate the no of touches in a particular button. Is this can be calculated by onTouch process if yes can anyone give me an example or idea.
Try below code
First Create an Global variable
int numberOfClick = 0;
Now for your button try following code
clickButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
numberOfClick++;
}
}
now you can get the number of clicks by this variable
A click on a button is sent to the app via the onClick event. So if you have a Button:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(myClickListener);
You can set up your onClickListener to do whatever you want when the button is clicked.
// Create an anonymous implementation of OnClickListener
private OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
// increment the counter on click
numberOfClicks++;
}
};