android app

Post on 21-Dec-2015

49 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

DESCRIPTION

Android App Development

TRANSCRIPT

www.chandanverma.com 1

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

3 www.chandanverma.com

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

• 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

• 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

• 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

8 www.chandanverma.com

• 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

• 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

• 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

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

www.chandanverma.com 13

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"

BACKGROUND COLOR:

android:background="#aabbcc“

BACKGROUND IMAGE:

SAVE IMAGE(.PNG) INTO DRAWABLE FOLODER

android:background="@drawable/images"

www.chandanverma.com 15

• 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

• findViewById();

• onClick(View v);

• Gettext();

• Settext();

• OnClickListener();

• setOnClickListener();

• Toast.makeText

• Toast.LENGTH_LONG

• show()

• getApplicationContext(),

www.chandanverma.com 17

www.chandanverma.com 18

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

• 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

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

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

ImageView i;

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

For showing the Image

i.setImageResource(R.drawable.ic_launcher);

www.chandanverma.com 23

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

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

Stringbuffer

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

String result = new

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

www.chandanverma.com 26

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

www.chandanverma.com 28

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

• 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)

www.chandanverma.com 31

<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

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

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

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

www.chandanverma.com 36

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

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

• 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

• android:checked="false

• android:text="@string/b"

www.chandanverma.com 40

c.setOnClickListener(this);

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

• 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

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

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

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

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

if (checkBox1.isChecked())

{

checkBox1.setChecked(false);

} if (checkBox2.isChecked())

{

checkBox2.setChecked(false);

}

www.chandanverma.com 47

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

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

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

www.chandanverma.com 51

www.chandanverma.com 52

Starting activities

startActivity()

Example:

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

Intent.ACTION_VIEW

Uri.parse

www.chandanverma.com 53

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

• 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

www.chandanverma.com 56

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

www.chandanverma.com 58

www.chandanverma.com 59

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

www.chandanverma.com 61

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

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

Button b;

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

b.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

finish();

}

});

www.chandanverma.com 64

www.chandanverma.com 65

top related