Sunday 2 February 2014

Post 13: Learning Sorting Algorithm App

Today I recaptured my knowledge about implementing a simple Android App by creating an App with which you can learn sorting Algorithm.

In the main Screen it has a list of various sorting Algorithm.


By clicking on one of these you can see the definition of the selected Sorting Algorithm:

 
The MainActivity.class:
package com.example.appsearchalogrithm;

import com.example.options.BubbleSortActivity;
import com.example.options.BucketSortActivity;
import com.example.options.HeapSortActivity;
import com.example.options.InsertionSortActivity;
import com.example.options.MergeSortActivity;
import com.example.options.QuickSortActivity;
import com.example.options.RadixSortActivity;
import com.example.options.SelectionSortActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  Button bBubbleSort=(Button)findViewById(R.id.bBubbleSort);
  Button bSelectionSort=(Button)findViewById(R.id.bSelectionSort);
  Button bInsertionSort=(Button)findViewById(R.id.bInsertionSort);
  Button bBucketSort=(Button)findViewById(R.id.bBucketSort);
  Button bRadixSort=(Button)findViewById(R.id.bRadixSort);
  Button bQuickSort=(Button)findViewById(R.id.bQuicksort);
  Button bMergeSort=(Button)findViewById(R.id.bMergeSort);
  Button bHeapSort=(Button)findViewById(R.id.bHeapSort);
  
  bBubbleSort.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent;
    intent=new Intent(MainActivity.this, BubbleSortActivity.class);
    startActivity(intent);
   }
   
  });
  
  bSelectionSort.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent;
    intent=new Intent(MainActivity.this, SelectionSortActivity.class);
    startActivity(intent);
   }
 
  });
  
  bInsertionSort.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent;
    intent=new Intent(MainActivity.this, InsertionSortActivity.class);
    startActivity(intent);
   }
   
  });
  
  bBucketSort.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent;
    intent=new Intent(MainActivity.this, BucketSortActivity.class);
    startActivity(intent);
   }
   
  });
  
  bRadixSort.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent;
    intent=new Intent(MainActivity.this, RadixSortActivity.class);
    startActivity(intent);
   }
   
  });
  bQuickSort.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent;
    intent=new Intent(MainActivity.this, QuickSortActivity.class);
    startActivity(intent);
   }
   
  });
  bMergeSort.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent;
    intent=new Intent(MainActivity.this, MergeSortActivity.class);
    startActivity(intent);
   }
   
  });
  bHeapSort.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent;
    intent =new Intent(MainActivity.this, HeapSortActivity.class);
    startActivity(intent);
   }
   
  });
  
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

}


The option class, here as an example the BubblesortActivity.class: it basically only implements the scrolling Method
package com.example.options;

import com.example.appsearchalogrithm.R;
import com.example.appsearchalogrithm.R.layout;
import com.example.appsearchalogrithm.R.menu;

import android.os.Bundle;
import android.app.Activity;
import android.text.method.ScrollingMovementMethod;
import android.view.Menu;
import android.widget.TextView;

public class BubbleSortActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_bubble_sort);
  
  TextView tv=(TextView)findViewById(R.id.tvBubble);
  tv.setMovementMethod(new ScrollingMovementMethod());
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.bubble_sort, menu);
  return true;
 }

}


Here's the layout. Again as an example the layout for the bubble layout. The most important part is that implements a "scrollbars" attribute, so if the text is too long you can scroll the text. Furthermore the text-size as well as the text it self is in the strings.xml respectively dimens.xml.


    




The Strings.xml isn't that interesting. But the dimens.xml is a tad more interesting, so I'll post them here. What is interesting is that the text size is defined as "sp". This makes the text size independet from the device, whether it's Tablet, Smartphone, Phablet or what have you.


    
    16dp
    16dp
 50sp



I pushed the whole source code on here on GitHub.

P.S.: I also made an App for learning Stack and Queue. It's similar to the one above:
The code of this App can be found here.

You can  also download the app on your mobile device here:

Opera Mobile Store

No comments:

Post a Comment

Tweet