Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Group quiz no : 02

Activity.main.xml

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


<androidx.constraintlayout.widget.ConstraintLayout
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">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.558"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.471" />

<TextView
android:id="@+id/result"
android:layout_width="153dp"
android:layout_height="60dp"
app:layout_constraintBottom_toBottomOf="@+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.424"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.492" />

</androidx.constraintlayout.widget.ConstraintLayout>

Activity.main.java

package com.example.grpquiz1;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=findViewById(R.id.button);
TextView txt=findViewById(R.id.result);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
View custom=getLayoutInflater().inflate(R.layout.custom,null);
builder.setView(custom);
builder.setPositiveButton("Ok", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int
i) {
EditText year=custom.findViewById(R.id.year);
int v=Integer.parseInt(year.getText().toString());
int a=2022;
int b=0;
b=a-v;
txt.setText("your age is: "+b);

}
});
AlertDialog dialog=builder.create();
dialog.show();

}
});
}
}

Custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Date of birth"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<EditText
android:id="@+id/day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Day" />

<EditText
android:id="@+id/month"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Month" />

<EditText
android:id="@+id/year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Year" />

</LinearLayout>

Notificaton

Activity.main.xml

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


<androidx.constraintlayout.widget.ConstraintLayout
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">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Notification!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Activity.main.java
package com.example.notification;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showNotification();
}
});
}
private void showNotification(){
int notificationId=new Random().nextInt(100);
String channelId="notification_channel_1";
NotificationManager notificationManager=(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// Intent intent=new
Intent(getApplicationContext(),NotificationActivity2.class);
Intent intent=new Intent(Intent.ACTION_VIEW,
Uri.parse("https://m.youtube.com/"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent
pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_IMMU
TABLE);
NotificationCompat.Builder builder=new
NotificationCompat.Builder(getApplicationContext(),channelId);
builder.setSmallIcon(R.drawable.ic_notifications);
builder.setContentTitle("what is coding?");
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setContentText("Coding is basically the computer language used
to develop apps, websites, and software. Without it, we'd have none of the
most popular technology we've come to ...");
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("Coding
is basically the computer language used to develop apps, websites, and
software. Without it, we'd have none of the most popular technology we've come
to ..."));
builder.setContentIntent(pendingIntent);
//builder.setContentText(message)
builder.setAutoCancel(true);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
if (notificationManager!
=null&&notificationManager.getNotificationChannel(channelId)==null){
NotificationChannel notificationChannel=new
NotificationChannel(
channelId,"Notification channel 1",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("this notificarion
channel");
notificationChannel.enableVibration(true);
notificationChannel.enableLights(true);

notificationManager.createNotificationChannel(notificationChannel);

}
}
Notification notification=builder.build();

if (notificationManager!=null) {
notificationManager.notify(notificationId,notification);
}
}
}

Option Menu:
Activity.main.xml

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


<androidx.constraintlayout.widget.ConstraintLayout package
com.example.notification;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showNotification();
}
});
}
private void showNotification(){
int notificationId=new Random().nextInt(100);
String channelId="notification_channel_1";
NotificationManager notificationManager=(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// Intent intent=new
Intent(getApplicationContext(),NotificationActivity2.class);
Intent intent=new Intent(Intent.ACTION_VIEW,
Uri.parse("https://m.youtube.com/"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent
pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_IMMU
TABLE);
NotificationCompat.Builder builder=new
NotificationCompat.Builder(getApplicationContext(),channelId);
builder.setSmallIcon(R.drawable.ic_notifications);
builder.setContentTitle("what is coding?");
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
builder.setContentText("Coding is basically the computer language used
to develop apps, websites, and software. Without it, we'd have none of the
most popular technology we've come to ...");
builder.setStyle(new NotificationCompat.BigTextStyle().bigText("Coding
is basically the computer language used to develop apps, websites, and
software. Without it, we'd have none of the most popular technology we've come
to ..."));
builder.setContentIntent(pendingIntent);
//builder.setContentText(message)
builder.setAutoCancel(true);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
if (notificationManager!
=null&&notificationManager.getNotificationChannel(channelId)==null){
NotificationChannel notificationChannel=new
NotificationChannel(
channelId,"Notification channel 1",
NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("this notificarion
channel");
notificationChannel.enableVibration(true);
notificationChannel.enableLights(true);

notificationManager.createNotificationChannel(notificationChannel);

}
}
Notification notification=builder.build();

if (notificationManager!=null) {
notificationManager.notify(notificationId,notification);
}
}
}
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 i AM SOFIA Hera!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Mainmenu.xml

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


<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item android:title="Share"
android:id="@+id/menu_share">
<menu>
<item android:title="Via text"
android:id="@+id/viatext"/>
<item android:title="Via email"
android:id="@+id/viamail"/>
</menu>
</item>
<item android:title="Send Text"
android:id="@+id/menu_sendtext"/>
<item android:title="Make call"
android:id="@+id/menu_makecall"/>
<item android:title="Edit Contact"
android:id="@+id/menu_editcontact"/>
<item android:title="Delete Contact"
android:id="@+id/menu_deletecontact"/>
</menu>

MainActivity.java
package com.example.optionmenu;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu( Menu menu){
getMenuInflater().inflate(R.menu.main_menu,menu);
return true;
}
}
Context menu task 3

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">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_view"
>

</ListView>

</RelativeLayout>

Activity.main.java
package com.example.contextmenut3;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


ListView listView;
List<String> list=new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.list_view);
list.add("monitor");
list.add("keyboard");
list.add("mouse");
list.add("motherboard");
list.add("blutooth");
adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_expandable_list_item_1,list);
listView.setAdapter(adapter);
registerForContextMenu(listView);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu,v,menuInfo);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.context,menu);
}
@Override
public boolean onContextItemSelected(MenuItem item){
AdapterView.AdapterContextMenuInfo
info=(AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.delete_id:
list.remove(info.position);
adapter.notifyDataSetChanged();
return true;
case R.id.Edit_item:
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
View alert = getLayoutInflater().inflate(R.layout.alert,
null);
builder.setView(alert);
builder.setPositiveButton("ok", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int
i) {
EditText editText = alert.findViewById(R.id.edittext);
String newItem = editText.getText().toString();
list.set(info.position, newItem);
adapter.notifyDataSetChanged();
}
});
AlertDialog dialog = builder.create();
dialog.show();
return true;
//case R.id.Edit_item:
// adapter.notifyDataSetChanged();
// return true;
default:
return super.onContextItemSelected(item);
}
}
}

content.xml

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


<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:title="Delete item"
android:id="@+id/delete_id"/>
<item
android:title="Edit"
android:id="@+id/Edit_item"/>
</menu>

You might also like