Oct 28, 2011

Read raw resource of text

Read raw resource of text

Create a raw resource of text, /res/raw/mytext.txt.
Hello!

It's a example of raw text in 
/res/raw/mytext.txt

Some text here:
Android is an operating system for mobile devices such as smartphones and tablet computers. It is developed by the Open Handset Alliance led by Google.


Java main code:
package com.AndroidTextResource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidTextResourceActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
        TextView myTextView = new TextView(this);
        setContentView(myTextView);
        

        InputStream inputStream = getResources().openRawResource(R.raw.mytext);
        
        ByteArrayOutputStream byteArrayOutputStream 
         = new ByteArrayOutputStream();
        
        String myText = "";
        int in;
  try {
   in = inputStream.read();
   while (in != -1)
         {
    byteArrayOutputStream.write(in);
          in = inputStream.read();
         }
   inputStream.close();
   myText = byteArrayOutputStream.toString();
  }catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

        myTextView.setText(myText);
    }
}


- Read raw text file, and display in ScrollView.

Oct 27, 2011

Using shape to define round drawable

Using shape to define round drawable

To define our shape as a drawable, create XML(s) under /res/drawable/ folder.

/res/drawable/redroundrect.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#FFFF0000"/>
<corners
android:radius="15dp" />
<padding
android:left="20dp"
android:top="20dp"
android:right="20dp"
android:bottom="2dp" />
</shape>


/res/drawable/greenroundrect.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#FF00FF00"/>
<corners
android:radius="15dp" />
<padding
android:left="20dp"
android:top="20dp"
android:right="20dp"
android:bottom="2dp" />
</shape>


/res/drawable/blueroundrect.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid
android:color="#FF0000FF"/>
<corners
android:radius="15dp" />
<padding
android:left="20dp"
android:top="20dp"
android:right="20dp"
android:bottom="2dp" />
</shape>


Using it inside main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RED in XML"
android:background="@drawable/redroundrect" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="GREEN in XML"
android:background="@drawable/greenroundrect" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BLUE in XML"
android:background="@drawable/blueroundrect" />
<TextView
android:id="@+id/javared"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RED using Java"/>
<TextView
android:id="@+id/javagreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="GREEN using Java"/>
<TextView
android:id="@+id/javablue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BLUE using Java"/>
</LinearLayout>


In Java code
package com.AndroidTestResources;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidTestResourcesActivity extends Activity {

TextView javaRed, javaGreen, javaBlue;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

javaRed = (TextView)findViewById(R.id.javared);
javaGreen = (TextView)findViewById(R.id.javagreen);
javaBlue = (TextView)findViewById(R.id.javablue);

Drawable redRoundRect
= getResources().getDrawable(R.drawable.redroundrect);
Drawable greenRoundRect
= getResources().getDrawable(R.drawable.greenroundrect);
Drawable blueRoundRect
= getResources().getDrawable(R.drawable.blueroundrect);

javaRed.setBackgroundDrawable(redRoundRect);
javaGreen.setBackgroundDrawable(greenRoundRect);
javaBlue.setBackgroundDrawable(blueRoundRect);
}
}



Related: Define color drawable in XML



Oct 26, 2011

Define color drawable in XML

Define color drawable in XML

We can define our custom color in XML under /res/values/ folder.

example: /res/values/myresources.xml
<resources>
<drawable name="red_color">#ff0000</drawable>
<drawable name="blue_color">#0000ff</drawable>
<drawable name="green_color">#00ff00</drawable>
</resources>


After color defined, it can be acccessed as drawable in XML or using Java code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RED in XML"
android:background="@drawable/red_color" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="GREEN in XML"
android:background="@drawable/green_color" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BLUE in XML"
android:background="@drawable/blue_color" />
<TextView
android:id="@+id/javared"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RED using Java"/>
<TextView
android:id="@+id/javagreen"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="GREEN using Java"/>
<TextView
android:id="@+id/javablue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="BLUE using Java"/>
</LinearLayout>


package com.AndroidTestResources;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidTestResourcesActivity extends Activity {

TextView javaRed, javaGreen, javaBlue;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

javaRed = (TextView)findViewById(R.id.javared);
javaGreen = (TextView)findViewById(R.id.javagreen);
javaBlue = (TextView)findViewById(R.id.javablue);

ColorDrawable red = (ColorDrawable)getResources().getDrawable(R.drawable.red_color);
ColorDrawable green = (ColorDrawable)getResources().getDrawable(R.drawable.green_color);
ColorDrawable blue = (ColorDrawable)getResources().getDrawable(R.drawable.blue_color);

javaRed.setBackgroundDrawable(red);
javaGreen.setBackgroundDrawable(green);
javaBlue.setBackgroundDrawable(blue);
}
}



Related: Using shape to define round drawable



Using Android device build-in light sensor

It's a simple light meter Using Android device build-in light sensor. A ProgressBar is defined in main.xml as a indicator of the light sensor. The default value of 100 defined in android:max is a dummy value, it will be updated after lightSensor.getMaximumRange(). The updated reading will be be read in lightSensorEventListener, and update the ProgressBar accordingly.

Using Android device build-in light sensor

package com.AndroidLightSensor;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidLightSensorActivity extends Activity {

ProgressBar lightMeter;
TextView textMax, textReading;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lightMeter = (ProgressBar)findViewById(R.id.lightmeter);
textMax = (TextView)findViewById(R.id.max);
textReading = (TextView)findViewById(R.id.reading);

SensorManager sensorManager
= (SensorManager)getSystemService(Context.SENSOR_SERVICE);
Sensor lightSensor
= sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

if (lightSensor == null){
Toast.makeText(AndroidLightSensorActivity.this,
"No Light Sensor! quit-",
Toast.LENGTH_LONG).show();
}else{
float max = lightSensor.getMaximumRange();
lightMeter.setMax((int)max);
textMax.setText("Max Reading: " + String.valueOf(max));

sensorManager.registerListener(lightSensorEventListener,
lightSensor,
SensorManager.SENSOR_DELAY_NORMAL);

}
}

SensorEventListener lightSensorEventListener
= new SensorEventListener(){

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if(event.sensor.getType()==Sensor.TYPE_LIGHT){
float currentReading = event.values[0];
lightMeter.setProgress((int)currentReading);
textReading.setText("Current Reading: " + String.valueOf(currentReading));
}
}

};
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ProgressBar
android:id="@+id/lightmeter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="20dp"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="0"
/>
<TextView
android:id="@+id/max"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/reading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>




Oct 25, 2011

Start Battery use checking using Intent.ACTION_POWER_USAGE_SUMMARY

Start Battery use checking using Intent.ACTION_POWER_USAGE_SUMMARY

package com.AndroidBatteryUsage;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.widget.Toast;

public class AndroidBatteryUsageActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
ResolveInfo resolveInfo = packageManager.resolveActivity(intent, 0);

if(resolveInfo == null){
Toast.makeText(AndroidBatteryUsageActivity.this,
"No suitable Activity found!",
Toast.LENGTH_LONG).show();
}else{
startActivity(intent);
}
}
}




Infolinks In Text Ads