0% found this document useful (0 votes)
19 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)
19 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

string

public class MainActivity extends AppCompatActivity {

EditText inputText;
TextView resultText;
Button reverseButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

inputText = findViewById([Link]);
resultText = findViewById([Link]);
reverseButton = findViewById([Link]);

[Link](v -> {
String text = [Link]().toString();
String reversedText = new StringBuilder(text).reverse().toString();
[Link]("Reversed String: " + reversedText);
});
}
}

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


<LinearLayout
xmlns:android="[Link]
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 [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

ConstraintLayout mainLayout; // Root layout reference


Button changeColorButton; // Button reference

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

// Find the root layout and button by their IDs


mainLayout = findViewById([Link]);
changeColorButton = findViewById([Link]);

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


[Link](new [Link]() {
@Override
public void onClick(View v) {
// Change the background color dynamically to a random color
[Link]([Link]("#FFBB86FC")); //
Light purple
}
});
}
}

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


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>

You might also like