Wednesday, May 7, 2014

Android Spinners(Droplists)

It provides a easy way to select  value from  list. Clicking spinner will display a dropdownlist with the available values.Lets see this concept with an example, which is quite simple with very few lines of code.

Step 1:
- Add a spinner object to your layout as shown below.
              
            <Spinner
        android:id="@+id/spinnerstate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

--  Add a text view control that displays the selected value from spinner.

Step 2
        --  Declare a string array which is going to be the source for the spinner.


private String[] state = { "Select Photo Sizes","Passport Sizes","Auto Sizes","MRP Sizes" };


This source needs to be bound with the spinner through ArrayAdapter. We will be making use of the below constructor.where we need to pass the current context, id, the objects to represent in the ListView - as shown below.

ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, state);
  adapter_state

Step 3
         -- Set the layout resource to create the drop down views and bind it to spinner object
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinnerState.setAdapter(adapter_state);

          -- Set the spinner to listen to selected events.
                 spinnerState.setOnItemSelectedListener(this);
Step 4
   -- Once an item is selected, the below callback fucntion will get invoked and we need to perform user defined functions here. I have displayed the selected value.

activity_main.xml:
 
<?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"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/osversions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />

    <TextView
        android:id="@+id/selVersion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/osversions"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp" />

</RelativeLayout>


Main_activity.java:

package com.rabeen.spinnerr;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements OnItemSelectedListener {
 Spinner spinnerOsversions;
 TextView selVersion;
 private String[] state = { "Select Photo size", "Auto Size", "Auto Size", "MRP Size" };

  @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  System.out.println(state.length);
  selVersion = (TextView) findViewById(R.id.selVersion);
  spinnerOsversions = (Spinner) findViewById(R.id.osversions);
  ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, state);
  adapter_state
    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinnerOsversions.setAdapter(adapter_state);
  spinnerOsversions.setOnItemSelectedListener(this);

  }

  public void onItemSelected(AdapterView<?> parent, View view, int position,
   long id) {
  spinnerOsversions.setSelection(position);
  String selState = (String) spinnerOsversions.getSelectedItem();
  selVersion.setText("Selected Android OS:" + selState);
 }

  @Override
 public void onNothingSelected(AdapterView<?> arg0) {
  // TODO Auto-generated method stub

  }



Button




<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/baackground"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@android:color/black"
        android:textColor="@android:color/white"
         
        android:text="Select Photo sizes" />

</RelativeLayout>







in activity.xml use the following code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/baackground"
  
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        style="@style/Custom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:text="Button" />
    
    
</RelativeLayout>

now make a folder-drawable in res folder where we create a selector.xml file and following code is used...

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"  >

    <solid android:color="#f00"/>
    <size android:width="55dip"
        android:height="55dip"/>
</shape>


now,finally inside the values folder where we can find the styles.xml file,here we code our program like:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    <style name="Custom" parent="@android:style/Widget.Button">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
       <item name="android:background">@drawable/selector</item>
    </style>

</resources>

As we look above i have called the selector in item name of styles.xml..


Tuesday, May 6, 2014

Tab

Start a new Android Application, named AndrodTab extends TabActivity and modify the xml file as below.


To use TabWidget, a TabHost have to be used to contain the entire layout of the Activity. A TabHost requires two descendant elements: a TabWidget and a FrameLayout. In order to properly layout these elements, we've put them inside a vertical LinearLayout. Otherwise, the TabWidget and FrameLayout will overlay each other. The FrameLayout is where we keep the content that will change with each tab. Each child in the FrameLayout will be associated with a different tab.

Modify the main.xml :

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >
    <LinearLayout
        android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget 
   android:id="@android:id/tabs"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>
<FrameLayout
   android:id="@android:id/tabcontent"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabview1">
 <TextView 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="tabview1"/>
    <Button 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="just try it"/>
    
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/tabview2">
  <TextView 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="it's here"/>
  <Button 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="A"/>
  <Button 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="B"/>
  <Button 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="C"/>
  
  
</LinearLayout>
<TextView
    android:id="@+id/tabview3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="upcomming"/>
</FrameLayout>
</LinearLayout>
</TabHost>


Finally, it is needed to modify the method onCreate() in AndroidTab.java. Now modify your java like this:

package com.rabeen.tabactivity;

import android.app.TabActivity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TabHost;

@SuppressWarnings("deprecation")
public class MainActivity extends TabActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost mTabHost =getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1")
.setIndicator("TAB 1")
.setContent(R.id.tabview1));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2")
.setIndicator("TAB 2")
.setContent(R.id.tabview2));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3")
.setIndicator("TAB 3")
.setContent(R.id.tabview3));
mTabHost.setCurrentTab(0);
}




Monday, May 5, 2014

Working with camera

work with manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rabeen.camera"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<uses-permisson android:name="android.permisson.CAMERA"/>
<uses-permisson android:name="android.permisson.WRITE_EXTERNAL_STORAGE"/>
 <uses-permisson android:name="android.permisson.RECORD_AUDIO"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.rabeen.camera.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>

Now deal with activity_main.xml which appear as:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button 
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:layout_marginTop="24dp"
      android:text="@string/take"/>
    
    
    <ImageView 
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="115dp"
        android:contentDescription="@string/TODO"
        android:src="@drawable/ic_launcher" />
    


</RelativeLayout>

In MainActivity.java code appear as:

package com.rabeen.camera;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

Button b;
ImageView iv;
Bitmap bm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView1);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 0);

}
});


}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap) data.getExtras().get("data");

iv.setImageBitmap(bitmap);

This is the end of code for camera..