Thursday, March 6, 2014

Background image in Android


In android activity we can give the background image. using the android:background="@drawable/imagename" we can give the image of particular activity.


Steps :-
step1:- crete the new android project.
step2:- copy the image.
step3:- paste it into res/drawable-hdpi folder.
DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD step4:- open the main_activity.xml file and write the code        android:background="@drawable/yourimagename".
   Note:-give the image name without extension.


following some screen shots helps you .

Before copy of the image in you project your project structure is:



Copy image in your drive
paste your image into drawable-hdpi folder
 
After pase image file in drawable-hdpi folder your project structure shoud be like this


 in main_activity.xml file coding image

Coding:-
MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


2)activity_main.xml
<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@drawable/background"
    tools:context=".MainActivity" >

</RelativeLayout>




OUTPUT


 

Tuesday, February 25, 2014

Intent in Android

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities.
Simply and activity means if you press on particular button the another activity will be open.

Types of Android Activities/Intent.
  • Explicit Intent
  • Implicit Intent
Explicit Intent: After writing a single Activity, There comes a need   to call another activity to perform another task . For this we need the Explit Intent.

   Android provide android.content package in that we have to use Intent class. import the that class. as like import android.content.Intent;  
                                                       1)MainActivity.
   
                                                 2)AnotherActivity


Steps to implement Explicit Intent.

Step 1:- create New android project.

Step 2: Take one button in activity_main.xml file .
Step 3: set onClickListner  on them.
Step 4: create Intent class object and pass two argument of that class constructor that is (getApplicationContext(), AnotherActivity.class) 
1) first parametr is getApplicationContext() it will call the system resources
2)second parameter is your another activity class file name.

Step 5:How to create new activity in you project.
  •    Click on your project and press ctrl+n . or click on the prject then new->other->AndroidActivity.
        
  •    Click Next Button then give the of your own name for the your activity.

         Then automatically you .java and .xml file will be created.You can chek.
 

Following are the coding file of this Application.

MainActivity.java
package com.example.explicitintentdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    Button b;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        b=(Button)findViewById(R.id.button_call_another_activity);
        b.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            Intent intent=new Intent(getApplicationContext(),AnotherActivity.class);
            startActivity(intent);
               
            }
        });
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}


AnotherActivity.java
package com.example.explicitintentdemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class AnotherActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.another, menu);
        return true;
    }

}

activity_main.xml

 <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="#ff00ff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button_call_another_activity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="41dp"
        android:text="@string/call_another_activity" />

</RelativeLayout>
 
activity_another.xml

<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="#fff000"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".AnotherActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="62dp"
        android:text="This is Another Activity"
        android:textSize="30sp" />

</RelativeLayout>
String.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">ExplicitIntentDemo</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="call_another_activity">Call Another Activity</string>
    <string name="title_activity_another">AnotherActivity</string>

</resources>


AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.explicitintentdemo.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>
        <activity
            android:name="com.example.explicitintentdemo.AnotherActivity"
            android:label="@string/title_activity_another" >
        </activity>
    </application>

</manifest>

Output:-
                                                         Our Application Output will be.
                                                  1)MainActivity
                                        if you click on call Another Activity then AnotherActiviy will be call.

Saturday, February 8, 2014

working with Button

User Interface in Android. 
     After successfully running the your hellow world example we turn to some UI (User Interface)Concept.
in Android.

     The graphical user interface for an Android app is built using a hierarchy of View and ViewGroup objects. View objects are usually UI widgets such as buttons or text fields and ViewGroup objects are invisible view containers that define how the child views are laid out, such as in a grid or a vertical list.
Android provides an XML vocabulary that corresponds to the subclasses of View and ViewGroup so you can define your UI in XML using a hierarchy of UI elements.

     Their are various User Interface are aviable thar are as follows:
  1. TextView.
  2. Button.
  3. ToggleButton.
  4. CheckBox.
  5. RadioButton.
  6. Spinner.
  7. ProgressBar.
  8. SeekBar.
  9. ImageView.
  10. DatePicker & TimePicker. 
Our Output Will be Following:-


working with Button  
 

Step 1:- First you need to create the new  Android Applicaiton Project.
 How to create it goto File->New->AndroidApplicationProject. One window will be open in that fill the requird information such as, Application Name, Project Name, Package Name etc.
  

    




















step2 :- follow the process till Finish Window will not come.
























After clicking the finish button. it will create the folder structure. and font of the window show the activity_main.xml and MainActivity.java file's.

















Step 3:-delete the hello world content in activity_main.xml file.
            on the left hand site of the screen Paletee ber select the  FormWidgest tab and take button from that.
 
















Step 4:- On Button you see the one triange in yellow color that is warning how to solve that warning.
              Goto res->values->string.xml(click on it) . Android Resources(Default) window will open on the     screen in that click on string  ->Add  




















Step 5:- In activiy_main.xml just click on button . and right bottom corner Propery window is there in that click on Button then one window will be disple in that choose the name wich you register in string.xml file ->ok.













after completing the this process you warning will remove from button.




















Step  6:- we have to perform the action on the button. that's why we need to write code in java file just open MainActivity.java file following is code of the java file.

MainActivity.java       

package com.baba.workingwithbutton;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //getting the reference of button.
        Button button=(Button)findViewById(R.id.button1);
       
        //set the event handeling on button
        button.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View arg0) {
                //when you click on the button then tost will be execuated
                Toast.makeText(getApplicationContext(), "Congratulation ! Your Button is working ",Toast.LENGTH_LONG).show();
               
            }
        });
       
       
    }
}


2)activity_main.xml

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    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="76dp"
        android:text="@string/button" />

</RelativeLayout>






















 Output
                                                                          






 
 



Wednesday, February 5, 2014

Creating the Android First Application.

After completely installation of android adt bundle in you machion. Then you can start the building Application. how to create first Application ? follow the following steps to create Your first Application.

Step 1:- Goto   File->New-> Android Project.















         
Step 2:-Clicking on Android Application Project  one window will be display.
             1)In first textbox you have to provide you Applcation name.
                (Note:- You Project Name first Word First Letter should be capital)
             2)In Third window you can make you package hierarchy.
             3)Click on Next Button.

















Step 3:- After clicking next button one window will be open. follow the instruction and click on Next button.
















Step 4:- You can change you application launcher image on this window.(if you want to change it then you can change other wise it will take bydefault android symbol) Click on Next Button.
















Step 5:-Just Click on Next Button.


















Step 6:- Click Next Button.


















Step 7:- Click on Finish Button.

Step 8:- After creating your project on main window left hand side you can see the your project name click on that after clicking you see the 8 folder under that.


 













Step 9:- In Src folder your package name and .java files are stored.

















Step 10:- In bin folder AndroidManifeast.xml file is avilable. in that file we have to give some system permissons.(But till now we dont want to take any permissions so dont thik much about this of this stage )

















Step 11:- in res folder we have also some sub folder  that is
               1) drawable-hdpi :- all the drawable folder we store the image.
               2)layout :- in layout folder all .xml files are sotred (Or all activities).
               3)values :- in values folder string.xml , menu.xml , style.xml files are aviable.





















How to Run You Application:-
Step 12:- Right click on you project ->Run As -> Android Application
               (or just press ctrl +F11)


















Step 13:- Just wait for few minute you application is loading /installing on you emulator.



























Your first program output:-
Follow the Above Procedure and run you Android first Application .

Tuesday, February 4, 2014

How To install adt-bundle on you computer


For the Android development you need to install adt-bundle on you computer machion. This is eclipse tool.

Pre Software Rquired:-
1) JDK (Java Development Kit 1.5 onwares any Version ):-
    For dowanloading java got to http://www.oracle.com/technetwork/java/javase/downloads/index.html

2)ADT Bundle:-
    For dowanloading adt bundle to to http://developer.android.com/sdk/index.html


Steps to Install adt-bundle:-
Step 1:- Unzip jdk and install on you computer.

Step 2:- Unzip adt bunde zip file. in that you find the sdk  form adt bunlde folder.

Step 3:-  Open eclipse folder in that you find the eclipse logo (that is Application file) just click on that.

Step 4:- Choose the you workspace (it means where you want to store you project files).

Creating AVD manager / Emulator:-

1. In eclipse go to window menu run android virtual device manager and choose new option and give the details device name , device , version means in which version you want to create AVD and click ok button.

Follw the above procedure you will get successfully install adt bundle on you machion.