This video shows the steps to program an Android App in Android Studio to sort a given list of Strings. This video shows the steps to create a a very simple example in which it makes a simple layout with EditText, TextView and a button. EditText Widget is used to get the inputs from the user in multi-line plain text format. TextView shows the final output of Sorted String.
We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
package com.example.mysrtingsort;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {
private EditText myEditText;
private TextView myTextView;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);myEditText = findViewById(R.id.editText);
myTextView = findViewById(R.id.TextView);
}@RequiresApi(api = Build.VERSION_CODES.O)
public void mySortString(View view){
String myString = myEditText.getText().toString();
String myStringList[] = myString.split(“\n”);
String mySortedString[] = myStringList.clone();int i, j, k;
try {
for(i=1; i < myStringList.length; i++){
for (j=0;j<i;j++){
if(myStringList[i].compareTo(mySortedString[j]) < 0){
break;
}
}
if(j==i){
mySortedString[j] = myStringList[i];
} else {
for(k = i; k > j ; k–){
mySortedString[k] = mySortedString[k-1];
}
mySortedString[j] = myStringList[i];
}
}
}
catch (Exception e){
e.printStackTrace();
}myTextView.setText(String.join(“\n”, mySortedString));
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.mysrtingsort”><application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” /><category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application></manifest>
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”><TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”100dp”
android:layout_marginTop=”400dp”
android:text=”@string/output_text_here”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” /><EditText
android:id=”@+id/editText”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”100dp”
android:layout_marginTop=”120dp”
android:ems=”10″
android:gravity=”start|top”
android:hint=”@string/input_text_here”
android:inputType=”textMultiLine”
android:autofillHints=”” /><Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”200dp”
android:layout_marginTop=”20dp”
android:text=”@string/sort” /></RelativeLayout>