Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Ron Nico De Castro

BSIT302C

Input text

After pressing swap

After pressing check/compare


Mainactivity code

package com.example.swapanddisplay;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

EditText editText1, editText2;

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

editText1 = findViewById(R.id.editText1);
editText2 = findViewById(R.id.editText2);

Button swapButton = findViewById(R.id.swapButton);


Button compareButton = findViewById(R.id.compareButton);

swapButton.setOnClickListener(v -> {
String temp = editText1.getText().toString();
editText1.setText(editText2.getText().toString());
editText2.setText(temp);
});
compareButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
String text1 = editText1.getText().toString().trim();
String text2 = editText2.getText().toString().trim();

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

if (text1.equals(text2)) {
resultTextView.setText("Same");
} else {
resultTextView.setText("Not the Same");
}
}
});
}
}

activity 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"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text Field 1"
tools:ignore="Autofill,HardcodedText,TextFields" />

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@id/editText1"
android:hint="Text Field 2"
tools:ignore="Autofill,HardcodedText,TextFields" />

<Button
android:id="@+id/swapButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText2"
android:text="Swap Text"
tools:ignore="HardcodedText" />

<Button
android:id="@+id/compareButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/swapButton"
android:text="Compare Text"
tools:ignore="HardcodedText" />

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

</RelativeLayout>

Manifest code

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SwapAndDisplay">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>

You might also like