Search This Blog

Wednesday, February 29, 2012

Progressbar using Thread

Here i am going to give the example of the Progress bar using thread.

ProgressBar is a visual indicator of progress in some operation. Displays a bar to the userrepresenting how far the operation has progressed; the application can change the amount of progress (modifying the length of the bar) as it moves forward.

First of all make an .xml file and insert the following into it



<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressbar_default"
/>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:id="@+id/progressbar_Horizontal"
android:max="100"
/>
</LinearLayout>


make an JAVA file and enter the following code into it.

package com.exercise.AndroidProgressBar;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;

public class AndroidProgressBar extends Activity {

         ProgressBar myProgressBar;
         int myProgress = 0;

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

                        myProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal);

                        new Thread(myThread).start();
         }

         private Runnable myThread = new Runnable(){

         @Override
         public void run() {
                   // TODO Auto-generated method stub
                   while (myProgress<100){
                   try{
                        myHandle.sendMessage(myHandle.obtainMessage());
                        Thread.sleep(1000);
                        }
                   catch(Throwable t){
                     }
            }
 }

          Handler myHandle = new Handler(){

         @Override
          public void handleMessage(Message msg) {
                       // TODO Auto-generated method stub
                       myProgress++;
                       myProgressBar.setProgress(myProgress);
           }

        };
   };
}



Tuesday, February 28, 2012

Uploading Image to the server using webservice

first of all reach to the gallery of the phone using the intent.


Button btnBrowse = (Button)findViewById(R.id.btn_browse);
        btnBrowse.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                "Select Picture"), SELECT_PICTURE);

}
});


Now onActivityResult should be like this.





public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode == RESULT_OK) {
       if (requestCode == SELECT_PICTURE) {
           Uri selectedImageUri = data.getData();
           selectedImagePath = getPath(selectedImageUri);
           EditText imageBrowse = (EditText)findViewById(R.id.thumb_url);
           imageBrowse.setText(selectedImagePath);
           byte[] strBytes = convertToBytes(selectedImagePath);
           
           imageBytes = strBytes;
       }
   }
}
    


    public byte[] convertToBytes(String selectedImagePath)
    {
    try
    {
    FileInputStream fs = new FileInputStream(selectedImagePath);

    Bitmap bitmap = BitmapFactory.decodeStream(fs);
   
    ByteArrayOutputStream bOutput = new ByteArrayOutputStream();
   
    bitmap.compress(CompressFormat.JPEG,1, bOutput);
   
    byte[] dataImage = bOutput.toByteArray();
   
    return dataImage;
    }
    catch(NullPointerException ex)
    {
    ex.printStackTrace();
    return null;
   
    catch (FileNotFoundException e)
    {  
e.printStackTrace();
return null;
}
   
    }


public String getPath(Uri uri) {
   String[] projection = { MediaStore.Images.Media.DATA };
   Cursor cursor = managedQuery(uri, projection, null, null, null);
   int column_index = cursor
           .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   cursor.moveToFirst();
   return cursor.getString(column_index);
}




Using this bytes you'll get the image uploaded to the web service and store the image on to the server.

Monday, February 27, 2012

Creating the gradient effect in button

First of all make an .xml file which can be selected the button stats and give them the effect in the gradients.
I have created the button_shape.xml and put this code into it.


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
<shape android:shape="rectangle">
       <gradient android:endColor="#75B11D"
                android:startColor="#A3C723" android:angle="270" />
   <stroke android:width="1dp" android:color="#75B11D" />
   <padding android:left="10dp" android:right="10dp" android:top="5dp" android:bottom="5dp" />
   <corners
       android:bottomLeftRadius="5dip"
       android:topRightRadius="5dip"
       android:topLeftRadius="5dip"
       android:bottomRightRadius="5dip" />
</shape>
    </item>
    <item android:state_active="true">
<shape android:shape="rectangle">
            <gradient android:endColor="#4CC417"
                android:startColor="#4AA02C" android:angle="270" />
<stroke android:width="1dp" android:color="#181818" />
   <padding android:left="10dp" android:right="10dp" android:top="5dp" android:bottom="5dp" />
   <corners
       android:bottomLeftRadius="3dip"
       android:topRightRadius="3dip"
       android:topLeftRadius="3dip"
       android:bottomRightRadius="3dip" />
</shape>
    </item>
    <item android:state_focused="true">
<shape android:shape="rectangle">
       <gradient android:endColor="#4CC417"
                android:startColor="#4AA02C" android:angle="270" />
   <stroke android:width="1dp" android:color="#181818" />
   <padding android:left="10dp" android:right="10dp" android:top="5dp" android:bottom="5dp" />
   <corners
       android:bottomLeftRadius="3dip"
       android:topRightRadius="3dip"
       android:topLeftRadius="3dip"
       android:bottomRightRadius="3dip" />
</shape>
    </item>
    <item android:state_pressed="true">
<shape android:shape="rectangle">
            <gradient android:endColor="#4CC417"
                android:startColor="#4AA02C" android:angle="270" />
   <stroke android:width="1dp" android:color="#181818" />
   <padding android:left="10dp" android:right="10dp" android:top="5dp" android:bottom="5dp" />
   <corners
       android:bottomLeftRadius="3dip"
       android:topRightRadius="3dip"
       android:topLeftRadius="3dip"
       android:bottomRightRadius="3dip" />
</shape>
    </item>
    <item>
    <shape android:shape="rectangle">
            <gradient android:endColor="#75B11D"
                android:startColor="#A3C723" android:angle="270" />
   
   <padding android:left="10dp" android:right="10dp" android:top="5dp" android:bottom="5dp" />
   <corners
       android:bottomLeftRadius="3dip"
       android:topRightRadius="3dip"
       android:topLeftRadius="3dip"
       android:bottomRightRadius="3dip" />
</shape>
    </item>
</selector>




Now  in the button background set this .xml file and see the effect. Change the start and end color according to your requirement.



<Button
         android:id="@+id/Login"
         android:layout_width="63dp"
         android:layout_height="25dp"
         android:layout_x="245dp"
         android:layout_y="2dp"
         android:background="@drawable/button_shape"
         android:text="Login"
         android:textSize="11dp" />




Give comment for the further requirement.



Thursday, February 16, 2012

Read AndroidOS version info., using System.getProperty Method

Now this is the another class of reading the android Os version info with the getProperty Method.

Make an .xml file with the following 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="Android System:"
 />
<TextView
 android:id="@+id/SYSinfo"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
</LinearLayout>

Now make the java file for the getProperty method with the following code.



public class OSversion extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.osversion);
TextView SYSinfo = (TextView) findViewById(R.id.SYSinfo);
    SYSinfo.setText(ReadSYSinfo());
}


private static StringBuffer SYSinfoBuffer;


private String ReadSYSinfo()
{
 SYSinfoBuffer = new StringBuffer();
 
 getProperty("os.name", "os.name", SYSinfoBuffer);
 getProperty("os.version", "os.version", SYSinfoBuffer);
 
 getProperty("java.vendor.url", "java.vendor.url", SYSinfoBuffer);
 getProperty("java.version", "java.version", SYSinfoBuffer);
 getProperty("java.class.path", "java.class.path", SYSinfoBuffer);
 getProperty("java.class.version", "java.class.version", SYSinfoBuffer);
 getProperty("java.vendor", "java.vendor", SYSinfoBuffer);
 getProperty("java.home", "java.home", SYSinfoBuffer);
 
 getProperty("user.name", "user.name", SYSinfoBuffer);
 getProperty("user.home", "user.home", SYSinfoBuffer);
 getProperty("user.dir", "user.dir", SYSinfoBuffer);
 
 return SYSinfoBuffer.toString();
}


private void getProperty(String name, String property, StringBuffer tBuffer)
{
 tBuffer.append(name);
 tBuffer.append(" : ");
 tBuffer.append(System.getProperty(property));
 tBuffer.append("\n");
}
}



Here is the output of the above code



How to Read OS/kernel Version

First of all make an .xml file and put the following code into it.


<?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:gravity="center_horizontal"
android:text="typicaljava.blogspot.com"
android:autoLink="web"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Android OS:"
/>
<TextView
android:id="@+id/OSinfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Now make a JAVA file and put the following code below.



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


                 TextView OSinfo = (TextView) findViewById(R.id.OSinfo);
                 OSinfo.setText(ReadOSinfo());

        }



         private String ReadOSinfo()
        {
             ProcessBuilder cmd;
             String result="";

             try{

                        String[] args = {"/system/bin/cat", "/proc/version"};
                        cmd = new ProcessBuilder(args);

                           


                        Process process = cmd.start();

                        InputStream in = process.getInputStream();
                        byte[] re = new byte[1024];
                        while(in.read(re) != -1){
                        System.out.println(new String(re));
                        result = result + new String(re);
                }
                in.close();
           } 
           catch(IOException ex)
          {
               ex.printStackTrace();
           }
        return result;
     }
}


you'll get the following output.





Tuesday, February 14, 2012

Color Picker Dialog With Seekbar

This color picker dialog has sliders (SeekBars) and  EditTexts for red, green, blue and alpha values. It has a live preview of the color. It returns int values for R, G, B, A for easy use with Color.argb().

Make an .xml file main.xml and put the code below.


<?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" >
    <ImageView
android:id="@+id/color"
android:layout_width="fill_parent"
android:layout_height="60dip" />
    <Button 
    android:id="@+id/select"
    android:text="Select Color" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
</LinearLayout>


Now make an another .xml file named color_layout.xml and put the code below.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:id="@+id/main"
    android:orientation="vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:minWidth="300dp">
    
    
<TextView
android:id="@+id/red_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="red" />
<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:gravity="center_vertical|left">
<SeekBar 
  android:id="@+id/red_bar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="255"
  android:layout_weight="1"
  android:paddingLeft="7dp"
  android:paddingRight="7dp" />
<EditText
android:id="@+id/red_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0"  />
</LinearLayout>


<TextView
android:id="@+id/green_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="green" />
<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:gravity="center_vertical|left">
<SeekBar 
  android:id="@+id/green_bar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="255"
  android:layout_weight="1"
  android:paddingLeft="7dp"
  android:paddingRight="7dp" />
<EditText
android:id="@+id/green_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0"  />
</LinearLayout>


<TextView
android:id="@+id/blue_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="blue" />
<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:gravity="center_vertical|left">
<SeekBar 
  android:id="@+id/blue_bar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="255"
  android:layout_weight="1"
  android:paddingLeft="7dp"
  android:paddingRight="7dp" />
<EditText
android:id="@+id/blue_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number"
android:text="0"  />
</LinearLayout>


<TextView
android:id="@+id/alpha_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="alpha" />
<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:gravity="center_vertical|left">
<SeekBar 
  android:id="@+id/alpha_bar"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:max="255"
  android:progress="255"
  android:layout_weight="1"
  android:paddingLeft="7dp"
  android:paddingRight="7dp" />
<EditText
android:id="@+id/alpha_text"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:lines="1"
android:gravity="center_vertical|right"
android:maxLength="3"
android:inputType="number" 
android:text="255" 
/>
</LinearLayout>


<ImageView
android:id="@+id/color_preview"
android:layout_width="fill_parent"
android:layout_height="40dip" />

<LinearLayout
android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal"
    android:gravity="center_vertical|center_horizontal"
    android:layout_marginTop="20dp">
<Button 
  android:id="@+id/ok"
  android:layout_width="100dp"
  android:layout_height="wrap_content"
  android:text="OK" />
  <Button 
  android:id="@+id/cancel"
  android:layout_width="100dp"
  android:layout_height="wrap_content"
  android:text="Cancel" />

</LinearLayout>

</LinearLayout>




First of all make a java file named ColorPickerDialog and insert the following code into it.


public class ColorPickerDialog extends Dialog implements SeekBar.OnSeekBarChangeListener, TextWatcher, OnClickListener {
SeekBar   redBar;
EditText  redText;
SeekBar   greenBar;
EditText  greenText;
SeekBar   blueBar;
EditText  blueText;
SeekBar   alphaBar;
EditText  alphaText;
ImageView colorPreview;
Button    ok;
Button    cancel;
String    color;

    public interface OnColorChangedListener {
        void colorChanged(int a, int r, int g, int b);
    }


    private OnColorChangedListener mListener;




    public ColorPickerDialog(Context context, OnColorChangedListener listener, String color) {
        super(context);
        mListener = listener;
        this.color = color;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.color_layout);
        setTitle("Color Picker");
        
        redBar    = (SeekBar)  findViewById(R.id.red_bar);
redText   = (EditText) findViewById(R.id.red_text);
greenBar  = (SeekBar)  findViewById(R.id.green_bar);
greenText = (EditText) findViewById(R.id.green_text);
blueBar   = (SeekBar)  findViewById(R.id.blue_bar);
blueText  = (EditText) findViewById(R.id.blue_text);
alphaBar  = (SeekBar)  findViewById(R.id.alpha_bar);
alphaText = (EditText) findViewById(R.id.alpha_text);
ok         = (Button)   findViewById(R.id.ok);
cancel     = (Button)   findViewById(R.id.cancel);

colorPreview = (ImageView) findViewById(R.id.color_preview);

//set initial colors
String[] colorVal = color.split(",");
int a = Integer.parseInt(colorVal[0]);
int r = Integer.parseInt(colorVal[1]);
int g = Integer.parseInt(colorVal[2]);
int b = Integer.parseInt(colorVal[3]);

colorPreview.setBackgroundColor(Color.argb(a, r, g, b));
redBar.setProgress(r);
greenBar.setProgress(g);
blueBar.setProgress(b);
alphaBar.setProgress(a);

redText.setText(colorVal[1]);
greenText.setText(colorVal[2]);
blueText.setText(colorVal[3]);
alphaText.setText(colorVal[0]);

redBar.setOnSeekBarChangeListener(this);
greenBar.setOnSeekBarChangeListener(this);
blueBar.setOnSeekBarChangeListener(this);
alphaBar.setOnSeekBarChangeListener(this);

redText.addTextChangedListener(this);
greenText.addTextChangedListener(this);
blueText.addTextChangedListener(this);
alphaText.addTextChangedListener(this);


ok.setOnClickListener(this);
cancel.setOnClickListener(this);

setCancelable(false);




    }


    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser){
switch (seekBar.getId()) {
        case R.id.red_bar:  
        redText.setText(Integer.toString(progress));
        break;
        case R.id.green_bar:  
        greenText.setText(Integer.toString(progress));
        break;
        case R.id.blue_bar:  
        blueText.setText(Integer.toString(progress));
        break;
        case R.id.alpha_bar:  
        alphaText.setText(Integer.toString(progress));
        break;
       }
}

colorPreview.setBackgroundColor(Color.argb(alphaBar.getProgress(), redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress()));


}


public void onStartTrackingTouch(SeekBar seekBar) {
}


public void onStopTrackingTouch(SeekBar seekBar) {
}


public void afterTextChanged(Editable arg0) {
if(Integer.parseInt(redText.getText().toString()) > 255)
redText.setText("255");

if(!redText.getText().toString().equals("")){
if(Integer.parseInt(redText.getText().toString()) > 255)
redText.setText("255");
redBar.setProgress(Integer.parseInt(redText.getText().toString()));
} else 
redBar.setProgress(0);


if(!greenText.getText().toString().equals("")){
if(Integer.parseInt(greenText.getText().toString()) > 255)
greenText.setText("255");
greenBar.setProgress(Integer.parseInt(greenText.getText().toString()));
} else 
greenBar.setProgress(0);


if(!blueText.getText().toString().equals("")){
if(Integer.parseInt(blueText.getText().toString()) > 255)
blueText.setText("255");
blueBar.setProgress(Integer.parseInt(blueText.getText().toString()));
}else 
blueBar.setProgress(0);


if(!alphaText.getText().toString().equals("")){
if(Integer.parseInt(alphaText.getText().toString()) > 255)
alphaText.setText("255");
alphaBar.setProgress(Integer.parseInt(alphaText.getText().toString()));
}else 
alphaBar.setProgress(0);


}


public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}


public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}



public void onClick(View v) {
switch (v.getId()) {
        case R.id.ok:  
        mListener.colorChanged(alphaBar.getProgress(), redBar.getProgress(), greenBar.getProgress(), blueBar.getProgress());
        dismiss();
        break;
        case R.id.cancel:  
        dismiss();
        break;
}

}
}


Now make a main java File named MyActivity with implementing the ColorPickerDialog.OnColorChangedListener. And put the code in it.



public class MyActivity extends Activity implements ColorPickerDialog.OnColorChangedListener {




String color = "255,255,000,000"; //alpha, red, green, blue
    
ImageView image;


    
public void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        image = (ImageView) findViewById(R.id.color);
        String[] colorVal = color.split(",");
        image.setBackgroundColor(Color.argb(Integer.parseInt(colorVal[0]),Integer.parseInt(colorVal[1]),Integer.parseInt(colorVal[2]),Integer.parseInt(colorVal[3])));
        
        Button btn = (Button) findViewById(R.id.select);
        btn.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                new ColorPickerDialog(MyActivity.this, MyActivity.this, color).show();
            }
        });


    }


    public void colorChanged(int a, int r, int g, int b) {
    color = a + "," + r + "," + g + "," + b;
        image.setBackgroundColor(Color.argb(a,r,g,b));


    }


}