Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Q1.

Write a simple Android program to create a first display screen of any search
engine using auto complete textview.
Ans:
MainActivity.java:

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

public class MainActivity extends AppCompatActivity {

private AutoCompleteTextView searchTerm;


private Button searchButton;

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

searchTerm = findViewById(R.id.search_term);
searchButton = findViewById(R.id.search_button);

String[] suggestions = {"Google", "Bing", "DuckDuckGo", "Yahoo!", "Ecosia"};

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


android.R.layout.simple_dropdown_item_1line, suggestions);

searchTerm.setAdapter(adapter);
}
});
}
}

activity_main.xml:

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

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

<ImageView
android:id="@+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/your_search_engine_logo" />

<AutoCompleteTextView
android:id="@+id/search_term"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/logo"
android:hint="Search the web"
android:inputType="text"
android:threshold="1" />

<Button
android:id="@+id/search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/search_term"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="Search" />

</RelativeLayout>

Q2. Write a simple Android program to capture an image using camera and display it.
Ans:
MainActivity.java:

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 {


private Button captureButton;
private ImageView capturedImage;
private static final int REQUEST_IMAGE_CAPTURE = 1;

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

captureButton = findViewById(R.id.capture_button);
capturedImage = findViewById(R.id.captured_image);

captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Open camera intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
{
Bitmap image = (Bitmap) data.getExtras().get("data");

capturedImage.setImageBitmap(image);
}
}
}

activity_main.xml:

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

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/capture_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture Image" />

<ImageView
android:id="@+id/captured_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/capture_button" />

</RelativeLayout>

AndroidManifest.xml:

<uses-feature android:name="android.hardware.camera" />


<uses-permission android:name="android.permission.CAMERA" />

Q3. Write a simple Android program to demonstrate button, toggle button, image
button.
Ans:
MainActivity.java:

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.Toast;
import android.widget.ToggleButton;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private Button button1;


private ToggleButton toggleButton1;
private ImageButton imageButton1;

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

button1 = findViewById(R.id.button1);
toggleButton1 = findViewById(R.id.toggleButton1);
imageButton1 = findViewById(R.id.imageButton1);

// Implement your desired actions for each button:

button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Show a toast message on click
Toast.makeText(MainActivity.this, "Button clicked!",
Toast.LENGTH_SHORT).show();
}
});

toggleButton1.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Change text or background based on toggle state
if (isChecked) {
toggleButton1.setText("On");
} else {
toggleButton1.setText("Off");
}
}
});

imageButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Perform an action like opening a new activity
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
});
}
}

activity_main.xml:

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


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

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />

<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On/Off"
android:layout_centerHorizontal="true"
android:layout_below="@id/button1"
android:layout_marginTop="16dp" />

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/your_image"
android:layout_centerHorizontal="true"
android:layout_below="@id/toggleButton1"
android:layout_marginTop="16dp" />

</RelativeLayout>

Q4. a) Write a simple Android program to create a list view, grid view, image view,
scroll view.
Ans:
MainActivity.java:

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

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[] listViewItems = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, listViewItems);
listView.setAdapter(listViewAdapter);

GridView gridView = findViewById(R.id.gridView);


String[] gridViewItems = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
ArrayAdapter<String> gridViewAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, gridViewItems);
gridView.setAdapter(gridViewAdapter);

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


imageView.setImageResource(R.drawable.sample_image);

ScrollView scrollView = findViewById(R.id.scrollView);


}
}

activity_main.xml:

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

<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="2"/>

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/sample_image"/>

<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<!-- Add your content here -->

</ScrollView>
</LinearLayout>

You might also like