Mad for Coding: Android Layouts Guide
Mad for Coding: Android Layouts Guide
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView in a Linear Layout" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView in an Absolute Layout"
android:layout_x="100dp"
android:layout_y="100dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_x="200dp"
android:layout_y="200dp"/>
</AbsoluteLayout>
</LinearLayout>
2. Develop a program to implement frame layout, table layout and relative layout.
Xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView in a FrameLayout"
android:layout_gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_gravity="bottom|end"/>
</FrameLayout>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"/>
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your email"/>
</TableRow>
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_alignParentEnd="true"/>
</RelativeLayout>
</RelativeLayout>
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:layout_marginTop="20dp"/>
<TextView
android:id="@+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:layout_below="@+id/heading"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"/>
<EditText
android:id="@+id/name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/nameText"
android:layout_marginLeft="20dp"
android:layout_below="@id/heading"/>
<TextView
android:id="@+id/ageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:layout_below="@+id/nameText"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<EditText
android:id="@+id/age"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/nameText"
android:layout_marginLeft="20dp"
android:layout_below="@id/name"
android:inputType="number"/>
<TextView
android:id="@+id/emailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:layout_below="@+id/ageText"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>
<EditText
android:id="@+id/email"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/nameText"
android:layout_marginLeft="20dp"
android:layout_below="@id/age"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/btn"
android:layout_below="@id/email"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:onClick="submit"
android:inputType="textEmailAddress"/>
</RelativeLayout>
JAVA:
package [Link].pr3_registrationform;
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:onClick="btnClick"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background"
android:onClick="imgClick"
android:layout_marginTop="20dp"/>
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:onClick="toggle"
android:textOff="Click to show image"
android:textOn="Click to hide image"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"/>
</LinearLayout>
Java:
package [Link].pr4_btnimg_btntoggle_btn;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
tb = findViewById([Link]);
iv = findViewById([Link]);
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:onClick="send"/>
</LinearLayout>
JAVA:
package [Link].pr5_loginwindow;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText t1,t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
t1=findViewById([Link]);
t2=findViewById([Link]);
}
public void send(View v){
String username = [Link]().toString();
String password = [Link]().toString();
}
6. Develop a program to implement Progress Bar.
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleX="4"
android:scaleY="4"
android:progress="0"
style="@style/[Link]"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/pb"
android:layout_marginTop="80dp"
android:layout_centerHorizontal="true"
android:text="Start"
android:onClick="start"/>
</RelativeLayout>
JAVA:
package [Link].pr6_progressbar;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
int progress = 0;
ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
pb = findViewById([Link]);
}
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="[Link]
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3" />
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:src="@drawable/img" />
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="952dp"
android:layout_marginTop="60dp"
android:entries="@array/cricketers" />
</LinearLayout>
</ScrollView>
[Link]:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
[Link]:
<resources>
<string name="app_name">pr7_list,grid,image_view</string>
<string-array name="cricketers">
<item>ROHIT</item>
<item>VIRAT</item>
<item>DHONI</item>
<item>ROHIT</item>
<item>VIRAT</item>
<item>DHONI</item>
</string-array>
</resources>
[Link]:
package [Link].pr7_listgridimage_view;
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
8. Develop a program to implement new activity using explicit intent and implicit intent.
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/explicitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explicit Intent" />
<Button
android:id="@+id/implicitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implicit Intent" />
</LinearLayout>
[Link]:
package [Link].pr8_intents;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Explicit Intent: Start SecondActivity
Intent explicitIntent = new Intent([Link], [Link]);
startActivity(explicitIntent);
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
// Implicit Intent: Open a web page
Intent implicitIntent = new Intent(Intent.ACTION_VIEW,
[Link]("[Link]
startActivity(implicitIntent);
}
});
}
}
Activity_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:backgroundTint="#039BE5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello User...You have selected explicit intent"
android:textSize="20dp" />
</LinearLayout>
[Link]:
package [Link].pr8_intents;
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
}
}
9. Develop a program to implement sensors.
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:id="@+id/linearlayout" >
</LinearLayout>
[Link]:
package [Link].pr9_sensors;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
linearLayout = findViewById([Link]);
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT){
[Link]([Link]);
}
else{
[Link]([Link]);
}
}
}
Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:text="Capture Image"
android:onClick="capture"/>
</LinearLayout>
[Link]:
package [Link].pr10_camera;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
iv = findViewById([Link].image2);
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/buttonOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Bluetooth On"
android:onClick="on"/>
<Button
android:id="@+id/buttonOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Bluetooth Off"
android:onClick="off"/>
</LinearLayout>
Java:
package [Link].pr11_bluetooth;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends Activity {
Button b1, b2;
private BluetoothAdapter BA;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
b1 = findViewById([Link]);
b2 = findViewById([Link]);
BA = [Link]();
}
public void on(View v) {
if (![Link]()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
[Link](getApplicationContext(), "Turning on",
Toast.LENGTH_SHORT).show();
} else {
[Link](getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
}
}
public void off(View v) {
if ([Link]()) {
[Link]();
[Link](getApplicationContext(), "Turning off",
Toast.LENGTH_SHORT).show();
} else {
[Link](getApplicationContext(), "Already off",
Toast.LENGTH_LONG).show();
}
}
}
Manifest:
<uses-permission android:name="[Link]" />
<uses-permission android:name="[Link].BLUETOOTH_ADMIN"/>
<uses-permission android:name="[Link].BLUETOOTH_CONNECT" />
13. Create sample application with login module. (Check username and password) On
successful login, Change Text View “Login Successful” and on login fail, alert user using Toast
“Login fail”.
Xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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="Username:"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:id="@+id/unameText"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/uname"
android:layout_toRightOf="@id/unameText"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:id="@+id/passText"
android:layout_below="@id/unameText"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/password"
android:layout_toRightOf="@id/unameText"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_below="@id/uname"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_below="@id/password"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:onClick="submit"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/text"/>
</RelativeLayout>
Java:
package [Link].pr13;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
e1 = findViewById([Link]);
e2 = findViewById([Link]);
t1 = findViewById([Link]);
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/usernameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/passwordInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center_horizontal"
android:enabled="false"
android:onClick="login"/>
</LinearLayout>
Java:
package [Link].pr14;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText u, p;
private Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
u = findViewById([Link]);
p = findViewById([Link]);
b = findViewById([Link]);
TextWatcher loginTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkFieldsForEmptyValues();
}
@Override
public void afterTextChanged(Editable s) {
}
};
[Link](loginTextWatcher);
[Link](loginTextWatcher);
}
private void checkFieldsForEmptyValues() {
String username = [Link]().toString();
String password = [Link]().toString();
[Link](![Link]() && ![Link]());
}
public void login(View v){
String username = [Link]().toString();
String password = [Link]().toString();
if([Link]("admin") && [Link]("123"))
[Link](this, "Login successful", Toast.LENGTH_SHORT).show();
else
[Link](this, "Invalid details", Toast.LENGTH_SHORT).show();
}
}
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="@+id/mblTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mobile No"
android:inputType="number"/>
<EditText
android:id="@+id/msgTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message" />
<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"/>
</LinearLayout>
Java:
package [Link].pr15_sms;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
phoneNo = findViewById([Link]);
message = findViewById([Link]);
if ([Link](this, [Link].SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
[Link](this, new
String[]{[Link].SEND_SMS}, PERMISSION_REQUEST_SMS);
}
}
else
{
[Link](this, "Please enter phone no. and message",
Toast.LENGTH_SHORT).show();
}
}
}
Manifest:
<uses-permission android:name="[Link].SEND_SMS"/>
Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="To:" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text_to"
android:inputType="textEmailAddress"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subject:" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text_subject"
android:inputType="text"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message:" />
<EditText
android:id="@+id/edit_text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|top"
android:lines="10"/>
<Button
android:id="@+id/button_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Executed By Sagar Chanchlani - 33"
android:textStyle="bold"
android:layout_marginLeft="30dp" />
</LinearLayout>
Java:
package [Link].pr16_email;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
private EditText mEditTextTo, mEditTextSubject, mEditTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
mEditTextTo = findViewById([Link].edit_text_to);
mEditTextSubject = findViewById([Link].edit_text_subject);
mEditTextMessage = findViewById([Link].edit_text_message);
Button buttonSend = findViewById([Link].button_send);
[Link](new [Link]() {
@Override
public void onClick(View v) {
sendMail();
}
});
}
private void sendMail() {
String recipientList = [Link]().toString();
String subject = [Link]().toString();
String message = [Link]().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
[Link](Intent.EXTRA_EMAIL, new String[]{recipientList});
[Link](Intent.EXTRA_SUBJECT, subject);
[Link](Intent.EXTRA_TEXT, message);
[Link]("message/rfc822");
startActivity([Link](intent, "Choose an email client"));
}
}
Manifest:
<uses-permission android:name="[Link]" />
<uses-permission android:name="[Link].SEND_EMAIL" />
18. Deploy map-based application. Part II(Source To Destination)
Xml:
<LinearLayout
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_source"
android:hint="Enter Source Location"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_destination"
android:hint="Enter Destination Location"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_track"
android:text="Display Location"/>
</LinearLayout>
Java:
package [Link].pr18_sourcetodestination;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
EditText etSource, etDestination;
Button btTrack;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
etSource = findViewById([Link].et_source);
etDestination = findViewById([Link].et_destination);
btTrack = findViewById([Link].bt_track);
[Link](new [Link]() {
@Override
public void onClick(View v) {
String sSource = [Link]().toString().trim();
String sDestination = [Link]().toString().trim();
if ([Link]() || [Link]()) {
[Link]([Link], "Please enter both locations.",
Toast.LENGTH_SHORT).show();
} else {
displayTrack(sSource, sDestination);
}
}
});
}
private void displayTrack(String sSource, String sDestination) {
Uri uri = [Link]("[Link] + sSource + "/" +
sDestination);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
[Link]("[Link]");
if ([Link](getPackageManager()) != null) {
startActivity(intent);
} else {
Uri playStoreUri =
[Link]("[Link]
Intent playStoreIntent = new Intent(Intent.ACTION_VIEW, playStoreUri);
startActivity(playStoreIntent);
}
}
}
Manifest:
<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link].ACCESS_FINE_LOCATION"/>
<uses-permission android:name="[Link].ACCESS_COARSE_LOCATION"/>