Android change button params on click then reset - android

I have a small button and when clicked it will expand to width:match_parent. When clicked again I want the button parameters from the xml to be applied. Its a custom button so its not as easy as just resizing the width.
My current code looks like:
btnBottomSheetTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mSheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
mSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
btnBottomSheetTitle.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, 150));
btnBottomSheetTitle.setCornerRadius(0);
} else {
mSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
});

If You have style for button You can set it programmatically. Before setting setOnClickListener save current button's LayoutParams
final android.view.ViewGroup.LayoutParams savedParams = btnBottomSheetTitle.getLayoutParams();
In Your else add:
btnBottomSheetTitle.setBackgroundResource(R.drawable.your_style);
btnBottomSheetTitle.setLayoutParams(savedParams);

Related

Android Studio - Change image when in different state

I want to have a favorite button on my app like in Gmail
So, when the user click on the star, the star became yellow, and when the user clicked it again, it turn back to normal
How can i make this happen with my custom image?
i have two images
when its not favorited (heart-grey.png)
and when its favorited (heart-red.png)
You can use visibilty to do this.You have to define the xml to have both images at the same postion(It can be done using Relative layout).
final ImageView play = (ImageView) findViewById(R.id.play);
final ImageView play2 = (ImageView) findViewById(R.id.play2);
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawable playImage = play.getDrawable();
if (playImage.isVisible()){
play.setVisibility(View.GONE);
play2.setVisibility(View.VISIBLE);
mediaPlayer.start(); }
}
});
play2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable playImage2 = play2.getDrawable();
if (playImage2.isVisible()){
play2.setVisibility(View.GONE);
play.setVisibility(View.VISIBLE);
mediaPlayer.pause();
}
If your star is an ImageButton you can do something like this :
starSelected.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//add a condition to detect if it is already a favorite or not
if(starSelected.getDrawable() == R.drawable.theimage2) {
starSelected.setImageResource(R.drawable.thenewimage);
}else{
starSelected.setImageResource(R.drawable.thenewimage2);
}
}
});
Hope it helps
In your java code create a boolean flag:
boolean isSelected = false
Set an onClickListener inside your onCreate for the star (ImageView, Button whatever it is). Inside onClick, check for the flag like this:
if (isSelected) {
// change image src to unselected
isSelected = false;
} else {
// change image src to selected
isSelected = true;
}
and also you can save the boolean state with SharedPreferences to make sure you get the correct state every time.

Imagebutton Validation not working

I am trying to put validations in my ImageButton's onClick(). It is like the tile in Boggle game. If I clicked image 1 then the nearest ImageButtons must be the only ImageButton that is clickable and the remaining buttons will be set as unclickable. How can I achieve it? Here's my code declared at onCreate().
public void tileClick() {
if (image1.isPressed()) {
image1.setClickable(false);
image1.setImageResource(R.drawable.changes);
//clickable when image1 is pressed/clicked
image2.setClickable(true);
image5.setClickable(true);
image6.setClickable(true);
//unclickable
image3.setClickable(false);
image4.setClickable(false);
image7.setClickable(false);
image8.setClickable(false);
image9.setClickable(false);
image10.setClickable(false);
image11.setClickable(false);
image12.setClickable(false);
image13.setClickable(false);
image14.setClickable(false);
image15.setClickable(false);
image16.setClickable(false);
}
}
CustomClickListener
//get ImageButton letter
View.OnClickListener myCommoClickListner = new View.OnClickListener(){
#Override
public void onClick(View arg0) {
Log.i(TAG, "arg0.getId()=" + arg0.getId());
if (arg0.getId()==R.drawable.a){
Log.i(TAG,"arg0.getId()="+arg0.getId());
generatedString=generatedString+("a");
text.setText(generatedString);
//change ImageButton's background when clicked
((ImageButton) arg0).setImageResource(R.drawable.changea);
//Set ImageButton clickable = false when already clicked
arg0.setClickable(false);
}
}
};
//all 16 ImageButtons declared like this :
image1.setOnClickListener(myCommoClickListner);
Change your method to accept an ImageButton
public void tileClick(ImageButton clickedBtn) {
// validation logic
}
pass the clicked ImageButton to the function from onClick()
View.OnClickListener myCommoClickListner = new View.OnClickListener(){
#Override
public void onClick(View arg0) {
...
tileClick((ImageButton) arg0) // should change arg0 to something meaningful (v, view, etc...)
...
}
}
};
Then set the buttons clickable true/false according to the button passed.
You could put the ImageButtons in an Array and iterate over them and set the clickable according to which button was pressed.

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 :)

Android: seekbar in button

I have a Button in my android application, and I need to expand it on click and display a Seekbar inside it, like at the picture below. What is the best practice to do that?
Use below code to handle tap of button to set visiblility of seekbar invisible if visible or visible if invisible.
Button click = (Button) findViewById(R.id.but);
Seekbar seek = (Seekbar) findViewById(R.id.seek);
click.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
if (seek.getVisibility() == View.VISIBLE) {
seek.setVisibility(View.INVISIBLE);
} else {
seek.setVisibility(View.VISIBLE);
}
}
});

ImageView click problem - Android

Can I make ImageView in android make some action when I click on that ImageView ?
For example, when I click on ImageView imgV that some panel be visible and when I click on other ImageView imgUV that he become unvisible .
imgV.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
int i=tbrSearchNear.VISIBLE;
tbrSearchNear.setVisibility(1-i);
tbrSearchCriterium.setVisibility(i);
}
});
It looks like that doesn't register click at all .
Maybe ImageButton is what you are looking for? http://developer.android.com/reference/android/widget/ImageButton.html
Setting visbility to 1-View.VISIBLE translates to 1-0; if you want to hide it, write something like:
imgV.setOnClickListener( new OnClickListener() {
public void onClick(View v)
{
// toggle visibility
int visibility=tbrSearchNear.getVisbility() == View.VISIBLE ? View.GONE : View.VISIBLE;
tbrSearchCriterium.setVisibility(tbrSearchNear.getVisbility());
tbrSearchNear.setVisibility(visbility);
}
});

Categories

Resources