Stack Program for Android

3/17/2012 code-examplejavaandroid_stack_output

# Summary

My first android program showing the stack mechanism.

You can add a new value say Push it in a stack, pop the last entered value.

This stack has a limit of 5.

# Screenshot

Android Stack Output

The Code below only represents the main class that contains the basic logic, the layout is handled using xml files & the android program flow is managed using android manifest file.

# Code

# StackActivity.java

package com.rohansakhale.stack;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class StackActivity extends Activity {
    /** Called when the activity is first created. */
	
	int stack_position = 0;
	int [] value = new int[5]; // Created a stack of size 5
	TextView display;
	Button push,pop;
	EditText getValue;
	TextView operation;
	
	// Push method is used to add the value to our stack and places it at the top	
	public void push(int number)
	{
		value[stack_position] = number;
		operation.setText("Operation: Successfully Added New Value");
		stack_position++;
	}
	
	// Pop method is used to remove the last value entered in the stack
	public void pop()
	{
		stack_position--;
		operation.setText("Operation: Successfully Poped Out: " + value[stack_position]);
		value[stack_position] = 0;	
	}
	
	// Print_stack method is used to print the entire stack
	public void print_stack()
	{
		String temp = "Stack";
		for(int i = stack_position-1;i >= 0 ; i--)
		{
			temp = temp + "\n" + value[i];
		}
		display.setText(temp);
	}
	
	// Displays error or success message
	public void print_error(String err)
	{		
		operation.setText("Operation: " + err);
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Initialize our data members
        display = (TextView) findViewById(R.id.display);
        push = (Button) findViewById(R.id.push);
        pop = (Button) findViewById(R.id.pop);
        getValue = (EditText) findViewById(R.id.editText1);
        operation = (TextView) findViewById(R.id.operationDisplay);
        
        // Handling the User Clicking Events 
        push.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(!getValue.getText().toString().equals(""))
				{
					if(stack_position < 5)
					{
						push(Integer.parseInt(getValue.getText().toString()));						
						print_stack();
					}	
					else
					{
						 print_error("Stack is full");
					}	
					getValue.setText("");
				}
				else
				{
					print_error("Entered some value in Text Field");
				}
			}
		});
        
        pop.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {

					if(stack_position > 0)
					{
						pop();					
						print_stack();					
					}	
					else
					{
						print_error("Stack is Empty");
					}					
			}
		});
        
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107

# main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >



    <TextView
        android:id="@+id/display"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textSize="30dp" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/push"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/push"
        android:textSize="25dp" />

    <Button
        android:id="@+id/pop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/pop"
        android:textSize="25dp" />

    <TextView
        android:id="@+id/operationDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/operation" />

</LinearLayout>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45