def __init__(self, context, locationHandler):

        RelativeLayout.__init__(self, context)

        self.currentItem = -1
        self.mode = "normal"

        self.locationHandler = locationHandler
        self.adapter = self.getAdapter()

        self.listView = ListView(context)
        self.listView.setAdapter(self.adapter)
        self.listView.setOnItemClickListener(self)
        self.listView.setOnItemLongClickListener(self)

        self.addWidget = AddWidget(context, self)
        self.addWidget.setId(1)
        self.removeWidget = RemoveWidget(context, self)

        listParams = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        listParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)
        listParams.addRule(RelativeLayout.ABOVE, 1)

        self.addView(self.listView, listParams)
        self.addView(self.addWidget, self.getAddParams())
示例#2
0
文件: app.py 项目: jvmaia/todoapp-voc
    def __init__(self, value, context, layout=None, callback=None):
        self.callback = callback
        self.context = context
        self.value = value
        if layout:
            self.layout = layout
            self.checkbox = layout.getChildAt(0)
            self.text_view = layout.getChildAt(1)
        else:
            self.layout = LinearLayout(self.context)
            self.checkbox = CheckBox(self.context)
            self.checkbox.setOnClickListener(OnClick(self.update))
            self.layout.addView(self.checkbox)

            print(value)
            self.text_view = StrikeableTextView(self.context,
                                                striked=value['finished'])
            self.text_view.setTextSize(25)
            self.layout.addView(self.text_view)

            self.button_delete = Button(self.context)
            self.button_delete.setOnClickListener(OnClick(self.delete))
            self.button_delete.getBackground().setColorFilter(
                0xffff4444, PorterDuff.Mode.MULTIPLY)
            self.button_delete.setText('X')
            relative1 = RelativeLayout(
                self.context)  # relative inside horizontal layout
            relative1.addView(self.button_delete, _create_layout_params())
            self.layout.addView(relative1)

        self.text_view.setText(value['title'])
        self.checkbox.setChecked(value['finished'])
示例#3
0
    def __init__(self, sale, context, callback=None, back=None):
        self.sale = sale
        self.back = back
        self.context = context
        self.callback = callback
        self.layout = LinearLayout(self.context)

        self.button_details = Button(self.context)
        self.button_details.setOnClickListener(ButtonClick(self.details))
        self.button_details.setText('+')
        self.button_details.getBackground().setColorFilter(
            0xff9e9e9e, PorterDuff.Mode.MULTIPLY)
        self.layout.addView(self.button_details)

        self.text_view = StrikeableTextView(self.context, striked=sale['paid'])
        self.text_view.setText(self.sale['person'])
        self.text_view.setTextSize(22)
        self.layout.addView(self.text_view)

        self.button_pay = Button(self.context)
        self.button_pay.setOnClickListener(ButtonClick(self.pay))
        self.button_pay.setText('V')
        self.button_pay.getBackground().setColorFilter(
            0xff8bc34a, PorterDuff.Mode.MULTIPLY)

        relative1 = RelativeLayout(self.context)
        relative1.addView(self.button_pay, _create_layout_params('right'))

        self.layout.addView(relative1)
示例#4
0
    def __init__(self, product, context, callback=None):
        self.product = product
        self.context = context
        self.callback = callback

        self.layout = LinearLayout(self.context)
        self.text_view = TextView(self.context)
        self.text_view.setTextSize(20)
        self.text_view.setText('%s | %d | R$%.2f' %
                               (self.product['name'], self.product['quantity'],
                                self.product['value']))
        self.layout.addView(self.text_view)

        hbuttons = LinearLayout(self.context)
        hbuttons.setOrientation(LinearLayout.HORIZONTAL)

        self.add_button = Button(self.context)
        self.add_button.setOnClickListener(ButtonClick(self.add))
        self.add_button.getBackground().setColorFilter(
            0xff8bc34a, PorterDuff.Mode.MULTIPLY)
        self.add_button.setText('+')
        hbuttons.addView(self.add_button)

        self.remove_button = Button(self.context)
        self.remove_button.setOnClickListener(ButtonClick(self.remove))
        self.remove_button.getBackground().setColorFilter(
            0xffff0000, PorterDuff.Mode.MULTIPLY)
        self.remove_button.setText('-')
        hbuttons.addView(self.remove_button)

        relative = RelativeLayout(self.context)
        relative.addView(hbuttons, _create_layout_params('right'))

        self.layout.addView(relative)
示例#5
0
 def add_return_button(self, view, value=None, flayout=True):
     self.return_button = Button(self._activity)
     self.return_button.setOnClickListener(
         ButtonClick(self.return_view, view, value=value))
     self.return_button.setText('Return')
     self.relative_rb = RelativeLayout(self._activity)
     self.relative_rb.addView(self.return_button,
                              _create_layout_params('bottom'))
     if flayout:
         self.flayout.addView(self.relative_rb)
     else:
         self.vlayout.addView(self.relative_rb)
    def getAddParams(self):

        addParams = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        addParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
        addParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
        addParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
        return addParams
示例#7
0
    def __init__(self, client, context, callback=None):
        self.client = client
        self.callback = callback
        self.context = context
        self.layout = LinearLayout(self.context)

        self.text_view = TextView(self.context)
        self.text_view.setText(self.client)
        self.text_view.setTextSize(22)
        self.layout.addView(self.text_view)

        self.add_button = Button(self.context)
        self.add_button.setOnClickListener(ButtonClick(self.view_sales))
        self.add_button.getBackground().setColorFilter(
            0xff9e9e9e, PorterDuff.Mode.MULTIPLY)
        self.add_button.setText('+')
        relative = RelativeLayout(self.context)
        relative.addView(self.add_button, _create_layout_params('right'))
        self.layout.addView(relative)
示例#8
0
def _create_layout_params(side):
    params = RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT)
    if side == 'right':
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
    elif side == 'bottom':
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)

    return params
示例#9
0
文件: app.py 项目: jvmaia/todoapp-voc
    def onCreate(self):
        print('Starting TodoApp')
        self.dbitems = self.db.fetch_items()

        if not self.dbitems:
            print('populating DB')
            self._populate_db()
            self.dbitems = self.db.fetch_items()

        print('dbitems', self.dbitems)

        hlayout = LinearLayout(self._activity)
        hlayout.setOrientation(LinearLayout.HORIZONTAL)

        self.entry_text = EditText(self._activity)
        self.entry_text.setHint('Enter a new item...')
        hlayout.addView(self.entry_text)

        button_create = Button(self._activity)
        button_create.setText('Add')
        button_create.setOnClickListener(OnClick(self.create_item))

        rlayout = RelativeLayout(self._activity)
        rlayout.addView(button_create, _create_layout_params())
        hlayout.addView(rlayout)

        vlayout = LinearLayout(self._activity)
        vlayout.setOrientation(LinearLayout.VERTICAL)
        vlayout.addView(hlayout)

        self.adapter = ListAdapter(self._activity,
                                   self.dbitems,
                                   listener=self._dispatch_event)
        self.listView = ListView(self._activity)
        self.listView.setAdapter(self.adapter)

        vlayout.addView(self.listView)

        self._activity.setContentView(vlayout)
示例#10
0
    def main_view(self):
        self.vlayout.removeAllViews()
        self.flayout.removeAllViews()

        create_sale = Button(self._activity)
        create_sale.setText('Create sale')
        create_sale.setOnClickListener(ButtonClick(self.create_sale_view))
        self.vlayout.addView(create_sale)

        create_product = Button(self._activity)
        create_product.setText('Create product')
        create_product.setOnClickListener(ButtonClick(
            self.create_product_view))
        self.vlayout.addView(create_product)

        sales_view = Button(self._activity)
        sales_view.setText('View sales')
        sales_view.setOnClickListener(ButtonClick(self.sales_view))
        self.vlayout.addView(sales_view)

        products_view = Button(self._activity)
        products_view.setText('View products')
        products_view.setOnClickListener(ButtonClick(self.products_view))
        self.vlayout.addView(products_view)

        clients_view = Button(self._activity)
        clients_view.setText('View clients')
        clients_view.setOnClickListener(ButtonClick(self.clients_view))
        self.vlayout.addView(clients_view)

        hlayout = LinearLayout(self._activity)
        hlayout.setOrientation(LinearLayout.HORIZONTAL)
        relative_bottom = RelativeLayout(self._activity)
        relative_right = RelativeLayout(self._activity)

        received, to_receive = self.get_balance()
        received_view = TextView(self._activity)
        received_view.setText('Received %.2f' % (float(received)))
        received_view.setTextSize(18)
        to_receive_view = TextView(self._activity)
        to_receive_view.setText('To receive %.2f' % (float(to_receive)))
        to_receive_view.setTextSize(18)

        relative_right.addView(to_receive_view, _create_layout_params('right'))
        hlayout.addView(received_view)
        hlayout.addView(relative_right)
        relative_bottom.addView(hlayout, _create_layout_params('bottom'))
        self.vlayout.addView(relative_bottom)
示例#11
0
    def enterRemoveMode(self):

        self.removeView(self.addWidget)
        self.addWidget.setId(2)

        removeParams = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        removeParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
        removeParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
        removeParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)

        self.addView(self.removeWidget, removeParams)
        self.removeWidget.setId(1)
        self.mode = "remove"
示例#12
0
    def itemLayout(self):

        return RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                                           ViewGroup.LayoutParams.WRAP_CONTENT)
示例#13
0
    def addForecasts(self, forecasts):

        self.forecastLayout.removeAllViews()
        self.scrollView.scrollTo(0, 0)

        if len(forecasts) == 0:
            return

        self.placeLabel.setText(forecasts[0].place)
        self.creditLabel.setText(forecasts[0].credit)

        firstDate = forecasts[0].from_
        calendar = Calendar.getInstance()
        calendar.setTime(firstDate)

        currentDay = calendar.get(Calendar.DAY_OF_MONTH)

        context = self.getContext()

        for forecast in forecasts:

            #             Date
            # Temperature Symbol Description
            #                    Wind

            # Get the day of the month.
            date = forecast.from_
            calendar.setTime(date)
            day = calendar.get(Calendar.DAY_OF_MONTH)

            # Add an item for the date for the first item and any item
            # following a day change.
            if date == firstDate or day != currentDay:
                dateView = TextView(context)
                dateView.setText(
                    calendar.getDisplayName(Calendar.DAY_OF_WEEK,
                        Calendar.LONG, Locale.getDefault()) + " " + \
                    str(day) + " " + \
                    calendar.getDisplayName(Calendar.MONTH,
                        Calendar.LONG, Locale.getDefault()) + " " + \
                    str(calendar.get(Calendar.YEAR)))

                dateView.setGravity(Gravity.CENTER)
                dateView.setTypeface(Typeface.create(None, Typeface.BOLD))
                dateView.setBackgroundColor(self.lightBackground)
                dateView.setTextColor(0xff000000)

                self.forecastLayout.addView(dateView, self.rowLayout())

            currentDay = day

            # Time
            timeString = String.format(
                "%02d:%02d:%02d - ",
                array([
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    calendar.get(Calendar.SECOND)
                ]))

            date = forecast.to_
            calendar.setTime(date)

            timeString += String.format(
                "%02d:%02d:%02d",
                array([
                    calendar.get(Calendar.HOUR_OF_DAY),
                    calendar.get(Calendar.MINUTE),
                    calendar.get(Calendar.SECOND)
                ]))

            timeView = TextView(context)
            timeView.setText(timeString)

            timeView.setGravity(Gravity.CENTER)
            timeView.setTypeface(Typeface.create(None, Typeface.BOLD))

            self.forecastLayout.addView(timeView, self.rowLayout())

            # Symbol, temperature, description and wind
            row = RelativeLayout(context)

            # Symbol
            lp = self.itemLayout()
            lp.addRule(RelativeLayout.CENTER_IN_PARENT)

            if forecast.symbol != -1:
                imageView = ImageView(context)
                imageView.setImageResource(forecast.symbol)
                row.addView(imageView, lp)
            else:
                spacer = Space(context)
                row.addView(spacer, lp)

            # Temperature
            tempView = TextView(context)
            tempView.setTextSize(tempView.getTextSize() * 2)

            if forecast.temperatureUnit == "celsius":
                tempView.setText(forecast.temperature + u"\u2103")
            else:
                tempView.setText(forecast.temperature + " " +
                                 forecast.temperatureUnit)

            lp = self.itemLayout()
            lp.addRule(RelativeLayout.CENTER_VERTICAL)
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT)
            row.addView(tempView, lp)

            # Description and wind speed
            descLayout = LinearLayout(context)
            descLayout.setOrientation(LinearLayout.VERTICAL)

            descView = TextView(context)
            descView.setText(forecast.description)
            descLayout.addView(descView, lp)

            windView = TextView(context)
            windView.setText(forecast.windSpeed)
            descLayout.addView(windView, lp)

            lp = self.itemLayout()
            lp.addRule(RelativeLayout.CENTER_VERTICAL)
            lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
            row.addView(descLayout, lp)

            self.forecastLayout.addView(row, self.rowLayout())
示例#14
0
    def __init__(self, context):

        RelativeLayout.__init__(self, context)

        # This getColor call deprecated in API level 23.
        self.lightBackground = context.getResources().getColor(
            android.R.color.background_light)
        self.darkText = 0xff000000

        # Header
        header = LinearLayout(context)
        header.setOrientation(LinearLayout.VERTICAL)
        header.setId(1)

        self.placeLabel = TextView(context)
        self.placeLabel.setTextSize(self.placeLabel.getTextSize() * 1.5)
        self.placeLabel.setGravity(Gravity.CENTER)

        headerLine = View(context)
        headerLine.setBackgroundColor(self.lightBackground)
        headerLineParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, 1)  # 1 pixel in height

        header.addView(self.placeLabel)
        header.addView(headerLine, headerLineParams)

        # Middle - containing the forecast layout
        self.scrollView = ScrollView(context)
        self.scrollView.setId(2)

        # Footer
        footer = LinearLayout(context)
        footer.setOrientation(LinearLayout.VERTICAL)
        footer.setId(3)

        footerLine = View(context)
        footerLine.setBackgroundColor(self.lightBackground)
        footerLineParams = LinearLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT, 1)  # 1 pixel in height

        self.creditLabel = TextView(context)

        footer.addView(footerLine, footerLineParams)
        footer.addView(self.creditLabel)

        # The forecast layout
        self.forecastLayout = LinearLayout(context)
        self.forecastLayout.setOrientation(LinearLayout.VERTICAL)
        self.scrollView.addView(self.forecastLayout)

        # Layout parameters
        headerParams = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        headerParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)
        headerParams.addRule(RelativeLayout.CENTER_HORIZONTAL)

        scrollParams = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        scrollParams.addRule(RelativeLayout.CENTER_HORIZONTAL)
        scrollParams.addRule(RelativeLayout.BELOW, 1)
        scrollParams.addRule(RelativeLayout.ABOVE, 3)

        footerParams = RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT)
        footerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)

        self.addView(header, headerParams)
        self.addView(self.scrollView, scrollParams)
        self.addView(footer, footerParams)
示例#15
0
文件: app.py 项目: jvmaia/todoapp-voc
def _create_layout_params():
    params = RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT)
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
    return params
示例#16
0
class MainApp:
    def __init__(self):
        self._activity = android.PythonActivity.setListener(self)
        self.db = manamoneyDB(self._activity)

    def onCreate(self):
        self.vlayout = LinearLayout(self._activity)
        self.vlayout.setOrientation(LinearLayout.VERTICAL)
        self.flayout = FrameLayout(self._activity)
        self.vlayout.addView(self.flayout)
        self._activity.setContentView(self.vlayout)
        self.main_view()

    def main_view(self):
        self.vlayout.removeAllViews()
        self.flayout.removeAllViews()

        create_sale = Button(self._activity)
        create_sale.setText('Create sale')
        create_sale.setOnClickListener(ButtonClick(self.create_sale_view))
        self.vlayout.addView(create_sale)

        create_product = Button(self._activity)
        create_product.setText('Create product')
        create_product.setOnClickListener(ButtonClick(
            self.create_product_view))
        self.vlayout.addView(create_product)

        sales_view = Button(self._activity)
        sales_view.setText('View sales')
        sales_view.setOnClickListener(ButtonClick(self.sales_view))
        self.vlayout.addView(sales_view)

        products_view = Button(self._activity)
        products_view.setText('View products')
        products_view.setOnClickListener(ButtonClick(self.products_view))
        self.vlayout.addView(products_view)

        clients_view = Button(self._activity)
        clients_view.setText('View clients')
        clients_view.setOnClickListener(ButtonClick(self.clients_view))
        self.vlayout.addView(clients_view)

        hlayout = LinearLayout(self._activity)
        hlayout.setOrientation(LinearLayout.HORIZONTAL)
        relative_bottom = RelativeLayout(self._activity)
        relative_right = RelativeLayout(self._activity)

        received, to_receive = self.get_balance()
        received_view = TextView(self._activity)
        received_view.setText('Received %.2f' % (float(received)))
        received_view.setTextSize(18)
        to_receive_view = TextView(self._activity)
        to_receive_view.setText('To receive %.2f' % (float(to_receive)))
        to_receive_view.setTextSize(18)

        relative_right.addView(to_receive_view, _create_layout_params('right'))
        hlayout.addView(received_view)
        hlayout.addView(relative_right)
        relative_bottom.addView(hlayout, _create_layout_params('bottom'))
        self.vlayout.addView(relative_bottom)

    def create_product_view(self):
        self.vlayout.removeAllViews()

        self.product_name = EditText(self._activity)
        self.product_name.setHint('Product name')
        self.vlayout.addView(self.product_name)

        self.product_quantity = EditText(self._activity)
        self.product_quantity.setHint('Product quantity')
        self.product_quantity.setInputType(0x00000002)
        self.vlayout.addView(self.product_quantity)

        self.product_price = EditText(self._activity)
        self.product_price.setHint('Product price')
        self.product_price.setInputType(0x00000002 | 0x00002000)
        self.vlayout.addView(self.product_price)

        create_button = Button(self._activity)
        create_button.setOnClickListener(ButtonClick(self.create_product))
        create_button.setText('Create product')
        self.vlayout.addView(create_button)

        self.add_error_text()
        self.add_return_button('main', flayout=False)

    def create_sale_view(self):
        self.vlayout.removeAllViews()

        self.sale_person = EditText(self._activity)
        self.sale_person.setHint('Client')
        self.vlayout.addView(self.sale_person)

        horizontalProducts = LinearLayout(self._activity)
        horizontalProducts.setOrientation(LinearLayout.HORIZONTAL)

        productsText = TextView(self._activity)
        productsText.setText('See products || quantity || value: ')
        horizontalProducts.addView(productsText)

        productSpinner = Spinner(self._activity)
        RawProducts = list(self.db.fetch_products())
        products = [(product['name'] + ' || ' + str(product['quantity']) +
                     ' || ' + str(product['value']))
                    for product in RawProducts]
        ProductsAdapter = ArrayAdapter(self._activity, 0x01090008, products)
        ProductsAdapter.setDropDownViewResource(0x01090009)
        productSpinner.setAdapter(ProductsAdapter)
        horizontalProducts.addView(productSpinner)

        self.sale_description = EditText(self._activity)
        self.sale_description.setHint('product:quantity')
        self.vlayout.addView(self.sale_description)

        self.sale_value = EditText(self._activity)
        self.sale_value.setInputType(0x00000002 | 0x00002000)
        self.sale_value.setHint('Value')
        self.vlayout.addView(self.sale_value)

        hlayout = LinearLayout(self._activity)

        text = TextView(self._activity)
        text.setText('Paid')
        text.setTextSize(22)
        hlayout.addView(text)
        self.sale_paid = CheckBox(self._activity)
        hlayout.addView(self.sale_paid)
        self.vlayout.addView(hlayout)

        generate_price_button = Button(self._activity)
        generate_price_button.setOnClickListener(
            ButtonClick(self.generate_price))
        generate_price_button.setText('Generate price')
        self.vlayout.addView(generate_price_button)

        create_button = Button(self._activity)
        create_button.setText('Sale')
        self.vlayout.addView(create_button)

        self.add_error_text()
        self.add_return_button('main', flayout=False)

    def products_view(self):
        self.vlayout.removeAllViews()
        self.flayout.removeAllViews()
        self.vlayout.addView(self.flayout)

        self.productsItems = self.db.fetch_products()
        self.adapterProducts = ProductsListAdapter(
            self._activity, self.productsItems, listener=self._dispatch_event)
        self.listViewProducts = ListView(self._activity)
        self.listViewProducts.setAdapter(self.adapterProducts)
        self.flayout.addView(self.listViewProducts)

        self.add_return_button('main')

    def sales_view(self, sales=None, back=None):
        self.vlayout.removeAllViews()
        self.flayout.removeAllViews()
        self.vlayout.addView(self.flayout)

        if sales:
            self.salesItems = sales
        else:
            self.salesItems = self.db.fetch_sales()

        self.adapterSales = SalesListAdapter(self._activity,
                                             self.salesItems,
                                             listener=self._dispatch_event,
                                             back=back)
        self.listViewSales = ListView(self._activity)
        self.listViewSales.setAdapter(self.adapterSales)
        self.flayout.addView(self.listViewSales)

        if back:
            self.add_return_button('clients')
        else:
            self.add_return_button('main')

    def clients_view(self):
        self.vlayout.removeAllViews()
        self.flayout.removeAllViews()
        self.vlayout.addView(self.flayout)

        self.clientsItems = self.db.fetch_clients()
        self.adapterClients = ClientsListAdapter(self._activity,
                                                 self.clientsItems,
                                                 listener=self._dispatch_event)
        self.listViewClients = ListView(self._activity)
        self.listViewClients.setAdapter(self.adapterClients)

        self.flayout.addView(self.listViewClients)
        self.add_return_button('main')

    def details_sale_view(self, sale, back=None):
        self.vlayout.removeAllViews()

        person_text = TextView(self._activity)
        person_text.setText('Client: %s' % (sale['person']))
        person_text.setTextSize(22)
        self.vlayout.addView(person_text)

        value_text = TextView(self._activity)
        value_text.setText('\nValue: R$%.2f' % (sale['value']))
        value_text.setTextSize(22)
        self.vlayout.addView(value_text)

        discount_text = TextView(self._activity)
        total_price = self.db.get_price(sale['description'].replace(' ', '\n'))
        discount = total_price - sale['value']
        discount_p = discount * 100.0 / sale['value']
        discount_text.setText('\nDiscount: %.2f%% = R$%.2f' %
                              (discount_p, discount))
        discount_text.setTextSize(22)
        self.vlayout.addView(discount_text)

        description_text = TextView(self._activity)
        description_text.setText('\nDescription: %s' % (sale['description']))
        description_text.setTextSize(22)
        self.vlayout.addView(description_text)

        date_text = TextView(self._activity)
        date = sale['date'].replace(' ', '-')
        date_text.setText('\nDate: %s' % (date))
        date_text.setTextSize(22)
        self.vlayout.addView(date_text)

        if back:
            self.add_return_button('sales_client',
                                   value=sale['person'],
                                   flayout=False)
        else:
            self.add_return_button('sales_view', flayout=False)

    def create_product(self):
        product = {}
        product['name'] = str(self.product_name.getText())
        if len(product['name']) == 0:
            self.error_text.setText('Enter a valid name please')
            return

        try:
            product['quantity'] = int(str(self.product_quantity.getText()))
            product['price'] = float(str(self.product_price.getText()))
        except ValueError:
            self.error_text.setText('Enter a valid number please')
            return

        self.db.create_product(product)
        self.main_view()

    def create_sale(self):
        sale = {}
        sale['person'] = str(self.sale_person.getText())
        if len(sale['person']) == 0:
            self.error_text.setText('Enter a valid name please')
            return

        sale['description'] = str(self.sale_description.getText())
        if len(sale['description']) == 0:
            self.error_text.setText('Enter a valid description please')
            return

        try:
            sale['value'] = float(str(self.sale_value.getText()))
        except ValueError:
            self.error_text.setText('Enter a valid value please')
            return

        sale['paid'] = int(self.sale_paid.isChecked())

        self.db.create_sale(sale)
        self.main_view()

    def _dispatch_event(self, event, value, back=None):
        if event == 'update_sale':
            self.db.update_sale(sale=value)
        elif event == 'update_product':
            self.db.changeQuantity_product(sale=value)
        elif event == 'details_sale':
            self.details_sale_view(sale=value, back=back)
        elif event == 'sales_client':
            sales = self.db.fetch_sales(client=value)
            self.sales_view(sales=sales, back='sales_clients')
        elif event == 'clients':
            self.return_view('clients')

    def add_return_button(self, view, value=None, flayout=True):
        self.return_button = Button(self._activity)
        self.return_button.setOnClickListener(
            ButtonClick(self.return_view, view, value=value))
        self.return_button.setText('Return')
        self.relative_rb = RelativeLayout(self._activity)
        self.relative_rb.addView(self.return_button,
                                 _create_layout_params('bottom'))
        if flayout:
            self.flayout.addView(self.relative_rb)
        else:
            self.vlayout.addView(self.relative_rb)

    def add_error_text(self):
        self.error_text = TextView(self._activity)
        self.vlayout.addView(self.error_text)

    def return_view(self, view, value=None):
        if view == 'main':
            self.main_view()
        elif view == 'sales_view':
            self.sales_view()
        elif view == 'clients':
            self.clients_view()
        elif view == 'sales_client':
            self._dispatch_event('sales_client', back=view, value=value)

    def get_balance(self):
        return self.db.get_balance()

    def generate_price(self):
        description = str(self.sale_description.getText())
        value = self.db.get_price(description)
        if type(value) == str:
            self.error_text.setText(value)
            return
        value = '%.2f' % value
        self.sale_value.setText(value.replace(',', '.'))