0% found this document useful (0 votes)
113 views13 pages

Android Day 2 PDF

The document describes examples of basic Android app development concepts including: 1. Creating a simple app with a button click that displays a toast notification. 2. Using intents to move between activities by starting a new activity on button click. 3. Passing data between activities using intents by adding extras to the intent. 4. Demonstrating a relative layout with text views positioned above and centered in the parent layout.

Uploaded by

akash saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views13 pages

Android Day 2 PDF

The document describes examples of basic Android app development concepts including: 1. Creating a simple app with a button click that displays a toast notification. 2. Using intents to move between activities by starting a new activity on button click. 3. Passing data between activities using intents by adding extras to the intent. 4. Demonstrating a relative layout with text views positioned above and centered in the parent layout.

Uploaded by

akash saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.

​First Application with button click and toast

public class ​MainActivity ​extends ​AppCompatActivity {

TextView ​tv_text​;
Button ​bt_submit​;
​@Override
​protected void ​onCreate(Bundle savedInstanceState) {
​super​.onCreate(savedInstanceState);
setContentView(R.layout.​activity_main​);

​tv_text ​= (TextView) findViewById(R.id.​tv_text)​ ;


​bt_submit ​= (Button) findViewById(R.id.​bt_submit​);

​bt_submit​.setOnClickListener(​new ​View.OnClickListener() {
​@Override
​public void ​onClick(View v) {
Toast.​makeText​(MainActivity.​this​,​"clicked"​,Toast.​LENGTH_SHORT​).show();
}
});
}
}

activity_main.xml

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


<​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=​"300dp"
​tools​:context=​".MainActivity"​>
<​TextView
​android​:id=​"@+id/tv_text"
​android​:layout_width=​"300dp"
​android​:layout_height=​"40dp"
​android​:gravity=​"center"
​android​:text=​"@string/app_name"
​android​:layout_centerInParent=​"true"
​/>
<​Button
​android​:id=​"@+id/bt_submit"
​android​:layout_width=​"200dp"
​android​:layout_height=​"40dp"
​android​:text=​"submit"
​android​:layout_alignParentBottom=​"true"
​android​:layout_centerHorizontal=​"true"

​/>
</​RelativeLayout​>
2.Move from one Activity to another activity using intents

public class ​MainActivity ​extends ​AppCompatActivity {

TextView ​tv_text​;
Button ​bt_submit​;

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

​tv_text ​= (TextView) findViewById(R.id.​tv_text)​ ;


​bt_submit ​= (Button) findViewById(R.id.​bt_submit​);

​bt_submit​.setOnClickListener(​new ​View.OnClickListener() {
​@Override
​public void ​onClick(View v) {
Intent intent = ​new ​Intent(MainActivity.​this​,Main2Activity.​class​);
startActivity(intent);
}
});
}
}

activity_main.xml

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


<​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=​"300dp"
​tools​:context=​".MainActivity"​>
<​TextView
​android​:id=​"@+id/tv_text"
​android​:layout_width=​"300dp"
​android​:layout_height=​"40dp"
​android​:gravity=​"center"
​android​:text=​"@string/app_name"
​android​:layout_centerInParent=​"true"
​/>
<​Button
​android​:id=​"@+id/bt_submit"
​android​:layout_width=​"200dp"
​android​:layout_height=​"40dp"
​android​:text=​"submit"
​android​:layout_alignParentBottom=​"true"
​android​:layout_centerHorizontal=​"true"

​/>
</​RelativeLayout​>

Second Activity

public class ​Main2Activity ​extends ​AppCompatActivity {

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

activity_main2.xml

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


<​RelativeLayout ​xmlns:​android​=​"http://schemas.android.com/apk/res/android"
​xmlns:​app​=​"http://schemas.android.com/apk/res-auto"
​xmlns:​tools​=​"http://schemas.android.com/tools"
​android​:layout_width=​"match_parent"
​android​:layout_height=​"match_parent"
​tools​:context=​".Main2Activity"​>

<​TextView
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content"
​android​:layout_centerInParent=​"true"
​android​:textColor=​"#000000"
​android​:textSize=​"25sp"
​android​:text=​"second screen"​/>

</​RelativeLayout​>
3.Intents with data transfer from one activity to another activity

public class ​MainActivity ​extends ​AppCompatActivity {

EditText et_name,et_password;
Button bt_submit;
​@Override
​protected void ​onCreate(Bundle savedInstanceState) {
​super​.onCreate(savedInstanceState);
setContentView(R.layout.​activity_main​);

et_name = (EditText) findViewById(R.id.​et_name​);


et_password = (EditText) findViewById(R.id.​et_password​);
bt_submit = (Button) findViewById(R.id.​bt_submit​);
bt_submit.setOnClickListener(​new ​View.OnClickListener() {
​@Override
​public void ​onClick(View view) {
Intent intent = ​new ​Intent(MainActivity.​this​,SecondActivity.​class​);
intent.putExtra(​"name"​,et_name.getText().toString());
intent.putExtra(​"pwd"​,et_password.getText().toString());
startActivity(intent);
}
});

}
}

activity_main.xml

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


<​RelativeLayout x ​ mlns:​android= ​ ​"http://schemas.android.com/apk/res/android"
​xmlns:a ​ pp​=​"http://schemas.android.com/apk/res-auto"
​xmlns:t​ ools= ​ "​ http://schemas.android.com/tools"
​android​:layout_width=​"match_parent"
​android​:layout_height="​ match_parent"
​tools:​ context="​ .MainActivity"> ​
​ inearLayout
<L
​android:​ layout_width="​ match_parent"
​android:​ layout_height="​ wrap_content"
​android:​ layout_centerInParent=​"true"
​android:​ gravity=​"center"
​android:​ orientation=​"vertical"> ​
<​EditText
a ​ ndroid:​ id=​"@+id/et_name"
​ ndroid​:layout_width=​"match_parent"
a
a ​ ndroid:​ layout_marginLeft=​"40dp"
a​ ndroid​:layout_marginRight="​ 40dp"
a ​ ndroid:​ layout_height=​"wrap_content"
a ​ ndroid​:hint=​"Name"/​ >
<​EditText
a ​ ndroid:​ id=​"@+id/et_password"
​ ndroid​:layout_width=​"match_parent"
a
a ​ ndroid:​ layout_marginLeft=​"40dp"
a​ ndroid​:layout_marginRight="​ 40dp"
a ​ ndroid:​ inputType=​"textPassword"
a ​ ndroid​:layout_height=​"wrap_content"
a ​ ndroid:​ hint="​ Password"​/>

<​Button
a ​ ndroid​:id=​"@+id/bt_submit"
a ​ ndroid:​ layout_width=​"wrap_content"
a ​ ndroid​:layout_height=​"wrap_content"
a ​ ndroid:​ text="​ submit"
/​ >

</​LinearLayout​>
​ elativeLayout>
</R ​

Second activity

public class ​SecondActivity ​extends ​AppCompatActivity {

TextView tv_name,tv_password;
Bundle extras;
​@Override
​protected void ​onCreate(Bundle savedInstanceState) {
​super​.onCreate(savedInstanceState);
setContentView(R.layout.​activity_second)​ ;

tv_name = (TextView) findViewById(R.id.​tv_name)​ ;


tv_password = (TextView) findViewById(R.id.​tv_password​);

​if​(getIntent().getExtras()!=​null​){
extras = getIntent().getExtras();

String name = extras.getString(​"name"​);


String password = extras.getString(​"pwd"​);

tv_name.setText(name);
tv_password.setText(password);

}
}
}

activity_second.xml
<?​xml version=​"1.0" ​encoding=​"utf-8"​?>
<​RelativeLayout ​xmlns:​android​=​"http://schemas.android.com/apk/res/android"
​xmlns:​app​=​"http://schemas.android.com/apk/res-auto"
​xmlns:​tools​=​"http://schemas.android.com/tools"
​android​:layout_width=​"match_parent"
​android​:layout_height=​"match_parent"
​tools​:context=​".SecondActivity"​>

<​LinearLayout
​android​:layout_width=​"match_parent"
​android​:layout_height=​"match_parent"
​android​:layout_centerInParent=​"true"
​android​:gravity=​"center"
​android​:orientation=​"vertical"​>

<​TextView
​android​:id=​"@+id/tv_name"
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content" ​/>

<​TextView
​android​:id=​"@+id/tv_password"
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content" ​/>

</​LinearLayout​>

</​RelativeLayout​>

4.Relativelayout example

public class ​MainActivity ​extends ​AppCompatActivity {

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

activity_main.xml

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


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

​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content"
​android​:text=​"Hello World!"
​android​:layout_above=​"@+id/main"
​android​:layout_centerInParent=​"true"
​/>

<​TextView
​android​:id=​"@+id/main"
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content"
​android​:layout_centerInParent=​"true"
​android​:text=​"@string/text_second"​/>

<​TextView
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content"
​android​:layout_below=​"@+id/main"
​android​:layout_centerInParent=​"true"
​android​:text=​"@string/text_second"​/>

</​RelativeLayout​>

5.LinearLayout Example

public class ​MainActivity ​extends ​AppCompatActivity {

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

activity_main.xml

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


<​LinearLayout ​xmlns:​android​=​"http://schemas.android.com/apk/res/android"
​xmlns:​app​=​"http://schemas.android.com/apk/res-auto"
​xmlns:​tools​=​"http://schemas.android.com/tools"
​android​:layout_width=​"match_parent"
​android​:layout_height=​"wrap_content"
​android​:orientation=​"vertical"
​tools​:context=​".MainActivity"​>

<​TextView
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content"
​android​:text=​"test1"​/>
<​TextView
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content"
​android​:text=​"text2"​/>

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

<​LinearLayout
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content"
​android​:orientation=​"horizontal"​>

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

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

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

</​LinearLayout​>

</​LinearLayout​>

6.Validation example

public class ​MainActivity ​extends ​AppCompatActivity {

EditText ​et_name​, ​et_password​;


Button ​bt_submit​;

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

​et_name ​= (EditText) findViewById(R.id.​et_name​);


​et_password ​= (EditText) findViewById(R.id.​et_password)​ ;
​bt_submit ​= (Button) findViewById(R.id.​bt_submit​);
​bt_submit​.setOnClickListener(​new ​View.OnClickListener() {
​@Override
​public void ​onClick(View view) {
String name = ​et_name​.getText().toString();
String password = ​et_password​.getText().toString();

​if ​(TextUtils.​isEmpty(​ name)) {


Toast.​makeText​(MainActivity.​this​, ​"enter your name.."​, Toast.​LENGTH_SHORT​).show();
​return​;
}
​if ​(TextUtils.​isEmpty(​ password)) {
Toast.​makeText​(MainActivity.​this​, ​"enter your password.."​, Toast.​LENGTH_SHORT)​ .show();
​return​;
}

Intent intent = ​new ​Intent(MainActivity.​this​, SecondActivity.​class​);


intent.putExtra(​"name"​, name);
intent.putExtra(​"pwd"​, password);
startActivity(intent);
}
});

​public final static boolean ​isValidEmail(CharSequence target) {


​return ​!TextUtils.​isEmpty(​ target) && android.util.Patterns.​EMAIL_ADDRESS.​ matcher(target).matches();
}

activity_main.xml

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


<​RelativeLayout ​xmlns:​android​=​"http://schemas.android.com/apk/res/android"
​xmlns:​app​=​"http://schemas.android.com/apk/res-auto"
​xmlns:​tools​=​"http://schemas.android.com/tools"
​android​:layout_width=​"match_parent"
​android​:layout_height=​"match_parent"
​tools​:context=​".MainActivity"​>
<​LinearLayout
​android​:layout_width=​"match_parent"
​android​:layout_height=​"wrap_content"
​android​:layout_centerInParent=​"true"
​android​:gravity=​"center"
​android​:orientation=​"vertical"​>
<​EditText
​android​:id=​"@+id/et_name"
​android​:layout_width=​"match_parent"
​android​:layout_marginLeft=​"40dp"
​android​:layout_marginRight=​"40dp"
​android​:inputType=​"textEmailAddress"
​android​:layout_height=​"wrap_content"
​android​:hint=​"enter email id"​/>

<​EditText
​android​:id=​"@+id/et_password"
​android​:layout_width=​"match_parent"
​android​:layout_marginLeft=​"40dp"
​android​:layout_marginRight=​"40dp"
​android​:inputType=​"textPassword"
​android​:layout_height=​"wrap_content"
​android​:hint=​"Password"​/>

<​Button
​android​:id=​"@+id/bt_submit"
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content"
​android​:text=​"submit"
​/>

</​LinearLayout​>
</​RelativeLayout​>

Second Activity

public class ​SecondActivity ​extends ​AppCompatActivity {

TextView ​tv_name​,​tv_password​;
Bundle ​extras​;
​@Override
​protected void ​onCreate(Bundle savedInstanceState) {
​super​.onCreate(savedInstanceState);
setContentView(R.layout.​activity_second)​ ;

​tv_name ​= (TextView) findViewById(R.id.​tv_name)​ ;


​tv_password ​= (TextView) findViewById(R.id.​tv_password​);

​if​(getIntent().getExtras()!=​null​){
​extras ​= getIntent().getExtras();

String name = ​extras​.getString(​"name"​);


String password = ​extras​.getString(​"pwd"​);

​tv_name​.setText(name);
​tv_password​.setText(password);

}
}

activity_second.xml

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


<​RelativeLayout ​xmlns:​android​=​"http://schemas.android.com/apk/res/android"
​xmlns:​app​=​"http://schemas.android.com/apk/res-auto"
​xmlns:​tools​=​"http://schemas.android.com/tools"
​android​:layout_width=​"match_parent"
​android​:layout_height=​"match_parent"
​tools​:context=​".SecondActivity"​>

<​LinearLayout
​android​:layout_width=​"match_parent"
​android​:layout_height=​"match_parent"
​android​:layout_centerInParent=​"true"
​android​:gravity=​"center"
​android​:orientation=​"vertical"​>

<​TextView
​android​:id=​"@+id/tv_name"
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content" ​/>

<​TextView
​android​:id=​"@+id/tv_password"
​android​:layout_width=​"wrap_content"
​android​:layout_height=​"wrap_content" ​/>

</​LinearLayout​>

</​RelativeLayout​>

7.ImageView Example

public class ​MainActivity ​extends ​AppCompatActivity {

ImageView ​iv_pic​;
​@Override
​protected void ​onCreate(Bundle savedInstanceState) {
​super​.onCreate(savedInstanceState);
setContentView(R.layout.​activity_main)​ ;
​iv_pic ​= (ImageView) findViewById(R.id.​iv_pic​);
​iv_pic​.setImageResource(R.drawable.​ab​);

}
}

activity_main.xml

<?x ​ ml version=​"1.0" ​encoding=​"utf-8"​?>


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

<​ImageView
​android​:id=​"@+id/iv_pic"
​android​:layout_width=​"300dp"
​android​:layout_height=​"200dp"
​android​:layout_centerInParent=​"true"
​android​:src=​"@drawable/ic_launcher_background"
​/>

</​RelativeLayout​>

8.VideoView Example

● Give internet permission in manifest file just above the application tag.

<​uses-permission ​android​:name=​"android.permission.INTERNET" ​/>

public class ​MainActivity ​extends ​AppCompatActivity {


VideoView ​video_view​;
​@Override
​protected void ​onCreate(Bundle savedInstanceState) {
​super​.onCreate(savedInstanceState);
setContentView(R.layout.​activity_main)​ ;
String path = ​"http://videocdn.bodybuilding.com/video/mp4/62000/62792m.mp4"​;
Uri uri = Uri.​parse(​ path);

​video_view ​= (VideoView)findViewById(R.id.​video_view​);
​video_view​.setVideoURI(uri);
​video_view​.start();
}
}
activity_main.xml

<?x ​ ml version=​"1.0" ​encoding=​"utf-8"​?>


<​LinearLayout ​xmlns:​android​=​"http://schemas.android.com/apk/res/android"
​xmlns:​app​=​"http://schemas.android.com/apk/res-auto"
​xmlns:​tools​=​"http://schemas.android.com/tools"
​android​:layout_width=​"match_parent"
​android​:layout_height=​"match_parent"
​android​:orientation=​"vertical"
​tools​:context=​".MainActivity"​>

<​VideoView
​android​:id=​"@+id/video_view"
​android​:layout_width=​"match_parent"
​android​:layout_height=​"wrap_content" ​/>

</​LinearLayout​>

You might also like