Friday, 17 August 2012

Installing the Eclipse and ADK

First download Eclipse
http://www.eclipse.org/downloads/

Start Eclipse, then select Help > Install New Software.
  1. Click Add, in the top-right corner.
  2. In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location  :         https://dl-ssl.google.com/android/eclipse/
  3. Click OK
If you have problem try "http" connection (  http://dl-ssl.google.com/android/eclipse/  ).

From time to time you should make updates.
  1. Select Help > Check for Updates.
  2. If there are updates available, select all, then click Next.
  3. In the Update Details dialog, click Next.
  4. Restart Eclipse.
After that .
  1.  Select Windows > Android SDK Manager.
  2.  Select all and install packages

Thursday, 16 August 2012

Simple load URL (Android)

package test.app;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Test extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);       
       
        WebView web = (WebView) findViewById(R.id.webview);         
        web.loadUrl("http://www.google.com");
            
    }
   
    private class Callback extends WebViewClient {
      @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return(false);
        }
    } 
}

//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">

<WebView android:id="@+id/webview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
   
</LinearLayout>

Draw text (Android)

package test.app;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.widget.ImageView;
import android.view.View.OnKeyListener;
import android.view.View;
import android.widget.EditText;

public class Test extends Activity {
      ImageView drawingImage;
      String text;

      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        addKeyListener();
       
        drawingImage = (ImageView) this.findViewById(R.id.drawingImageView1);
        Bitmap bitmap = Bitmap.createBitmap((int) getWindowManager()
            .getDefaultDisplay().getWidth(), (int) getWindowManager()
            .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawingImage.setImageBitmap(bitmap);

        // Draw Text

        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setTextSize(64);
        float text_x = 120;
        float text_y = 120;
        canvas.drawText(text, text_x, text_y, paint);

    }
      private void addKeyListener() {
        final EditText edittext1 = (EditText) findViewById(R.id.editText1);
        edittext1.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if ((event.getAction() == KeyEvent.ACTION_DOWN)
                        && (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    text = edittext1.getText().toString();
                    return true;   
                }
                return false;
            }
        });   
    }
}

Wednesday, 15 August 2012

Change spinner style (Android)


You must create a new xml file. spinner.xml

spinner_pressed and spinner_normal are the images and they should end with .9.png

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item
           android:state_pressed="true"
           android:drawable="@drawable/spinner_pressed" />
     <item
           android:drawable="@drawable/spinner_normal" />
</selector>

After that put this where is declar Spinners

<Spinner
.....  
android:background="@drawable/spinner.xml"
.....
/> 

Image Button ( Android )

package com.test.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Test extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("TestImageButton);
    setContentView(R.layout.main);
  }
}

//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="wrap_content">

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ImageButton" />      

<ImageButton android:id="@+id/imagebutton"
  android:src="@drawable/icon"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>

</LinearLayout>

Tuesday, 14 August 2012

TextView and change text with button ( Android )

 package com.test.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Test extends Activity { 
     private TextView t1;
     private Button btn;


    @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        t1 = new TextView(this);
        t1 = (TextView)findViewById(R.id.text1);
        t1.setText(" Hello ");

       addBtnListener();

    }
    private void addBtnListener() {
        btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0){
                t1.setText(" My name is Android ");
            }
        });
    }
}

Monday, 13 August 2012

Seekbar (Android)

package com.test.seekbar;

import com.test.seekbar.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.preference.DialogPreference;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;

public final class SeekBarPreference extends DialogPreference implements OnSeekBarChangeListener {

    // Default values
    private static final int DEFAULT_CURRENT_VALUE = 50;
    private static final int DEFAULT_MIN_VALUE = 0;
    private static final int DEFAULT_MAX_VALUE = 100;

    private final int DefaultValue;
    private final int MaxValue;
    private final int MinValue;
   
    private int mCurrentValue;
   
    private SeekBar SeekBar;
    private TextView ValueText;

    public SeekBarPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    MinValue = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/com.mnm.seekbarpreference", "minValue", DEFAULT_MIN_VALUE);
    MaxValue = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/com.mnm.seekbarpreference", "maxValue", DEFAULT_MAX_VALUE);
    DefaultValue = attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android", "defaultValue", DEFAULT_CURRENT_VALUE);
    }

    @Override
    protected View onCreateDialogView() {
    mCurrentValue = getPersistedInt(DefaultValue);

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.dialog_slider, null);

    ((TextView) view.findViewById(R.id.min)).setText(Integer.toString(MinValue));
    ((TextView) view.findViewById(R.id.max)).setText(Integer.toString(MaxValue));

    SeekBar = (SeekBar) view.findViewById(R.id.seek_bar);
    SeekBar.setMax(MaxValue - MinValue);
    SeekBar.setProgress(mCurrentValue - MinValue);
    SeekBar.setOnSeekBarChangeListener(this);

    ValueText = (TextView) view.findViewById(R.id.current_value);
    ValueText.setText(Integer.toString(mCurrentValue));

    return view;
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

        if (!positiveResult) {
            return;
        }
   
        if (shouldPersist()) {
            persistInt(mCurrentValue);
        }

        notifyChanged();
    }

    @Override
    public CharSequence getSummary() {
        String summary = super.getSummary().toString();
        int value = getPersistedInt(DefaultValue);
        return String.format(summary, value);
    }
   
    public void onProgressChanged(SeekBar seek, int value, boolean fromTouch) {
        mCurrentValue = value + MinValue;
        ValueText.setText(Integer.toString(mCurrentValue));
    }

    public void onStartTrackingTouch(SeekBar seek) {
        //ValueText.setText(getString(R.string.seekbar_on));
    }

    public void onStopTrackingTouch(SeekBar seek) {
        // ValueText.setText(getString(R.string.seekbar_off)); 
}

class GPS (Android)

package test.gps;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;

class GpsMethods {

  private static LocationManager locManager; 
  static Location loc;

  void getLocation(Activity activity) {
      locManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
      locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationUpdateHandler());
  }
 
  static Location getLastKnownLocation() {
    String locationProvider = LocationManager.GPS_PROVIDER;
    Location lastLocation = locationManager.getLastKnownLocation(locationProvider);
    return lastLocation;
  }
 
  public class LocationUpdateHandler implements LocationListener {
      public void onLocationChanged(Location loc) {
          makeUseOfNewLocation(loc);
        }
        private void makeUseOfNewLocation(Location loc) {
          location = loc;
          locationManager.removeUpdates(this);
        }

        public void onStatusChanged(String provider, int status, Bundle extras){
            // code
        }
        public void onProviderEnabled(String provider) {
            // code
        }
        public void onProviderDisabled(String provider) {
            // code
        }
   
  }

}

Sunday, 12 August 2012

ScrollView .xml (Android)

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">
  <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="0">
    <TableRow>
      <View
        android:layout_height="80px"
        android:background="#000000"/>
      <TextView android:text="#000000"
        android:paddingLeft="4px"
        android:layout_gravity="center_vertical" />
    </TableRow>
    <TableRow>
      <View
        android:layout_height="80px"
        android:background="#440000" />
      <TextView android:text="#440000"
        android:paddingLeft="4px"
        android:layout_gravity="center_vertical" />
    </TableRow>
    <TableRow>
      <View
        android:layout_height="80px"
        android:background="#884400" />
      <TextView android:text="#884400"
        android:paddingLeft="4px"
        android:layout_gravity="center_vertical" />
    </TableRow>
    <TableRow>
      <View
        android:layout_height="80px"
        android:background="#aa8844" />
      <TextView android:text="#aa8844"
        android:paddingLeft="4px"
        android:layout_gravity="center_vertical" />
    </TableRow>
    <TableRow>
      <View
        android:layout_height="80px"
        android:background="#ffaa88" />
      <TextView android:text="#ffaa88"
        android:paddingLeft="4px"
        android:layout_gravity="center_vertical" />
    </TableRow>
    <TableRow>
      <View
        android:layout_height="80px"
        android:background="#ffffaa" />
      <TextView android:text="#ffffaa"
        android:paddingLeft="4px"
        android:layout_gravity="center_vertical" />
    </TableRow>
    <TableRow>
      <View
        android:layout_height="80px"
        android:background="#ffffff" />
      <TextView android:text="#ffffff"
        android:paddingLeft="4px"
        android:layout_gravity="center_vertical" />
    </TableRow>
  </TableLayout>
</ScrollView>