0% found this document useful (0 votes)
5 views14 pages

3 Android

The document contains multiple Android application examples demonstrating different functionalities. It includes a color-changing app with buttons, a custom view for drawing shapes, and animations for text effects. Each section provides code snippets for activities and XML layouts to implement these features.

Uploaded by

mbs598678
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)
5 views14 pages

3 Android

The document contains multiple Android application examples demonstrating different functionalities. It includes a color-changing app with buttons, a custom view for drawing shapes, and animations for text effects. Each section provides code snippets for activities and XML layouts to implement these features.

Uploaded by

mbs598678
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/ 14

package com.example.

kumar;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


Button b1, b2, b3, b4;

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

b1 = findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setMyscreencolor(Color.GREEN);
}
});

b2 = findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setMyscreencolor(Color.BLUE);
}
});

b3 = findViewById(R.id.button3);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setMyscreencolor(Color.WHITE);
}
});

b4 = findViewById(R.id.button4);
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setMyscreencolor(Color.YELLOW);
}
});
}

public void setMyscreencolor(int color) {


// Set the background color of the entire screen
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
}

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


<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="red">#FF0000</color>
<color name="yellow">#FFFF00</color>
<color name="blue">#0000FF</color>
<color name="green">#00FF00</color>
</resources>
2d shape

package com.example.myapplication;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyCustomView(this)); // Set custom view
}

// Custom view class


public class MyCustomView extends View {

public MyCustomView(MainActivity context) {


super(context);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

int x = getWidth(); // Width of the canvas


int y = getHeight(); // Height of the canvas

// Paint object for drawing shapes


Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);

// Draw Circle (Red)


paint.setColor(Color.RED); // Set color to red
int radius = 100;
canvas.drawCircle(x / 4, y / 4, radius, paint); // Circle at top-left
// Draw Rectangle (Green)
paint.setColor(Color.GREEN); // Set color to green
int rectLeft = x / 2;
int rectTop = y / 4;
int rectRight = rectLeft + 300;
int rectBottom = rectTop + 150;
canvas.drawRect(rectLeft, rectTop, rectRight, rectBottom, paint); // Rectangle at
middle-top

// Draw Square (Blue)


paint.setColor(Color.BLUE); // Set color to blue
int sideLength = 150;
canvas.drawRect(x / 4, y / 2, x / 4 + sideLength, y / 2 + sideLength, paint); // Square at
bottom-left
}
}
}
Animation

package com.example.myapplication;

import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

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

// Find the TextView from the layout


TextView animatedText = findViewById(R.id.animatedText);

// Apply AlphaAnimation for fade-in effect


AlphaAnimation fadeIn = new AlphaAnimation(0.0f, 1.0f); // Start from transparent to
opaque
fadeIn.setDuration(2000); // Duration: 2 seconds
fadeIn.setFillAfter(true); // Keep the text visible after animation ends

// Apply TranslateAnimation for moving the text


TranslateAnimation move = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, -1.0f, // Start from the left
Animation.RELATIVE_TO_PARENT, 0.0f, // End at the original position
Animation.RELATIVE_TO_PARENT, 0.0f, // No vertical movement
Animation.RELATIVE_TO_PARENT, 0.0f); // No vertical movement

move.setDuration(2000); // Duration: 2 seconds


move.setFillAfter(true); // Keep the final position after animation

// Start both animations on the TextView


animatedText.startAnimation(fadeIn); // Fade-in effect
animatedText.startAnimation(move); // Move effect
}
}

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

<TextView
android:id="@+id/animatedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Android Animation!"
android:textSize="24sp"
android:textColor="#000000"
android:layout_centerInParent="true"/>
</RelativeLayout>
package com.example.myapplication;

import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


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

// Find the TextView from the layout


TextView animatedText = findViewById(R.id.animatedText);

// 1. Zoom Animation (Scale)


ScaleAnimation zoom = new ScaleAnimation(
1.0f, 2.0f, // Scale X axis from 1.0 to 2.0 (zoom in)
1.0f, 2.0f, // Scale Y axis from 1.0 to 2.0 (zoom in)
Animation.RELATIVE_TO_SELF, 0.5f, // Pivot point X (center of the TextView)
Animation.RELATIVE_TO_SELF, 0.5f); // Pivot point Y (center of the TextView)

zoom.setDuration(1000); // Duration: 1 second


zoom.setRepeatCount(Animation.INFINITE); // Repeat the animation indefinitely
zoom.setRepeatMode(Animation.REVERSE); // Reverse the animation (zoom in and out)

// 2. Blink Animation (Alpha fade in and out)


AlphaAnimation blink = new AlphaAnimation(1.0f, 0.0f); // Fade from opaque to
transparent
blink.setDuration(500); // Duration: 0.5 second
blink.setRepeatCount(Animation.INFINITE); // Repeat the animation indefinitely
blink.setRepeatMode(Animation.REVERSE); // Fade in and out (blink effect)

// Start both animations on the TextView


animatedText.startAnimation(zoom); // Zoom effect
animatedText.startAnimation(blink); // Blink effect
}
}

<?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">
<TextView
android:id="@+id/animatedText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zoom and Blink Animation!"
android:textSize="24sp"
android:textColor="#000000"
android:layout_centerInParent="true"/>
</RelativeLayout>

You might also like