I have buttons in my Recyclerview what is want to do is whenever user clicks first button the background of that button should changed and when user clicks next button that time first button should appear as normal button. how to do it? Please help me.
Here is my sample code:
#Override
public void onBindViewHolder(final AreaRecyclerViewAdapter.ViewHolder Viewholder, final int position) {
final GetAreaAdapter getAreaAdapter1 = getAreaAdapter.get(position);
Viewholder.btn_name.setText(getAreaAdapter1.getBtn_name());
setImageIntoButton( Viewholder.btn_name,getAreaAdapter1.isSelected());
Viewholder.btn_name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getAreaAdapter1.setSelected(!getAreaAdapter1.isSelected());
setImageIntoButton(Viewholder.btn_name,getAreaAdapter1.isSelected());
Viewholder.btn_name.setBackgroundResource(R.drawable.ripple_effect);
}
});
}
private void setImageIntoButton(Button buttonView,boolean isSelected){
if(isSelected)
buttonView.setBackgroundResource(R.drawable.ripple_effect);
else
buttonView.setBackgroundResource(R.drawable.button_2);
}
Use State-List drawables
create a xml file in your drawable directory
and add something like
button_bg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/some_drawable_for_pressed_state"
android:state_pressed="true"/>
<item android:drawable="#drawable/default_drawable"/>
</selector>
and set it as background for the button.
android:background="#drawable/button_bg.xml".
Related
I'm making an app that has buttons and when a button is clicked, it will play a sound. My problem is that I couldn't find a way to change mSoundButton's background when pressed and released. I'm using 'background' instead of 'src' so I can shrink the button without cutting from edges. I haven't added sounds yet because I want to solve this issue before starting to add sounds.
Here is my codes for the button in MainActivity.java (It works okay, but not in the way I wanted. It changes background when pressed but not released.)
final ImageButton mSoundBtn;
final boolean[] soundBtnClicked = {false};
mSoundBtn = findViewById(R.id.soundButton);
mSoundBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(soundBtnClicked[0])
mSoundBtn.setBackgroundResource(R.drawable.button_clicked);
else
mSoundBtn.setBackgroundResource(R.drawable.button_not_clicked);
soundBtnClicked[0] = !soundBtnClicked[0];
}
});
and here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:id="#+id/soundButton"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#drawable/button_not_clicked"
android:contentDescription="TODO" />
</LinearLayout>
UPDATE (PROBLEM SOLVED):
I wanted the mSoundBtn to change image when pressed, then change image again when it is released. I used the Handler class and the postDelayed() method to create a delay between two image changes. It doesn't actually wait for user to release the button but still solved my issue. Following is the solution I found:
public ImageButton mSoundBtn;
Handler h = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSoundBtn = findViewById(R.id.soundButton);
mSoundBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSoundBtn.setBackgroundResource(R.drawable.button_clicked);
h.postDelayed(new Runnable() {
public void run() {
mSoundBtn.setBackgroundResource(R.drawable.button_not_clicked);
}
}, 1000); // 1 Second
}
});
What happens is when the mSoundBtn is clicked, It changes image and waits for 1000 milliseconds (1 second), then changes back to previous image which creates some kind of animation when it is pressed so the user can understand when the button is pressed.
Thanks for anyone who have tried to help, thanks for reading. :)
You shouldn't define it as 'final'
Sample:
public class SampleActivity extends AppCompatActivity {
//Variables
public ImageButton mSoundBtn;
public boolean isSoundBtnSelected = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
mSoundBtn = findViewById(R.id.soundButton);
mSoundBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(!isSoundBtnSelected)
mSoundBtn.setBackgroundResource(R.drawable.button_clicked);
else
mSoundBtn.setBackgroundResource(R.drawable.button_not_clicked);
isSoundBtnSelected = !isSoundBtnSelected;
}
});
}
}
If I understand you correctly, you want an action to be done after the user removed the finger from the button (meaning the press is done).
Try using OnTouchListener instead of OnClickListener:
imageButton.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
// change background and other stuff...
return true;
}
return false;
}
});
If you only wanted to change the button's background when it pressed, you can use a selector drawable for that, like so:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/black" /> <!-- pressed -->
<item android:state_activated="true"
android:drawable="#color/colorAccent" /> <!-- focused -->
<item android:drawable="#color/colorPrimary" /> <!-- default -->
and just place it as the button's background.
Another option is to use a toggle button and OnChecked listener and change the background when the user checks (press) the button. If you want the background to be permanently changed after the first toggle you can implement this by changing the background only after the first toggle.
I have a like button in my RecyclerView,what I want is when user hit the like button for the 1st time,the button background color will change to red color,and when the same user hit the like button,the button will change back to default color which is white.
I checked for few SO question,but still havent get what I want.So far my solution is like below,doesnt produce any error but when clicked the button,nothing happen.
likeButton =(Button) view.findViewById(R.id.likeButton);
//here for user like the post
holder.likeButton.setOnClickListener(new View.OnClickListener() {
boolean clicked = true;
#Override
public void onClick(View v) {
if(!clicked){
holder.likeButton.setBackgroundColor(Color.RED);
clicked = true;
//here i will update the database
}else{
holder.likeButton.setBackgroundColor(Color.WHITE);
clicked = false;
//here i will update the database
}
}
});
I checked this SO answer too,so I modified my code as below,but still nothing happens when the button is clicked.
holder.likeButton.setBackgroundColor(Color.WHITE);
holder.likeButton.setOnClickListener(new View.OnClickListener() {
ValueAnimator buttonColorAnim = null;
#Override
public void onClick(View v) {
if(buttonColorAnim != null){
buttonColorAnim.reverse();
buttonColorAnim = null;
//here i will update the database
}else{
final Button button = (Button) v;//here is the line I dont undestand
buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.WHITE);
buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animator) {
// set the background color
button.setBackgroundColor((Integer) animator.getAnimatedValue());
}
//here i will update the database
});
buttonColorAnim.start();
}
}
});
Somebody please point out what I'm missing,what I want is change button color programmatically when being click for 1st time,and change back to default for next click(which avoid multiple like from a same user).
You should create a selector file. Create a file in drawable folder like color_change.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/button_pressed"/> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#color/button_focused"/> <!-- focused -->
<item android:drawable="#color/button_default"/> <!-- default -->
</selector>
and declare it in the button like this
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/color_change"
android:text="Click Me" />
Hi try to this hope this can help you...
In XML
<Button
android:id="#+id/btnClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:text="click"/>
In Adapter Class
boolean click = true;
holder.btnClick.setTag(position);
holder.btnClick.setId(position);
holder.btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (click) {
holder.btnClick.setBackgroundColor(Color.RED);
click = false;
} else {
holder.btnClick.setBackgroundColor(Color.WHITE);
click = true;
}
notifyDataSetChanged();
}
});
Instead of clicked or not condition, make and use condition from you update database for like and dislike operation. So, In click-listener get data previously user like or not then change background and update database as per new click.
Try adding this line to your row.xml file in the main layout :
android:descendantFocusability="blocksDescendants"
Have a look at this. Here, I changed the button text color on click. First time, all buttons appear white and after click it toggles between red and white as you expected. --
//LikeAdapter.java
public class LikeAdapter extends RecyclerView.Adapter<LikeAdapter.LikeHolder> {
public LikeAdapter() {
}
#Override
public LikeAdapter.LikeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.like_item,parent,false);
return new LikeHolder(view);
}
#Override
public void onBindViewHolder(final LikeAdapter.LikeHolder holder, int position) {
holder.red_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.red_btn.getVisibility() == View.VISIBLE) {
holder.red_btn.setVisibility(View.GONE);
holder.white_btn.setVisibility(View.VISIBLE);
} else {
holder.red_btn.setVisibility(View.VISIBLE);
holder.white_btn.setVisibility(View.GONE);
}
}
});
holder.white_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (holder.white_btn.getVisibility() == View.VISIBLE) {
holder.red_btn.setVisibility(View.VISIBLE);
holder.white_btn.setVisibility(View.GONE);
} else {
holder.red_btn.setVisibility(View.GONE);
holder.white_btn.setVisibility(View.VISIBLE);
}
}
});
}
#Override
public int getItemCount() {
return 5;
}
public class LikeHolder extends RecyclerView.ViewHolder {
private Button red_btn, white_btn;
public LikeHolder(View itemView) {
super(itemView);
red_btn = (Button) itemView.findViewById(R.id.red_btn);
white_btn = (Button) itemView.findViewById(R.id.white_btn);
red_btn.setBackgroundColor(Color.RED);
white_btn.setBackgroundColor(Color.WHITE);
}
}
}
//like_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<Button
android:id="#+id/red_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Red"/>
<Button
android:id="#+id/white_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="White"/>
</RelativeLayout>
I want to change the image of the imagebutton when I click on it. This imageButton is part of layout out file which I have inflated in my custom adapter which feeds a kind of card view (Image with few buttons) to my main container layout.
I have added the imagebutton listener in my CustomAdapter.java:
cardHolder.mLikeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Sushil", "clicked like button!!!!!");
updateHeartButton(true);
}
});
When I click on the button, my listener is called and from inside I call a method to update the image of button:
private void updateHeartButton(boolean animated) {
cardHolder.mLikeButton.setImageResource(R.drawable.ic_heart_red);
}
But the imagebutton does not get updated with new image. can someone help me.
Thank You
Since cardHolder changes (part of the adapter getView(), the last cardHolder will be called rather the one which was tapped.
Please change your code as follow and try again:
cardHolder.mLikeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("Sushil", "clicked like button!!!!!");
((ImageButton) view).setImageResource(R.drawable.ic_heart_red);
}
});
Try with setBackgroundResource(R.drawable.ic_heart_red);
Or in other way if you need to go back to the previous image when clicking back on it:
You can create a btn.xml file in the drawable folder :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- When selected, use grey -->
<item android:drawable="#drawable/like_selected"
android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/like_normal"/>
</selector>
In your xml for the button you set this xml to the background:
android:background="#drawable/btn"
In your updateHeartButton() method you can use:
cardHolder.mLikeButton.setSelected(true)
this work for me
cardHolder.mLikeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cardHolder.mLikeButton.setImageResource(R.drawable.ic_heart_red);
}
});
I am having a button I want to do toggle on button like on and off,What I required is I am having two backgroundimages.
When I first time tap on button first background image should be done and on second time second background image should be changed like in toggle button.
Anybody please help me as I am new to android.
This is my activity:
public class MainActivity extends Activity {
private Button Button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button1 = (Button) findViewById(R.id.Button1);
Button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.setBackgroundResource(R.drawable.ic_launcher);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Thanks.
Create a xml file in the drawable with the following code and change the background of the button to this drawable
customButton.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:drawable="#drawable/image_not_pressed"> </item>
<item android:state_pressed="true" android:drawable="#drawable/image_pressed"></item>
</selector>
Then add background to the image
<ImageView
android:layoutWidth="wrap_content"
android:layoutWidth="wrap_content"
android:background="#drawable/customButtom" />
You can use setTag to do that.
First>
Button1.setTag("ON");
and secondly in your onclicklistener add this:
if (v.getTag().toString().equals("ON")) {
v.setTag("OFF");
v.setBackgroundResource(R.drawable.ic_launcher);
}else {
v.setTag("ON");
v.setBackgroundResource(R.drawable.ic_launcher2);
}
Use ImageButton instead of Button,change image on the surface of the ImageButton by the setImageResource(int) method.
You can maintain the setSelected state for the Button and change the image by checking if it isSelected on Button click
Try using
Button button = new Button(this);
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
if (v.isSelected())
{
v.setSelected(false);
}
else
{
v.setSelected(true);
}
}
});
Let me know if this works.
As attached in screenshot when I click on edit button then i want to hide edit button and show save button. How can i do that?
My menu file is as below:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/edit_button"
android:icon="#drawable/edit_button"
android:orderInCategory="100"
android:showAsAction="always"
android:title="#string/edit"/>
</menu>
Is it possible this with image as edit to another image as save when button is clicked once and as save to edit when button is clicked again.
Try this
#Override
public boolean onOptionsItemSelected(MenuItem item){
{
if(editing){
item.setIcon(R.drawable.ic_save);
}else{
item.setIcon(R.drawable.ic_edit);
}
return super.onOptionsItemSelected(menu);
}
final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
final Button btn2 = (Button) findViewById(R.id.btnDate2);
dp2.setVisibility(View.GONE);
or
dp2.setVisibility(View.INVISIBLE);
btn2.setVisibility(View.GONE);
or
btn2.setVisibility(View.INVISIBLE);
when need visible:
btn2.setVisibility(View.VISIBLE);
or you use Invisible or Gone, but not both!
Based on your comments, you need other than asked.
Make not relevant, but change the
<item
android:id="#+id/edit_button"
to
<item
android:id="#+id/edit_or_save_button"
From the name you should know what will be the next:
if you press the button when it was an Edit, than you will do stuff in the activity which allows editing your data, but this button will change his text ( and action) to Save!
Of course you will assign different action listener, which will do the validation and save action.
If you really want to stick with 2 button idea (highly disagree with that) :
Button edit_button = (Button)findViewById(R.id.edit_button);
Button save_button = (Button)findViewById(R.id.save_button);
...
edit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
edit_button.setVisibility(View.INVISIBLE);
save_button.setVisibility(View.INVISIBLE);
DoEdit(v);
}
});
save_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
save_button.setVisibility(View.INVISIBLE);
edit_button.setVisibility(View.INVISIBLE);
DoValidationAndSave(v);
}
});
I hope it solve your problem!
Try this,
public boolean checkHide = false
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case EDIT:
if(checkHide){
checkHide=false;
item.setTitle("edit");
// ToDo your function
}
else{
checkHide=true;
item.setTitle("save");
// ToDo your function
}
}