0% found this document useful (0 votes)
2 views3 pages

Android

The document contains two Android application examples. The first example reverses a string input by the user and displays the result, while the second example changes the background color of a layout when a button is clicked. Both examples include Java code and corresponding XML layout files.

Uploaded by

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

Android

The document contains two Android application examples. The first example reverses a string input by the user and displays the result, while the second example changes the background color of a layout when a button is clicked. Both examples include Java code and corresponding XML layout files.

Uploaded by

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

string

public class MainActivity extends AppCompatActivity {

EditText inputText;
TextView resultText;
Button reverseButton;

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

inputText = findViewById(R.id.inputText);
resultText = findViewById(R.id.resultText);
reverseButton = findViewById(R.id.reverseButton);

reverseButton.setOnClickListener(v -> {
String text = inputText.getText().toString();
String reversedText = new StringBuilder(text).reverse().toString();
resultText.setText("Reversed String: " + reversedText);
});
}
}

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<!-- Input field for string -->


<EditText
android:id="@+id/inputText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter text to reverse"
android:textSize="18sp" />

<!-- Button to trigger reverse action -->


<Button
android:id="@+id/reverseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reverse String"
android:layout_gravity="center"
android:layout_marginTop="20dp" />

<!-- TextView to display result -->


<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reversed String: "
android:textSize="18sp"
android:layout_gravity="center"
android:layout_marginTop="20dp" />

</LinearLayout>

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

background

package com.example.backgroundcolorchange;

import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.constraintlayout.widget.ConstraintLayout;

public class MainActivity extends AppCompatActivity {

ConstraintLayout mainLayout; // Root layout reference


Button changeColorButton; // Button reference

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

// Find the root layout and button by their IDs


mainLayout = findViewById(R.id.mainLayout);
changeColorButton = findViewById(R.id.changeColorButton);

// Set an OnClickListener for the button to change the background color


changeColorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Change the background color dynamically to a random color
mainLayout.setBackgroundColor(Color.parseColor("#FFBB86FC")); //
Light purple
}
});
}
}

<?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:id="@+id/mainLayout" <!-- Root layout ID -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">

<Button
android:id="@+id/changeColorButton" <!-- Button ID -->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Background Color"
android:layout_marginTop="100dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:textSize="18sp" />
</androidx.constraintlayout.widget.ConstraintLayout>

You might also like