Wednesday, 13 March 2013

Free Android Java Code

It can be tricky finding good resources when you are starting out, so I want to recommend the above website  for people studying programming http://www.deitel.com/. Once you register you get access to gigabytes of information, tutorials, code etc.. without even having to buy their books. AWESOME! Thanks Deitel. I am currently studying the Android-Programmers-App-Driven-Developer-ebook. I use the Java forum at Stack Overflow for asking questions. Happy Troll-free surfing ;)

There are probably others out there so I'll post them when I find them. Please feel free to post links to useful sites in the comments. Nice for viewing code outside of IDE: Programmer's Notepad.

Java Thread Example for a Beginner in Android

 /*
 * Simple use of Android Thread
 * Flash the screen between an array of colours at an interval
 *
 */

At first finding out how to work with threads in Android Java was quite tricky, so I've posted this simple example I put together from various tutorials and expert input from Stack Overflow.



Code:------------------------------------------------------------------

package biz.consett.mydraw;

public class AColor // a list of colours you can use in hex format
{

    final static int RED = 0xFFFF0000; // hex alpha_R_G_B
    final static int GREEN = 0xFF00FF00;
    final static int BLUE = 0xFF0000FF;
    final static int PURPLE = 0xFFAA00AA;
}


package biz.consett.mydraw;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

/*
 * Simple use of Android Thread
 * Flash the screen between an array of colours at an interval
 *
 */

public class MainActivity extends Activity
{

    static final int[] COLORS =
    {AColor.RED,  AColor.BLUE, AColor.GREEN};// colour array
    // Colour Red Green Blue
    // Index [0] [1] [2]

    private int currentColor = 0;

    private View MYVIEW = null;
    //boolean whichColor = true;

    final int interval = 100; // 0.6 second

    @Override
    public void onCreate(Bundle savedInstanceState)
    {

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

        MYVIEW = (View) findViewById(R.id.my_view);

        MYVIEW.setBackgroundColor(Color.RED);// set initial colour

        new Thread(new Runnable()
        {

            // @Override
            public void run()
            {

                while (true)
                {
                    try
                    {
                        Thread.sleep(interval); // sleep for interval
                      
                    }
                  
                    catch (InterruptedException e)
                    {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                        updateColor();
                  
                } // end of  while true loop
              
              
            }// end of run

        }).start(); // end of runnable

    } // end of onCreate bundle

    private void updateColor()
    {
      
        runOnUiThread(new Runnable()
        {

            @Override
            public void run()
            {

                if (currentColor > COLORS.length - 1)
                {
                    currentColor = 0;
                  
                }
                MYVIEW.setBackgroundColor(COLORS[currentColor]);
              
                currentColor++;
              
            }// end of run
        });
      
    }
}// -------------END OF MainActivity extends Activity-----------------------


MANIFEST:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="biz.consett.mydraw"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="biz.consett.mydraw.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


LAYOUT:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <View
      
    android:id="@+id/my_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  
    </View>
  
</RelativeLayout>


END CODE:-----------------------------------------

No comments:

Post a Comment