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 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 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 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()