VisibilityAwareImageButton Usage - android

I was recently working with ImageButtons and I came across this new type of ImageButton 'VisibilityAwareImageButton'. It would be really helpful if someone could tell me the usage of this ImageButton and how is it different from the regular ImageButton? Thanks in advance :)

Here's the full source for VisibilityAwareImageButton.
class VisibilityAwareImageButton extends ImageButton {
private int mUserSetVisibility;
public VisibilityAwareImageButton(Context context) {
this(context, null);
}
public VisibilityAwareImageButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public VisibilityAwareImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mUserSetVisibility = getVisibility();
}
#Override
public void setVisibility(int visibility) {
internalSetVisibility(visibility, true);
}
final void internalSetVisibility(int visibility, boolean fromUser) {
super.setVisibility(visibility);
if (fromUser) {
mUserSetVisibility = visibility;
}
}
final int getUserSetVisibility() {
return mUserSetVisibility;
}
}
It appears to be almost exactly the same as a regular ImageButton, only it keeps track of the last visibility actually set by the user. The only usage I can find is in the FloatingActionButton source. It is used to keep track of what the user wants the visibility of the view to be while it does it's own internal changes and animations. i.e.
if (child.getUserSetVisibility() != VISIBLE) {
// The view isn't set to be visible so skip changing it's visibility
return false;
}
It's in the design support library and has package visibility, so it seems like Google intends on using it internally (and seemingly only for the FAB implementation at this time).

Related

How to Disable Sound Effects in Android TV app without Muting all System Sounds

I would like to disable sound effects when browsing over RecycleView items and also clicking sounds in an Android TV app. But, I do not want to disable all other sounds (e.g., There is Exoplayer in the app that its output sounds should not be muted).
I noticed there are some other questions similar to this on Stackoverflow and the suggested solutions are:
Disable Sound effect in the Layout Files by setting android:soundEffectsEnabled="false" (I put this in every Layout). However, this does not have any effect and there is still clicking and item browsing sound effects.
Disable sound effects using AudioManager. I tried the following:
audioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_MUTE, 0); and audioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_MUTE, 0); These mute all app sounds including Media sounds.
I would be grateful if someone can help with this issue. Thanks
Finally I found a solution for this problem.
Issue 1: Disabling sound effect on pressing DPAD_CENTER key. I could resolve this issue by programmatically disabling sound effect in CardPresenter (for Leanback ListRowPresenter) and CardAdapter (for RecyclerView).
Issue 2: Disabling sound effect on pressing DPAD navigation keys (DPAD_RIGHT, DPAD_LEFT, ...). Digging into the ViewRootImpl.java class, it turns out that navigation sound is always played without checking the soundEffect flag. Here is parts of the code in ViewRootImpl.java
if (v.requestFocus(direction, mTempRect)) {
boolean isFastScrolling = event.getRepeatCount() > 0;
playSoundEffect(
SoundEffectConstants.getConstantForFocusDirection(direction,
isFastScrolling));
return true;
So a workaround that I came up with is to override the requestFocus method in my views and always return false to prevent playing sound effect.
Code for Leanback ListRowPresenter:
CardPresenter.java
public class CardPresenter extends Presenter {
....
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent) {
....
Context mContext = parent.getContext();
CustomImageCardView mCardView = new CustomImageCardView(mContext);
mCardView.setSoundEffectsEnabled(false);
return new ViewHolder(mCardView);
}
CustomImageCardView.java
public class CustomImageCardView extends ImageCardView {
public CustomImageCardView(Context context, int themeResId) {
super(context, themeResId);
}
public CustomImageCardView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomImageCardView(Context context) {
super(context);
}
public CustomImageCardView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
super.requestFocus(direction, previouslyFocusedRect);
return false;
}
}
Code for RecyclerView:
CardAdapter.java
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
...
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = mLayoutInflater.inflate(R.layout.recycler_view, viewGroup, false);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
view.setFocusable(true);
view.setSoundEffectsEnabled(false);
}
mViewHolder = new ViewHolder(view);
return mViewHolder;
}
CustomLinearLayout.java (Root View for Recycler View)
public class CustomLinearLayout extends LinearLayout {
public CustomLinearLayout(Context context) {
super(context);
}
public CustomLinearLayout(Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomLinearLayout(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public void playSoundEffect(int soundConstant) {
super.playSoundEffect(soundConstant);
}
#Override
public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
super.requestFocus(direction, previouslyFocusedRect);
return false;
}
}
If you just want to mute android tv navigation system sound effect and there is no custom navigation behavior, I found a way by overriding onKeyDown.
First, I added a GlobalFocusChangeListener at Activity and Dialog to listen and keep the reference of focused view.
window.decorView.viewTreeObserver.addOnGlobalFocusChangeListener { oldFocus, newFocus ->
focusView = newFocus
}
Second, I overrided onKeyDown method at Activity and Dialog and implement like this.
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
focusView?.let {
when (keyCode) {
KeyEvent.KEYCODE_DPAD_UP -> View.FOCUS_UP
KeyEvent.KEYCODE_DPAD_DOWN -> View.FOCUS_DOWN
KeyEvent.KEYCODE_DPAD_LEFT -> View.FOCUS_LEFT
KeyEvent.KEYCODE_DPAD_RIGHT -> View.FOCUS_RIGHT
else -> null
}?.let { direction ->
val nextFocusView = it.focusSearch(direction)
if (nextFocusView != null) {
nextFocusView.requestFocus()
return true
}
}
}
return super.onKeyDown(keyCode, event)
}
This is work on android tv emulator and my Xiaomi TV. I think this change will not effect any touch behavior on phone or tablet.

Clickable days from a List of days

Hi all I have a question ,if someone can help me implement this design or give me a path to look from.
Actually i want to implement this UI , a user can select one ore more days ,when he selecte a day the fond of the day become bold when he unselect the day the style of the day become normal like Saturday in our case
i tried to implement this UI using toogle button but unfortunately i failed can anyone help me achieve this goal
thank you all for your help
You could have something like this if you are using multiple TextView's in something like a LinearLayout.
public class BoldTextView extends TextView implements View.OnClickListener {
private boolean bold = false;
public BoldTextView(Context context) {
this(context, null);
}
public BoldTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BoldTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (bold) {
bold = false;
setTypeface(null, Typeface.NORMAL);
} else {
bold = true;
setTypeface(null, Typeface.BOLD);
}
}
public boolean isBold() {
return bold;
}
}

TextInputLayout.setError() leaves empty space after clearing the error

I recently used TextInputLayout and it's setError() method. The problem I'm getting is, when I clear the error by calling setError(null) it leaves so much of empty space at the bottom.
Normal:
With error:
After clearing error:
After looking at the source, I found that they are making the view INVISIBLE instead of GONE
.setListener(new ViewPropertyAnimatorListenerAdapter() {
#Override
public void onAnimationEnd(View view) {
view.setVisibility(INVISIBLE); // here it is
updateLabelVisibility(true);
} }).start();
I'm wondering why is it so? How to resolve this to avoid the empty space?
Check out the docs for
public void setErrorEnabled (boolean enabled)
It says
Whether the error functionality is enabled or not in this layout.
Enabling this functionality before setting an error message via
setError(CharSequence), will mean that this layout will not change
size when an error is displayed.
Well based on this, try setting setErrorEnabled(true) before setError(), and, set setErrorEnabled(false) after setError(null).
Method setErrorEnabled(false) will clear the extra space, so call it after setError(null).
Dont use setErrorEnabled(boolean), it just doesnt show up the error from the second time.
public class MyTextInputLayout extends android.support.design.widget.TextInputLayout {
public MyTextInputLayout(Context context) {
super(context);
}
public MyTextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setError(#Nullable CharSequence error) {
super.setError(error);
View layout = getChildAt(1);
if (layout != null) {
if (error != null && !"".equals(error.toString().trim())) {
layout.setVisibility(VISIBLE);
} else {
layout.setVisibility(GONE);
}
}
}
}
Then just setError(errorMessage); or setError(null);
See this page. Google will release the fix in future support library version. It says,
If you want to fix it now you can extends the TextInputLayout and
override the setErrorEnabled() method, but I cant guarantee the
backward compatibility. Because its some danger to change state in
TextInputLayout.
public class TextInputLayout extends android.support.design.widget.TextInputLayout{
public TextInputLayout(Context context) {
super(context);
}
public TextInputLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setErrorEnabled(boolean enabled) {
super.setErrorEnabled(enabled);
if (enabled) {
return;
}
if (getChildCount() > 1) {
View view = getChildAt(1);
if (view != null) {
view.setVisibility(View.GONE);
}
}
}
}
I create a custom view for avoiding repeated code and override setError method.
public class UserInputView extends TextInputLayout {
public UserInputView(Context context) {
this(context, null);
}
public UserInputView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public UserInputView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setError(#Nullable CharSequence error) {
boolean isErrorEnabled = error != null;
setErrorEnabled(isErrorEnabled);
super.setError(error);
}
}
The source code of TextInputLayout show the following:
If you need to clear the error, just use
til.setErrorEnabled(false);
This will hide the error text and stretch the bottom space to its standard size.
In case you need to set the error again, just use
til.setError("Your text");
which automatically calls til.setErrorEnabled(true) as it assumes you need the error functionality.
This is extension in kotlin solving problem:
fun TextInputLayout.clearError() {
error = null
isErrorEnabled = false
}
The following code works fine
textInputLatout.getEditText().addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() < 1) {
textInputLayout.setErrorEnabled(true);
textInputLayout.setError("Please enter a value");
}
if (s.length() > 0) {
textInputLayout.setError(null);
textInputLayout.setErrorEnabled(false);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
By using mTextInputLayout.setErrorEnabled(false); i have solved this problem
Then you should override it like so:
#Override
public void onAnimationEnd(View view)
{
view.setVisibility(GONE); // <-- this is where you make it GONE
updateLabelVisibility(true);
}
Or try this i.e. on a button or whatever you are using:
final Button btn = (Button) findViewById(R.id.btn);
btn.setVisibility(View.GONE); //<--- makes the button gone

opacity of textview in android

In my application, initially I'm setting opacity of TextView to 60.
After that, when the user presses a button, I want to decrease or increase the opacity of the TextView as per pressing in a button that increases it or a button that decreases it.
I have tried this, but every time when I get the opacity of text view its -3 or -1 which is actually not.
public void decreaseOpacity(View v){
int op=txtView.getBackground().getOpacity();// its alwz -ve value
txtView.getBackground().setAlpha(op-1);
}
try this code
public class AlphaTextView extends TextView {
public AlphaTextView(Context context) {
super(context);
}
public AlphaTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlphaTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean onSetAlpha(int alpha) {
setTextColor(getTextColors().withAlpha(alpha));
setHintTextColor(getHintTextColors().withAlpha(alpha));
setLinkTextColor(getLinkTextColors().withAlpha(alpha));
return true;
}
}
Drawable.getOpacity() dos not take into account changes made by setAlpha(). See the docs:
Note that the returned value does not take into account a custom alpha
or color filter that has been applied by the client through the
setAlpha(int) or setColorFilter(ColorFilter) methods.
You may need to store the alpha value as a variable and not use getOpacity(), on example:
private int mTextViewAlpha = 255;
public void decreaseOpacity(View v){
if ( mTextViewAlpha-- <= 0 ) mTextViewAlpha = 0;
txtView.getBackground().setAlpha(mTextViewAlpha);
txtView.getBackground().invalidateSelf();
}

Prevent ToggleButton from switching state

I have a ToggleButton, when you click it, I don't want the state to change. I will handle state changes myself when after I receive feedback from whatever the button toggled. How might I prevent the state change on click?
You can implement your own ToggleButton with overriden toggle() method with empty body.
You could simply use the CheckedTextView instead.
Of course, you need to set a background image and a text based on the state, but other than those (which you might have used already), it's a nice alternative solution.
here's a sample code in case you miss the textOn and textOff attributes:
CheckableTextView.java :
public class CheckableTextView extends CheckedTextView {
private CharSequence mTextOn, mTextOff;
public CheckableTextView (final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CheckableTextView, defStyle, 0);
mTextOn = a.getString(R.styleable.CheckableTextView_textOn);
mTextOff = a.getString(R.styleable.CheckableTextView_textOff);
a.recycle();
}
public CheckableTextView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public CheckableTextView(final Context context) {
this(context, null, 0);
}
#Override
public void setChecked(final boolean checked) {
super.setChecked(checked);
if (mTextOn == null && mTextOff == null)
return;
if (checked)
super.setText(mTextOn);
else
super.setText(mTextOff);
}
public void setTextOff(final CharSequence textOff) {
this.mTextOff = textOff;
}
public void setTextOn(final CharSequence textOn) {
this.mTextOn = textOn;
}
public CharSequence getTextOff() {
return this.mTextOff;
}
public CharSequence getTextOn() {
return this.mTextOn;
}
}
in res/values/attr.xml :
<declare-styleable name="SyncMeCheckableTextView">
<attr name="textOn" format="reference|string" />
<attr name="textOff" format="reference|string" />
</declare-styleable>
another possible solution would be to use setClickable(false) on the ToggleButton, and handle onTouchListener when the motion action is ACTION_UP .
While I think you can just mark it as disabled, I don't think it is a good idea, as users are used to a certain semantic of such a button.
If you only want to show some state, why don't you use an ImageView and show different images depending on state?

Categories

Resources