In my application, I have two buttons. I am taking an input and based on which button is clicked, displaying an output. However, for this, I have to write input and output statements in each of the button's OnClickListener(). This is making the code bulky. Is there any way by which I can write common input and output statements.
My current code looks something like this.
Button1.setOnClickListener(new View.onClickListener() {
public void onClick(View arg0) {
input();
process1();
output();
}
});
Button2.setOnClickListener(new View.onClickListener() {
public void onClick(View arg1) {
input();
process2();
output();
}
});
I want my code to be something like this:
input();
Button1.setOnClickListener(new View.onClickListener() {
public void onClick(View arg0) {
process1();
}
});
Button2.setOnClickListener(new View.onClickListener() {
public void onClick(View arg1) {
process2();
}
});
output();
Is this possible ? If yes then how ? If no then why ?
Thank You !
Edit:
input() is: input = Double.parseDouble(inputString.getText().toString());
output() is: outputString.setText(String.valueOf(output));
input and output are double and defined already.
output is calculated from input by simple arithmetic process1() and process2().
inputString is EditText and outputString is TextView.
Not sure if it's worth the trouble, but you could create an abstract class BaseListener that implements OnClickListener. Its onClick method would look like this :
public void onClick(View arg1)
{
input();
process();
output();
}
process() would be an abstract method.
Each Button would have a listener that extends BaseListener and implements process().
For example :
Button1.setOnClickListener(new BaseListener () {
protected void process() {
process1();
}
});
Is not the best way, but you can extract a method:
void process(int btn) {
input();
if(btn == 1) process1();
else if (btn == 2) process2();
output();
}
And call it this way:
Button1.setOnClickListener(new View.onClickListener() {
public void onClick(View arg0) {
process(1);
}
});
Button2.setOnClickListener(new View.onClickListener() {
public void onClick(View arg1) {
process(2);
}
});
This can be done in many ways...on way is like this.
Button1.setOnClickListener(onClickListener);
Button2.setOnClickListener(onClickListener);
View.OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View view) {
input();
switch (view.getId()) {
case R.id.button1:
process1();
output();
break;
case R.id.button2:
process2();
break;
}
output();
}
};
Related
I want to send the data of a button1 by clicking another button2 is that possible in android, without showing button1 to the user?
You can programmatically "click" a button by calling its callOnClick method.
See this for reference.
You can do that either using View.performClick() or View.callOnClick().
But you should revisit your code. Is the code inside the buttons can be refactored to methods? Is the code can be reused can recall in another button?
What you're code now probably something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
buttonA.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button A clicked.
}
});
buttonB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button B clicked.
}
});
...
}
You can refactor the inside of each of your buttons to methods, something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
buttonA.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button A clicked.
processButtonA();
}
});
buttonB.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do process when button B clicked.
processButtonB();
}
});
...
}
private void processButtonA() {
// something here.
}
private void processButtonB() {
// something here.
}
then, if you need to call process related with the button B from button A, you can do the following:
buttonA.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
processButtonA();
processButtonB();
}
});
I want to do different things for one button.
But I am changing instructions after the button was clicked every time.
First I am creating two buttons, after I clicked the button I want it to change its text. For each text I want to create different instruction.
I created an if/else for this. But I am not sure why my isn’t not working.
I only do instructions in the first statement.
Here is my code.
package com.example.iqbal.destini;
public class MAINPAGE extends AppCompatActivity {
Button mAnswer_1_Button;
Button mAnswer_2_Button;
TextView mTextbody;
int counter1 = 0;
int counter2 = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mainpage);
mAnswer_1_Button = (Button) findViewById(R.id.answer_1);
mAnswer_2_Button = (Button) findViewById(R.id.answer_2);
mTextbody = (TextView) findViewById(R.id.text_body);
if(mTextbody.getText().toString().equals(getResources().getString(R.string.T1_Story)) && mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T1_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T1_Ans1)) ){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T3_Story);
mAnswer_1_Button.setText(R.string.T3_Ans1);
mAnswer_2_Button.setText(R.string.T3_Ans2);
}
});
mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T2_Story);
mAnswer_1_Button.setText(R.string.T2_Ans1);
mAnswer_2_Button.setText(R.string.T2_Ans2);
}
});
}
else if(mTextbody.getText().toString().equals(getResources().getString(R.string.T2_Story))&& mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T2_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T2_Ans1))){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T3_Story);
mAnswer_1_Button.setText(R.string.T3_Ans1);
mAnswer_2_Button.setText(R.string.T3_Ans2);
}
});
mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T4_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
}
else if(mTextbody.getText().toString().equals(getResources().getString(R.string.T3_Story))&& mAnswer_2_Button.getText().toString().equals(getResources().getString(R.string.T3_Ans2))&&mAnswer_1_Button.getText().toString().equals(getResources().getString(R.string.T3_Ans1))){
mAnswer_1_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T6_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
**mAnswer_2_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextbody.setText(R.string.T5_End);
mAnswer_1_Button.setVisibility(View.GONE);
mAnswer_2_Button.setVisibility(View.GONE);
}
});
}
}
}**
mTextbody.getText() always is empty where it's currently located (unless you have a default string in the TextView)
Move the if conditions into the onClick method body.
Conditionally perform the action. Don't conditionally add the listener.
And you only need one OnClickListener per button
I am making a simple calculator and for the -/+ button i write the code bellow:
float ab=-1;
this.txtScreen = (EditText) findViewById(R.id.txtScreen);
unary=(Button)findViewById(R.id.plusminus);
mod=(Button)findViewById(R.id.modul);
unary.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtScreen.setText(ab*txtScreen);
}
});
and showing me error * operator cannot applied to float.
Try this (it needs string):
float ab=-1;
unary=(Button)findViewById(R.id.plusminus);
mod=(Button)findViewById(R.id.modul);
unary.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtScreen.setText(String.valueOf(ab * Float.valueOf(txtScreen.getText().toString())));
}
});
Edit: Getting the value from the txtScreen in calculation.
Do like this
float ab=-1;
unary=(Button)findViewById(R.id.plusminus);
mod=(Button)findViewById(R.id.modul);
unary.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtScreen.setText(String.valueOf(ab*Float.parseFloat(txtScreen.getText().toString())));
}
});
I'm doing a calculator for fun, the code is in this post:
Start of activity not working Java
I have many buttons in my calculator, and one of them is a button that i want to use to clear the last charater of text in the textbox when i click and when i press it for more that x seconds i want it to clear all the text.
I've searched for many posts and i learned that the OnTouch method using handler is good for things that are being pressed, but how can i implement both things for my button?
Thanks in advance for the answers!
UPDATE
I used the Onclick listener and OnLongClick listener and it works exactly as i want!
You just want to set a View.OnClickListener, and a View.OnLongClickListener, like so:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s = mEditText.getText().toString();
if (s != null && s.length() > 0) {
mEditText.setText(s.substring(0, s.length()-1));
}
}
});
mButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mEditText.setText("");
return true;
}
});
Do this:
Button myClearButton;
//After doing myClearButton = findViewById(int id) or other methods of getting your button call this method:
setClearButtonClickListeners(){
myClearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
clearLastChar();
}
});
myClearButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
clearAllChars();
return true;
}
});
}
Just implement the clearLastChar() and clearAllChars() methods;
clearButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clear();
Toast.makeText(HomeSafeActivity.this, "Normal Press", Toast.LENGTH_LONG).show();
});
clearButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(HomeSafeActivity.this, "Long preess", Toast.LENGTH_LONG).show();
yourEditText.setText("");
return true;
}
});
public void clear(){
Editable editableText = yourEditText.getEditableText();
int length = editableText.length();
if (length > 0) {
editableText.delete(length - 1, length);
}
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//
}
});
btn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//
return true;
}
});
I have an activity with custom popup window (quickaction style). There are some buttons leading to other activities. I want to close popup after pressing button (about or email button) in this popup (now when I go back popup appears again).
public class FirstActivity extends Activity implements OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// some code
Button quickButton = (Button) findViewById(R.id.button_quickaction);
quickButton.setOnClickListener(this);
final ActionItem about = new ActionItem();
final ActionItem email = new ActionItem();
quickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
QuickAction qa = new QuickAction(v);
qa.addActionItem(about);
qa.addActionItem(email);
qa.setAnimStyle(QuickAction.ANIM_GROW_FROM_RIGHT);
qa.show();
}
});
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some code
}
});
email.setTitle("Email");
email.setIcon(getResources().getDrawable(R.drawable.email));
email.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some code
}
});
}
}
Please, help.
Added:
I need something like this:
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
qa.dismiss();
}
});
But qa cannot be resolved. Even if I add final to QuickAction qa = new QuickAction(v);.
try finish() on button's onClick method.
updated:
QuickAction qa;
quickButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
qa = new QuickAction(v);
qa.addActionItem(about);
qa.addActionItem(email);
qa.setAnimStyle(QuickAction.ANIM_GROW_FROM_RIGHT);
qa.show();
}
});
about.setTitle("About");
about.setIcon(getResources().getDrawable(R.drawable.about));
about.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(qa.isShowing())
qa.dismiss();
//some code
}
});
email.setTitle("Email");
email.setIcon(getResources().getDrawable(R.drawable.email));
email.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(qa.isShowing())
qa.dismiss();
//some code
}
});
you can also put private QuickAction qa; at your activity.