Get Text from Pressed Button

Get text from pressed button

The view you get passed in on onClick() is the Button you are looking for.

public void onClick(View v) {
// 1) Possibly check for instance of first
Button b = (Button)v;
String buttonText = b.getText().toString();
}

1) If you are using a non-anonymous class as onClickListener, you may want to check for the
type of the view before casting it, as it may be something different than a Button.

How to get Button text using onclicklistner in Android Kotlin?

fun getJoke(v:View) {
val b = v as Button
val buttonText = b.getText().toString()
}

How to get the text from the Button when clicked?

Button is a subclass of View, so the argument to onClick, v, is the Button being clicked. Try

public void onClick(View v) {
((Button) v).getText();
}

Android get text from button

There are different options. If that is the only type of View then you can iterate through the layout's children with something like

for (int i=0; i<myLL.getChildCount(); i++) // assuming you have  a LinearLayout named myLL
{
Button btn = (Button) myLL.getChildAt(i); // cast the View to a Button
String text = btn.getText().toString();
// check the text here and do what you need
}

Or you could put them in an ArrayList at the beginning then iterate through that and check the text with something like

ArrayList<Button> myBtns = new ArrayList<Button>();
myBtns.add(R.id.someBtnId);
// iterate through them when needed.

There may be easier ways with listeners and such but without more information this is the best I can give you.

Python tkinter how to get text from button I clicked on

Ok so here is an example using a class to perform what I think it is you are asking.

You want to use lambda in your command and assign the value of text to a variable. Then you pass that variable to the getTest(self, text) method to be able to print your button.

From your comment

Whole code is not need i just need way to get buttons text nothing else

I have created a bit of code to illustrate what you are wanting.

EDIT: I have added code that will allow you to change the configs of the button as well.

import tkinter as tk

# created this variable in order to test your code.
seznamTextu = ["1st Button", "2nd Button", "3rd Button", "4th Button", "5th Button"]

class MyButton(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.obsahOkna()
def obsahOkna(self):

radek = 0
bunka = 0
for i in range(5):
btn = tk.Button(self.parent, text=seznamTextu[i])
btn.config(command= lambda t=seznamTextu[i], btn = btn: self.getText(t, btn))
# in order for this to work you need to add the command in the config after the button is created.
# in the lambda you need to create the variables to be passed then pass them to the function you want.
btn.grid(row=radek, column=bunka)

bunka += 1
if bunka == 2 : # changed this variable to make it easier to test code.
bunka = 0
radek +=1

def getText(self, text, btn):
btn.configure(background = 'black', foreground = "white")
print("successfully called getText")
print(text)

if __name__ == "__main__":
root = tk.Tk()
myApp = MyButton(root)

root.mainloop()

Here is the result of running the program and pressing a couple buttons.

Sample Image

How to get the pressed button in a function

Hello and happy new 2021!

I think this might be a slight duplicate of this.

As Gabriele said, you can get the HTML element by using the target. If you need some logic for differentiating the structures (using them in some state later on), you would need to assign an id or a different class.

Get the text value of the button that was clicked

The object which fired the event is sender, so:

private void button2_Click(object sender, EventArgs e)
{
string s = (sender as Button).Text;
}


Related Topics



Leave a reply



Submit