def onCreate(self, state): AppCompatActivity.onCreate(self, state) self.setContentView(R.layout.activity_main) self.topics = get_main_topics() main_topics_scroll = self.findViewById(R.id.main_topics_scroll) # class MainTopicButtonListener(dynamic_proxy(View.OnClickListener)): # def __init__(self, main_activity, **kwargs): # super(MainTopicButtonListener, self).__init__() # self.main_activity = main_activity # # @Override(jvoid, [View]) # def onClick(self, v): # #print(v.getId()) # intent = Intent(self.main_activity, data_view.DataViewActivity.getClass()) # intent.putExtra("id", v.getId()) # Activity.startActivity(intent) for topic in self.topics: topic_button = Button(self) topic_button.setText(topic['text']) topic_button.setId(topic['id']) params = GridLayout.LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1.0), GridLayout.spec(GridLayout.UNDEFINED, 1.0)) params.width = 0 topic_button.setLayoutParams(params) topic_button.setOnClickListener(self) main_topics_scroll.addView(topic_button)
def __init__(self, context, handler): HBox.__init__(self, context) self.handler = handler # Read the lists of place names and place specifications from the # application's resources, creating a dictionary from the two lists. self.places = {} resources = context.getResources() place_names = resources.getStringArray(R.array.place_names) for name, place in zip(place_names, resources.getStringArray(R.array.places)): self.places[name] = place # Use a specialised adapter to provide filtered lists of data for an # auto-complete-enabled text view. adapter = FilterStringArrayAdapter( context, android.R.layout.simple_dropdown_item_1line, place_names) self.locationEdit = AutoCompleteTextView(context) self.locationEdit.setAdapter(adapter) self.addButton = Button(context) self.addButton.setText("Add") self.addButton.setOnClickListener(self) self.addWeightedView(self.locationEdit, 2) self.addWeightedView(self.addButton, 0)
def onCreate(self): vlayout = LinearLayout(self._activity) vlayout.setOrientation(LinearLayout.VERTICAL) self.entry = EditText(self._activity) self.entry.setInputType(0x00000002 | 0x00002000 | 0x00001000) # set input class and flags # see https://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType self.entry.setGravity(Gravity.CENTER) vlayout.addView(self.entry) hlayout = LinearLayout(self._activity) hlayout.setOrientation(LinearLayout.HORIZONTAL) hlayout.setGravity(Gravity.CENTER) self.convert_to_fahrenheit = Button(self._activity) self.convert_to_fahrenheit.setOnClickListener(ButtonClick(self.to_fahrenheit)) self.convert_to_fahrenheit.setText('To Fahrenheit') hlayout.addView(self.convert_to_fahrenheit) self.convert_to_celsius = Button(self._activity) self.convert_to_celsius.setOnClickListener(ButtonClick(self.to_celsius)) self.convert_to_celsius.setText('To Celsius') hlayout.addView(self.convert_to_celsius) vlayout.addView(hlayout) self.result = TextView(self._activity) self.result.setTextSize(26) self.result.setText('Convert units') self.result.setGravity(Gravity.CENTER) vlayout.addView(self.result) self._activity.setContentView(vlayout)
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)
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)
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'])
def onCreate(self): # Building the UI vlayout = LinearLayout(self.activity) vlayout.setOrientation(LinearLayout.VERTICAL) # Top clock top_vlayout = LinearLayout(self.activity) self.top_clock = TextView(self.activity) self.top_clock.setTextSize(50) self.top_delay = TextView(self.activity) self.top_delay.setTextSize(15) top_vlayout.setOnClickListener(ButtonClick(self.top_player_touched)) top_vlayout.addView(self.top_clock) top_vlayout.addView(self.top_delay) vlayout.addView(top_vlayout) # Button row button_layout = LinearLayout(self.activity) button_layout.setOrientation(LinearLayout.HORIZONTAL) vlayout.addView(button_layout) self.pause_button = Button(self.activity) self.pause_button.setText('Pause') self.pause_button.setOnClickListener( ButtonClick(self.pause_or_resume_clicked)) button_layout.addView(self.pause_button) self.restart_button = Button(self.activity) self.restart_button.setText('Restart') self.restart_button.setOnClickListener( ButtonClick(self.restart_clicked)) button_layout.addView(self.restart_button) # Bottom clock bot_vlayout = LinearLayout(self.activity) self.bot_clock = TextView(self.activity) self.bot_clock.setTextSize(50) self.bot_delay = TextView(self.activity) self.bot_delay.setTextSize(15) bot_vlayout.setOnClickListener(ButtonClick(self.bottom_player_touched)) bot_vlayout.addView(self.bot_clock) bot_vlayout.addView(self.bot_delay) vlayout.addView(bot_vlayout) self.activity.setContentView(vlayout) # Clock should be create after setContentView self.clock = CounterupTimer(self) self.clock.set_timers((0, 0), None)
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 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)
class AddWidget(HBox): __interfaces__ = [View.OnClickListener] @args(void, [Context, AddLocationListener]) def __init__(self, context, handler): HBox.__init__(self, context) self.handler = handler # Read the lists of place names and place specifications from the # application's resources, creating a dictionary from the two lists. self.places = {} resources = context.getResources() place_names = resources.getStringArray(R.array.place_names) for name, place in zip(place_names, resources.getStringArray(R.array.places)): self.places[name] = place # Use a specialised adapter to provide filtered lists of data for an # auto-complete-enabled text view. adapter = FilterStringArrayAdapter( context, android.R.layout.simple_dropdown_item_1line, place_names) self.locationEdit = AutoCompleteTextView(context) self.locationEdit.setAdapter(adapter) self.addButton = Button(context) self.addButton.setText("Add") self.addButton.setOnClickListener(self) self.addWeightedView(self.locationEdit, 2) self.addWeightedView(self.addButton, 0) def onClick(self, view): text = str(CAST(self.locationEdit, TextView).getText()) name = text.trim() try: spec = self.places[name] except KeyError: return # Remove the country from the name. name = name[:name.indexOf(", ")] self.handler.addLocation(name, spec) self.locationEdit.setText("")
class RemoveWidget(HBox): __interfaces__ = [View.OnClickListener] @args(void, [Context, RemoveLocationListener]) def __init__(self, context, handler): HBox.__init__(self, context) self.handler = handler self.removeButton = Button(context) self.removeButton.setText("Remove") self.removeButton.setOnClickListener(self) self.cancelButton = Button(context) self.cancelButton.setText("Cancel") self.cancelButton.setOnClickListener(self) self.addWeightedView(self.removeButton, 1) self.addWeightedView(self.cancelButton, 1) def onClick(self, view): if view == self.removeButton: self.handler.removeLocation() else: self.handler.cancelRemove()
def onCreate(self): def create_button_row(): return [ Button(self._activity), Button(self._activity), Button(self._activity), ] self.buttons = [ create_button_row(), create_button_row(), create_button_row(), ] vlayout = LinearLayout(self._activity) vlayout.setOrientation(LinearLayout.VERTICAL) self.top_label = TextView(self._activity) self.top_label.setTextSize(50) vlayout.addView(self.top_label) for row in self.buttons: hlayout = LinearLayout(self._activity) hlayout.setOrientation(LinearLayout.HORIZONTAL) hlayout.setGravity(Gravity.CENTER) for button in row: button.setTextSize(50) hlayout.addView(button) vlayout.addView(hlayout) for i in range(3): for j in range(3): self.buttons[i][j].setOnClickListener( ButtonClick(self.play, i, j)) self.restart_button = Button(self._activity) self.restart_button.setText('Restart') self.restart_button.setOnClickListener(ButtonClick(self.restart_game)) vlayout.addView(self.restart_button) footer = TextView(self._activity) footer.setText('Powered by Python') footer.setGravity(Gravity.CENTER) vlayout.addView(footer) self.updateUI() self._activity.setContentView(vlayout)
def __init__(self, context, handler): HBox.__init__(self, context) self.handler = handler self.removeButton = Button(context) self.removeButton.setText("Remove") self.removeButton.setOnClickListener(self) self.cancelButton = Button(context) self.cancelButton.setText("Cancel") self.cancelButton.setOnClickListener(self) self.addWeightedView(self.removeButton, 1) self.addWeightedView(self.cancelButton, 1)
def onCreate(self): self.label = TextView(self._activity) self.label.setTextSize(50) self.label.setText(self.message) vlayout = LinearLayout(self._activity) vlayout.setOrientation(LinearLayout.VERTICAL) vlayout.addView(self.label) self.switch_button = Button(self._activity) self.switch_button.setText(self.message_button) self.switch_button.setOnClickListener(ButtonClick(self.update)) vlayout.addView(self.switch_button) self._activity.setContentView(vlayout)
def create(self): self._impl = AndroidButton() self._listener = TogaButtonListener() self._listener._interface = self self._impl.setOnClickListener(self._listener) self.rehint()
class MainApp: def __init__(self): self._activity = android.PythonActivity.setListener(self) def onCreate(self): vlayout = LinearLayout(self._activity) vlayout.setOrientation(LinearLayout.VERTICAL) self.entry = EditText(self._activity) self.entry.setInputType(0x00000002 | 0x00002000 | 0x00001000) # set input class and flags # see https://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType self.entry.setGravity(Gravity.CENTER) vlayout.addView(self.entry) hlayout = LinearLayout(self._activity) hlayout.setOrientation(LinearLayout.HORIZONTAL) hlayout.setGravity(Gravity.CENTER) self.convert_to_fahrenheit = Button(self._activity) self.convert_to_fahrenheit.setOnClickListener(ButtonClick(self.to_fahrenheit)) self.convert_to_fahrenheit.setText('To Fahrenheit') hlayout.addView(self.convert_to_fahrenheit) self.convert_to_celsius = Button(self._activity) self.convert_to_celsius.setOnClickListener(ButtonClick(self.to_celsius)) self.convert_to_celsius.setText('To Celsius') hlayout.addView(self.convert_to_celsius) vlayout.addView(hlayout) self.result = TextView(self._activity) self.result.setTextSize(26) self.result.setText('Convert units') self.result.setGravity(Gravity.CENTER) vlayout.addView(self.result) self._activity.setContentView(vlayout) def to_celsius(self): fahrenheit = str(self.entry.getText()) # capture entry_text and transform it into string if len(fahrenheit) == 0: self.result.setText('Please enter a valid number!') return fahrenheit = float(fahrenheit) celsius = (fahrenheit-32.0) / 1.8 self.result.setText('Celsius: %.2f'%(celsius)) def to_fahrenheit(self): celsius = str(self.entry.getText()) # capture entry_text and transform it into string if len(celsius) == 0: self.result.setText('Please enter a valid number!') return celsius = float(celsius) fahrenheit = celsius * 1.8 + 32.0 self.result.setText('Fahrenheit: %.2f'%(fahrenheit))
class Button(ButtonInterface, WidgetMixin): def __init__(self, label, id=None, style=None, on_press=None): super().__init__(label, id=id, style=style, on_press=on_press) self._create() def create(self): self._impl = AndroidButton() self._listener = TogaButtonListener() self._listener._interface = self self._impl.setOnClickListener(self._listener) self.rehint() def _set_label(self, label): self._impl.setText(self.label) self.rehint() def rehint(self): # print("REHINT", self, self._impl.get_preferred_width(), self._impl.get_preferred_height(), getattr(self, '_fixed_height', False), getattr(self, '_fixed_width', False)) # hints = {} # width = self._impl.get_preferred_width() # minimum_width = width[0] # natural_width = width[1] # height = self._impl.get_preferred_height() # minimum_height = height[0] # natural_height = height[1] # if minimum_width > 0: # hints['min_width'] = minimum_width # if minimum_height > 0: # hints['min_height'] = minimum_height # if natural_height > 0: # hints['height'] = natural_height # if hints: # self.style.hint(**hints) self.style.hint( min_width=200, height=30 )
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)
def on_create(self, parent_layout): vlayout = LinearLayout(self.activity) vlayout.setOrientation(LinearLayout.VERTICAL) vlayout.setGravity(Gravity.BOTTOM) if self.is_inverted: vlayout.setRotation(180) parent_layout.addView(vlayout) self.top_layout = LinearLayout(self.activity) self.top_layout.setOrientation(LinearLayout.HORIZONTAL) self.top_label = TextView(self.activity) # noinspection PyUnresolvedReferences self.top_label.setLayoutParams( LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1)) self.top_label.setTextSize(50) self.top_layout.addView(self.top_label) self.next_button = Button(self.activity) # noinspection PyUnresolvedReferences self.next_button.setLayoutParams( LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 0)) self.next_button.setText('Hint') self.next_button.setOnClickListener(ButtonClick(self.next)) self.top_layout.addView(self.next_button) vlayout.addView(self.top_layout) self.solution_layout = LinearLayout(self.activity) self.solution_layout.setOrientation(LinearLayout.HORIZONTAL) self.solution_layout.setGravity(Gravity.LEFT) self.solution_label = TextView(self.activity) self.solution_label.setText('') self.solution_label.setTextSize(50) self.solution_layout.addView(self.solution_label) vlayout.addView(self.solution_layout) self.shelf_layout = LinearLayout(self.activity) self.shelf_layout.setOrientation(LinearLayout.HORIZONTAL) self.shelf_layout.setGravity(Gravity.CENTER) vlayout.addView(self.shelf_layout)
class MyApp: def __init__(self): self._activity = android.PythonActivity.setListener(self) self.message = 'click on button to change me' self.message_button = 'try me' def onCreate(self): self.label = TextView(self._activity) self.label.setTextSize(50) self.label.setText(self.message) vlayout = LinearLayout(self._activity) vlayout.setOrientation(LinearLayout.VERTICAL) vlayout.addView(self.label) self.switch_button = Button(self._activity) self.switch_button.setText(self.message_button) self.switch_button.setOnClickListener(ButtonClick(self.update)) vlayout.addView(self.switch_button) self._activity.setContentView(vlayout) def update(self): if self.message == 'On': self.message = 'Off' self.message_button = 'On' else: self.message = 'On' self.message_button = 'Off' self.switch_button.setText(self.message_button) self.label.setText(self.message)
def onCreate(self): self.vlayout = LinearLayout(self._activity) self.vlayout.setOrientation(LinearLayout.VERTICAL) self.textbox = EditText(self._activity) self.vlayout.addView(self.textbox) self.update_text_button = Button(self._activity) self.update_text_button.setText('update text') self.update_text_button.setOnClickListener( ButtonClick(self.update_text)) self.vlayout.addView(self.update_text_button) self.text_result = TextView(self._activity) self.text_result.setText('test') self.vlayout.addView(self.text_result) self.change_view_button = Button(self._activity) self.change_view_button.setText('change view') self.change_view_button.setOnClickListener( ButtonClick(self.change_view)) self.vlayout.addView(self.change_view_button) self._activity.setContentView(self.vlayout)
class MainApp: def __init__(self): self._activity = android.PythonActivity.setListener(self) def onCreate(self): self.vlayout = LinearLayout(self._activity) self.vlayout.setOrientation(LinearLayout.VERTICAL) self.textbox = EditText(self._activity) self.vlayout.addView(self.textbox) self.update_text_button = Button(self._activity) self.update_text_button.setText('update text') self.update_text_button.setOnClickListener( ButtonClick(self.update_text)) self.vlayout.addView(self.update_text_button) self.text_result = TextView(self._activity) self.text_result.setText('test') self.vlayout.addView(self.text_result) self.change_view_button = Button(self._activity) self.change_view_button.setText('change view') self.change_view_button.setOnClickListener( ButtonClick(self.change_view)) self.vlayout.addView(self.change_view_button) self._activity.setContentView(self.vlayout) def update_text(self): text = self.textbox.getText() self.text_result.setText(text) def change_view(self): self.vlayout.removeAllViews() self.vlayout.addView(self.text_result) calendar = Calendar.getInstance() dateformat = SimpleDateFormat('yyyy / MM / dd') now = dateformat.format(calendar.getTime()) self.text_result.setText(now)
def onCreate(self): self.vlayout = LinearLayout(self._activity) self.vlayout.setOrientation(LinearLayout.VERTICAL) self.entrytext = EditText(self._activity) self.entrytext.setHint('Write a url to request') self.vlayout.addView(self.entrytext) button_send = Button(self._activity) button_send.setText('Request GET') button_send.setOnClickListener(OnClick(self.send_request)) self.vlayout.addView(button_send) self.result = TextView(self._activity) self.result.setText('Waiting a request') self.vlayout.addView(self.result) self._activity.setContentView(self.vlayout)
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)
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)
class Player: def __init__(self, activity, is_inverted, solved_callback): """ Initialize. :param activity: the main activity for the app :param is_inverted: True if this player should be displayed upside down :param solved_callback: to call when the sentence is solved - solved_callback(player)""" self.activity = activity self.is_inverted = is_inverted self.solved_callback = solved_callback self.solution_buttons = [] self.shelf_buttons = [] self.solution_sources = [] self.opponent = None self.unlimited_hints = False self.top_label = None self.next_button = None self.solution_label = None self.sentence = None self.shelf_layout = self.solution_layout = self.top_layout = None def on_create(self, parent_layout): vlayout = LinearLayout(self.activity) vlayout.setOrientation(LinearLayout.VERTICAL) vlayout.setGravity(Gravity.BOTTOM) if self.is_inverted: vlayout.setRotation(180) parent_layout.addView(vlayout) self.top_layout = LinearLayout(self.activity) self.top_layout.setOrientation(LinearLayout.HORIZONTAL) self.top_label = TextView(self.activity) # noinspection PyUnresolvedReferences self.top_label.setLayoutParams( LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1)) self.top_label.setTextSize(50) self.top_layout.addView(self.top_label) self.next_button = Button(self.activity) # noinspection PyUnresolvedReferences self.next_button.setLayoutParams( LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 0)) self.next_button.setText('Hint') self.next_button.setOnClickListener(ButtonClick(self.next)) self.top_layout.addView(self.next_button) vlayout.addView(self.top_layout) self.solution_layout = LinearLayout(self.activity) self.solution_layout.setOrientation(LinearLayout.HORIZONTAL) self.solution_layout.setGravity(Gravity.LEFT) self.solution_label = TextView(self.activity) self.solution_label.setText('') self.solution_label.setTextSize(50) self.solution_layout.addView(self.solution_label) vlayout.addView(self.solution_layout) self.shelf_layout = LinearLayout(self.activity) self.shelf_layout.setOrientation(LinearLayout.HORIZONTAL) self.shelf_layout.setGravity(Gravity.CENTER) vlayout.addView(self.shelf_layout) def set_sentence(self, sentence): self.sentence = sentence self.top_label.setText(self.sentence.translation) self.solution_label.setText('') button_count = len(self.sentence.source) while self.shelf_layout.getChildCount() > button_count: self.shelf_layout.removeViewAt(0) self.shelf_buttons.pop(0) self.solution_layout.removeViewAt(1) self.solution_buttons.pop(0) while self.shelf_layout.getChildCount() < button_count: self.add_button(self.shelf_layout, self.shelf_buttons) self.add_button(self.solution_layout, self.solution_buttons) self.update_display() def update_display(self): self.update_buttons(self.solution_buttons, self.sentence.selected, self.sentence.hint_size) self.update_buttons(self.shelf_buttons, self.sentence.source) if self.sentence.is_solved: self.next_button.setText('Next') self.next_button.setEnabled(True) # noinspection PyMethodMayBeStatic def update_buttons(self, buttons, characters, disabled_count=0): for i, (button, text) in enumerate(zip(buttons, characters)): button.setEnabled(i >= disabled_count) button.setText(text) button.setVisibility(button.INVISIBLE if text == ' ' else button.VISIBLE) def add_button(self, layout, button_list): button = Button(self.activity) button.setTextSize(50) layout.addView(button) button_list.append(button) button.setOnClickListener(ButtonClick(self.move_button, button)) def move_button(self, button): if button in self.solution_buttons: solution_index = self.solution_buttons.index(button) self.sentence.replace(solution_index) else: source_index = self.shelf_buttons.index(button) self.sentence.select(source_index) self.update_display() if self.sentence.is_solved: self.solution_label.setText(self.sentence.text) for b in self.solution_buttons: b.setVisibility(b.INVISIBLE) def get_buttons(self): buttons = [button.getText() for button in self.solution_sources] return buttons def enable_hint(self): self.next_button.setEnabled(True) def next(self): self.next_button.setEnabled(self.unlimited_hints) self.opponent.enable_hint() if self.sentence.is_solved: self.next_button.setText('Hint') self.solved_callback(self) else: self.sentence.get_hint() self.update_display()
def onCreate(self, savedInstanceState: android.os.Bundle) -> void: super().onCreate(savedInstanceState) r = self.getResources() # #******************************************************** layout = LinearLayout(self) layout.setLayoutParams( LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)) layout.setPadding( TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, r.getDisplayMetrics()), TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, r.getDisplayMetrics()), TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, r.getDisplayMetrics()), TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, r.getDisplayMetrics())) layout.setOrientation(LinearLayout.VERTICAL) to_field = EditText(self) to_field.setLayoutParams( LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)) to_field.setHint("To") subject_field = EditText(self) subject_field.setLayoutParams( LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)) subject_field.setHint("Subject") message_field = EditText(self) message_field.setLayoutParams( LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1.0)) message_field.setGravity(Gravity.TOP) message_field.setHint("Message") send_button = Button(self) lp = LinearLayout.LayoutParams( TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, r.getDisplayMetrics()), LinearLayout.LayoutParams.WRAP_CONTENT) lp.gravity = Gravity.RIGHT send_button.setLayoutParams(lp) send_button.setHint("Send") send_button.setOnClickListener(OnClickListener(message_field)) layout.addView(to_field) layout.addView(subject_field) layout.addView(message_field) layout.addView(send_button) self.setContentView(layout)
def create_button_row(): return [ Button(self._activity), Button(self._activity), Button(self._activity), ]
class MyApplication: def __init__(self): self.activity = None def link(self, activity): self.activity = activity def onCreate(self): # Building the UI vlayout = LinearLayout(self.activity) vlayout.setOrientation(LinearLayout.VERTICAL) # Top clock top_vlayout = LinearLayout(self.activity) self.top_clock = TextView(self.activity) self.top_clock.setTextSize(50) self.top_delay = TextView(self.activity) self.top_delay.setTextSize(15) top_vlayout.setOnClickListener(ButtonClick(self.top_player_touched)) top_vlayout.addView(self.top_clock) top_vlayout.addView(self.top_delay) vlayout.addView(top_vlayout) # Button row button_layout = LinearLayout(self.activity) button_layout.setOrientation(LinearLayout.HORIZONTAL) vlayout.addView(button_layout) self.pause_button = Button(self.activity) self.pause_button.setText('Pause') self.pause_button.setOnClickListener( ButtonClick(self.pause_or_resume_clicked)) button_layout.addView(self.pause_button) self.restart_button = Button(self.activity) self.restart_button.setText('Restart') self.restart_button.setOnClickListener( ButtonClick(self.restart_clicked)) button_layout.addView(self.restart_button) # Bottom clock bot_vlayout = LinearLayout(self.activity) self.bot_clock = TextView(self.activity) self.bot_clock.setTextSize(50) self.bot_delay = TextView(self.activity) self.bot_delay.setTextSize(15) bot_vlayout.setOnClickListener(ButtonClick(self.bottom_player_touched)) bot_vlayout.addView(self.bot_clock) bot_vlayout.addView(self.bot_delay) vlayout.addView(bot_vlayout) self.activity.setContentView(vlayout) # Clock should be create after setContentView self.clock = CounterupTimer(self) self.clock.set_timers((0, 0), None) # self.clock = TimerPerMove(self) # self.clock.set_timers((10000, 10000), None) # self.clock.set_timers((10000, 10000), (2000, 2000)) def top_player_touched(self): self.clock.on_switch_click('w') return True def bottom_player_touched(self): self.clock.on_switch_click('b') return True def pause_or_resume_clicked(self): Log.i('MyApplication', 'called pause_clicked()') self.clock.pause_or_resume() def restart_clicked(self): Log.i('MyApplication', 'called restart_clicked()') self.clock.restart() def update_ui_state(self): state = self.clock.state turn = self.clock.turn if state == 'new': self.top_clock.setAlpha(1.0) self.bot_clock.setAlpha(1.0) self.update_timers() elif state == 'active': self.pause_button.setText('Pause') if turn == 'w': self.bot_clock.setAlpha(0.2) self.top_clock.setAlpha(1.0) else: self.top_clock.setAlpha(0.2) self.bot_clock.setAlpha(1.0) elif state == 'paused': self.pause_button.setText('Resume') elif state == 'finished': self.top_clock.setAlpha(1.0) self.bot_clock.setAlpha(1.0) def update_timers(self): self.top_clock.setText(format_time(self.clock.time[0])) self.bot_clock.setText(format_time(self.clock.time[1])) self.top_delay.setText(format_time(self.clock.delay[0])) self.bot_delay.setText(format_time(self.clock.delay[1]))
class MyApp: def __init__(self): self._activity = android.PythonActivity.setListener(self) self.buttons = [] self.top_label = None self.game = tictactoe.Game() self.message = None def onCreate(self): def create_button_row(): return [ Button(self._activity), Button(self._activity), Button(self._activity), ] self.buttons = [ create_button_row(), create_button_row(), create_button_row(), ] vlayout = LinearLayout(self._activity) vlayout.setOrientation(LinearLayout.VERTICAL) self.top_label = TextView(self._activity) self.top_label.setTextSize(50) vlayout.addView(self.top_label) for row in self.buttons: hlayout = LinearLayout(self._activity) hlayout.setOrientation(LinearLayout.HORIZONTAL) hlayout.setGravity(Gravity.CENTER) for button in row: button.setTextSize(50) hlayout.addView(button) vlayout.addView(hlayout) for i in range(3): for j in range(3): self.buttons[i][j].setOnClickListener( ButtonClick(self.play, i, j)) self.restart_button = Button(self._activity) self.restart_button.setText('Restart') self.restart_button.setOnClickListener(ButtonClick(self.restart_game)) vlayout.addView(self.restart_button) footer = TextView(self._activity) footer.setText('Powered by Python') footer.setGravity(Gravity.CENTER) vlayout.addView(footer) self.updateUI() self._activity.setContentView(vlayout) def restart_game(self): self.game = tictactoe.Game() self.message = None self.updateUI() def updateUI(self): if self.message: self.top_label.setText(self.message) else: self.top_label.setText('Player: ' + self.game.current_player) for i, row in enumerate(self.buttons): for j, button in enumerate(row): thing = self.game.board[i][j] if thing: button.setText(thing) else: button.setText(' ') def play(self, i, j): if self.game.game_over: return print('going to play game', self.game, ' in position:', i, j) try: self.game.play(i, j) self.message = None except KeyError as e: message = str(e)[1:-1] print('message', message) self.message = message self.updateUI()