0% found this document useful (0 votes)
14 views16 pages

Mad Prac

The document describes how to develop a program to implement camera, list view, grid view, image view, scroll view, frame layout and relative layout in Android. It provides XML layout code and Java code to add these features to an Android app.

Uploaded by

fmail8465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Mad Prac

The document describes how to develop a program to implement camera, list view, grid view, image view, scroll view, frame layout and relative layout in Android. It provides XML layout code and Java code to add these features to an Android app.

Uploaded by

fmail8465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Develop a program to implement camera.

Xml code

<?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"
tools:context=".MainActivity">
<Button
android:id="@+id/camera_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="camera" />
<ImageView
android:id="@+id/click_image"
android:layout_width="350dp"
android:layout_height="450dp"
android:layout_marginStart="30dp"
android:layout_marginTop="70dp"
android:layout_marginBottom="10dp" />
</LinearLayout>

Java

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


Button cameraButton;
ImageView imageView;
ActivityResultLauncher<Intent> cameraLauncher;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraButton = findViewById(R.id.camera_button);
imageView = findViewById(R.id.click_image);
cameraLauncher = registerForActivityResult(new
ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == RESULT_OK) {
Bitmap photo = (Bitmap) result.getData().getExtras().get("data");
imageView.setImageBitmap(photo);
}
});
cameraButton.setOnClickListener(view -> openCamera());
}
private void openCamera() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraLauncher.launch(cameraIntent);
}
}
Develop a program to implement List View, Grid View, Image View and Scroll View.

Xml code

<?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:padding="16dp"
android:id="@+id/ll"
tools:context=".MainActivity">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<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="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_foreground"
android:layout_gravity="center_horizontal" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Wikipedia[note 3] is a free content online encyclopedia written and
maintained by a community of volunteers, known as Wikipedians, through open collaboration and
the use of the wiki-based editing system MediaWiki. Wikipedia is the largest and most-read
reference work in history.[3][4] It is consistently ranked as one of the ten most popular websites in
the world, and as of 2024 is ranked the fifth most visited website on the Internet by Semrush,[5] and
second by Ahrefs.[6] Founded by Jimmy Wales and Larry Sanger on January 15, 2001, Wikipedia is
hosted by the Wikimedia Foundation, an American nonprofit organization that employs a staff of
over 700 people.[7]
Initially only available in English, editions in other languages have been developed. Wikipedia's
editions, when combined, comprise more than 62 million articles, attracting around 2 billion unique
device visits per month and more than 14 million edits per month (about 5.2 edits per second on
average) as of November 2023.[8][W 1] Roughly 26% of Wikipedia's traffic is from the United States,
followed by Japan at 5.9%, the United Kingdom at 5.4%, Germany at 5%, Russia at 4.8%, and the
remaining 54% split among other countries, according to data provided by Similarweb.[9]
Wikipedia has been praised for its enablement of the democratization of knowledge, extent of
coverage, unique structure, and culture. It has been criticized for exhibiting systemic bias, particularly
gender bias against women and geographical bias against the Global South (Eurocentrism).[10][11]
[failed verification] While the reliability of Wikipedia was frequently criticized in the 2000s, it has
improved over time, receiving greater praise from the late 2010s onward[3][10][12] while becoming
an important fact-checking site.[13][14]
Wikipedia has been censored by some national governments, ranging from specific pages to the
entire site.[15][16] Articles on breaking news are often accessed as sources for frequently updated
information about those events.[17][18] "/>
</LinearLayout>
</ScrollView>
</LinearLayout>

Java

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

ListView listView;
GridView gridView;
ImageView imageView;

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

listView = findViewById(R.id.listView);
gridView = findViewById(R.id.gridView);
imageView = findViewById(R.id.imageView);

List<String> dataList = Arrays.asList("Item 1", "Item 2", "Item 3", "Item 4", "Item 5");
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, dataList);
listView.setAdapter(listViewAdapter);

List<String> gridDataList = new ArrayList<>(dataList);


gridDataList.addAll(dataList);
gridDataList.addAll(dataList);
ArrayAdapter<String> gridViewAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, gridDataList);
gridView.setAdapter(gridViewAdapter);

}
}
Develop a program to implement frame layout, relative layout. (Add text view, edit text,

radio button, Progress bar, )

Xml code

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relative and Frame Layout"
android:textSize="24dp"
android:layout_gravity="center"/>

</FrameLayout>

<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_below="@id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp">

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text here"
android:layout_marginTop="16dp"/>

<RadioButton
android:id="@+id/radioButton"
android:layout_below="@id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton"/>

<ProgressBar
android:id="@+id/progressBar"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_below="@id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

<Button
android:id="@+id/button"
android:layout_below="@id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Progress"
android:layout_centerHorizontal="true"
/>

</RelativeLayout>

</RelativeLayout>

Java

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private TextView textView;


private EditText editText;
private RadioButton radioButton;
private ProgressBar progressBar;
private Button startButton;
private int progressStatus = 0;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);
radioButton = findViewById(R.id.radioButton);
progressBar = findViewById(R.id.progressBar);
startButton = findViewById(R.id.button);

startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressStatus = 0;
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
}
});
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
}
}
Develop a program to implement linear layout and absolute layout. (Add text view, edit

text, image button, check box)

Xml code

<?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:padding="16dp"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="387dp"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Linear Layout Example" />

<EditText
android:id="@+id/editTextLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text here" />

<ImageButton
android:id="@+id/imgbtn"
android:layout_width="wrap_content"
android:layout_height="253dp"
android:contentDescription="Image Button"
android:scaleType="fitCenter"
android:src="@drawable/img" />

<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Checkbox" />

</LinearLayout>
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
>

<TextView
android:id="@+id/textViewAbsolute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Absolute Layout Example"
android:layout_x="16dp"
android:layout_y="0dp"/>

<EditText
android:id="@+id/editTextAbsolute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter text here"
android:layout_x="16dp"
android:layout_y="48dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:layout_x="16dp"
android:layout_y="100dp"
android:text="click"/>

</AbsoluteLayout>

</LinearLayout>

Java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {

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

EditText editText1 = findViewById(R.id.editTextLinear);


EditText editText2 = findViewById(R.id.editTextAbsolute);
CheckBox checkbox = findViewById(R.id.checkbox);
TextView textView = findViewById(R.id.textViewAbsolute);
ImageButton img = (ImageButton) findViewById(R.id.imgbtn);
Button btn = findViewById(R.id.btn);

img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"you've click the image", Toast.LENGTH_SHORT).show();
}
});
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = editText2.getText().toString();
Log.d("Response has been send: ",name); //open the logcat (terminal) to check the
response
}
});
}
}
Develop a program to implement Custom Toast Alert.

Xml code

<?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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Toast"
android:layout_centerHorizontal="true"
android:id="@+id/tv"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:layout_marginTop="196dp"
android:layout_centerHorizontal="true"
android:id="@+id/bt"
android:onClick="Toast"
android:text="Toast check" />

</RelativeLayout>

Java

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Toast(View v){
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.TOP | Gravity.LEFT,0,0);
toast.setText("You've Clicked the button");
toast.show();

}
}
Develop a program to create an activity.

Xml code

<?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">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World!"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

java code

package com.example.create_activity;

import androidx.appcompat.app.AppCompatActivity;

import android.util.Log;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Log.d("lifecycle", "onCreate invoked");

@Override

protected void onStart() {

super.onStart();

Log.i("lifecycle", "onStart invoked");

@Override

protected void onResume() {

super.onResume();

Log.v("lifecycle", "onResume invoked");

@Override

protected void onPause() {

super.onPause();

Log.d("lifecycle", "onPause invoked");

@Override

protected void onStop() {

super.onStop();

Log.d("lifecycle", "onStop invoked");

@Override

protected void onRestart() {


super.onRestart();

Log.d("lifecycle", "onRestart invoked");

@Override

protected void onDestroy() {

super.onDestroy();

Log.d("lifecycle", "onDestroy invoked");

You might also like