android app

65
www.chandanverma.com 1

Upload: ishanmourya5

Post on 21-Dec-2015

36 views

Category:

Documents


4 download

DESCRIPTION

Android App Development

TRANSCRIPT

Page 1: Android App

www.chandanverma.com 1

Page 2: Android App

2

• Activities – visual user interface focused on a

single thing a user can do

• Services – no visual interface – they run in the

background

• Broadcast Receivers – receive and react to

broadcast announcements

• Content Providers – allow data exchange

between applications

www.chandanverma.com

Page 3: Android App

3 www.chandanverma.com

Page 4: Android App

4

Java code for our activity All source code here

Generated Java code

Helps link resources to

Java code

Layout of the activity

Strings used in the

program

All non-code

resources

Android Manifest

Images

www.chandanverma.com

Page 5: Android App

• src – your source code

• gen – auto-generated code (usually just R.java)

• Included libraries

• Resources

– Drawables (like .png images)

– Layouts

– Values (like strings)

• Manifest file

5 www.chandanverma.com

Page 6: Android App

• Used to define some of the resources

– Layouts (UI)

– Strings

• Manifest file

• Shouldn’t usuall have to edit it directl , Eclipse can do that for you

• Preferred way of creating UIs

– Separates the description of the layout from any actual code that controls it

– Can easily take a UI from one platform to another

6 www.chandanverma.com

Page 7: Android App

• Auto-generated: ou shouldn’t edit it • Contains IDs of the project resources

• Enforces good software engineering

• Use findViewById and Resources object to get

access to the resources

– Ex. Button b = (Button)findViewById(R.id.button1)

– Ex. getResources().getString(R.string.hello));

7 www.chandanverma.com

Page 8: Android App

8 www.chandanverma.com

Page 9: Android App

• A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing

www.chandanverma.com 9

Page 10: Android App

• text: defines the text that would be displayed on the screen.

• textStyle: sets the style of the text.

• textSize: defines the size of the text.

• textColor: sets the color of the text.

• background: sets the background color.

• autoLink: identifies the links into the text and converts them into clickable ones. The available choices are web, email, phone.

www.chandanverma.com 10

Page 11: Android App

• android:text="@stri g/* a e“ • android:textSize="30sp“ • android:textAppearance="?android:attr/t

extAppearanceLarge“ • android:id="@+id/te t2“

• android:fontFamily="Arial“ • android:textStyle="bold“ • android:textColor="#aarrggbb“

• android:background="#aarrggbb"

www.chandanverma.com 11

Page 12: Android App

SP/DP

• px is one pixel.

• sp is scale-independent pixels.

• dp is Density-independent pixels.

• sp for font sizes

• dp for everything else.

www.chandanverma.com 12

Page 13: Android App

www.chandanverma.com 13

Page 14: Android App

www.chandanverma.com 14

You have to create the Color.xml file in the res/value folder of your project. The

code of Color.xml is

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

<color name="white">#FFFFFF</color>

<color name="yellow">#FFFF00</color>

<color name="fuchsia">#FF00FF</color>

<color name="red">#FF0000</color>

<color name="silver">#C0C0C0</color>

<color name="gray">#808080</color>

<color name="olive">#808000</color>

<color name="purple">#800080</color>

<color name="maroon">#800000</color>

<color name="aqua">#00FFFF</color>

<color name="lime">#00FF00</color>

<color name="teal">#008080</color>

<color name="green">#008000</color>

<color name="blue">#0000FF</color>

<color name="navy">#000080</color>

<color name="black">#000000</color>

android:textColor="@color/red"

Page 15: Android App

BACKGROUND COLOR:

android:background="#aabbcc“

BACKGROUND IMAGE:

SAVE IMAGE(.PNG) INTO DRAWABLE FOLODER

android:background="@drawable/images"

www.chandanverma.com 15

Page 16: Android App

• android:layout_marginBottom= ..dp

• android:layout_marginLeft= ..dp

• android:layout_gravity= ..center

• android:gravity= ..center

• android:layout_marginTop="dp“ • android:layout_width=“dp"

• android:layout_height="dp"

www.chandanverma.com 16

Page 17: Android App

• findViewById();

• onClick(View v);

• Gettext();

• Settext();

• OnClickListener();

• setOnClickListener();

• Toast.makeText

• Toast.LENGTH_LONG

• show()

• getApplicationContext(),

www.chandanverma.com 17

Page 18: Android App

www.chandanverma.com 18

Page 19: Android App

Change button name/size/color:

android:text="@stri g/a“ android:textSize="30sp"

android:textColor="#aabbcc“ android:textStyle="bold“ android:background=“#aabbcc“ android:layout_width="100dp"

www.chandanverma.com 19

Page 20: Android App

• In Android, Toast is a notification message that

pop up, display a certain amount of time, and

automatically fades in and out.

DISPLAY THE MESSAGE: //display in short period of time

Toast.makeText(getApplicationContext(), "msg msg",

Toast.LENGTH_SHORT).show();

//display in long period of time

Toast.makeText(getApplicationContext(), "msg msg",

Toast.LENGTH_LONG).show();

www.chandanverma.com 20

Page 21: Android App

BUTTON EVENT

Button bt.

bt=(Button)findViewById(R.id.button1);

bt.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Toast.makeText(getApplicationContext(),

"hi dear", Toast.LENGTH_LONG).show();

}

});

www.chandanverma.com 21

Page 22: Android App

Button bt;

TextView t;

bt=(Button)findViewById(R.id.button1);

t=(TextView)findViewById(R.id.textView1);

bt.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

t.setText “ our string" ;

}

www.chandanverma.com 22

Page 23: Android App

ImageView i;

i=(ImageView)findViewById(R.id.imageView1);

For showing the Image

i.setImageResource(R.drawable.ic_launcher);

www.chandanverma.com 23

Page 24: Android App

Retrieving data from an EditText

getText()

EXAMPLE:

EditText etName = (EditText) findViewById(R.id.editText1);

strName = etName.getText().toString();.

android:layout_weight="1"

android:maxLength="5"

android:inputType="number"

www.chandanverma.com 24

Page 25: Android App

b=(Button)findViewById(R.id.button1);

t=(EditText)findViewById(R.id.editText1);

b.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

String mystr=t.getText().toString(); Toast msg=Toast.makeText(getBaseContext(),mystr,Toast.LENGTH_LONG);

msg.show();

}

www.chandanverma.com 25

Page 26: Android App

Stringbuffer

String mystr=t.getText().toString();

String result = new

StringBuffer(mystr).reverse().toString();

www.chandanverma.com 26

Page 27: Android App

FOR PLUS:

public void onClick(View arg0) {

try{

int x=Integer.parseInt(a.getText().toString());

int y=Integer.parseInt(b.getText().toString());

int sum=x+y;

shw.setText(""+sum);

// TODO Auto-generated method stub

}

catch(Exception e)

{

}

}

});

www.chandanverma.com 27

Page 28: Android App

www.chandanverma.com 28

Page 29: Android App

Toggle Button on and off

Android ToggleButton is two state button same as RadioButton and

CheckBox. And these two states are on and Off . When ToggleButton

is on then it shows a green bar and when it is in off state it shows a grey

bar. this is default behavior.

www.chandanverma.com 29

Page 30: Android App

• android:layout_width="match_parent

• android:layout_height="wrap_content

• android:text="ToggleButton

• android:textOff="Toggele button off

• android:textOn="Toggle button is on

• setChecked(true)

• setChecked(false)

• isChecked()

www.chandanverma.com 30

Toggle Button( XML)

Page 31: Android App

www.chandanverma.com 31

Page 32: Android App

<ToggleButton

android:id="@+id/toggleButton1"

android:layout_width="127dp"

android:layout_height="wrap_content"

android:textOn="yes"

android:textOff="no"

android:checked="false"/>

www.chandanverma.com 32

Page 33: Android App

ToggleButton t;

Button b;

TextView et;

t=(ToggleButton) findViewById(R.id.toggleButton1);

b=(Button) findViewById(R.id.button1);

et=(TextView) findViewById(R.id.textView1);

www.chandanverma.com 33

Page 34: Android App

b.setOnClickListener( new OnClickListener() {

public void onClick(View arg0) {

if(t.isChecked())

{

et.setText("toggle button is pressed");

t.setChecked(true);

}

else

{

et.setText("toggle button is off");

t.setChecked(false);

}

}

});

www.chandanverma.com 34

Page 35: Android App

You often use radio buttons when a user

should be allowed to only select one item

from a small group of items.

android:checked= true

android:checked="false"

getCheckedRadioButtonId();

www.chandanverma.com 35

Page 36: Android App

www.chandanverma.com 36

Page 37: Android App

1.Link with XML RadioGroup rg;

Button b;

rg=(RadioGroup)findViewById(R.id.radioGroup1);

b=(Button)findViewById(R.id.button1);

2.Attached Click listener to Button

b.setOnClickListener(this);

3. Implements OnClickListener

public class Radio1Activity extends Activity implements OnClickListener{

**ADD UNIMPLEMENTED METHOD

www.chandanverma.com 37

Page 38: Android App

public void onClick(View v) {

if( v==b)

{

RadioButton selectRadio = (RadioButton)

findViewById(rg.getCheckedRadioButtonId());

String opinion = selectRadio.getText().toString();

Toast.makeText(this, "Your Opinion is : " +

opinion,Toast.LENGTH_LONG).show();

}

www.chandanverma.com 38

Page 39: Android App

• One of the easiest and most common ways to

accept user input in an Android Application is

the Checkbox component.

• Implement view.onclickListener

• isChecked Method

Determine if the box is checked using

isChecked() method

www.chandanverma.com 39

Page 40: Android App

• android:checked="false

• android:text="@string/b"

www.chandanverma.com 40

c.setOnClickListener(this);

Page 41: Android App

public class CheboxActivity extends Activity {

/** Called when the activity is first created. */

CheckBox c;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

c=(CheckBox) findViewById(R.id.checkBox1);

c.setOnClickListener(this);

}

www.chandanverma.com 41

Page 42: Android App

• Implement the OnClickListener interface for

the activity class .

• Call the setOnClickListener() object by passing

the context as a paramter to it .

• Override the onClick(View v) method inside

the activity class .

www.chandanverma.com 42

Page 43: Android App

public class CheboxActivity extends Activity

implements OnClickListener {

/** Called when the activity is first created. */

CheckBox c;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

c=(CheckBox) findViewById(R.id.checkBox1);

c.setOnClickListener(this);

}

}

**ADD UNIMPLEMENTED METHOD

www.chandanverma.com 43

Page 44: Android App

public void onClick(View v) {

// TODO Auto-generated method stub

}

This is a method which is get called whether

Checkbox Is checked or not.

www.chandanverma.com 44

(View v) is checkbox object

Page 45: Android App

public void onClick(View v) {

CheckBox t=(CheckBox) v;

if(t.isChecked())

{

Toast.makeText(getApplicationContext , your msg" , Toast.LENGTH_LONG).show();

}

else

{

Toast.makeText(getApplicationContext , your msg" , Toast.LENGTH_LONG).show();

}

}

www.chandanverma.com 45

Page 46: Android App

public void onClick(View v)

{

switch(v.getId())

{

case R.id.checkbox1:

break;

case R.id.cbox2:

break;

............and so on }

}

www.chandanverma.com 46

Page 47: Android App

if (checkBox1.isChecked())

{

checkBox1.setChecked(false);

} if (checkBox2.isChecked())

{

checkBox2.setChecked(false);

}

www.chandanverma.com 47

Page 48: Android App

public void onClick(View v) {

switch(v.getId())

{

case R.id.checkBox1:

c2.setChecked(false);

c3.setChecked(false);

c4.setChecked(false);

Toast.makeText(this, "yes", Toast.LENGTH_LONG).show();

break;

So on.....................

www.chandanverma.com 48

Page 49: Android App

//String edit="";

Button btn;

EditText ed;

ToggleButton toggle;

btn = (Button)findViewById(R.id.button1);

ed = (EditText)findViewById(R.id.editText1);

toggle =

(ToggleButton)findViewById(R.id.toggleButton1);

//edits = ed.getText().toString();

www.chandanverma.com 49

Toggle Button(eg)

Page 50: Android App

btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

edit = ed.getText().toString();

if(edit.equals("1")){

toggle.setTextOff("TOGGLE ON");

toggle.setChecked(true);

}

else if(edit.equals("0")){

toggle.setTextOn("TOGGLE OFF");

toggle.setChecked(false);

}

}

});

www.chandanverma.com 50

Page 51: Android App

www.chandanverma.com 51

Page 52: Android App

www.chandanverma.com 52

Page 53: Android App

Starting activities

startActivity()

Example:

Intent i = new Intent(ac1,ac2); startActivity(i);

Intent.ACTION_VIEW

Uri.parse

www.chandanverma.com 53

Page 54: Android App

Intent can be used to perform following 3 tasks

• Open another Activity or Service from the

current Activity

• Pass data between Activities and Services

• Delegate responsibility to another application.

For example, you can use Intents to open the

browser application to display a URL.

www.chandanverma.com 54

Page 55: Android App

• When you know which component you want

to launch .

• When you open an activity from another

activity in the same Android app, you use

Explicit Intents.

www.chandanverma.com 55

Page 56: Android App

www.chandanverma.com 56

Page 57: Android App

you do not know which component should be

launched. Or if you want to give the user an

option to choose between a list of

components to use.

www.chandanverma.com 57

Page 58: Android App

www.chandanverma.com 58

Page 59: Android App

www.chandanverma.com 59

Page 60: Android App

Button go,fa,gm;

addListenerOnButton();//method

}

public void addListenerOnButton() {

gm=(Button)findViewById(R.id.button1);

go=(Button)findViewById(R.id.button2);

fa=(Button)findViewById(R.id.button3);

gm.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) { Intent bw=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.gmai.com"));

startActivity(bw);// TODO Auto-generated method stub

}

});

www.chandanverma.com 60

Page 61: Android App

www.chandanverma.com 61

Page 62: Android App

1.TWO ACTIVITY FILE

FirstActivit

secondActivity

2.TWO XML FILE

MAIN.XML

SECOND.XML

setContentView(R.layout.second);//CHANGE LAYOUT

3.Entry new activity into androidmainfest.xml

<activity android:name="secondActivity">

</activity>

www.chandanverma.com 62

Page 63: Android App

Button btn;

btn=(Button)findViewById(R.id.button1);

btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) { Intent i=new Intent(firstActivity.this,secondActivity.class);

startActivity(i);

}

});

www.chandanverma.com 63

Page 64: Android App

Button b;

b=(Button)findViewById(R.id.button1);

b.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

finish();

}

});

www.chandanverma.com 64

Page 65: Android App

www.chandanverma.com 65