I don't want to allow the user to be able to click on the various images (which have onClick="mark") and call the method mark().
Method mark:
public void mark(View view){
ImageView counter = (ImageView) view;
counter.setTranslationY(-1000f);
sett(view);
}
Instead I want only method retro() to be accessible when it is called.
Method retro:
public void retro(View v){
Button bq = (Button) findViewById(R.id.b1) ;
bq.setVisibility(Button.VISIBLE);
bq.animate().alpha(1f).setDuration(2000);
//restart(bq);
}
So what should I add to retro() in order to disable mark()
So you know which View is calling the method. It's like implementing the OnClickListener for you activity, the method created is onClick(View v) (or arg0 depending on your Eclipse), defining it from xml is just specifying a sort of listener for the View, and the method from the listener as that argument.
Once you're in the method, you can do a switch for the id of the button, to perform different actions:
public void myOnClickMethod(View v)
{
switch(v.getId())
{
case R.id.button1:
//Do something for button 1
break;
case R.id.button2:
//Do something for button 2
break;
}
}
In short. Android just implements the OnClickListener for you when you define the android:onClick="myOnClickMethod" attribute.
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 :)
I have a tic tac toe app, and I want to know whether it is possible to set all the tic tac toe buttons to one on_click event, and then create a variable to get the ID of the button clicked, then pass it as a parameter to another method which will do the actual functionality, OR do I need to create different on_click events for each button?
You can do something like this, and add as many "cases" as needed:
View.OnClickListener sharedClickHandler = new View.OnClickListener() {
public void onClick(View view) {
switch(view.getId()) {
case R.id.button1:
// handle first button
break;
case R.id.button2:
// handle second button
break;
}
}
}
You can just use one listener - the onClick method takes a View parameter, which is the view that was clicked on. You can then find out which of your buttons that was:
View.OnClickListener sharedClickHandler = new View.OnClickListener() {
public void onClick(View view) {
int id = view.getId();
// Do the right thing based on the ID
}
}
Exactly how you do what you need to do based on the ID is up to you. For simple examples you could just use a switch/case statement; in other cases if you're mapping from ID to something else (a mutable object representing game state for example) you could use a Map<Integer, GameObject> and just get the right one...
hello you can use the same click event for button, you can attach for example an integer as a tag to the button so that you can know which button was clicked and handle accordingly.
button1.setTag(1);
button2.setTag(2);
button3.setTag(3);
button1.setOnClickListener(buttonClick());
button2.setOnClickListener(buttonClick());
button3.setOnClickListener(buttonClick());
public View.OnClickListener buttonClick(){
View.OnClickListener click = new View.OnClickListener() {
#Override
public void onClick(View v) {
int numberClicked = v.getTag();
//You have now the button clicked
}
};
return click;
}
I've got very very basic problem in Java (Android) writing. Here it is, I've got code:
public void WriteValue (View sender){
Button bt=(Button)sender;
}
WriteValue is performed, when user click button. And now I want to compare button which user clicks, with button that Id I know. Something like
if(UserButton==ClearButton) Display.setText("0");
Thanks for help
Tux:)
You could just compare ids.-
public void WriteValue (View sender) {
Button bt = (Button)sender;
if(bt.getId() == R.id.clearButtonId) {
Display.setText("0");
}
}
step 1 : make your Class implement OnClickListener
step 2 :
// global variable
Button btOne ,bTwo;
step 3 :
// in onCreate() method
btOne = (Button)findViewById(R.id.btOne);
btTwo = (Button)findViewById(R.id.btTwo);
btOne.setOnClickListener(this);
btTwo.setOnClickListener(this);
step 4 : implement method from interface
puvlic void onClick(View v)
{
if(v==btOne)
{
// do work for btone
}
if(v==btTwo)
{
// do work for btTwo
}
}
You don't need to cast the View to a Button here. You can simply get the id of the View clicked which is the param sent to the function (assuming this is an onClick() function as defined in xml or by setting the listener in Java). So it could be something like this
public void WriteValue (View sender)
{
int id = sender.getId(); // get the id of the View clicked
swith (id) // switch logic on that id
{
case (R.id clearButtonId): // if the clearButton was clicked do this
Display.setText("0");
break;
default:
Display.setText("Some other String);
break;
}
}
You could also use if/else instead of a switch but this is more readable, IMHO. This easily allows you to add more logic for other Buttons.
Also consider using Java naming conventions. According to this code, I assume Display is a TextView or EditText so it should start with a lower-case letter (ex. display).
I have created a button in my Android application & I tried to set onclick listner to run onClick method like follows
...
Button btn_ok;
btn_ok = (Button)findViewById(R.id.button1);
btn_ok.setOnClickListener(this);
}
public void onClick() {
EditText uN = (EditText) findViewById(R.id.EditText04);
uN.setText("Clicked!");
}
But Eclipse shows an error & says that "setOnClickListener" need to Cast Argument. After casting it is like this
btn_ok.setOnClickListener((OnClickListener) this);
Then when I'm running the program Emulator says that "Program has stopped unexpectedly"...
How can I solve this problem ?
Make sure that your class implements View.OnClickListener. You can`t just add onClick method, you must implement interface
The signature of your onClick method is wrong, which leads me to believe you're not actually implementing the interface View.OnClickListener.
The signature should be:
public void onClick(View v)
{
//your implementation, v is your button that was clicked
}
Note that the View that was clicked is passed in as an argument, so there's no need to call findViewById from inside your onClick method.
implement the onClickListener from your activity and override the method:
#override
public void onClick(View v)
{
switch(v.getId()){
case R.id.button1:
EditText uN = (EditText) findViewById(R.id.EditText04);
uN.setText("Clicked!");
break;
case default:
break;
}
}
Hope it helps.
setOnClickListener take an OnClickListener instance as parameter and OnClickListener is an interface which content an onClick() method and you are passing here setOnClickListener(this); current context. so you have two option either implements OnClickListener in your activity and second use this way :
this.btn_ok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//do your work here
}
});
Make sure that you are implementing the interface View.OnClickListener and also pass View to onClick method
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();