New Blog Post

Android Spinner

Spinner spinner = findViewById(R.id.currency_spinner); // Create an ArrayAdapter using the String array and a spinner layout ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.spinner_values, R.layout.spinner_item); // Specify the layout to use when the list of choices appears adapter.setDropDownViewResource(R.layout.spinner_dropdown_item); // Apply the adapter to the spinner spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position,…

Switch between activities

public class MainActivity extends AppCompatActivity { private Button mAddButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mAddButton = findViewById(R.id.addButton); mAddButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openAddActivity(); } }); } public void openAddActivity(){ Intent intent = new Intent(this, Main2Activity.class); startActivity(intent); }

Android screen orientation

disable orientation change <activity android:name=”.MainActivity” android:screenOrientation=”portrait” tools:ignore=”LockedOrientationActivity”> Handling orientation state Saving instance state @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(“ScoreKey”, mScore); outState.putInt(“IndexKey”, mIndex); } Retrieving instance state @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState != null){ mScore = savedInstanceState.getInt(“ScoreKey”); mIndex = savedInstanceState.getInt(“IndexKey”); } else { mScore = 0; mIndex = 0;…

Android App Lifecycle

Code class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) Log.d(“LifecycleTest”, “onCreate”) } override fun onStart() { super.onStart() Log.d(“LifecycleTest”, “onStart”) } override fun onResume() { super.onResume() Log.d(“LifecycleTest”, “onResume”) } override fun onPause() { super.onPause() Log.d(“LifecycleTest”, “onPause”) } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) Log.d(“LifecycleTest”, “onSaveInstanceState”) } override fun onRestoreInstanceState(savedInstanceState: Bundle) { Log.d(“LifecycleTest”,…

Android Enable USB-Debugging

goto Settings > General > About phone > Software information and tap “Build number” seven times until it says “You are now a developer”  Goto Settings > General > Developer options and enable USB Debugging Drop down the notifications menu and click on “Tap for more USB options”. Most likely, the main title will say…

JavaScript modules

<head> <script src=”../js/constants.js” type=”module”></script> <script src=”../js/script.js” type=”module”></script> </head> constants.js export const projects = [ “Project-A”, “Project-B”, “Project-C”, ]; script.js import {projects} from “./constants.js”;

Introduction to matplotlib – Part 3

  After laying the foundation in Introduction to matplotlib and Introduction to matplotlib – Part 2 I want to show you another important chart Bar Charts A bar chart is useful to show total values over time e.g. the revenue of a company. years = (2017, 2018, 2019) revenue = (5000, 7000, 9000) plt.bar(years, revenue, width=0.35)…

Linear Regression with sklearn – cheat sheet

# import and instantiate model from sklearn.linear_model import LinearRegression model = LinearRegression() #prepare test data features_train = df_train.loc[:, ‘feature_name’] target_train = df_train.loc[:, ‘target_name’] #fit (train) model and print coefficient and intercept model.fit(features_train , target_train ) print(model.coef_) print(model.intercept_) # calculate model quality from sklearn.metrics import mean_squared_error from sklearn.metrics import r2_score target_prediction = model.predict(features_train) print(mean_squared_error(target_train , target_prediction))…

pytest Tutorial – Part 1

At PyConDE Berlin 2019 Florian Bruhin gave a really nice session about testing with pytest, which I try to recap here. Writing Tests If You want to work with pytest you can install it via: pip install pytest When you know basic python unittest fixtures, good news ahead: pytest is compatible and will run your…