How to change first button's value by pressing another one [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What I am seeking is to have button that has a x value and after pressing another button to change the value of the first. e.x first button value is, when pressed to open flashlight constantly, then I press the second button and now first's value button has changed, when pressed flashlight blinks!
Thanks in advance
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
// playSound();
params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
Change.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isFlashOn) {
// i want it to start blinking
} else {
//wait till opens to start blinking
}
}
}
}
and on clicking again button change, everything to return to normal.

Related

Calling onClick without clicking [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Do you know how to call the onClick method assigned to a button in the XML layout file without clicking the button? I've tried the performClick method already.
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
Button signUpButton = findViewById(R.id.signUpButton);
try {
signUpButton.performClick();
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
Step 1. Extract code performed in onClick to a method
Step 2. Call this method in place of button.performClick()
try callOnClick:
findViewById(R.id.hello).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.hello).callOnClick();
Do you know how to call the onClick method assigned to a button in the XML layout file without clicking the button?
You can call a function for when the user clicks the button and call that function independently.
#Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
if(i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
OnbuttonPressed();
}
return false;
}
....
public void OnbuttonPressed(){
Button signUpButton = findViewById(R.id.signUpButton);
try {
signUpButton.performClick();
} catch (Exception e) {
e.printStackTrace();
}
}
Then you can call the function "OnbuttonPressed()" anywhere else.

how can i get functionality like show/hide buttons in Android gallery on screen tap [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
In first image if you tap on screen than android gallery hide the all buttons as shown in second image while on again touch the screen you will find that all buttons re appear again. Note: On Zooming in-out,on Double tap or moving finger on image of galley don't effect the state it effect only when user tap on screen i want functionality like this. Please help me how can i implement such type of functionality to my Android app
please help me to solve it i had tried onTouchEvent etc with MotionEvent.ACTION_DOWN but it work every time when zoom in-out means every time when i tap on screen it workswhile ondouble tap or move finger on screen.
you can use 'GestureDetector' instead of ACTION_DOWN. Example,
GestureDetector gestureDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
//------------------apply your logic here------------
return super.onSingleTapUp(e);
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return super.onSingleTapConfirmed(e);
}
});
and pass touch event to gesture.
#Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
Edited
After watching comment,
now issue is onTouchEvent not called if pdfviewer attached => This haapen because pdfviewer already consumed touch event.
So you need to use dispatchTouchEvent instead of that. check here
#Override
public boolean dispatchTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return super.dispatchTouchEvent(event);
}

Android - setting up guidelines in the camera [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was wondering if there was a way to add a vertical and horizontal yellow lines in the middle of the width and length respectively on the camera, so that the user has a rough guideline when making the photo. I can post code if needed. Thanks in advance.
This code may help your:
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.setContentView(R.layout.guideline);
dialog.setCanceledOnTouchOutside(true);
//for dismissing anywhere you touch
View masterView = dialog.findViewById(R.id.guidelineView);
masterView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();

Correct use of save button code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So I've been learning and trying to build upon an app I made by following an online tutorial. It is a simple, bare bones, note taking application. The mainActivity simply shows note objects in a list view. The second screen/activity is the one I'm currently working on, trying to add code where I can. So far I've added a save button that will simply save the text/string value and take the user back to the main activity. I would like some feedback as to my implementation of the onButtonSave method:
public class NoteEditorActivity extends Activity {
private NoteItem note;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note_editor);
getActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = this.getIntent();
note = new NoteItem();
note.setKey(intent.getStringExtra("key"));
note.setText(intent.getStringExtra("text"));
EditText et = (EditText) findViewById(R.id.noteText);
et.setText(note.getText());
et.setSelection(note.getText().length());
// I'm wondering if this is the correct way to call my onButtonSave method
onButtonSave();
}
private void saveAndFinish() {
EditText et = (EditText) findViewById(R.id.noteText);
String noteText = et.getText().toString();
Intent intent = new Intent();
intent.putExtra("key", note.getKey());
intent.putExtra("text", noteText);
setResult(RESULT_OK, intent);
finish();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
saveAndFinish();
}
return false;
}
#Override
public void onBackPressed() {
saveAndFinish();
}
// This is the code I've added for the save button.
public void onButtonSave(){
final Button button = (Button) findViewById(R.id.saveButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
button.setText("Saved!");
saveAndFinish(); }
});
}
}
I assume you are curious as to whether or not you have covered all your cases. Breaking up the setup of listeners and UI components from your onCreate into separate methods can be a good practice for easier readability when there are a lot of things being initialized.
You cover the case when the back button is used.
You cover the case when the user presses the button.
From what can be seen you also cover the case with the menu selection of leaving the screen. A couple people have talked best about how to detect whether the screen goes to the background or not. If you really want to catch all cases, you can do your save within the onPause() of an Activity. This will be fired if you press back, go home or call another activity.
Distinguishing between another activity and the home button is tough. But some people have pointed towards onUserHint() as a way to detect this. Just thought I would provide some feedback to what I can understand of your question.

how can i perform some action on click of two buttons simultaneously [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
public void onClick(View v)
{
if (v.getId()== R.id.but1 && v.getId()== R.id.but2)
{
Intent intent=new Intent(First.this,Second.class);
startActivity(intent);
}
}
There isn't such event that can be associated to two controls. event handlers only associated to one control and that is different than assigning the same listener to two button. listener will receive a call from every button separately.
Also, listeners will never be triggered together because both run in the same thread (The UI thread). It's impossible to catch on click event for both controls at some moment. one listener will be triggered and then the other. Even if we assumed that the user managed to click those together at the same millisecond or so in the perfect world. Any way who can decide that when they are clicked at same millisecond they considered to be clicked to gather! why not same nanosecond. and why not the same hour :)
Ok, it's enough explaining the click event.
What we need is the touch event and it can be played as follow (the code will also explain how the touch event work):
Activity Class Members:
public boolean b1Down = false, b2Down = false;
onCreate Method Code:
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);
b1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
boolean consume = false;
if (event.getAction() == MotionEvent.ACTION_UP)
{
b1Down = false;
}
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b1Down = true;
if (b2Down)
{
// both are clicked now //
Toast.makeText(MainActivity.this, "Both are clicked now!", Toast.LENGTH_SHORT).show();
}
consume = true;
}
return consume;
}
});
b2.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
boolean consume = false;
if (event.getAction() == MotionEvent.ACTION_UP)
{
b2Down = false;
}
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
b2Down = true;
if (b1Down)
{
// both are clicked now //
Toast.makeText(MainActivity.this, "Both are clicked now!", Toast.LENGTH_SHORT).show();
}
consume = true;
}
return consume;
}
});

Categories

Resources