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;
}
}
Related
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).
I was looking at the documentation for the Button widget and noticed that it is a subclass of TextView. Is there any real difference between a Button and a TextView if I use the same selector drawable on both. In other words, I already am using TextViews with selectors for my app's "buttons", but is there anything to be gained from using the Button class instead?
No, there's essentially no difference between TextView and Button. Button just comes styled as a button out of the box.
Here is the source for Button from API 19.
#RemoteView
public class Button extends TextView {
public Button(Context context) {
this(context, null);
}
public Button(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.buttonStyle);
}
public Button(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(Button.class.getName());
}
#Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(Button.class.getName());
}
}
hi I am developing android application in which i am using one custom frame layout class. Inside that class I am using one drawable and with the help of canvas i m drawing that. I did this in following way :
public class BackgroundContainer extends FrameLayout implements OnTouchListener{
Drawable mShadowedBackground;
public BackgroundContainer(Context context) {
super(context);
init();
}
public BackgroundContainer(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BackgroundContainer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
mShadowedBackground =
getContext().getResources().getDrawable(R.drawable.actionbar);
}
#Override
public boolean onTouchEvent(MotionEvent event)
{
Log.i("OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO", "OOOOOOOOOOOOOOOOOOOOOO");
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN: {
invalidate();
}
}
return true;
}
#Override
protected void onDraw(Canvas canvas) {
mShadowedBackground.setBounds(getWidth()-150, 0, getWidth(), mOpenAreaHeight);
canvas.save();
canvas.translate(0, mOpenAreaTop);
mShadowedBackground.draw(canvas);
canvas.restore();
}
}
}
Now I am want to listen click even on my drawable. I implement ontouch event but its not working. Am i doing it in wrong way. Need help thank you.
Drawables are not clickable as it is not considered as a view.
Like deepak already said: by implementing the corresponding listener you just provide the behavior what should happen for a specific event. You still need to add the listener :) In your case this would help (in your init()):
setOnTouchListener(this);
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();
}
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?