Adsense Ad

Wednesday 13 September 2017

Simple Disco Light App

How to create simple disco light app for android

First create simple app with empty activity, clear all padding and set all default thing.
Secondly create and identified Text View.

<?xml version="1.0" encoding="utf-8"?><LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"    
android:orientation="vertical"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:id="@+id/discolayout"    >
<TextView    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:background="#dff9be"    
android:id="@+id/shade"/>
</LinearLayout>


Now change in java class and use timer with handler to accomplish the task.
public class MainActivity extends AppCompatActivity {
TextView shade;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shade = (TextView) view.findViewById(R.id.shade);
final Handler h = new Handler();
TimerTask tt = new TimerTask() {
    @Override    public void run() {
     h.post(new Runnable() {
         @Override         public void run() {
             Random rand = new Random();
shade.setBackgroundColor(Color.argb(255, rand.nextInt(256), rand.nextInt(256), rand.nextInt(256)));
         }
     });
    }
};
Timer timer = new Timer();
timer.schedule(tt,100,100);
    }
}

No comments: