6

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

6_1 :

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imgV"
android:layout_height="400dp"
android:layout_width="match_parent"/>

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Image"/>

</LinearLayout>
______________________________

package com.example.cam;

import android.content.Intent;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


ImageView iv;
Button b;

int REQ_CODE = 101;

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

iv = findViewById(R.id.imgV);
b = findViewById(R.id.btn);

b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivityForResult(new
Intent(MediaStore.ACTION_IMAGE_CAPTURE),REQ_CODE);
}
});

@Override
protected void onActivityResult(int reqCode, int resCode, Intent data) {
super.onActivityResult(reqCode, resCode, data);

if(reqCode == REQ_CODE && resCode == RESULT_OK){


Bitmap ibm = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(ibm);
}
}
}
____________________________________
-------------------------------------

6_2 :

package com.example.email;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

Button button;
EditText to, sub, body;

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

to = findViewById(R.id.editText1);
sub = findViewById(R.id.editText2);
body = findViewById(R.id.editText3);
button = findViewById(R.id.button);

button.setOnClickListener(view -> {
String emailsend = to.getText().toString();
String emailsubject = sub.getText().toString();
String emailbody = body.getText().toString();

Intent intent = new Intent(Intent.ACTION_SEND);

intent.putExtra(Intent.EXTRA_EMAIL, new String[]{emailsend});


intent.putExtra(Intent.EXTRA_SUBJECT, emailsubject);
intent.putExtra(Intent.EXTRA_TEXT, emailbody);

intent.setType("message/rfc822");

startActivity(Intent.createChooser(intent, "Choose an Email


client :"));
});

_____________________________________________
---------------------------------------------

6_3 :

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="vertical">

<TableRow >
<EditText
android:id="@+id/en1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter No.1" />
</TableRow>
<TableRow >
<EditText
android:id="@+id/en2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter No.2 "
/>
</TableRow>
<TableRow >

<TextView
android:id="@+id/res"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="22dp"
android:text="Result : "
/>
</TableRow>
<TableRow >
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+" />

<Button
android:id="@+id/sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />

<Button
android:id="@+id/mul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="*" />

<Button
android:id="@+id/div"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/" />
</TableRow>

</TableLayout>

----------------------

package com.example.cal;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText en1, en2;


TextView res;
Button add, sub, mul, div;

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

// Initialize UI components
en1 = findViewById(R.id.en1);
en2 = findViewById(R.id.en2);
res = findViewById(R.id.res);
add = findViewById(R.id.add);
sub = findViewById(R.id.sub);
mul = findViewById(R.id.mul);
div = findViewById(R.id.div);

// Set button click listeners


add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('+');
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('-');
}
});

mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('*');
}
});

div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculate('/');
}
});
}

private void calculate(char operator) {

float n1 = Float.parseFloat(en1.getText().toString());
float n2 = Float.parseFloat(en2.getText().toString());

double result;

switch (operator) {
case '+':
result = n1 + n2;
break;
case '-':
result = n1 - n2;
break;
case '*':
result = n1 * n2;
break;
case '/':
if (n2 == 0) {
Toast.makeText(this, "Cannot divide by zero",
Toast.LENGTH_SHORT).show();
return;
}
result = n1 / n2;
break;
default:
return;
}

res.setText("Result: " + result);


}
}

---------------------------------
________________________________
6_5 :

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="@+id/map"/>
</RelativeLayout>

-------------------------------------

package com.example.carloaction;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.*;
import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.*;
import android.location.Location;
import com.google.android.gms.tasks.*;

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {


private Location carLoc;
private FusedLocationProviderClient flpc;
private static final int REQ_CODE = 101;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flpc = LocationServices.getFusedLocationProviderClient(this);
fetchLastLocation();
}

private void fetchLastLocation() {


if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, REQ_CODE);
return;
}
flpc.getLastLocation().addOnSuccessListener(new
OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
carLoc = location;
SupportMapFragment mf = (SupportMapFragment)
getSupportFragmentManager().findFragmentById(R.id.map);
if (mf != null) {
mf.getMapAsync(MainActivity.this);
}
}
}
});
}

@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
if (carLoc != null) {
LatLng latLng = new LatLng(carLoc.getLatitude(),
carLoc.getLongitude());

googleMap.addMarker(new MarkerOptions().position(latLng).title("Your
Car"));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,
15F));
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQ_CODE && grantResults.length > 0 && grantResults[0]
== PackageManager.PERMISSION_GRANTED) {
fetchLastLocation();
}
}
}

You might also like