Android Day 2 PDF
Android Day 2 PDF
TextView tv_text;
Button bt_submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"clicked",Toast.LENGTH_SHORT).show();
}
});
}
}
activity_main.xml
/>
</RelativeLayout>
2.Move from one Activity to another activity using intents
TextView tv_text;
Button bt_submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main) ;
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
/>
</RelativeLayout>
Second Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2) ;
}
}
activity_main2.xml
<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
EditText et_name,et_password;
Button bt_submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<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
TextView tv_name,tv_password;
Bundle extras;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second) ;
if(getIntent().getExtras()!=null){
extras = getIntent().getExtras();
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
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main) ;
}
}
activity_main.xml
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
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main) ;
}
}
activity_main.xml
<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
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main) ;
activity_main.xml
<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
TextView tv_name,tv_password;
Bundle extras;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second) ;
if(getIntent().getExtras()!=null){
extras = getIntent().getExtras();
tv_name.setText(name);
tv_password.setText(password);
}
}
activity_second.xml
<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
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
<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.
video_view = (VideoView)findViewById(R.id.video_view);
video_view.setVideoURI(uri);
video_view.start();
}
}
activity_main.xml
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>