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))
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 getView(self, position, convertView, parent): context = parent.getContext() layout = LinearLayout(context) layout.setOrientation(LinearLayout.VERTICAL) imageView = ImageView(context) if self.cache.containsKey(position): entry = self.cache[position] name = entry.name bitmap = entry.bitmap imageView.setImageBitmap(bitmap) if len(self.positions) > self.cache_size: self.cache.remove(self.positions.remove()) else: # Create a placeholder bitmap to put into the view. bitmap = SpriteRenderer.emptyBitmap(self.preview_size, self.preview_size, False) imageView.setImageBitmap(bitmap) # Schedule the rendering process. name = self.items[position] self.scheduleRender(WorkItem(position, imageView)) textView = TextView(context) textView.setText(name) textView.setGravity(0x01) # center_horizontal layout.addView(imageView) layout.addView(textView) return layout
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())
class ForecastWidget(RelativeLayout): 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) @args(void, [List(Forecast)]) 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()) @args(LinearLayout.LayoutParams, []) def rowLayout(self): return LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) @args(RelativeLayout.LayoutParams, []) def itemLayout(self): return RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)