Get info from view that triggered onclick? - android

I'm completely new to Java and android. I ended a tutorial and I'm building a simple calculator to learn the ropes.
I basically have a view like this:
<button android:onClick="addText" android:text="2" />
And function addText is this:
public void addText(View view) { // Add view's text
TextView calc = (TextView) findViewById(R.id.edit_text);
t.setText( calc.getText() + ????? );
}
onclick it should add the text from the clicked button to a TextView. If you click 1, it does calc.getText() + "1" if you click 2 it does calc.getText() + "2". I don't know how to get the clicked view's text. I tried this: t.setText( calc.getText() + this.getText() ); which didn't work. How is this done?

You can do this:
public void addText(View view) { // Add view's text
Button button = (Button) view; //casts the View into the Button class
TextView calc = (TextView) findViewById(R.id.edit_text);
t.setText(calc.getText().toString() + button.getText().toString());
}
Since we know that Button is a subclass of View, and the getText() method works with buttons, we can make a new variable and turn the View parameter into a Button, via class casting. From there we can use all the Button's methods and continue with execution.

Other way is by setting TAG object to your views
Button mButtonCalcOne = (Button) findViewById(R.id.Button1);
mButtonCalcOne.setTag(1);
OnClickListener mButtonListener = new OnClickListener()
{
#Override
public void onClick( View v )
{
int lButtonValue = (Integer) v.getTag(); //DO STH WITH IT ; )
};
}
mButtonCalcOne.setOnClickListener( mButtonListener );

Related

FindViewById returning null EditText

I have common problem with findViewById() function, it returns null:
<EditText
android:id="#+id/nameText"
/>
<Button
android:text="Save"
android:onClick="buttonClick1"
/>
</TableRow
android:onClick="buttonClick1"
/>
Activity1.java:
public class Activity1 extends ActionBarActivity {
public void buttonClick1(View view) {
setContentView(view);
EditText nameText = (EditText) view.findViewById(R.id.nameText);
EditText lastNameText = (EditText) view.findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) view.findViewById(R.id.indexNumberText);
Log.d(">>>> ", nameText.getText().toString());
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
}
}
}
In buttonClick1() findViewById() returns null. Please explain why?
Remove setContentView(view); from buttonClick1 method
and initialise all your textview in this manner by removing view.
EditText nameText = (EditText) findViewById(R.id.nameText);
EditText lastNameText = (EditText) findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) findViewById(R.id.indexNumberText);
Also initialize the controls in the xml properly by giving height and width to controls
Multiple problems unless you're not posting all your code.
1) In your XML you close a TableRow tag, but I don't see you opening it. Might just be lazy copy-pasting.
2) In your onCreate you seem to be missing a listener for your button. There is nothing to indicate that you have a button anywhere. You need to find the view of your button as follows:
Button button = (Button) findViewById(R.id.nameOfButton);
Then you need to set a listener for it like this:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here goes whatever should happen when you click the button.
}
});
3) As for your ButtonClick1 method, delete it. It looks like you were trying to create a listener, but it is pretty far from what it should look like.
Try to replace your code by this one :
public class Activity1 extends ActionBarActivity {
#
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
EditText nameText = (EditText) findViewById(R.id.nameText);
EditText lastNameText = (EditText) findViewById(R.id.lastNameText);
EditText indexNumberText = (EditText) findViewById(R.id.indexNumberText);
}
public void buttonClick1(View view) {
//Your stuff
Log.d(">>>> ", nameText.getText().toString());
}
}
Since now would be the normal code that have you tried, if you want to set a click on a button you'll have to declare it as :
Button button1 = (Button) findViewById(R.id.button1);
Then you can do the :
public void buttonClick1(View v) {
// Your stuff
}
EDIT
Make sure that you have on your XML all of your EditText and all of your Button for example : or , also make sure that you have an android:id="#+id/yourID in all of your stuff..., by the way instead of using an onClick method in your XML for your Button, you could use this to use an OnClickListener
The point 1 to 3 should go inside of onCreate the point 4 should go outside of onCreate.
1.- Implements View.OnClickListener in your Activity1
public class Activity1 extends ActionBarActivity implements View.OnClickListener {
2.- Declare it
Button button1 = (Button) findViewById(R.id.button1);
3.- Do the setOnClickListener like this :
button1.setOnClickListener(this);
4.- Create an OnClick method :
#Override
public void onClick(View view) {
if (View.equals(button1))
//Your stuff
}

how to get "View" clicked in ListView not "Complete Row" :: android

We make listener for Items(Rows) click listener in ListView. How to get know, in a particular row a specific View get clicked?
R1 --> TextView1 | TextView2
R2 --> TextView1 | TextView2
I can get which row is clicked. but i also want to know which View of the row get clicked.
You need to set position as tag to textView, and then onClick you can receive its tag.
So basicly you set textView's staff on adapter's getView method
textView.setTag(position);
textView.setOnClickListener(listener);
And then you'll have click listener like this
private OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
int position = Integer.parseInt(v.getTag().toString());
// Do whatever you like...
}
};
You can add specific click handlers to the textViews:
<TextView
android:onClick="clickHandler">
Then in your activity:
public void clickHandler(View v) {
// obtain parent row
LinearLayout parentView = (LinearLayout) v.getParent();
// with the parentView you can get all other views as well:
TextView textView1 = (TextView) parentRow.getChildAt(0);
}
I do it like this:
assign onClickListener to whatever can be clicked
I assign tag to it to identify v.setTag(R.id.itemId, item.id);
In onclick I do:
#Override
public void onClick(View v) {
final int id = (Integer) v.getTag(R.id.itemId);
}

onClickListener only working for the last element

In my onCreate, I insert every buttons of my Activity in a ArrayList and I loop on them to bind a clickListener. Only the last element gets the bind. Why is that?
for(Button bouton: tousLesBoutons) {
bouton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
resultat.setText(((Button) v).getText());
}
});
}
i do something similar but in the layout i set the onClick value to the same function for each button and then have a function like below. All 10 buttons hit this function,maybe you can try this approach
public void onButtonClick( View v )
{
Button but = (Button) findViewById( v.getId() );
String input = but.getText().toString();

How to remove a Button added programatically

I am adding buttons to a linear layout at runtime and need to add the functionality of removing them if the user desires. At the moment I have a button which opens a popup with a list consisting of the text for each button added. If possible, would I be able to have each onItemClick delete the corresponding button? If not, what would be the best way to remove a specific button?
Here is the code for adding buttons:
private void addButton(){
LinearLayout lL = (LinearLayout) findViewById(R.id.requirement_linear);
lL.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
p.setMargins(0,2,0,0);
Button b = new Button(this);
b.setBackgroundResource(R.drawable.blue_button);
b.setOnClickListener(openRequirement);
b.setTextColor(Color.parseColor("#FFFFFF"));
String button_text = (index + 2) + ". " + requirement_list.get(index + 1).getName();
b.setText(button_text);
requirements_text.add(button_text);// requirements_text is an arraylist<string> which stores the text so I can display them in my popup to delete them.
index ++;
lL.addView(b,p);
}
You can use removeView() on your LinearLayout and pass your button. You can identify the button on the OnClick(View view) callback, the view there is the button.
as requested, here is an example.
final LinearLayout lL = (LinearLayout) findViewById(R.id.requirement_linear);
Button b = new Button(this);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View button) {
lL.removeView(button);
}
});
lL.addView(b);
Alternatively you can use removeViewAt() to remove a child view by index. You can use the index of your 'list of text of buttons'. Assuming its a listview, you can try this.
lview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
lL.removeViewAt(position);
}
});
here is how i hide and show the buttons
Button b = new Button(this);
b.setVisibility(View.GONE); // this will hide the button and it will now show
b.setVisibility(View.VISIBLE); // this will show the button if it was hidden before
Enjoy!

setClickable() is not working on button

I want to make button unclickable using setClicable() but it's not working. I am using inflater because I need.
This is my code:
mContactList = (LinearLayout) findViewById(R.id.contactList);
LayoutInflater inflater = getLayoutInflater();
for (ListIterator<ContactModel> it = contactList.listIterator(); it.hasNext();){
ContactModel contact = it.next();
View view = inflater.inflate(R.layout.contact_unknown_list_row, null);
view.findViewById(R.id.inviteButton).setTag(contact.getEmail());
view.findViewById(R.id.inviteButton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String address = (String) v.getTag();
sendInvatoin(address);
if(v.findViewById(R.id.inviteButton).isClickable())
v.findViewById(R.id.inviteButton).setClickable(false);
}
});
mContactList.addView(view);
}
Try using.
button.setEnabled(false);
In your case, you will do something like this:
view.findViewById(R.id.inviteButton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String address = (String) v.getTag();
sendInvitatoins(address);
Button b = (Button)v;
b.setEnabled(false);
}
});
When using setOnClickListener, unclickable views (= v.setClickable(false)) would become clickable as mentioned in Docs.
... a callback to be invoked when this view is clicked. If this
view is not clickable, it becomes clickable.
Better to use v.setEnabled(false) if you want to set an OnClickListener to the button or any other view...
This will work in case of Imageview as well as the button.
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (imageview.isEnabled()){
//I have wrapped all code inside onClick() in this if condition
//Your onClick() code will only execute if the imageview is enabled
//Now we can use setEnabled() instead of setClickable() everywhere
}}
};
Inside onCreate(), you can do setEnabled(false) which will be equivalent to setClickable(false).
We are able to use setEnabled() as tag because it's state remains unaffected on invocation of click (unlike setClickable() whose state changes).

Categories

Resources