Monday, June 11, 2012

Create a colorful flashlight in android

Create an android project in eclipse and pasted the following code in the main activity class:

package com.rayhan.nothing;

import java.util.Random;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class AndruTestActivity extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

View v=findViewById(R.id.mainlayout);

v.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
Random rnd=new Random();

v.setBackgroundColor(rnd.nextInt());

}
});

}
}

//------------------------------------------------

And here is the xml file for the layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

</LinearLayout>

No comments:

Post a Comment