I may be asking a basic question, but to be honest, I have no real developement or code knowledge. I've been requested to make a prototybe of some basic app, that is supposed mainly to be buttons on screens, activable or desactivable. I've written some kind of TL;DR in case my explanations are bad
I've been coding this on Android Studio 3.0, I (hardly) managed to place PNGs files on the screen, making it looking like a button.
Thing is, while some parts of the app are mainly constituted with independants togglable button. There a part where pressing a button must deselect the others. AND, if this button is pressed a second time open another activities.
Here's part of my code I'm using.
This one for independants buttons
indbutton1.setOnTouchListener(new View.OnTouchListener(){
// track if the image is selected or not
boolean isSelected = true;
public boolean onTouch(View v, MotionEvent event) {
if(isSelected) {
indbutton1.setImageResource(R.drawable.indbutton1slct);
} else {
indbutton1.setImageResource(R.drawable.indbutton1nosl);
}
// toggle the boolean
isSelected = !isSelected;
return false;
}
});
And this one for going into other activities
movements.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent gestesActivity = new Intent (getApplicationContext(), movements.class);
startActivity(movementsActivity);
finish();
}
});
TL;DR
How should I proceed to have a mix of pressing button, disabling others enabled, then, when pressed a second time, I go to another activity.
Thank you for any help :) -Pliskin
Here is how I would do that. Let's say you have 4 buttons.
// your class fields
boolean [] alreadyTouched = new boolean[4];
For each of the 4 buttons :
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!alreadyTouched[0]){
setAlreadyTouched(0);
// one click actions here
}else{
// second click's action
}
}
});
Now you make a private method in your class :
private void setAlreadyTouched(int index){
for (int i = 0; i< alreadyTouched.length; i++)
alreadyTouched[i] = false;
if(index != -1)
alreadyTouched[index] = true;
}
And to reset your boolean array when the button looses the focus :
button0.setOnFocusChangeListener(new View.OnFocusChangeListener(){
#Override
public void onFocusChange(View view, boolean hasFocus){
if(!hasFocus)
setAlreadyTouched(-1);
}
});
You can do exactly the same thing but with an array of int if you want more than two clicks with some slight modifications. For example :
// your class fields
int[] alreadyTouched = new int[4];
Your privat method :
private void setAlreadyTouched(int index){
if(index == -1){
for (int i = 0; i< alreadyTouched.length; i++)
alreadyTouched[i]=0;
}else
alreadyTouched[index] = alreadyTouched[index] +1 ;
}
Then just add some if in your onClickListeners :
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setAlreadyTouched(0);
switch(alreadyTouched[0]){
case 1:
// one click actions here
break:
case 2:
// second click's action
break:
// ... and so on
default:
// action for max number of clicks here.
}
}
});
Related
I'm trying to figure out, how I could interrogate if one of two buttons were clicked.
Thanks in forward.
When one of the buttons is clicked, set a variable into true, for example
boolean aButtonWasClicked = false;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
aButtonWasClicked = true;
}
});
So now, you have stored in aButtonWasClicked the result of the "interrogation". So you can do different things depending on it:
if(aButtonWasClicked){
//yes
} else {
//no
}
I just want to implement one button to do multiple actions like on first click make Textview1 visible and on second click make Textview2 visible and so on.
here is my code it works but for 2 actions only i want to set more visible component in one button i hope its clear and Thanks for any help
final TextView textView_r4 = findViewById(R.id.tv_r4);
final EditText editText_r4 = findViewById(R.id.input_R4);
final TextView textView_r5 = findViewById(R.id.tv_r5);
final EditText editText_r5 = findViewById(R.id.input_R5);
findViewById(R.id.Addbtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView_r4.setVisibility(View.VISIBLE);
editText_r4.setVisibility(View.VISIBLE);
}
});
findViewById(R.id.Addbtn).setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
textView_r5.setVisibility(View.VISIBLE);
editText_r5.setVisibility(View.VISIBLE);
return true;
}
});
You can add an enum State to keep track of which state your button is in. Create a class field in the same class (activity) that these methods are in, and change the state every time you click. Then in the .setOnClickListener method you can check which state the button is in, and depending on that do different actions.
private State state = INITIAL;
findViewById(R.id.Addbtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (state) {
case INITIAL:
// do first action
state = State.CLICKED_ONCE;
break;
case CLICKED_ONCE:
// do second action
state = State.CLICKED_TWICE;
break;
default:
// clicked too many times, no action taken
break;
}
}
});
private enum State { INITIAL, CLICKED_ONCE, CLICKED_TWICE }
I want disable the multi-touch action and the pulling action from my screen and from my activity. I want only use the impact action for my activity. How can I do it?
I want disable the multi-touch and the pulling action from the a view.
a.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getX(0)>=20 && event.getY(0)>=60 && event.getX(0)<=160 && event.getY(0)<=150) {
B b = new B(getApplicationContext());
relativeLayout.addView(b);
}
return true;
}
})
;
If I'm to understand your question correctly, I think you want to use an OnClickListener instead of an OnTouchListener.
Try this:
a.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
B b = new B(getApplicationContext());
relativeLayout.addView(b);
}
});
This is assuming you want to tap anywhere within a to add b to your relativeLayout. If you want to tap a specific area within a you should add a new view to a that covers that area and then add the click listener to that new view.
I have 2 activities, A and B.
In activity A I have radiogroup, a CHECK button and NEXT button .
before I can press the NEXT button , I want to choose first one of the radio button in the radio group and CHECK it first so that I can know if its correct or incorrect before I can press the next button.
here's my code:
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if (btnCheck.isPressed()) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(i);
}
} else {
return;
}
}
});
I can't press the NEXT button even I have chosen one of the radio button and check it.
Please help.
Your answers will be appreciated.
Thank You.
I'm not sure what the problem is, but you could just create a boolean to check if you're concerned that isPressed() isn't working as you'd expect.
boolean hasChecked = false;
btnCheck.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// put conditions here to check if it was pressed at the right time
hasChecked = true;
}
});
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if (hasChecked) {
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(i);
}
}
}
});
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
//Do something here.
}
The above if you want to check if any one of the radio buttons is checked.
if (radio1.isChecked() && radio2.isChecked() && radio3.isChecked()) {
//Do something here.
}
The above if you want to check if all the radio buttons are checked.
If I understand you correctly, you also want to check some btnChk's click, before you press NEXT. So put a FLAG (Global Variable) in your btnChk's onClick method, if you want to log that if that particular button was presed.
If you want anyone of the radio button checks to be true, use the || condition in the if statement. Then to check if btnChk was pressed do the following:
Declare a Global FLAG at just below the class declaration:
public int FLAG = 0;
Set a buttonclick event on your button in onCreate():
Button btnChk = (Button)findViewById(R.id.YourId);
btnChk.setOnClickListener(btn);
private View.OnClickListener btn = new View.OnClickListener() {
#Override
public void onClick(View v) {
FLAG =1;
}
};
Then in your NEXT button check the following:
if (radio1.isChecked() || radio2.isChecked() || radio3.isChecked()) {
if(FLAG ==1){
//Do something here.
}
}
What the FLAG does: FLAG is init to zero initially, as soon as you press btnChk, FLAG becomes 1, if btnChk is not pressed, FLAG remains 0. so in the button click of Next button you check the value of FLAG and do your stuff accordingly.
I am beginner to Android development. I have 3 edit boxes and one "Edit" button. When I launch the activity all the edit boxes should be disabled. When I click on the Edit button all the 3 edit boxes should get enabled and button text should change to "Save". After updating the data in the edit boxes, when I click on the "Save" button, I should be able to send the updated data to the backend.
My problem is how can I make use of a single button for two function "Edit" and "Save".
Please help me.
You can do it this way:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String ButtonText = button.getText().toString();
if(ButtonText.equals("Save"){
//code for save
button.setText("Edit");
}
else{
//code for edit
button.setText("Save");
}
}
});
If I were you I would actually use two buttons one for edit, and one for save. Make them the same size and in the same position, when you want to switch between them make one invisible, and the other visible. Doing it that way would let you keep your onClickListeners separate which would make your code more understandable in my mind.
That being said you could technically achieve it with a single button as well. Just change the text on the button when you want to switch between them, and add an if statement into your click listener to check which "mode" your button is currently in to determine which action it should take.
I am not sure there is an easy way to do this or not. but you can sure use different behaviors of button clicks like
// When you press it for long time.
dummyButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true; // Can do lot more stuff here I am just returning boolean
}
});
// Normal click of button
dummyButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do lot more stuff here.
}
});
Do it this way :
Make a public boolean variable
public boolean isClickedFirstTime = true;
make your 3 editTexts enabled false in xml and
onClick of your button
#Override
public void onClick(View v) {
if (v.getId() == R.id.edit_button_id) { //whatever your id of button
Button button = (Button) findViewById(R.id.edit_button_id);
if(isClickedFirstTime)
{
edit1.setEnabled(true);
edit2.setEnabled(true);
edit3.setEnabled(true);
butt.setText("Save");
isClickedFirstTime = false;
}
else
{
....//Get your values from editText and update your database
isClickedFirstTime = true;
}
}