0% found this document useful (0 votes)
25 views8 pages

MAD Programs

The document provides practical examples of mobile application development using Java and XML for Android. It covers various functionalities such as displaying messages based on screen orientation, creating a login window, using explicit and implicit intents, implementing a splash screen, managing local data with Shared Preferences, and sending emails. Each section includes code snippets for the main activity and corresponding XML layouts.

Uploaded by

r9935057
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)
25 views8 pages

MAD Programs

The document provides practical examples of mobile application development using Java and XML for Android. It covers various functionalities such as displaying messages based on screen orientation, creating a login window, using explicit and implicit intents, implementing a splash screen, managing local data with Shared Preferences, and sending emails. Each section includes code snippets for the main activity and corresponding XML layouts.

Uploaded by

r9935057
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

Mobile Application Development

Practical Programs — Java + XML

Q1. Display Message Based on Screen Orientation


[Link] → add to <activity>: android:configChanges="orientation|screenSize"

[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
tv = findViewById([Link]);
checkOrientation();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
[Link](newConfig);
checkOrientation();
}

void checkOrientation() {
if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
[Link]("Landscape Mode");
} else {
[Link]("Portrait Mode");
}
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:text="Checking orientation..." />
</LinearLayout>

Q2. Login Window Using UI Controls (also Q8 & Q9)


Q8 = same program. Q9 = same program (Toast on failure already included).

[Link]
package [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];

public class MainActivity extends AppCompatActivity {


EditText etUser, etPass;
Button btnLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
etUser = findViewById([Link]);
etPass = findViewById([Link]);
btnLogin = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String user = [Link]().toString();
String pass = [Link]().toString();
if ([Link]("admin") && [Link]("1234")) {
[Link]([Link],
"Login Successful!", Toast.LENGTH_SHORT).show();
} else {
[Link]([Link],
"Invalid Credentials!", Toast.LENGTH_SHORT).show();
}
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center" android:orientation="vertical" android:padding="24dp">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"


android:text="Login" android:textSize="28sp" android:textStyle="bold"
android:layout_marginBottom="20dp"/>

<EditText android:id="@+id/etUsername" android:layout_width="match_parent"


android:layout_height="wrap_content" android:hint="Username"
android:inputType="text" android:layout_marginBottom="12dp"/>

<EditText android:id="@+id/etPassword" android:layout_width="match_parent"


android:layout_height="wrap_content" android:hint="Password"
android:inputType="textPassword" android:layout_marginBottom="20dp"/>

<Button android:id="@+id/btnLogin" android:layout_width="match_parent"


android:layout_height="wrap_content" android:text="Login"/>
</LinearLayout>
Q3. New Activity Using Explicit Intent
Register SecondActivity in [Link]: <activity android:name=".SecondActivity"/>

[Link]
package [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);
Button btn = findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
Intent intent = new Intent([Link], [Link]);
[Link]("message", "Hello from MainActivity!");
startActivity(intent);
}
});
}
}

[Link]
package [Link];
import [Link];
import [Link];
import [Link];

public class SecondActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_second);
String msg = getIntent().getStringExtra("message");
TextView tv = findViewById([Link]);
[Link](msg);
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center" android:orientation="vertical">
<Button android:id="@+id/btnGo" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Go to Second Activity"/>
</LinearLayout>

activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center" android:orientation="vertical">
<TextView android:id="@+id/tvMessage" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="20sp"/>
</LinearLayout>

Q4. Implicit Intent + Content Provider (also Q10)


Q10 = same program covers both implicit intent + content provider (tel: URI).
[Link]
package [Link];
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);

Button btnWeb = findViewById([Link]);


Button btnDial = findViewById([Link]);

[Link](new [Link]() {
@Override public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW,
[Link]("[Link]
startActivity(i);
}
});
[Link](new [Link]() {
@Override public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_DIAL,
[Link]("[Link]
startActivity(i);
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center" android:orientation="vertical" android:padding="16dp">

<Button android:id="@+id/btnWeb" android:layout_width="wrap_content"


android:layout_height="wrap_content"
android:text="Open Website (Implicit)" android:layout_marginBottom="16dp"/>

<Button android:id="@+id/btnDial" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:text="Dial (Content Provider)"/>
</LinearLayout>
Q5. Custom Designed Opening / Splash Screen
Set SplashActivity as LAUNCHER in AndroidManifest. Register MainActivity too.

[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class SplashActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_splash);
new Handler().postDelayed(new Runnable() {
@Override public void run() {
startActivity(new Intent([Link], [Link]));
finish();
}
}, 3000); // 3 second splash
}
}

[Link]
package [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
}

activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center" android:orientation="vertical"
android:background="#3F51B5">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"


android:text="Welcome!" android:textSize="36sp"
android:textColor="#FFFFFF" android:textStyle="bold"/>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"


android:text="My App" android:textSize="18sp"
android:textColor="#FFFFFF" android:layout_marginTop="8dp"/>
</LinearLayout>

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Home Screen" android:textSize="24sp"/>
</LinearLayout>

[Link] snippet
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="[Link]"/>
<category android:name="[Link]"/>
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>

Q6. Read / Write Local Data (Shared Preferences)


[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];

public class MainActivity extends AppCompatActivity {


EditText etData;
Button btnSave, btnRead;
TextView tvResult;
SharedPreferences sp;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
etData = findViewById([Link]);
btnSave = findViewById([Link]);
btnRead = findViewById([Link]);
tvResult = findViewById([Link]);
sp = getSharedPreferences("MyData", MODE_PRIVATE);

[Link](new [Link]() {
@Override public void onClick(View v) {
[Link]().putString("saved_data",
[Link]().toString()).apply();
[Link]([Link],
"Saved!", Toast.LENGTH_SHORT).show();
}
});
[Link](new [Link]() {
@Override public void onClick(View v) {
[Link]("Read: " +
[Link]("saved_data", "No data found"));
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center" android:orientation="vertical" android:padding="24dp">

<EditText android:id="@+id/etData" android:layout_width="match_parent"


android:layout_height="wrap_content" android:hint="Enter data to save"
android:layout_marginBottom="12dp"/>

<Button android:id="@+id/btnSave" android:layout_width="match_parent"


android:layout_height="wrap_content" android:text="Save Data"
android:layout_marginBottom="8dp"/>

<Button android:id="@+id/btnRead" android:layout_width="match_parent"


android:layout_height="wrap_content" android:text="Read Data"
android:layout_marginBottom="16dp"/>

<TextView android:id="@+id/tvResult" android:layout_width="wrap_content"


android:layout_height="wrap_content" android:textSize="18sp"/>
</LinearLayout>
Q7. Send an E-mail
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];

public class MainActivity extends AppCompatActivity {


EditText etTo, etSubject, etBody;
Button btnSend;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
etTo = findViewById([Link]);
etSubject = findViewById([Link]);
etBody = findViewById([Link]);
btnSend = findViewById([Link]);

[Link](new [Link]() {
@Override public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_SEND);
[Link]("message/rfc822");
[Link](Intent.EXTRA_EMAIL,
new String[]{[Link]().toString()});
[Link](Intent.EXTRA_SUBJECT,
[Link]().toString());
[Link](Intent.EXTRA_TEXT,
[Link]().toString());
startActivity([Link](i, "Choose Email App"));
}
});
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent" android:layout_height="match_parent"
android:gravity="center" android:orientation="vertical" android:padding="24dp">

<EditText android:id="@+id/etTo" android:layout_width="match_parent"


android:layout_height="wrap_content" android:hint="To (Email)"
android:inputType="textEmailAddress" android:layout_marginBottom="10dp"/>

<EditText android:id="@+id/etSubject" android:layout_width="match_parent"


android:layout_height="wrap_content" android:hint="Subject"
android:layout_marginBottom="10dp"/>

<EditText android:id="@+id/etBody" android:layout_width="match_parent"


android:layout_height="wrap_content" android:hint="Message"
android:lines="4" android:gravity="top" android:layout_marginBottom="16dp"/>

<Button android:id="@+id/btnSend" android:layout_width="match_parent"


android:layout_height="wrap_content" android:text="Send Email"/>
</LinearLayout>

You might also like