Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.textjumble;
- import android.app.AlertDialog;
- import android.os.Bundle;
- import android.content.DialogInterface;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- import androidx.appcompat.app.AppCompatActivity;
- import java.util.ArrayList;
- import java.util.List;
- public class MainActivity extends AppCompatActivity implements OnClickListener
- {
- private Button exitButton;
- private EditText nameEntry;
- private String name;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- exitButton = (Button)findViewById(R.id.exitButton);
- nameEntry = (EditText)findViewById(R.id.nameEntry);
- nameEntry.setWidth(120);
- exitButton.setOnClickListener(this);
- nameEntry.setFocusable(true);
- }// End of onCreate
- @Override
- public void onClick(View v)
- {
- // Check for exit button. Pop up dialogue if found
- if (v == exitButton)
- {
- name = nameEntry.getText().toString();
- showtbDialog(name);
- }
- }// End of onClick
- private void showtbDialog(String s)
- {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- char [] array = s.toCharArray();
- int len = s.length();
- for (int i = 0; i < 10; i++)
- {
- int max = array.length;
- int min = 1;
- int a = (int)(Math.random() * ((max - min) + 1)) + min;
- int b = (int)(Math.random() * ((max - min) + 1)) + min;
- a--;
- b--;
- char temp = array[a];
- array[a] = array[b];
- array[b] = temp;
- }
- String newS = new String (array);
- builder.setMessage(newS);
- builder.setCancelable(true);
- AlertDialog alert = builder.create();
- alert.show();
- }
- }// End of Activity class
- -------------------------------------------------------------------
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Welcome to Mobile Device Programming"
- />
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- >
- <TextView
- android:id="@+id/nameLabel"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Enter string to jumble "
- />
- <EditText
- android:id="@+id/nameEntry"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:inputType="text"
- android:text=""
- />
- <requestFocus />
- </LinearLayout>
- <Button
- android:id="@+id/exitButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Press to Jumble"
- />
- </LinearLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement