how to make android chronometer invisible and visible when clicked - android

I am working on app which has a titlebar with chronometer on the left and a textview centered in a RelativeLayout.
RelativeLayout take height of textview and fill screen width
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/titlecontainer"
android:orientation="vertical"
android:padding="5dip"
android:layout_height="wrap_content"
android:layout_width="fill_parent" android:background="#color/titlebckgrnd">
I want to hide the chornometer when user clicks on it and unhide it user clicks again.
How this can be achieved?
EDIT, further to the answers and comments:
Here is the color file code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="titlebckgrnd">#FFD55A2E</color>
<color name="titletext">#FFFFFFFF</color>
</resources>
I used the following code as suggested
final TextView chron = (TextView)findViewById(R.id.chronometer);
((Chronometer) chron).start();
chron.setOnClickListener(new OnClickListener() {
private boolean mToggle = true;
#Override
public void onClick(View v) {
Log.d("Gaurav", "Invisible");
if(mToggle) {
Log.d("Gaurav", String.valueOf(chron.getCurrentTextColor()));
chron.setTextColor(R.color.titlebckgrnd);
mToggle = false;
}
else {
chron.setTextColor(R.color.titletext);
mToogle = true;
}
//chronImage.setVisibility(View.VISIBLE);
//v.setVisibility(View.INVISIBLE);
}
});
but the result is
and it does not respond to any more clicks.
LogCat Results
Even the debugger breakpoints show change in Textcolor value but color change in display does not happen.

If I properly understood your question you can do that way to hide the chronometer:
// We are in your Activity:
final View chronometer= findViewById(R.id.chronometer);
chronometer.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
chronometer.setVisibility(View.GONE);
// Use View.INVISIBLE if you would like to keep the room for this view
}
});
But there will be no view displayed anymore. So depending of where the user should click to have it displayed again, you may have to give your view and/or its text the same color as the background instead of making them invisible:
// should chronometer be a TextView that displays the time:
final TextView chronometer = (TextView) findViewById(R.id.chronometer);
chronometer.setOnClickListener(new View.OnClickListener() {
private boolean mToggle = true;
public void onClick(View v) {
if (mToggle ) {
chronometer.setTextColor(Color.BLACK);
mToggle = false;
}
else {
chronometer.setTextColor(Color.WHITE);
mToggle = true;
}
}
});

Related

ImageButton Won't Change 'background' When Pressed and Released

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.

How to change color of button when being click,and revert back to default color in next click?

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>

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.

Add Up-Navigation Properties in Custom ActionBar Button in Android

I want to create my own ActionBar Layout.
Like this (created in Paint for example)
Is it possible to give the second Button the Up-Navigation Properties? So if I press it, it finish this Activity and starts it's parent.
I want to have the burger Icon for the Navigation Drawer, the Up-Icon for Up-Navigation and the Title of the Activity.
Is it possible? Or is there a solution already?
Actually, it's fairly easy(though it's little hacky) to do.
First, create a drawable for back button(preferably - as a selector, to distinguish pressed/normal state:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/back_button_pressed"/>
<item android:drawable="#drawable/back_button"/>
</selector>
Next, set this drawable to the logo of the toolbar toolbar.setLogo(R.drawable.back_button_selector);
Then the only thing left is to set click-listener.
View logoView = getToolbarLogoIcon(toolbar);
logoView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
...
private View getToolbarLogoIcon(Toolbar toolbar){
//check if contentDescription previously was set
boolean hadContentDescription = android.text.TextUtils.isEmpty(toolbar.getLogoDescription());
String contentDescription = String.valueOf(!hadContentDescription ? toolbar.getLogoDescription() : "logoContentDescription");
toolbar.setLogoDescription(contentDescription);
ArrayList<View> potentialViews = new ArrayList<>();
//find the view based on it's content description, set programatically or with android:contentDescription
toolbar.findViewsWithText(potentialViews,contentDescription, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
//Nav icon is always instantiated at this point because calling setLogoDescription ensures its existence
View logoIcon = null;
if (potentialViews.size() > 0) {
logoIcon = potentialViews.get(0);
}
//Clear content description if not previously present
if (hadContentDescription) {
toolbar.setLogoDescription(null);
}
return logoIcon;
}
(Thanks Nicola's post here). Or if you are not scared of reflection, it can be easily done like this:
try {
Field declaredField = toolbar.getClass().getDeclaredField("mLogoView");
declaredField.setAccessible(true);
View logoView = (View) declaredField.get(toolbar);
logoView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
} catch (Exception ex) {
//error
}
Another possible solution would be to set custom layout to the ActionBar.
Though, I'm advocating to follow the UI/UX guidelines and double-check, if navigation drawer is essential in the secondary activity.

Programmatically change button background drawable onClick

I am trying to toggle my button's background drawables, so that when the user clicks the button its background is changed and when the user clicks the button again its background returns to defaul. Here is my code:
public void Favorites(View V) {
Button star = (Button) findViewById(R.id.buttonStar);
if(star.getBackground().equals(R.drawable.btn_star_off)) {
star.setBackgroundResource(R.drawable.btn_star_on);
} else {
star.setBackgroundResource(R.drawable.btn_star_off);
}
}
I am pretty sure this is not how you use drawables with if statements. Can someone suggest a way to do it?
private boolean isButtonClicked = false; // You should add a boolean flag to record the button on/off state
protected void onCreate(Bundle savedInstanceState) {
......
Button star = (Button) findViewById(R.id.buttonStar);
star.setOnClickListener(new OnClickListener() { // Then you should add add click listener for your button.
#Override
public void onClick(View v) {
if (v.getId() == R.id.buttonStar) {
isButtonClicked = !isButtonClicked; // toggle the boolean flag
v.setBackgroundResource(isButtonClicked ? R.drawable.btn_star_on : R.drawable.btn_star_off);
}
}
});
}
You can create an xml in the drawable folder.
This xml (you choose the name...let's call it "bg_button_star.xml") could look just like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/btn_star_on" />
<item android:drawable="#drawable/btn_star_off" />
Then you have to assign this drawable file to the Button background property in the layout file.
android:background="#drawable/bg_button_star"
If you want to do this programmatically then you just have to do:
button.setBackgroundResource(R.drawable.bg_button_star);
When the user click the first time on the button, you set the Selected state to 'true'. The background changes accordingly. (viceversa for the 'false' Selected state).
You can do in your onClick() something like:
if(star.getTag()==R.drawable.btn_star_on){
star.setTag(R.drawable.btn_star_off);
star.setBackgroundResource(R.drawable.btn_star_off);
} else {
star.setTag(R.drawable.btn_star_on);
star.setBackgroundResource(R.drawable.btn_star_on);
}
Obviously it's better to the the tag before the if and else statement based on your informations. I don't know the rest of your code and how you check if this button has to be iniziatilized with the drawable resource btn_star_off or btn_star_on
You can try this.
public void Favorites(View V) {
Button star = (Button) findViewById(R.id.buttonStar);
if(star.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.btn_star_off).getConstantState()))
{
star.setBackground(R.drawable.btn_star_on);
} else {
star.setBackground(R.drawable.btn_star_off);
}
}
But make sure you are calling this method onClick() of the start button.
Other wise you have to do something like this.
Button star = (Button) findViewById(R.id.buttonStar);
star.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.btn_star_off).getConstantState()))
{
v.setBackground(R.drawable.btn_star_on);
} else {
v.setBackground(R.drawable.btn_star_off);
}
}
});
In this case, instead of using Button you should use ToggleButton.
There is a API Guide for it:
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
Dont do it like that. Use a selector resource instead http://www.compiletimeerror.com/2014/03/android-button-selector-tutorial-with.html

Categories

Resources