Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, 30 April 2017

Set random color background in android

Hello friends,



activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">


    <FrameLayout
        android:id="@+id/flayout"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_alignParentBottom="true">

     
    </FrameLayout>

</RelativeLayout>


set random color


ActivityMain.java



@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

FrameLayout flayout=(FrameLayout)findViewById(R.id.flayout);


 int randomAndroidColor = color[new Random().nextInt(color.length)];

 GradientDrawable bgShape = (GradientDrawable) holder.llCircle.getBackground();

Random random = new Random();

flayout.setColor(Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)));

if you want use own color list then enable below line

flayout.setColor(randomAndroidColor);




}