def getView(self, position, convertView, parent): # If convertView is not None then reuse it. if convertView != None: return convertView view = TextView(parent.getContext()) view.setText(self.items[position]) view.setTextSize(view.getTextSize() * 1.25) return view
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)