how to set buttons visibility in kotlin - android

Hello I wanna know how to control somethings visibility with button clicker
VisibilityButton.setOnClickListener(
makebtninvisible()
)
public void makebtninvisible (View v) {
Something.setVisibiity(View.INVISILE)

It should be
VisibilityButton.setOnClickListener{
VisibilityButton.visibiity=View.GONE
}

try this way :
VisibilityButton.setOnClickListener(
VisibilityButton.visibility = if (VisibilityButton.visibility == View.VISIBLE){
VisibilityButton.visibiity=View.INVISILE
} else{
VisibilityButton.visibiity=View.VISIBLE
}
}

Related

How to set the background of button programmatically when that is at some of conditions in android?

I am going to change the background of button in android when text value is lower than 1.
btn_minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int temp = convertStringToInt(text_count.getText().toString());
if (temp != 1){
text_count.setText(temp-1);
} else {
btn_minus.setBackground(R.drawable.ic_circle_gray_minus);
}
}
});
btn_minus is the object of Button. and btn_minus.setBackground is not working now.
Using setBackgroundResource() function instead of the setBackground().
Use below code for changing button background color :
btn_minus.setBackgroundResource(R.drawable.ic_circle_gray_minus);

Programmatically hide views if it is shown. Show views if it is hidden

My button use code that shows and hides the views:
public void onClick (View v){
if (What code you need to enter here to determine hidden views or shown)
{
testActivity.setVisibility(View.VISIBLE);
}
else
{
testActivity.setVisibility(View.GONE);
}
}
What code I need to add in the "if()", so that clicking on my button was checked condition. If the activity is hidden, it should be shown, and Vice versa. If the views is shown, hide it.
I'm guessing since you are using setVisibility, that you want to check the visibility of a View , not an Activity.
In that case you just use getVisibility()
(I used != cause the visibility could be IINVISIBLE as well, change per your needs) :
public void onClick (View v){
if (testActivity.getVisibility() != View.VISIBLE)
{
testActivity.setVisibility(View.VISIBLE);
}
else
{
testActivity.setVisibility(View.GONE);
}
} });
Don't understand why, but only that removed the answer of a man who has solved my problem. Here is his response, and this code works:
public void onClick (View v){
if ((testActivity.getVisibility() == View.VISIBLE))
{
testActivity.setVisibility(View.GONE);
}
else
{
testActivity.setVisibility(View.VISIBLE);
}

How to disable click listener on image view android

I am working on android application in which i am making click listener on image view. I just want to disable image listener, for example i have a edit button , without clicking edit button image view listener should be disabled.
image = (ImageView) findViewById(R.id.image);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (clickCount == 0) {
Toast.makeText(getApplicationContext(), "Enabled",
Toast.LENGTH_SHORT).show();
fName.setEnabled(true);
lName.setEnabled(true);
mailText.setEnabled(true);
mobileText.setEnabled(true);
mCond.setEnabled(true);
mNotes.setEnabled(true);
medication.setEnabled(true);
alReact.setEnabled(true);
dofo.setEnabled(true);
image.setEnabled(true);
editText.setText("Done");
clickCount = 1;
}
else if (clickCount == 1) {
Toast.makeText(getApplicationContext(), "Done",
Toast.LENGTH_SHORT).show();
fName.setEnabled(false);
lName.setEnabled(false);
mailText.setEnabled(false);
mobileText.setEnabled(false);
meond.setEnabled(false);
mNotes.setEnabled(false);
meation.setEnabled(false);
alReact.setEnabled(false);
doInfo.setEnabled(false);
image.setEnabled(false);
editText.setText("Edit");
updatingUser();
clickCount = 0;
}
}
});
just add it in onCreate Method
image.setEnabled(false);
Use this line into your XML:
android:clickable="false"
If you want To clear the onClick-listenener of the imageView. Simply call
myImageView.setOnClickListener(null);
Hope it helps.
Update: "Advantage" (depends on what you want") of setOnClickListener to null is that it will not change the background of the button like it does at setClickable(false) or setEnabled(false).
Because the button wants to show the user if he is in an enabled(false) or setClickable(false) mode.
If you don't want that, simply use my answere. :) hope it helps and gives a direction of what you want
Try This:
image.setClickable(false);
Use image.setClickable(false) to set imageview clickable disable
try this
image = (ImageView) findViewById(R.id.image);
image.setClickable(false);
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
image.setClickable(true);
}
Hope this will help you friend :)

edittext visibility change at runtime depending upon it's visibility

Hello all i want to do
1]if edittext is visible then invisible and if invisible then visible to it for that i have done this
btn_search.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// fragment=new BBQ();
// Intent i=new Intent(getApplicationContext(),
// Search_Activity.class);
// startActivity(i);
ed= (EditText) findViewById(R.id.editText1);
if(ed.getVisibility()==arg0.INVISIBLE)
{
ed.setVisibility(arg0.VISIBLE);
}
if(ed.getVisibility()==arg0.VISIBLE)
{
ed.setVisibility(arg0.INVISIBLE);
}
}
for me if it if invisible then it make visible but on second click it not invisible what is wrong i am doing?
I'll say that you should else-if condition:
if(ed.getVisibility()==View.INVISIBLE) {
ed.setVisibility(View.VISIBLE);
} else if(ed.getVisibility()==View.VISIBLE) {
ed.setVisibility(View.INVISIBLE);
}
Or with the ternary operator:
ed.setVisibility (ed.getVisibility() != View.VISIBLE ? View.VISIBLE : View.INVISIBLE);
May be when the code enters the first if, later you modify the visibility and set again invisible, so it enters again in the second if, try this:
if(ed.getVisibility()==arg0.INVISIBLE)
{
ed.setVisibility(arg0.VISIBLE);
}
else if(ed.getVisibility()==arg0.VISIBLE)
{
ed.setVisibility(arg0.INVISIBLE)
}
Try this:
Boolean isVisible=true;
ed= (EditText) findViewById(R.id.editText1);
btn_search.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if(isVisible){
ed.setVisibility(arg0.INVISIBLE);
}else{
ed.setVisibility(arg0.VISIBLE);
}
isVisible=!isVisible;
}
Try this..
EditText having tag attribute initility set the tag as visible then while giving invisible setTag("invisible"); or setTag("visible");
<EditText
android:id="#+id/url_edittext"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="5dp"
android:tag="visible" />
Code
if(ed.getTag().equals("visible"))
{
ed.setVisibility(View.INVISIBLE);
ed.setTag("invisible");
}
else if(ed.getTag().equals("invisible"))
{
ed.setVisibility(View.VISIBLE);
ed.setTag("visible");
}
Edit your code
if(ed.getVisibility()==arg0.INVISIBLE)
{
ed.setVisibility(View.VISIBLE);
} else if(ed.getVisibility()==arg0.VISIBLE)
{
ed.setVisibility(View.GONE);
}
Hope this will solve your issue

How to make a view visible when a button is clicked and make that view invisible when the button is again clicked?

I have a layout which is invisible when the activity starts.When I click on a button the layout becomes visible.My requirement is when I click the button for the second time, the layout should be invisible.I know this is a silly question but as I am a new to android, I am unable to figure it out.
Try the following code to toggle the visibility of the view:
v.setVisibility(v.getVisibility() == View.INVISIBLE ? View.VISIBLE
: View.INVISIBLE);
You can also implement by using boolean FLAG.
e.g. Declare
boolean visibility_Flag = false;
button..setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(visibility_Flag){
YourView.setVisibility(View.INVISIBLE);
visibility_Flag = false;
} else {
YourView.setVisibility(View.VISIBLE);
visibility_Flag =true;
}
}
});

Categories

Resources