0% found this document useful (0 votes)
6 views

ANDROID problems

Uploaded by

Ayesha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ANDROID problems

Uploaded by

Ayesha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

AISHA SIDDIQUA BCA 5th E Roll no 5

Problem statement 13 - Write a Code in Android to implement external


intent that uses two buttons & with one button should call the current
activity (self-call), and the other button should call a different activity.
Objective:
Implement an Android app with two buttons:
 Button 1: Calls the current activity (self-call).
 Button 2: Calls a different activity.
XML Layout (activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<Button
android:id="@+id/btnSelfCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call Self Activity" />

<Button
android:id="@+id/btnNewActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call New Activity" />
</LinearLayout>
Java Code (MainActivity.java):
package com.example.intentexample;

import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
AISHA SIDDIQUA BCA 5th E Roll no 5

import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


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

Button btnSelfCall = findViewById(R.id.btnSelfCall);


Button btnNewActivity = findViewById(R.id.btnNewActivity);

btnSelfCall.setOnClickListener(view -> {
Intent selfIntent = new Intent(MainActivity.this, MainActivity.class);
startActivity(selfIntent);
});

btnNewActivity.setOnClickListener(view -> {
Intent newIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(newIntent);
});
}
}
Java Code (SecondActivity.java):
package com.example.intentexample;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
AISHA SIDDIQUA BCA 5th E Roll no 5

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}

Output –
AISHA SIDDIQUA BCA 5th E Roll no 5

Problem statement 14 - Write an Android application to implement


implicit intents to perform the following actions on pressing 4 different
buttons: Send an email. Send a text message. Make a phone call. Open
a URL in a web browser.
Objective: Implement implicit intents to:
1. Send an email.
2. Send a text message.
3. Make a phone call.
4. Open a URL in a web browser.
XML Layout (activity_main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<Button android:id="@+id/btnSendEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Email" />

<Button android:id="@+id/btnSendSMS"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send SMS" />

<Button android:id="@+id/btnMakeCall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Make Call" />

<Button android:id="@+id/btnOpenURL"
AISHA SIDDIQUA BCA 5th E Roll no 5

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open URL" />
</LinearLayout>
Java Code (MainActivity.java):
package com.example.implicitintentapp;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


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

Button btnSendEmail = findViewById(R.id.btnSendEmail);


Button btnSendSMS = findViewById(R.id.btnSendSMS);
Button btnMakeCall = findViewById(R.id.btnMakeCall);
Button btnOpenURL = findViewById(R.id.btnOpenURL);

btnSendEmail.setOnClickListener(view -> {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO,
Uri.fromParts("mailto", "[email protected]", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message");
startActivity(Intent.createChooser(emailIntent, "Send Email"));
});
AISHA SIDDIQUA BCA 5th E Roll no 5

btnSendSMS.setOnClickListener(view -> {
Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
Uri.parse("smsto:1234567890"));
smsIntent.putExtra("sms_body", "Hello, this is a test message.");
startActivity(smsIntent);
});

btnMakeCall.setOnClickListener(view -> {
Intent callIntent = new Intent(Intent.ACTION_DIAL,
Uri.parse("tel:1234567890"));
startActivity(callIntent);
});

btnOpenURL.setOnClickListener(view -> {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.example.com"));
startActivity(browserIntent);
});
}
}

X`Output –
AISHA SIDDIQUA BCA 5th E Roll no 5
AISHA SIDDIQUA BCA 5th E Roll no 5

Problem statement 15 - Write an Android Code to show multiple images


using a single imageview & show one image after another on hitting
button. Note - use atleast 5 images.
Objective: Show multiple images in one ImageView, changing on button press.
XML Layout (activity_main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">

<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginBottom="20dp" />

<Button
android:id="@+id/btnChangeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next Image" />
</LinearLayout>
Java Code (MainActivity.java):
package com.example.imageviewapp;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
AISHA SIDDIQUA BCA 5th E Roll no 5

public class MainActivity extends AppCompatActivity {


int[] images = {R.drawable.image1, R.drawable.image2, R.drawable.image3,
R.drawable.image4, R.drawable.image5};
int currentIndex = 0;

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

ImageView imageView = findViewById(R.id.imageView);


Button btnChangeImage = findViewById(R.id.btnChangeImage);

btnChangeImage.setOnClickListener(view -> {
currentIndex = (currentIndex + 1) % images.length;
imageView.setImageResource(images[currentIndex]);
});
}
}

Output –
AISHA SIDDIQUA BCA 5th E Roll no 5
AISHA SIDDIQUA BCA 5th E Roll no 5

Problem statement 16 - Write an Android application that implements a


ListView to display a list of items. write objective, xml, java code for
each
Objective: Implement a ListView to display a list of items.
XML Layout (activity_main.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Java Code (MainActivity.java):
package com.example.listviewapp;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {


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

ListView listView = findViewById(R.id.listView);


String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
AISHA SIDDIQUA BCA 5th E Roll no 5

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
}
}

Output –

You might also like