Android: Smooth background colour repeating with boarder - android

i did the smooth background colour repeating for TextView like below
final Handler mHandler2 = new Handler();
final Runnable mUpdateResults2 = new Runnable() {
public void run() {
ObjectAnimator colorFade = ObjectAnimator.ofObject(tvGoLive, "backgroundColor", new ArgbEvaluator(), Color.parseColor("#FFFFFF"), Color.parseColor("#000000"));
colorFade.setDuration(1000);
colorFade.start();
}
};
final Runnable mUpdateResults3 = new Runnable() {
public void run() {
ObjectAnimator colorFade = ObjectAnimator.ofObject(tvGoLive, "backgroundColor", new ArgbEvaluator(), Color.parseColor("#000000"), Color.parseColor("#FFFFFF"));
colorFade.setDuration(1000);
colorFade.start();
}
};
int delay2 = 1000;
int period2 = 1000;
Timer timer2 = new Timer();
timer2.scheduleAtFixedRate(new TimerTask() {
int rotation = 1;
public void run() {
if(rotation == 1){
mHandler2.post(mUpdateResults2);
rotation = 2;
}else if (rotation == 2) {
mHandler2.post(mUpdateResults3);
rotation = 1;
}
}
}, delay2, period2);
Now i need to set the boarder for this TextView programmatically. For this requirement i tried some codes in SO and Google, but i didn't get the boarder for text view. So, any one give me the code for setting boarder with smooth background colour repeating

Finally i got the solution
1. Drawable xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#0000FF" />
</shape>
</item>
</layer-list>
If you need boarder, then add <corners android:radius="5dp" /> inside shape of drawable xml
2. Layout
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:background="#drawable/btn_radious" >
<TextView
android:id="#+id/club_detail_go_live_tv"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_centerInParent="true"
android:background="#FFFFFF"
android:drawableLeft="#drawable/icon_go_live_small"
android:ellipsize="end"
android:gravity="center"
android:maxLength="30"
android:padding="5dp"
android:text="#string/app_name"
android:textColor="#000000" />
</RelativeLayout>
3. Code
Code as above question

Related

Circular Reveal Animation not working in the first call

I need to implement a circular reveal animation after the transition between 2 fragments is finished so that the ImageButton (android:id="#+id/update_profile_pic") will be revealed with a nice animation
The problem is that it doesn't work as it should:
setEnterSharedElementCallback(new SharedElementCallback() {
Handler handler = new Handler();
#Override
public void onSharedElementEnd(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
update_profile_pic.setVisibility(View.GONE);
// get the center for the clipping circle
int cx = update_profile_pic.getWidth() / 2;
int cy = update_profile_pic.getHeight() / 2;
// get the final radius for the clipping circle
float finalRadius = (float) Math.hypot(cx, cy);
// create the animator for this view (the start radius is zero)
Animator anim = ViewAnimationUtils.createCircularReveal(update_profile_pic, cx, cy, 0f, finalRadius);
// make the view visible and start the animation
update_profile_pic.setVisibility(View.VISIBLE);
anim.start();
}
}, 600);
}
});
When I try this animation in a button click listener, the button needs to be clicked twice to make the animation work
The layout is like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_simple_two"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".FragmentB"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.mikhaellopez.circularimageview.CircularImageView
android:id="#+id/profile_picture"
android:clickable="false"
android:src="#drawable/roni"
app:civ_border_color="#color/fadedText"
app:civ_border_width="0.1dp"
android:layout_width="260dp"
android:layout_height="260dp"
android:layout_margin="18dp"
android:transitionName="#string/simple_fragment_transition"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:scaleType="centerInside"
android:elevation="3dp"
android:layout_margin="26dp"
android:id="#+id/update_profile_pic"
android:src="#drawable/ic_menu_camera"
android:background="#drawable/round_button"
android:backgroundTint="#color/colorAccent"
app:layout_anchor="#id/profile_picture"
app:layout_anchorGravity="bottom|right|end"
android:visibility="gone"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Any help?
try to set visibility of your ImageButton to invisible instead of gone initially.

LayerDrawable change together

I have a magic problem. All instance of LayerDrawable change together.
I have several typical Views. So to done my task i decide "I will be used "Composite view". I got a problem:
When i change visibility on one Layer drawable< the change on all.
public abstract class BaseContentExpanded<DATA> extends FrameLayout implements ExpandableLayout.AdditionalAnimators {
// same not imprtand code
#Override
public List<? extends Animator> getAnimators(final boolean togle) {
final Drawable showDrawable;
final Drawable hideDraawable;
final LayerDrawable layerDrawable = (LayerDrawable) vStateSelectionIcon.getDrawable();
if (togle) {
showDrawable = layerDrawable.getDrawable(0);
hideDraawable = layerDrawable.getDrawable(isCanExpand ? 1 : 2);
} else {
hideDraawable = layerDrawable.getDrawable(0);
showDrawable = layerDrawable.getDrawable(isCanExpand ? 1 : 2);
}
ValueAnimator expandAnimator = ValueAnimator.ofFloat(0f, 1f);
expandAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.d(TAG, "getAnimators: " + layerDrawable);
float currentExpandAnimationValue = (float) animation.getAnimatedValue();
showDrawable.setAlpha((int) (255 * currentExpandAnimationValue));
hideDraawable.setAlpha((int) (255 * (1 - currentExpandAnimationValue)));
}
});
return Arrays.asList(expandAnimator);
}
}
And xml for LayerDrawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="#drawable/ic_close_light"/>
</item>
<item
android:drawable="#drawable/vector_ic_arrow_down_blue_stroke_24dp"/>
<item
android:drawable="#drawable/vector_ic_arrow_right_blue_24dp"/>
</layer-list>
Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#drawable/line_divider"
android:orientation="vertical"
android:showDividers="middle"
>
<com.learnx.android.ui.view.filters.LocationFilter
android:id="#+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<com.learnx.android.ui.view.filters.RatingFilter
android:id="#+id/rating"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I will be grateful for any help.
I wound the mistake. i forget call mutate();
final LayerDrawable layerDrawable = (LayerDrawable) vStateSelectionIcon.getDrawable();
LayerDrawable copyDrawable = (LayerDrawable) layerDrawable.mutate();

Start gradient effect after the 80% of the sceen height

What i want to achieve is to make a gradient drawable that the gradient effect starts at the bottom of the view, lets say after the 80% of the view's height.
So i want most of the view (80%) covered with startColor.
I tried:
<gradient
android:type="linear"
android:angle="90"
android:startColor="#70C272"
android:endColor="#F19941"
android:centerX="80%"
android:centerY="100%"
/>
But it doesn't seem to work.
EDIT:
Edit:
Using ShapeDrawable:
ShapeDrawable.ShaderFactory sf=new ShapeDrawable.ShaderFactory() {
#Override
public Shader resize(int width, int height) {
final float DY = 60;
final float heightFraction = height * 0.8f;
return new LinearGradient(0,heightFraction-DY,0,heightFraction+DY,
// new int[] {Color.parseColor("#E44342"), Color.parseColor("#F19941")},
new int[] {Color.BLUE, Color.GREEN},
new float[]{0.5f, 1},
Shader.TileMode.REPEAT);
}
};
ShapeDrawable sd = new ShapeDrawable(new RectShape());
sd.setShaderFactory(sf);
Well If you want to apply gradient for the bottom 20% of your View this is my approach,
I have a FrameLayout as the XML root
LinearLayout for gradient with background of gradient.XML
my xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="horizontal">
// whatever your views
<LinearLayout
android:id="#+id/my_view_gradient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/gradient_draw"
android:orientation="vertical"></LinearLayout>
</FrameLayout>
drawable resource gradient_draw.xml (you can use your own colors)
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:type="linear"
android:angle="90"
android:startColor="#70C27200"
android:endColor="#00000000"
android:centerX="80%"
android:centerY="100%"
/>
<corners android:radius="0dp" />
</shape>
Now I set this to bottom 20%
public class YourActivity extends Activity {
private int height,width;
private LinearLayout linearLayoutGradient ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
linearLayoutGradient = (LinearLayout) findViewById(R.id.my_view_gradient);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
FrameLayout.LayoutParams frameLparams= new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (height*20)/100);
frameLparams.topMargin = (height*80)/100;
linearLayoutGradient.setLayoutParams(frameLparams);
}
}

How to show notification count on ActionBar Menu items using Appcompat v21

Actually I am developing an application in android and I want to show the total no. of unread emails on the Action Bar Menu Item (like flipkart and all other applications already have) using latest Appcompat V21. I tried a code which I have found here but its not working with AppCompat V21.
Please help me if you can!
Posting the same code here:
=> layout/menu/menu_actionbar.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
...
<item android:id="#+id/menu_hotlist"
android:actionLayout="#layout/action_bar_notifitcation_icon"
android:showAsAction="always"enter code here
android:icon="#drawable/ic_bell"
android:title="#string/hotlist" />
...
</menu>
=> layout/action_bar_notifitcation_icon.xml
Note style and android:clickable properties. these make the layout the size of a button and make the background gray when touched.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="center"
android:clickable="true"
style="#android:style/Widget.ActionButton">
<ImageView
android:id="#+id/hotlist_bell"
android:src="#drawable/ic_bell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_margin="0dp"
android:contentDescription="bell"
/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hotlist_hot"
android:layout_width="wrap_content"
android:minWidth="17sp"
android:textSize="12sp"
android:textColor="#ffffffff"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#null"
android:layout_alignTop="#id/hotlist_bell"
android:layout_alignRight="#id/hotlist_bell"
android:layout_marginRight="0dp"
android:layout_marginTop="3dp"
android:paddingBottom="1dp"
android:paddingRight="4dp"
android:paddingLeft="4dp"
android:background="#drawable/rounded_square"/>
</RelativeLayout>
=> drawable-xhdpi/ic_bell.png
A 64x64 pixel image with 10 pixel wide paddings from all sides. You are supposed to have 8 pixel wide paddings, but I find most default items being slightly smaller than that. Of course, you'll want to use different sizes for different densities.
=> drawable/rounded_square.xml
Here, #ff222222 (color #222222 with alpha #ff (fully visible)) is the background color of my Action Bar.
**<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="2dp" />
<solid android:color="#ffff0000" />
<stroke android:color="#ff222222" android:width="2dp"/>
</shape>**
=> com/ipp/MyAppAndroid/MyActivity.java
private int hot_number = 0;
private TextView ui_hot = null;
#Override public boolean onCreateOptionsMenu(final Menu menu) {
MenuInflater menuInflater = getSupportMenuInflater();
menuInflater.inflate(R.menu.menu_actionbar, menu);
final View menu_hotlist = menu.findItem(R.id.menu_hotlist).getActionView();
ui_hot = (TextView) menu_hotlist.findViewById(R.id.hotlist_hot);
updateHotCount(hot_number);
new MyMenuItemStuffListener(menu_hotlist, "Show hot message") {
#Override
public void onClick(View v) {
onHotlistSelected();
}
};
return super.onCreateOptionsMenu(menu);
}
// call the updating code on the main thread,
// so we can call this asynchronously
public void updateHotCount(final int new_hot_number) {
hot_number = new_hot_number;
if (ui_hot == null) return;
runOnUiThread(new Runnable() {
#Override
public void run() {
if (new_hot_number == 0)
ui_hot.setVisibility(View.INVISIBLE);
else {
ui_hot.setVisibility(View.VISIBLE);
ui_hot.setText(Integer.toString(new_hot_number));
}
}
});
}
static abstract class MyMenuItemStuffListener implements View.OnClickListener, View.OnLongClickListener {
private String hint;
private View view;
MyMenuItemStuffListener(View view, String hint) {
this.view = view;
this.hint = hint;
view.setOnClickListener(this);
view.setOnLongClickListener(this);
}
#Override abstract public void onClick(View v);
#Override public boolean onLongClick(View v) {
final int[] screenPos = new int[2];
final Rect displayFrame = new Rect();
view.getLocationOnScreen(screenPos);
view.getWindowVisibleDisplayFrame(displayFrame);
final Context context = view.getContext();
final int width = view.getWidth();
final int height = view.getHeight();
final int midy = screenPos[1] + height / 2;
final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
Toast cheatSheet = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
if (midy < displayFrame.height()) {
cheatSheet.setGravity(Gravity.TOP | Gravity.RIGHT,
screenWidth - screenPos[0] - width / 2, height);
} else {
cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
}
cheatSheet.show();
return true;
}
}
Note: Due to lack to reputation, unable to post pic.

How to display an ImageView progressively down from top to bottom

is there a way to display an ImageView progressively from top to down,
like this:
Sorry for the crappy animation.
I'm not very familiar with android animations, but one(a little hackish) way is to wrap the image in a ClipDrawable and animate its level value. For example:
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/clip_source" />
Where clip_source is a drawable:
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:clipOrientation="vertical"
android:drawable="#drawable/your_own_drawable"
android:gravity="bottom" />
Then in code you would have:
// a field in your class
private int mLevel = 0;
ImageView img = (ImageView) findViewById(R.id.imageView1);
mImageDrawable = (ClipDrawable) img.getDrawable();
mImageDrawable.setLevel(0);
mHandler.post(animateImage);
The animateImage is a Runnable object:
private Runnable animateImage = new Runnable() {
#Override
public void run() {
doTheAnimation();
}
};
and the doTheAnimation method:
private void doTheAnimation() {
mLevel += 1000;
mImageDrawable.setLevel(mLevel);
if (mLevel <= 10000) {
mHandler.postDelayed(animateImage, 50);
} else {
mHandler.removeCallbacks(animateImage);
}
}

Categories

Resources