I had two Button:
<Button
android:id="#+id/fragment_remote_control_zeroButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="0" />
<Button
android:id="#+id/fragment_remote_control_oneButton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="1" />
And there is a listener for both of them:
View.OnClickListener numberButtonListener = new View.OnClickListener() {
public void onClick(View v) {
TextView textView = (TextView)v;
String working = mWorkingTextView.getText().toString();
String text = textView.getText().toString();
if (working.equals("0")) {
mWorkingTextView.setText(text);
} else {
mWorkingTextView.setText(working + text);
}
}
};
in listener's onClick(View v)method, the method parameter is The view that was clicked, that here is the clicked Button
but i'm wonder how it cast a Button to a TextView???is that refer to the text value in Button or not???
http://developer.android.com/reference/android/widget/Button.html
Button is actually a subclass of TextView
However, the code is definitely strange, in order to identify which button is clicked, you can check the id
e.g.
if (v.getId() == R.id.fragment_remote_control_zeroButton) {
mWorkingTextView.setText(text);
} else {
mWorkingTextView.setText(working + text);
}
Related
I have set an event listener for two buttons and event listener for a EditText control in my java code:
public void doNewButtonClick( View view ) {
View.OnClickListener onSnap = new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView calculatorTextView = (TextView) findViewById(R.id.textView);
calculatorTextView.setText( "GO TO SLEEP" );
Log.d(TAG, "In the Handler for NEW BUTTONS");
}
};
Button button1 = (Button) findViewById(R.id.buttonA);
Button button2 = (Button) findViewById(R.id.buttonB);
button1.setOnClickListener( onSnap );
button2.setOnClickListener( onSnap );
}
public void onEditTextInput( View view ){
EditText myMessage = (EditText) findViewById(R.id.userText);
myMessage.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int actionID, KeyEvent event) {
Log.d(TAG, "BEFORE THE SEND");
EditText userMessage = (EditText) textView;
TextView phone = (TextView)findViewById(R.id.textView2);
if( actionID == EditorInfo.IME_ACTION_GO){
Log.d(TAG, "IN THE SEND");
phone.setText( userMessage.getText() );
}
return false;
}
});
}
This is my xml:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/buttonA"
android:layout_below="#+id/button6"
android:layout_toLeftOf="#+id/button6"
android:layout_toStartOf="#+id/button6"
android:layout_marginTop="119dp"
android:drawableLeft="#drawable/abc_ic_star_black_16dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/buttonB"
android:layout_alignBottom="#+id/buttonA"
android:layout_alignRight="#+id/button6"
android:layout_alignEnd="#+id/button6" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/userText"
android:layout_above="#+id/buttonB"
android:layout_alignLeft="#+id/buttonA"
android:layout_alignStart="#+id/buttonA"
android:imeOptions="actionGo"
android:inputType="text"
android:layout_alignRight="#+id/buttonB"
android:layout_alignEnd="#+id/buttonB" />
I have two issues:
1) The event handlers don't work unless I also go into the control's properties and set the OnClick property to the desired method. In other words, I'll click a control many times and nothing will happen unless I set the OnClick property to a function, then the controls and their event handlers work as expected.
2) Setting the OnClick property of each control to the desired event handler( callback function) forces me to have to click the control twice for it to work. This happens since I am setting the OnClick property and also defining the OnClick function in the event handler.
Why are my event handlers not responding? Why do I have to set the OnClick property? Am I forced to set the OnClick property and not use the Anonymous class method to make event handlers?
Thank you!!
You can simple use onClick property on button as shown below
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/buttonB"
android:onClick="doNewButtonClick" //<--- Like this
android:layout_alignBottom="#+id/buttonA"
android:layout_alignRight="#+id/button6"
android:layout_alignEnd="#+id/button6" />
And your method will be
public void doNewButtonClick(View v){
// you can handle different views based on the
//'id' you get v.getId() and handle different functionalities for each button.
}
Or simply
Button btn = (Button) findViewById(R.id.mybutton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do anything
}
});
You can use either one of them.
i am basically developing a small mathematics app, in a activity their will be problems like additions subtractions etc. the user has to fill the answers in edittext from the custom buttons from 0-9, a dot, a slash button and a backspace button which i created on the same activity. now i like to add up and down button, so that when the up button is pressed the cursor has to move towards upside edit text and vice versa.
here is a sample code which i used
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#android:id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<EditText
android:id="#android:id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<Button
android:id="#android:id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
Class:
public class Example extends Activity {
TextView et1;
TextView et2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) findViewById(android.R.id.et1);
et2 = (EditText) findViewById(android.R.id.et2);
Button button = (Button) findViewById(android.R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Selection.setSelection((Editable) et2.getText(), et1.getSelectionStart());
et2.requestFocus();
}
});
}
}
You could create an array of of your EditTexts and a variable containing your current position, defaulted to 0, meaning the first text box. Then if the user presses the up button, if the variable is greater than 0, set the position -1 and then get the textbox object from the array and call focus(). Below is an example piece of code, its not accurate but should get you started
List<EditText> textfields = null;
int currentTextFieldPosition = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textfields = new ArrayList<EditText>();
textfield1 = (EditText)findViewById(R.id.textfield1);
.....
textfields.add(textfield1);
......
}
protected OnClickListener mBtnUpClickListener = new OnClickListener()
{
public boolean onClick()
{
if (currentTextfieldPosition > 0)
{
currentTextfieldPosition--;
textfields.get(currentTextFieldPosition).focus()
}
}
}
So I have a 2 buttons in a layout
like so:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onClick">
<Button
android:id="#+id/btnEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit Spot" />
<Button android:id="#+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"/>
</LinearLayout>
Then in the onCreate for the activity for this I have:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_spot);
// save button
ButtonEdit = (Button)findViewById(R.id.btnEdit);
ButtonGo = (Button) findViewById(R.id.btnGo);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
// Getting complete product details in background thread
new GetProductDetails().execute();
// save button click event
ButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Edit button pressed", Toast.LENGTH_LONG).show();
}
});
// Go button click event
ButtonGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Go button pressed!", Toast.LENGTH_LONG).show();
}
});
}
But for some reason when I tap the buttons nothing happens. I've looked around at others asking the question and tried those but still nothing. Can someone please help me figure out why it isn't working?
Thank you,
Tyler
You have to add the onClick properties in the Button's definition, not in the LinearLayout.
Even if you want to reuse the same method called onClick you can set a tag for each button, and do a switch for each tag. For instance:
In your layout:
android:tag="1"
In your code:
public void onClick(View v) {
String tag = (String) v.getTag();
switch (Integer.parseInt(tag)) {
case 1: // First button
...
break;
case 2: // Second button
...
break;
}
}
Remove onClick from your LinearLayout. Right now you have nested onClickListeners. The LinearLayout is intercepting the event and likely not passing it down.
edit: somebody is blindly downvoting all these answers for some reason. Post a comment explaining why this is wrong.
Please make your Button(s) clickable:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="onClick">
<Button
android:id="#+id/btnEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Edit Spot"
android:clickable:true />
<Button android:id="#+id/btnGo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Google Spot"
android:layout_weight="1"
android:clickable:true/>
</LinearLayout>
Also implement the onClick() method in your Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_spot);
// save button
ButtonEdit = (Button)findViewById(R.id.btnEdit);
ButtonGo = (Button) findViewById(R.id.btnGo);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
// Getting complete product details in background thread
new GetProductDetails().execute();
// save button click event
ButtonEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Edit button pressed", Toast.LENGTH_LONG).show();
}
});
// Go button click event
ButtonGo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Go button pressed!", Toast.LENGTH_LONG).show();
}
});
}
public void onCLick(View v)
{
//Your Implementation
}
I hope this helps.
I have the following text with me.
"I drink tea and coffee". Now the requirement is to change this text to "I drink EditText AND EditText"....Here the EditText is a edit box where in the user can enter answers once it is clicked. I need to make this change pro grammatically.
Any suggestions on this, as to how this can be achieved????
You could use the following code in button's click event handler:
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
You can ctreate activity structure in your layout xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="#+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="#+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
And set listners to your buttons
private String[] answers = { "tea", "coffee", "juice", "compote" };
...
Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
firstAnswer.setOnClickListener(new OnClickListener() {
private int position = 0;
#Override
public void onClick(View view) {
((Button) view).setText(answers[position]);
position++;
if (position == answers.length)
position = 0;
}
});
By using getText()
Example
Button mButton;
EditText mEdit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}
after that
mEdit.setText("Tea");
EditText et=(EditText)findViewByID(R.id.yourId);
Button bt=(Button)findViewById(R.id.yourBtId);
bt.setOnClickListener(
new View.setOnClickListener()
{
public void onClick(View v)
{
et.setText("You Drink EditText");
}
});
Put These code in onCreate() method
Use ViewSwitcher. This widget displays one of two views it contains:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ViewSwitcher>
And then, when button is pressed, switch it and load text from EditText to TextView or vice versa:
editText.setText(textView.getText());
viewswitcher.showNext();
or
textView.setText(editText.getText());
viewswitcher.showPrevious();
Refer to this for details.
You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively.
On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.
Just Use this : yourTextField.setText("..."+yourEditText.getText().toString());
I need to disable the click-event for a button in Android. Just as a sample, I have tried doing the following. I have taken a TextView named it (entered a text) as Name. The condition checks if, TextView is empty button and clickable should be set to false. However this does not happen when the Toast is printed. Can somemone tell me the reason. Also if the text field is not empty I want to reset the clickable event for button as true.
Java File:
public class ButtonclickabkeActivity extends Activity {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Name");
btn = (Button) findViewById(R.id.button1);
if (tv.getText().toString().length() != 0) {
btn.setClickable(false);
Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
} else {
btn.setClickable(true);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use
btn.setEnable(false);
instead of
btn.setClickable(false);
User 370305 is correct. .setEnable is what your looking for. Or you could use android:clickable in the layout XML file.
In my case
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.setEnabled(false);
}
});