0

There is a method in Java known as

void setActionCommand() and the complementary being

String getActionCommand() to get customized text from JButton / Button (default being text of the button itself).

Now I have switched to android, and converting an app from Java to Android. Is there any method in Android API which does the same thing with the android button, i.e. which can set and get a customized text for a button ?

Thanks !

EDIT : For Non- Swing API / Core Android people:

There are two methods associated with a swing button (JButton)
1. void setText(String s);
2. String getText();
and when an event occurs on a button, we take the command using object of ActionEvent (Event Listener class) using this method
ActionEventObject.getActionCommand()

we also have a method to set this command called setActionCommand(String) which can be called from a button reference Eg.
button1.setText("Option");
button1.setActionCommand("Android");

Now even if button1's text is "Option", in event handling procedure the button can be identified by the string ("Android"). Moreover, the text "Option" is visible to the user while "Android" is not visible to the user and is used for event handling procedures only.

Gagan93
  • 1,826
  • 2
  • 25
  • 38

1 Answers1

0

Simply get your button from your method (here assuming you're on an activity class, and use the setText method.

Button mButton=(Button) findViewById(R.id.contact);
mButton.setText("number");

Update

After looking at get/setActionCommand, i think tags are similar, have a look to this answer: https://stackoverflow.com/a/8083855/1766140

Community
  • 1
  • 1
S. A.
  • 3,714
  • 2
  • 20
  • 31
  • But setText() would set the same as the text of the button. I want that it should set the Command to my customized text, not the one which is visible to the user (see set and getActionCommand() in java) – Gagan93 Jan 22 '14 at 17:40
  • 1
    @Gagan93 If this is an Android question, then Android developers have no idea what "set the Command to my customized text" means. If you want a solution, you should explain what you mean rather than expect people to go and read what you already know. – Simon Jan 22 '14 at 17:44
  • @Simon : I "expect" Android Developers have knowledge of Core Java API and so I am asking my question in that context. If I had known a way for that, I would not have posted this question – Gagan93 Jan 22 '14 at 17:46
  • 1
    @Gagan93 Why? Since Android does not implement the core Java API? I've been developing Android apps with Java for 3 years+ and I have no idea what you are asking for. What does this API do and what are you trying to achieve? – Simon Jan 22 '14 at 17:50
  • @Simon : sorry if u didn't get this ,For better understanding ..consider this short JAVA code JButton b = new JButton("Hello"); b.setActionCommand("Hi"); using the setActionCommand function, the button would be recognized by string "Hi" rather than the String "Hello" if we use ActionEvent.getActionCommand() inside public void actionPerformed method. Now my requirement in android is similar to this. i.e. I have a button array (2D) and want to set action Command for each button using for loop but I dont want that text to be displayed to the user, that would be only for programming use – Gagan93 Jan 22 '14 at 18:01
  • @SergioAristizábal : that person is asking for something different, which he was able to achieve by the given answer. But my requirement is not same as his requirement – Gagan93 Jan 22 '14 at 18:02
  • @Gagan93 OK, 3 times I have asked, **what** are you trying to achieve? If setting the tag property is not sufficient, then please edit your question to explain what you are trying to do? Also, that method is not even part of the core API. It seems to be a SWING method of the JButton class - Android developers (like me) will have no idea about this since Android does not implement SWING or Jbutton. – Simon Jan 22 '14 at 18:06
  • @friends : I have edited the post and have tried to explain in the best possible way I could. Please suggest a solution, if available – Gagan93 Jan 23 '14 at 04:32
  • 1
    @Gagan93 I insist [View tags](http://developer.android.com/reference/android/view/View.html) is what you're looking for. http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view – S. A. Jan 23 '14 at 04:40
  • 1
    @Gagan93 Sergio is right. You set the "command text" in the tag then read the tag in the `onClickListener`. This is only needed if you want to change the function of the button at runtime but, there are better ways of doing this in Android. You described what those SWING methods do, but not what you are trying to do. Sergio's answer gives you a direct solution to replicating those methods but Android has better ways of handling them. Note also that the reference to the button which raised the click event is passed to the click listener so the listener can test which control caused the event. – Simon Jan 23 '14 at 06:49
  • thanks both of you ! I have seen that post and it seems that it is the thing which I was looking forward to. Will experiment and let you know. – Gagan93 Jan 23 '14 at 10:04