Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday, 1 March 2017

Programmatically create gradient in android

Hello friends,
If you want create gradient but not using xml then follow below instructions and code.

MainActivity.java


private LinearLayout ll_mainLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

   ll_mainLayout=(LinearLayout)findViewById(R.id. ll_main);
  ll_mainLayout.setBackgroundResource(R.drawable. getgradiantInit);



}


public GradientDrawable getgradiantInit() {
    int[] colors = {Color.parseColor("#F28542"), Color.parseColor("#F74368")};
    GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);
    gd.setCornerRadius(20f);
    return gd;
}

private GradientDrawable getgradiantFirst() {//3AA2E2
   
int[] colors1 = {Color.parseColor("#46AF62"), Color.parseColor("#3AA2E2")};//3AA2E2
   
GradientDrawable gd1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
    gd1.setCornerRadius(20f);

    return gd1;
}

private GradientDrawable getgradiantSecond() {
    int[] colors1 = {Color.parseColor("#EFB410"), Color.parseColor("#D14654")};
    GradientDrawable gd1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
    gd1.setCornerRadius(20f);

    return gd1;
}

private GradientDrawable getgradiantThird() {
    int[] colors1 = {Color.parseColor("#90CC57"), Color.parseColor("#46AF62")};
    GradientDrawable gd1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors1);
    gd1.setCornerRadius(10f);

    return gd1;
}

main_activity.xml

<LinearLayout
    android:id="@+id/ll_main"
    android:layout_width="300dp"
    android:layout_height="400dp"
    android:orientation="vertical">

<TextView
    android:layout_below="@+id/txtUserGameDate"
    android:id="@+id/txtUserGameTime"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="SAMSET
    android:textColor="@color/white"
    android:textSize="8sp"
    android:textStyle="bold"
    />


</LinearLayout>


Thank you