2

MainActivity's code

How can I get values from the text boxes and the qty box in that user will input the value. price will come from server and than i want to calculate the bill and show it in next layout. plz give me some easy explanation as i am not expert in it. thanks in advance.

MainActivity .java

package com.example.myone;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
     EditText textIn;
     Button buttonAdd;
     LinearLayout container;
     private Button done;
     private TextView tv;
     @Override
     protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              textIn = (EditText)findViewById(R.id.textin);
              buttonAdd = (Button)findViewById(R.id.add);
              container = (LinearLayout)findViewById(R.id.container);
              done = (Button) findViewById(R.id.cal);
              tv= (TextView) findViewById(R.id.tv);

             buttonAdd.setOnClickListener(new OnClickListener(){

              @Override
               public void onClick(View arg0) {
                LayoutInflater layoutInflater = 
                  (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater.inflate(R.layout.row, null);
                TextView textOut = (TextView)addView.findViewById(R.id.textout);
                textOut.setText(textIn.getText().toString());

                //(int1);

                Button buttonRemove = (Button)addView.findViewById(R.id.remove);
                buttonRemove.setOnClickListener(new OnClickListener(){

                 @Override
                 public void onClick(View v) {
                  ((LinearLayout)addView.getParent()).removeView(addView);
                 }});
                container.addView(addView);
               }});

      }

}

MainActivity.xml

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:background="@drawable/mainbg"
    tools:context=".MainActivity" >


  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/textin"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="Enter your item here"
            />
        <Button 
            android:id="@+id/cal"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_below="@+id/textin"
            android:text="Done!"
            android:layout_alignParentLeft="true"
            />
        <Button 
            android:id="@+id/add"
            android:text="Add"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignBaseline="@+id/cal"
            />

    </RelativeLayout>
    <LinearLayout 
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    </LinearLayout>

</LinearLayout>

Row.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="@drawable/borderbg"
    android:orientation="horizontal"
    android:id="@+id/rel"
    android:layout_height="wrap_content">

    <TextView 
        android:id="@+id/textout"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:hint="Item"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
                />

    <EditText
        android:id="@+id/qty"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_alignTop="@+id/price"
        android:layout_toRightOf="@+id/price"
        android:layout_marginLeft="50dp"
        android:textSize="12sp"
        android:hint="Qty" />

    <TextView
        android:id="@+id/price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textout"
        android:layout_alignBottom="@+id/textout"
        android:layout_marginLeft="42dp"
        android:layout_toRightOf="@+id/textout"

        android:hint="Price" />

    <Button
        android:id="@+id/remove"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="left"
        android:text="remove" />

</RelativeLayout>

Simply I want to create a basic shopping app that have 20 products. the price and product name will come from server and quantity will input by user so how do I that?

3

2 Answers 2

9

You are looking for something like this

int count = container.getChildCount(); 
for (int i = 0; i < count; i++) {
   final View row = container.getChildAt(i);
   TextView textOut = (TextView)row.findViewById(R.id.textout);
   String data = textOut.getText().toString();
}

Where container is your LinearLayout in which you have added your inflated rows.

1
3

Try this:-

To take user input from text box:-

int qty = Integer.parseInt(qtyTextBox.getText().toString());

Caluculate bill:-

double bill =  price*qty;

Send bill value to next activity:-

Intent in = new Intent();
in.putExtra("Bill", bill);
startActivity(in);

Get bill value in next activity:-

Intent in = getIntent();
int mBill = in.getExtra("Bill");
string bill = String.valueOf(mBill);

Now you can set the value of 'bill' to the TextView where you want to display it.

4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.