• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Need help with discount calculator

I am a newbie in android dev.
I want to develop a very small and simple discount rate calculator.
in which there will be drop down menu with options like:

calculate 10%
calculate 20%
calculate 30%

Based on the selection of option from the drop down menu the discount rate should change.

After selection, there will be a textbox which should ask user to enter price.
and on the click even of "Calculate" button, the resultant amount (discounted rate) should be displayed.

I guess it is very simple for you guys. If anyone can help me with the code, i will deeply appreciate.

thank you
 
ok this is what I have done till now:

I created an Android Project names as :MyDC

MyDC.java

Code:
package com.super.mydc;



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

public class MyDC extends Activity {
    
    private EditText amount1;
    private double x=0;
      private double y=2.0;
      private double z=0;
      private TextView tt;
      private Button calculate;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // We want to view some very simple text, so we need a TextView
        TextView tv = new TextView(this);
        // Put some text to the newly created TextVIew
        tv.setText("Test");
        // Tell our Application to display the textView
        
       
        this.setContentView(tv);
        
        super.onCreate(icicle);
        setContentView(R.layout.main);
        initControls();
        
            }
    
    private void initControls()
    {
        amount1=(EditText)findViewById(R.id.amount1);
        
        tt=(TextView)findViewById(R.id.tt);
        calculate=(Button)findViewById(R.id.calculate);
        calculate.setOnClickListener(new Button.OnClickListener()
        {public void onClick
        (View  v) { calculate();}});
    }
    
    private void calculate()
    {
        x=Double.parseDouble(amount1.getText().toString());
        z=x-(x*y/100);
        tt.setText(Double.toString(z));
    }
}


This is main.xml

Code:
<?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"
    >
    <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter the price"
    />
    <EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
    <Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

<EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
    
</LinearLayout>


This is string.xml

Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hi, MyDC!</string>
    <string name="app_name">My, DC</string>
    <string name="spinner1">Choose discount type</string>
   
    
</resources>

This is what I am able to get:

ozscreen.jpg



I am able to calculate discount on the button click, BUT i have hardcoded the discount rate.

I want to select discount rate based on the dropdown menu (I don't know how to set dropdown values with different discount rate in this problem)

In the dropdown menu I would like to have values like this:
Type 1 discount
Type 2 discount
Type 3 discount

Here is the respective discount rate for the above drop-down values:
Type 1 discount - 10%
Type 2 discount - 15%
Type 3 discount - 18%

PLZ HELP ME

 
thanks for the link, I will try that code.

Could you PLEASE help me with rest of the code for this problem ? Even if I am able to get the dropdown menu working, I don't know how to make it work in the logic for calculating discount.

I will deeply appreciate any kind of help.
 
Ok somehow I was able to add spinner tool on my application.

Now the question is how to select discount rate based on selected value from spinner.

PLZ HELP ME WITH THE CODE

Here is my current code:

MyDC.java

Code:
package com.super.mydc;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Button;

import android.view.View;

public class MyDC extends Activity {
    
    private EditText amount1;
    private double x=0;
      private double y=2.0;
      private double z=0;
      private TextView tt;
      private Button calculate;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // We want to view some very simple text, so we need a TextView
        TextView tv = new TextView(this);
        // Put some text to the newly created TextVIew
        tv.setText("Test");
        // Tell our Application to display the textView
        
       
        this.setContentView(tv);
        
        super.onCreate(icicle);
        setContentView(R.layout.main);
        
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.planets_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        
        
        initControls();
        
        
            }
    
    
    
    private void initControls()
    {
        amount1=(EditText)findViewById(R.id.amount1);
        
        tt=(TextView)findViewById(R.id.tt);
        calculate=(Button)findViewById(R.id.calculate);
        calculate.setOnClickListener(new Button.OnClickListener()
        {public void onClick
        (View  v) { calculate();}});
    }
    
    private void calculate()
    {
        x=Double.parseDouble(amount1.getText().toString());
        z=x-(x*y/100);
        tt.setText(Double.toString(z));
    }
}



main.xml


Code:
<?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"
    >
    <Spinner android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/spinner1"></Spinner>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:text="@string/planet_prompt"
    />
    <Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/planet_prompt"
    />
    
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Enter the price"
    />
    <EditText android:layout_height="wrap_content" android:id="@+id/amount1" android:text="" android:layout_width="match_parent"></EditText>
    <Button android:text="Calculate Result" android:id="@+id/calculate" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

<EditText android:layout_height="wrap_content" android:id="@+id/tt" android:text="" android:layout_width="match_parent"></EditText>
    
</LinearLayout>


strings.xml

Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hi, MyDC!</string>
    <string name="app_name">My, DC</string>
    <string name="spinner1">Choose a planet</string>
   
    <string name="planet_prompt">Choose a planet</string>
    <string-array name="planets_array">
        <item>10% discount A</item>
        <item>15% discount B</item>
        <item>18% discount C</item>
        
    </string-array>
    
</resources>
 
Back
Top Bottom