want to perform something untill button is pressed Android - java

In android,
for example
i want to perform a text.settext("something dynamic") until the buTTon is pressed. how to do it?

Try this:
android.os.Handler mHandler = new Handler();
Runnable rUpdateTextView = new Runnable() {
#Override
public void run () {
yourTextView.setText(returndate());
// Update your TextView every 200ms
mHandler.postDelayed(this, 200);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
[...]
mHandler.post(rUpdateTextView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mHandler.removeCallbacks(rUpdateTextView);
}
});
}

//click event for your button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String time = returndate();
//set your text to textView
test.setText(time);
}
});
}

Related

Different instructions for one button

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

How to automatically Click a Button in Android Every Second

How to automatically Click a Button in Android after a 5 second delay
I tried with the codes that are entered in the link but my application has crashed
My codes;
public class MainActivity extends AppCompatActivity {
Button button;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonClick();
Thread timer = new Thread() {
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
button.performClick();
}
}
};
timer.start();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
button.performClick();
}
}, 1000);
}
public void buttonClick() {
button=(Button) findViewById(R.id.button);
text=(TextView) findViewById(R.id.text);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Random s=new Random();
int number=s.nextInt(3);
switch (number)
{case 1:text.setText("1");
break;
case 2: text.setText("2");
break;
}
}
});
}
}
Logcat Error
You need to use
runOnUiThread(new Runnable() {
#Override
public void run() {
}
});
to avoid this error.
Please check Android "Only the original thread that created a view hierarchy can touch its views."
Also you can just use the Handler to perform the button click after a specified amount of time, no need to use the timer.
This is more simpler method to run every second. you dont need to trigger the button. just call the method you want to execute
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Handler handler = new Handler();
Runnable runnable = new Runnable(){
#Override
public void run() {
buttonClick();
if(handler!=null)
handler.postDelayed(runnable, 1000);
}
}
handler.postDelayed(runnable, 1000);
}

How to Register Multiple Listeners in Android?

In iOS I can register multiple target+action event handlers for e.g. a button like this:
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(20, 40, 300, 40)];
[button setBackgroundColor:[UIColor blueColor]];
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
[button addTarget:self action:#selector(handler1) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:#selector(handler2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)handler1 {
NSLog(#"Click handler 1"); // FIRES!
}
- (void)handler2 {
NSLog(#"Click handler 2"); // FIRES!
}
#end
I want to achieve something similar in Java/Android, unfortunately this won't work so far as the first listener gets replaced by the second:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setText("Click Me!");
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Test", "Click handler 1"); // DOES NOT FIRE!
}
});
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("Test", "Click handler 2"); // FIRES!
}
});
}
}
I appreciate any help & suggestions to achieve the same in Java/Android!
Read about RxJava it is the future :)
http://reactivex.io/
ViewObservable.clicks((Button) getActivity().findViewById(R.id.comment_btn))
.compose(lifecycleMainObservable().<OnClickEvent>bindLifecycle())
.subscribe(new Action1<OnClickEvent>() {
#Override
public void call(OnClickEvent onClickEvent) {
Log.d("Button click", "Button 1 click");
}
});
ViewObservable.clicks((Button) getActivity().findViewById(R.id.comment_btn))
.compose(lifecycleMainObservable().<OnClickEvent>bindLifecycle())
.subscribe(new Action1<OnClickEvent>() {
#Override
public void call(OnClickEvent onClickEvent) {
Log.d("Button click", "Button 2 click");
}
});
Is it not handled the same way in Android and it is just simpler to create two methods as such:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
method1();
method2();
}
});
private void method1(){
Log.d("Test", "Click handler 1");
}
private void method2(){
Log.d("Test", "Click handler 2");
}
As the docs for View.OnClickListener state:
Register a callback to be invoked when this view is context clicked. If the view is not context clickable, it becomes context clickable
Multiple callbacks are not implemented by default, but you could implement it yourself.

Button finish activity after 2 press is WRONG: Android

I want the button to finish the activity. Why do I have to press it TWICE for it to finish, I do not want this. Note, I have to press the button twice before the activity ends, which I do not want.
MainActivity Class:
private Runnable updateOkay= new Runnable() {
public void run() {
if (true) {
Intent i = new Intent(this, WorkTimerNotification.class);
startActivity(i); }
WorkTimerNotification class:
public Button confirmButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
confirmButton = (Button) findViewById(R.id.confirmOK_button);
confirmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
Solved by setting
android:launchMode = "singleInstance"
in AndroidManifest.xml file.

How to close popup window?

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.

Categories