I have this in my code and the .setPressed doesn't work:
Button btndesligado = (Button) findViewById(R.id.button12);
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});
What is wrong? It doesn't give me any error, it just doesn't work when I open it and click it. It was suposed to be pressed after I press once.
Android is changing the setPressed both before and after your onClickEvent
so change your code this code
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});
to this
btndesligado.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// show interest in events resulting from ACTION_DOWN
if (event.getAction() == MotionEvent.ACTION_DOWN) return true;
// don't handle event unless its ACTION_UP so "doSomething()" only runs once.
if (event.getAction() != MotionEvent.ACTION_UP) return false;
btndesligado.setPressed(true);
som = false;
vibrar = false;
return true;
}
});
It seems nothing wrong with your codes, It's working as you did, It sets the View's internal state to "pressed", or false to reverts the View's internal state from a previously set "pressed" state. And you've put that inside onClick of button means it acts only after you press the button,
If you want it pressed or revert then do like this,
Button btndesligado = (Button) findViewById(R.id.button12);
btndesligado.setPressed(true);
btndesligado.setOnClickListener(new OnClickListener() { //Botão para pôr silêncio!
#Override
public void onClick(View v) {
som = false;
vibrar = false;
}
});
Try this way.
btndesligado.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btndesligado.setPressed(true);
som = false;
vibrar = false;
}
});
Related
When opening the menu in my application it shows an extra option depending on the state of the game (paused or not started) however the controlling variable for what the button should do always returns as false.
I have attempted splitting the if statements into separate statements and performing the processing that was accepting the hit to performance but no changes happened to the outputs, upon hovering over the else if statement within the btnMenu method onClick it says:
Condition '!paused' is always 'true' when reached
related code below:
Button btnStart;
Button btnMenu;
Button btnQuit;
Button btnShop;
Button btnSettings;
ImageView swipeDetector;
boolean menuToggle = false;
int gameRunning = 0;
boolean paused = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//calling object from activity_main.xml
btnStart = (Button) findViewById(R.id.startButton);
btnMenu = (Button) findViewById(R.id.menuButton);
btnQuit = (Button) findViewById(R.id.quitBtn);
btnShop = (Button) findViewById(R.id.shopButton);
btnSettings = (Button) findViewById(R.id.settingsBtn);
swipeDetector = (ImageView) findViewById(R.id.swiperReader);
btnQuit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(gameRunning == 1) {
gameRunning = 0;
menuToggle = false;
paused = false;
//set game variables back to defaults
btnQuit.setVisibility(View.GONE);
btnSettings.setVisibility(View.GONE);
btnShop.setVisibility(View.VISIBLE);
btnStart.setVisibility(View.VISIBLE);
}
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gameRunning = 1;
menuToggle = false;
paused = false;
btnStart.setVisibility(View.GONE);
btnShop.setVisibility(View.GONE);
}
});
//broken, for some reason gameRunning always == 0
btnMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (gameRunning == 1){
paused = true;
}
if (!menuToggle && paused){
btnStart.setVisibility(View.GONE);
btnSettings.setVisibility(View.VISIBLE);
swipeDetector.setVisibility(View.GONE);
btnQuit.setVisibility(View.VISIBLE);
menuToggle = true;
//broken if statement
}else if(!menuToggle && !paused){
btnStart.setVisibility(View.GONE);
btnSettings.setVisibility(View.VISIBLE);
swipeDetector.setVisibility(View.GONE);
menuToggle = true;
} else {
paused = false;
btnSettings.setVisibility(View.GONE);
btnQuit.setVisibility(View.GONE);
btnStart.setVisibility(View.GONE);
swipeDetector.setVisibility(View.VISIBLE);
if(gameRunning == 0) {
btnStart.setVisibility(View.VISIBLE);
}
menuToggle = false;
}
}
});
There was no problems with the program as i was using a code together feature with some friends at university the current version of the application would not upload to the virtual device and so after hours of trying to find a solution to some very basic code it turned out that it was an issue with Floobits code together. Sorry for the confusion.
the solution as mentioned in the first comment was that this was stating my the time the else if statement was reached the only possible scenario would be the value would be true not that the value is always true throughout the code.
You are declaring a boolean condition that must be true, remove the !menuToggle from both the if and else if and it becomes more clear. Often, when I get into these situations I spend time cleaning up my code first and that often helps me sort out the issue. Here is an example of the last onClick method cleaned up.
public void onClick(View view) {
if (!menuToggle) {
btnStart.setVisibility(View.GONE);
btnSettings.setVisibility(View.VISIBLE);
swipeDetector.setVisibility(View.GONE);
menuToggle = true;
if(paused) {
btnQuit.setVisibility(View.VISIBLE);
}
} else {
btnSettings.setVisibility(View.GONE);
btnQuit.setVisibility(View.GONE);
btnStart.setVisibility(View.GONE);
swipeDetector.setVisibility(View.VISIBLE);
if(gameRunning == 0) {
btnStart.setVisibility(View.VISIBLE);
}
paused = false;
menuToggle = false;
}
}
i have a list with surveys and all those surveys contains a picture. when you finish a survey the app returns to the overview where i can see all completed and all uncompleted surveys and all rejected surveys.
What i want:
Now if i click long on the screen and the survey isnt rejected and is completed i want to get a preview of the picture as long as i press the screen and disappear if i release the screen.
The problems are:
How can i cancel or dismiss a AlertDialog without adding a positive
Button?
Is it possible to get the MotionEvent without an onTouch Listener and
just with longClickListener?
Here is my code with an onTouch and a longClick-listener and i want to eliminate one of them:
row.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
row.setOnLongClickListener(l -> {
// V3.0 only execute long click if the survey hasn't been declined
if (getContext() instanceof SurveyListPatientActivity && !erhebung.rejected() && !erhebung.isCompleted()) {
((SurveyListPatientActivity) getContext()).showRejectSurvey(erhebung);
}else if(getContext() instanceof SurveyListPatientActivity && !erhebung.rejected() && erhebung.isCompleted()){
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
ImageView imageView = new ImageView(getContext());
byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
imageView.setImageBitmap(bitmap);
imageView.setPadding(10,10,10,10);
adb.setView(imageView);
adb.create().show();
if (event.getAction() == MotionEvent.ACTION_UP){
// cancel AlertDialog
}
}
return true;
});
return false;
}
});
Thank you very much!
EDIT:
Whit the help of #brianoqr i changed my code into this.
Everything works fine but the Dialog dosent disappear.
row.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
ImageView imageView = new ImageView(getContext());
byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
imageView.setImageBitmap(bitmap);
imageView.setPadding(10,10,10,10);
adb.setView(imageView);
dialog = adb.create();
dialog.show();
isLongPressed = true;
return true;
}
});
row.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
System.out.println("1");
if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("2");
if (isLongPressed) {
dialog.cancel();
System.out.println("canceld");
isLongPressed = false;
}
}
return false;
}
});
I probably would do it differently, but to answer the question how to cancel a dialog with out a button:
AlertDialog dialog = adb.create();
dialog.show()
if (event.getAction() == MotionEvent.ACTION_UP){
dialog.dismiss()
}
The code you have written will not work as you want, the onlongpress is the leading action, you can structure your code something like in this ticket: Android - Detect End of Long Press
And your dialog will then need to be in a different scope to allow for automatic dismissal
EDIT
row.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
if(!isLongPressed){
isLongPressed = true;
AlertDialog.Builder adb = new AlertDialog.Builder(getContext());
ImageView imageView = new ImageView(getContext());
byte[] bytes = Base64.decode(erhebung.getPic(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
imageView.setImageBitmap(bitmap);
imageView.setPadding(10,10,10,10);
adb.setView(imageView);
dialog = adb.create();
dialog.show();
}
return true;
}
});
row.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
System.out.println("1");
if (event.getAction() == MotionEvent.ACTION_UP) {
System.out.println("2");
if (isLongPressed) {
dialog.cancel();
System.out.println("canceld");
isLongPressed = false;
}
}
return false;
}
});
I am using this code but its not working i am still able to click more than 2 times.
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
accept.setEnabled(false);
if (dialog1 != null && dialog1.isShowing()) {
dialog1.dismiss();
}
handler.removeCallbacks(runnable);
}
});
its not showing any error but not working as desired.
boolean run = true;
accept.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(run) {
run = false;
//your code
}
}
});
I have an edittext and want to change the border color after onclick.
After I click on it it shows me the red border color. But After I try it again nothing happens. It is still red.
first click red -> second click black -> third click red and so on
How can I fix it?
...
boolean focus = false
...
private void setOnFocusChangeListener(final EditText editText) {
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
editText.setBackgroundResource(R.drawable.black);
focus = false;
} else if (hasFocus) {
editText.setBackgroundResource(R.drawable.red);
focus = true;
} else if ((hasFocus) && focus) {
editText.setBackgroundResource(R.drawable.black);
focus = false;
}
}
});
}
In your case instead of onFocusChangeListener() you can use onClickListener() as you want to change on each click, you can do something like this :
int res = R.drawable.black; // Your default background
etEmailAdress.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (res == R.drawable.black) {
res = R.drawable.btn_green;
} else {
res = R.drawable.black;
}
etEmailAdress.setBackgroundResource(res);
}
});
I'm making a simple android app for recording sound. I have a startRecording() and stopRecording() methods. Now I implemented a ToggleButton named "Touch to record" so as you can already imagine, when the button is checked, you have to hold the record button to record sound and when the button is on "off" you have to click to start and then click to stop.
This is the current code:
touchToRecord.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked)
{
recBtn.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
chTimer.start();
chTimer.setTextColor(Color.GREEN);
startRecording();
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
chTimer.stop();
stopRecording();
nameAlert();
}
return true;
}
});
}
else
{
//onClickListener
}
}
});
Now I'm not sure how to make the onClickListener. If I try to do it like this:
recBtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
It won't work because it underlines setOnClickListener and says:
The method setOnClickListener (View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})
Also, one more thing after I get this working; how do I check if the method is already running with if statement? I want to do something like this:
if (startRecording == isRunning)
{
stopRecording();
}
You could try android toggle button. Please see more information on:
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
Solved the problem!
I avoided using onClickListener by setting onClick in XML and then just creating the method. Here's the code:
public void recordBtnClick(View v){
final ToggleButton touchToRecord = (ToggleButton)findViewById(R.id.tBtn1);
final ImageButton recBtn = (ImageButton) findViewById(com.whizzappseasyvoicenotepad.R.id.recButton);
if (touchToRecord.isChecked() == false)
{
if (recorder == null)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn_pressed);
chTimer.start();
chTimer.setTextColor(Color.GREEN);
startRecording();
}
else if (recorder != null)
{
recBtn.setImageResource(com.whizzappseasyvoicenotepad.R.drawable.record_btn);
chTimer.stop();
stopRecording();
nameAlert();
}
}
else
{
//DO NOTHING
}
}