Change visibility of one button by clicking another button - java

I am trying to change visibility of two buttons when pressing one of them.
So say I have an "on" and an "off" button. When I press the on button, I want the on button to hide itself and the off button to show, and vice versa.
How could one do this?

Basically, you have to inform on to point.
How can I handle the visibility ?
How can I listen to a click event ?
How can I handle the visibility ?
You can handle the visibility of a view by using the enter link description here property.
This property controls the visibility of the view.
Must be one of the following constant values.
GONE Completely hidden, as if the view had not been added.
INVISIBLE Not displayed, but taken into account during layout (space is left for it).
VISIBLE Visible on screen (the default value)
Now you know how you could display or not and element on your view.
How can I listen to a click event ?
Simply add on onClickListner on your button
Sample Code
btnOff, btnOn // your buttons
btnOff.setOnclickListner {
// handle visibility
btnOn.visibility = View.INVISIBLE
}
BTW. If you want an On/Off button, you should use Toggle Buttons there are like the switch buttons you used to have on settings screens

See this example in Kotlin, adapt it for your own needs:
val onButton = Button(this)
val offButton = Button(this)
onButton.setOnClickListener {
onButton.visibility = INVISIBLE
offButton.visibility = VISIBLE
}
offButton.setOnClickListener {
onButton.visibility = VISIBLE
offButton.visibility = INVISIBLE
}
Or in Java:
Button onButton = (Button) findViewById(R.id.onButton);
Button offButton = (Button) findViewById(R.id.offButton);
onButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onButton.visibility = INVISIBLE;
offButton.visibility = VISIBLE;
}
});
offButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
onButton.visibility = VISIBLE;
offButton.visibility = INVISIBLE;
}
});

Related

Disable button for every user and clickable button only for admin

I have a reset button which only admin can click and for users it wil be disabled always meaning only admin can reset counts. How to do it?
#Override
public void onClick(View arg0) {
count10counter=0;
tvcount10.setText ( String.valueOf ( count10counter ) );
}
} );```
At your activity, get the button by its id and set its visibility to invisible. Paste this code where you implement user log:
Button btn = findViewById (R.id.reset_button); // reset_button is the view id taken from the xml file
btn.setVisibility (View.GONE);

Set visibility visible but set enable false Android XML Java

I have a button named HideIt:
android:id="#+id/HideIt"
android:onClick="HideIt_onClick"
I have another button named HelloWorld:
android:id="#+id/HelloWorld"
android:onCick="HelloWorld_onClick"
And the third button named VisibleIt:
android:id="#+id/VisibleIt"
android:onCick="VisibleIt"
What is the Scenario?
First of all I click on the HideIt button, so it's function will Invisible and Disable the HelloWorld button as codes below:
public void HideIt_onClick(View v) {
Button DisableHide = findViewById(R.id.HelloWorld);
DisableHide.setVisibility(View.GONE);
DisableHide.setEnabled(false);}
Then I click on the VisibleIt button, so its function will Visible the HelloWorld button BUT it will NOT Enable the HelloWorld Button as codes below:
public void VisibleIt_onClick(View v) {
Button VisibleHelloWorld = findViewById(R.id.HelloWorld);
VisibleHelloWorld.setVisibility(View.VISIBLE);}
What is the problem?
The problem is when I click on VisibleIt, I expect that my HelloWorld button set Visible but still disabled, but it will never set visible and it's still hidden.
I added a line of code to VisibleIt codes to text as below:
DisableHide.setEnabled(true);}
So VisibleIt codes are as below:
public void VisibleIt_onClick(View v) {
Button VisibleHelloWorld = findViewById(R.id.HelloWorld);
VisibleHelloWorld.setVisibility(View.VISIBLE);
DisableHide.setEnabled(true);}
In this case when I click on VisibleIt Button, it comes Visible BUT NOT disabled as what I wanted, so I set the setEnable(false) in VisbileIt codes above, but I had the same problem as before, button HelloWorld is still HIDDEN.
So I tried a private void as codes below but still that problem:
public void VisibleIt_onClick(View v) {
Button VisibleHelloWorld = findViewById(R.id.HelloWorld);
VisibleHelloWorld.setVisibility(View.VISIBLE);
DisableHide.setEnabled(true);
DisableItNow();
}
private void DisableItNow() {
Button DisableItPlease = findViewById(R.id.HelloWorld);
DisableItPlease.setEnabled(false);
}
I need that HelloWorld button, which had been Disabled and Gone by another Java fuction , get visible by this fucntion BUT still disabled
Maybe this below code can help you.
public class MainActivity extends AppCompatActivity{
Button HideIt;
Button HelloWorld;
Button VisibleIt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HideIt = findViewById(R.id.HideIt);
HelloWorld = findViewById(R.id.HelloWorld);
VisibleIt = findViewById(R.id.VisibleIt);
HideIt.setOnClickListener(v -> {
HelloWorld.setVisibility(View.INVISIBLE);
HelloWorld.setEnabled(false);
});
VisibleIt.setOnClickListener(v -> {
HelloWorld.setVisibility(View.VISIBLE);
HelloWorld.setEnabled(true);
});
//in xml code and in Your activity java file you can now safely delete the
//function that you created for
//hiding or showing
}
}
Maybe just use an if statement inside the onClick() to check the visibility as follows:
public void VisibleIt_onClick(View v) {
//Button VisibleHelloWorld = findViewById(R.id.HelloWorld);
//v is already your button
if( v.getVisibility() == View.INVISIBLE)
v.setVisibility(View.VISIBLE);
else if(v.getVisibility() == View.VISIBLE)
v.setVisibility(View.INVISIBLE)
else{;/*the view is View.GONE*/}
}
so that if the view (your button) is visible it sets the visibility to View.INVISIBLE and if is invisible to View.VISIBLE.
For what you are trying to do there is no point in changing the button .setEnabled() property.

Create expand animation for default recyclerView (changing view's visibility from gone to visible)

Do i necessairly need to change my recyclerView to expandableRecyclerView for doing animation of expand + animation of arrow drop down -> arrow up? Is there any way to implement simple slide down animation?
On button drop down arrow click i change visibility of textView and imageView from GONE to VISIBLE (also changing src of arrow button in code)
From:
To:
Here is some code just in case
taskViewHolder.showDesc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(taskViewHolder.description.getVisibility()==View.GONE){
taskViewHolder.showDesc.setImageDrawable(resources.getDrawable(R.drawable.drop_up_arrow));
taskViewHolder.imgDesc.setVisibility(View.VISIBLE);
taskViewHolder.description.setText(task.getDescription());
taskViewHolder.description.setVisibility(View.VISIBLE);
}
else {
taskViewHolder.showDesc.setImageDrawable(resources.getDrawable(R.drawable.drop_down_arrow));
taskViewHolder.description.setVisibility(View.GONE);
taskViewHolder.imgDesc.setVisibility(View.GONE);
}
}
});
Have a Look on my old project, I had to do something like you have told:
https://bitbucket.org/MauzerTheCat/music-example-app/src/master/

Can a ToggleButton change the functionality of other buttons in the same Activity?

Summed up, can a ToggleButton change what other buttons in the activity do when toggled? If so, a more specific explanation of what I want to do is below:
Basically there are three buttons and a togglebutton. When the togglebutton is toggled, pressing any of the three buttons will take a picture and 'save it' for that button. When untoggled, pressing any of the three buttons simply displays their images. I think I can figure out the camera capture part, but I need some direction when it comes to the togglebutton.
Any help is appreciated and I can explain further if necessary.
What I would do is keep a couple flags for each state at the class level, like this:
public class MyClass {
private static final int STATE_SAVE = 0;
private static final int STATE_DISPLAY = 1;
private int currentState = STATE_DISPLAY;
// I made this default for the example,
// you should use what makes sense to your project.
}
Then, inside your toggle button, you can set the flag. Paraphrasing this code since I don't have an editor open:
toggleButton.setOnToggleListener(new OnToggleListener() {
#Override
public void onToggled(boolean toggled) {
if(toggled) {
currentState = STATE_SAVE;
} else {
currentState = STATE_DISPLAY;
}
});
Now, when the buttons are clicked, you can switch based on the state to do an action:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(currentState == STATE_SAVE) {
// Save the image.
} else if (currentState == STATE_DISPLAY) {
// Display the image.
}
});
Create a boolean such as isToggleOn that is true or false depending on the ToggleButton. Then for each of your buttons, you can simply do:
Button button1 = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isToggleOn){
//do one thing
} else {
//do other thing
}
}
});
Yes, you definitely can. Very rarely is anything impossible with code!
All you have to do is to change the listener of the three buttons when the togglebutton is pressed. You keep alternating between the listeners each time you toggle.
For your purpose, I'd suggest defining two sets of listeners - two for each of the three buttons and then keep changing between them.

It Is fine to define all Widget ID with same Variable?

I mean is like this
button = (Button)findViewById(R.id.button1);
button = (Button)findViewById(R.id.button2);
button = (Button)findViewById(R.id.button3);
button = (Button)findViewById(R.id.button4);
button = (Button)findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//Do same logic
}
});
}
it is possible to define like that? or it can causes force close?
and one question again, it is okay to use copying ID from current xml to another xml? i mean the xml was different but the Widget and ID is same, and define the ID in diferrent class. Because it is more simple to copying than make the Same widget with new Id, it is okay?
example :
so in activityone.xml
i had this
so i was copy the widget to activitytwo.xml so they have a same Widget and ID
it is okay if i do like that?
That will only set the listener for button5. Every time you assign button you lose the previous assignment, so when you set the listener the variable button doesn't know that it used to be pointing to button1 to 4.
So no, it will not work.
I put a comment about your second question.
For that you should have single listener but five different buttons (Objects). Currently you are assigning every button one by one to same button reference. In that way your button will have R.id.button5 and in further code you won't have reference to your previous buttons butons1-4. So, while you add listener to the button you are actually adding listener to only button 5 and any other buttons will not have that listener.
First of all you need five different buttons and you should create class which implement the listener and add instance of that listener to your setOnClicklistener method or in a better way you can just specify android:onClick="onClick" in your xml and add onClick method in your activity.
FOR EXAMPLE :
//.. on create
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
Button button4 = (Button)findViewById(R.id.button4);
Button button5 = (Button)findViewById(R.id.button5);
ListenerClass listener = new ListenerClass();
button1.setOnClickListener(listener);
button2.setOnClickListener(listener);
//..and so on
//Create class
public class ListenerClass implements View.OnClickListener {
//...
#Override
public void onClick(View view) {
//You can determine which button is clicked by view.getId()
//Add logic here
}
}
OR
public class YourActivity implements View.OnClickListener {
//...on create
button1.setOnClickListener(this);
button2.setOnClickListener(this);
//..and so on
//...
#Override
public void onClick(View view) {
//You can determine which button is clicked by view.getId()
//Add logic here
}
}
it is okay to use copying ID from current xml to another xml?
Yes it is fine but you should avoid using that as it may create confusion in a long run. In a wide project working with a team can create problem in understanding that. Other than that you can have same id because when you call findViewById it will refer to the current view and does not get influenced by ids of other views.

Categories

Resources