How to check edit text values are Anagram or Not and Count Occurrences of Anagrams in android?

AndroidMobile DevelopmentApps/Applications

<p>This example demonstrate about How to check edit text values are Anagram or Not and Count Occurrences of Anagrams in android.</p><p><strong>Step 1</strong> &minus; Create a new project in Android Studio, go to File &rArr; New Project and fill all required details to create a new project.</p><p><strong>Step 2</strong> &minus; Add the following code to res/layout/activity_main.xml.</p><pre class="prettyprint notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt; &lt;LinearLayout xmlns:android=&quot;; &nbsp; &nbsp;xmlns:app=&quot;; &nbsp; &nbsp;xmlns:tools=&quot;; &nbsp; &nbsp;android:layout_width=&quot;match_parent&quot; &nbsp; &nbsp;android:gravity=&quot;center&quot; &nbsp; &nbsp;android:layout_height=&quot;match_parent&quot; &nbsp; &nbsp;tools:context=&quot;.MainActivity&quot; &nbsp; &nbsp;android:orientation=&quot;vertical&quot;&gt; &nbsp; &nbsp;&lt;EditText &nbsp; &nbsp; &nbsp; android:id=&quot;@+id/edit_query&quot; &nbsp; &nbsp; &nbsp; android:layout_width=&quot;match_parent&quot; &nbsp; &nbsp; &nbsp; android:layout_height=&quot;wrap_content&quot; /&gt; &nbsp; &nbsp;&lt;EditText &nbsp; &nbsp; &nbsp; android:id=&quot;@+id/edit_query1&quot; &nbsp; &nbsp; &nbsp; android:layout_width=&quot;match_parent&quot; &nbsp; &nbsp; &nbsp; android:layout_height=&quot;wrap_content&quot; /&gt; &nbsp; &nbsp;&lt;Button &nbsp; &nbsp; &nbsp; android:id=&quot;@+id/buttonPanel&quot; &nbsp; &nbsp; &nbsp; android:text=&quot;Button&quot; &nbsp; &nbsp; &nbsp; android:layout_width=&quot;match_parent&quot; &nbsp; &nbsp; &nbsp; android:layout_height=&quot;wrap_content&quot;&gt;&lt;/Button&gt; &lt;/LinearLayout&gt;</pre><p>In the above code, we have taken two edit texts to take the data from user and when user click on button it will give result on toast.</p><p><strong>Step 3</strong> &minus; Add the following code to src/</p><pre class="prettyprint notranslate">package com.example.myapplication; import android.os.Build; import android.os.Bundle; import; import; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { &nbsp; &nbsp;EditText edit_query1,edit_query; &nbsp; &nbsp;String first,second; &nbsp; &nbsp;@RequiresApi(api = Build.VERSION_CODES.P) &nbsp; &nbsp;@Override &nbsp; &nbsp;protected void onCreate(Bundle savedInstanceState) { &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState); &nbsp; &nbsp; &nbsp; setContentView(R.layout.activity_main); &nbsp; &nbsp; &nbsp; edit_query1 = findViewById(; &nbsp; &nbsp; &nbsp; edit_query = findViewById(; &nbsp; &nbsp; &nbsp; findViewById( View.OnClickListener() { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Override &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void onClick(View v) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!edit_query.getText().toString().isEmpty() &amp;&amp; !edit_query1.getText().toString().isEmpty()) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;first = edit_query.getText().toString().trim(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;second = edit_query1.getText().toString().trim(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(araAnagram(first,second)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;countAnagrams(second,&quot;for&quot;); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; }); &nbsp; &nbsp;} &nbsp; &nbsp;private void countAnagrams(String first, String rea) { &nbsp; &nbsp; &nbsp; int res = 0; &nbsp; &nbsp; &nbsp; String s = null; &nbsp; &nbsp; &nbsp; int N = first.length(); &nbsp; &nbsp; &nbsp; int n = rea.length(); &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt;= N - n; i++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s = first.substring(i, i + n); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (araAnagram(rea.trim(), s)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; res++; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; Toast.makeText(MainActivity.this,&quot;&quot;+res,Toast.LENGTH_LONG).show(); &nbsp; &nbsp;} &nbsp; &nbsp;private boolean araAnagram(String first, String second) { &nbsp; &nbsp; &nbsp; boolean value=false; &nbsp; &nbsp; &nbsp; char[] firstchar = first.toCharArray(); &nbsp; &nbsp; &nbsp; char[] secondchar = second.toCharArray(); &nbsp; &nbsp; &nbsp; for(int i = 0; i&lt;firstchar.length;i++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for (int j = 0; j &lt; secondchar.length; j++) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (firstchar[i] == secondchar[j]) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value = true; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp;} &nbsp; &nbsp;return value; &nbsp; &nbsp;} }</pre><p>Let&#39;s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project&#39;s activity files and click Run <img src="" class="fr-fic fr-dii" width="14" height="14">&nbsp;icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen &minus;</p><p><img src="" class="fr-fic fr-dib" style="width:278px; height:556px" width="278" height="556"></p><p>Click <a href="/android/projects/how_to_check_edit_text_values_are_anagram_or_not_and_count_occurrences_of_anagrams_in_android/" rel="nofollow" target="_blank">here</a> to download the project code</p>
Updated on 26-Jun-2020 12:44:06