0% found this document useful (0 votes)
24 views10 pages

Android

Uploaded by

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

Android

Uploaded by

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

P1 hello world programmme

MainActivity.java

// Package declaration indicating the package in which the class is defined


package com.example.demo1;

// Import necessary classes from the Android SDK


import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

// Main activity class extending the Android Activity class


public class MainActivity extends Activity {

// Method called when the activity is first created


@Override
protected void onCreate(Bundle savedInstanceState) {
// Call the superclass implementation of onCreate
super.onCreate(savedInstanceState);
// Set the content view of the activity to the layout defined in activity_main.xml
setContentView(R.layout.activity_main);
}

// Method for creating the options menu in the app


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu from the XML resource file main.xml and add items to the action bar
getMenuInflater().inflate(R.menu.main, menu);
// Return true to indicate that the menu has been successfully created
return true;
}
}

Activity_main.xml

<!-- XML layout file using RelativeLayout as the root layout -->
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<!-- TextView element for displaying a text message -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome_msg"
android:layout_marginTop="100dp"
android:textStyle="bold"
/>

</RelativeLayout>

Programe 2

Programe to check number is prime or not

package com.example.demo1;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

// UI components
private EditText inputNumber;
private Button checkButton;
private TextView resultTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
// Call the superclass implementation of onCreate
super.onCreate(savedInstanceState);
// Set the content view of the activity to the layout defined in activity_main.xml
setContentView(R.layout.activity_main);

// Initialize UI components by finding them in the layout using their IDs


inputNumber = (EditText) findViewById(R.id.inputNumber);
checkButton = (Button) findViewById(R.id.checkButton);
resultTextView = (TextView) findViewById(R.id.resultTextView);

// Set a click listener for the check button


checkButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call the method to check if the entered number is prime
checkPrime();
}
});
}

// Method to check whether the entered number is prime or not


@SuppressLint("NewApi")
private void checkPrime() {
// Get the entered number from the EditText
String inputText = inputNumber.getText().toString();

if (!inputText.isEmpty()) {
// Parse the input text to an integer
int number = Integer.parseInt(inputText);

// Check if the number is prime


boolean isPrime = isPrimeNumber(number);

// Display the result


if (isPrime) {
resultTextView.setText(number + " is a prime number.");
} else {
resultTextView.setText(number + " is not a prime number.");
}
} else {
// If the input is empty, display an error message
resultTextView.setText("Please enter a number.");
}
}

// Method to check if a given number is prime


private boolean isPrimeNumber(int n) {
// Prime numbers are greater than 1
if (n <= 1) {
return false;
}

// Check for factors from 2 to the square root of the number


for (int i = 2; i <= Math.sqrt(n); i++) {
// If the number is divisible by any value in the range, it's not prime
if (n % i == 0) {
return false;
}
}

// If no factors are found, the number is prime


return true;
}
}

​ Initialization of UI Components:
● inputNumber: EditText for user input.
● checkButton: Button to trigger the prime checking.
● resultTextView: TextView to display the result.
​ onCreate Method:
● Initializes UI components by finding them using their respective IDs in
the layout.
● Sets a click listener for the checkButton.
​ checkPrime Method:
● Retrieves the entered number from the inputNumber EditText.
● Parses the input to an integer and checks whether it's prime.
● Displays the result in the resultTextView TextView.
​ isPrimeNumber Method:
● Checks if a given number is prime using a basic prime-checking
algorithm.
● Prime numbers are greater than 1, and factors are checked up to the
square root of the number.
​ Annotations:
● @SuppressLint("NewApi"): Suppresses lint warnings for API level
checks related to the use of Math.sqrt.

Prgrm 2 Xml

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<EditText
android:id="@+id/inputNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number" />

<Button
android:id="@+id/generateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/inputNumber"
android:layout_marginTop="16dp"
android:text="Generate Table" />

<TextView
android:id="@+id/resultTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/generateButton"
android:layout_marginTop="16dp" />
</RelativeLayout>

Prgm3 program to toast a message when a button press

MainActivity.java

// MainActivity.java
package com.example.prime;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the Button
Button showToastButton = (Button) findViewById(R.id.showToastButton);

// Set click listener for the button


showToastButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call the method to show the toast message
showToast();
}
});
}

private void showToast() {


// Display a simple toast message
Toast.makeText(this, "Hello, this is a toast message!", Toast.LENGTH_SHORT).show();
}
}

XML

<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:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">

<Button
android:id="@+id/showToastButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_centerInParent="true" />

</RelativeLayout>
Program 4 add 2 numbers

// MainActivity.java
package com.example.prime;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

private EditText number1EditText;


private EditText number2EditText;
private Button addButton;
private TextView resultTextView;

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

// Initialize UI components
number1EditText = (EditText) findViewById(R.id.number1EditText);
number2EditText = (EditText) findViewById(R.id.number2EditText);
addButton = (Button) findViewById((Integer) R.id.addButton);
resultTextView = (TextView) findViewById(R.id.resultTextView);

// Set click listener for the add button


addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call the method to add the numbers
addNumbers();
}
});
}

private void addNumbers() {


// Get the entered numbers from the EditText views
String num1Text = number1EditText.getText().toString();
String num2Text = number2EditText.getText().toString();

if (!num1Text.isEmpty() && !num2Text.isEmpty()) {


// Parse the input texts to doubles
double num1 = Double.parseDouble(num1Text);
double num2 = Double.parseDouble(num2Text);

// Perform the addition


double result = num1 + num2;

// Display the result


resultTextView.setText("Result: " + result);
} else {
// If any input is empty, display an error message
resultTextView.setText("Please enter both numbers.");
}
}
}

Xml

<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:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/number1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter number 1"
android:inputType="numberDecimal" />

<EditText
android:id="@+id/number2EditText"
android:layout_below="@id/number1EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:hint="Enter number 2"
android:inputType="numberDecimal" />

<Button
android:id="@+id/addButton"
android:layout_below="@id/number2EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Add Numbers" />

<TextView
android:id="@+id/resultTextView"
android:layout_below="@id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:textSize="18sp"
android:textStyle="bold" />

</RelativeLayout>

Program 5
// MainActivity.java
package com.example.backgroundimage;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

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

Xml

<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:paddingLeft="16dp"
android:paddingTop="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
tools:context=".MainActivity">

<!-- ImageView for the background image -->


<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable-hdpi/android.jpeg" <!-- Set your background image here -->
android:scaleType="centerCrop"/>

<!-- Your other UI components go here -->

</RelativeLayout>

You might also like