Various technical articles, IT-related tutorials, software information, and development journals
Sunday, April 21, 2019
Going back to an old Gradle version in Android Studio
Android Studio 3.4 comes with a major Gradle update (from Gradle 4.x to Gradle 5.1.1). When opening old projects, it recommends applying the Gradle update to them, but not all projects are ready for it. The File | Project Structure dialog can be used to change a project back by setting the Android Gradle Plugin Version to 3.3.2 and the Gradle version to 4.10.1. After sync, Android Studio will prompt to update once more, but it can be told to not ask again for the project.
Sunday, April 14, 2019
RichTextBox should be empty before setting tabs
I've been trying to get the Windows Forms RichTextBox control to use specified tab stops for alignment purposes. Sometimes my application needs to update these while there's text in the box. As the name suggests, SelectionTabs affects the tab stops in the current selection, so all text would need to be selected first before changing it. Even that's given me trouble, though. My current strategy is to clear the text box using ResetText, setting SelectionTabs, putting the text back, and restoring the SelectionStart.
Thursday, March 28, 2019
Fixing RichTextBox's ContentsResized boundaries with NBSPs
Previous I noticed that RichTextBox does not count trailing spaces as part of the contents when determining the needed size for the ContentsResized event. It does, however, count non-breaking spaces (NBSPs), which look identical to normal spaces. When the text in the text box is changed, I check whether the text contains a space, and if so, replace them all with NBSPs (character U+00A0). The selection/position needs to be saved before altering the text and restored afterward, otherwise the cursor will jump to the beginning. When using the text for other purposes, the NBSPs are first replaced with normal spaces.
The ContentsResized event is more accurate than GetPreferredSize
I'm trying to make a RichTextBox in a Windows Forms application grow and shrink to exactly contain its text. At first I tried TextRenderer.MeasureText, but it gave somewhat larger measurements than the text actually took. Calling GetPreferredSize on the control produced similarly wrong sizes. A Stack Overflow answer suggested listening for the ContentsResized event and using the provided rectangle. That indeed works after disabling WordWrap and scrollbars on the text box. The only downfall is that this measurer doesn't count trailing spaces in the rectangle, so as the user types a new word at the end the text jitters a bit.
Thursday, March 21, 2019
Testing SharedPreferences with Robolectric
I was looking for a way to simulate SharedPreferences for a Robolectric-tested application. It seems that the suggested way to do this involves AndroidX, a separate testing framework, which needs to be added as a dependency:
testImplementation 'androidx.test:core:1.1.0'
Then the application context can be retrieved with ApplicationProvider.getApplicationContext(), and a SharedPreferences can be obtained from getSharedPreferences as usual.
testImplementation 'androidx.test:core:1.1.0'
Then the application context can be retrieved with ApplicationProvider.getApplicationContext(), and a SharedPreferences can be obtained from getSharedPreferences as usual.
Wednesday, March 20, 2019
AutoScrollPosition coordinates need to be negated when restored
One of my Windows Forms applications has a Panel that auto-scrolls to contain many dynamically inserted controls. Sometimes its controls are cleared and regenerated. I want that to be seamless for the user, so the scroll position should be preserved. I tried recording the Value of HorizontalScroll and VerticalScroll and restoring those afterward, and that partially worked but didn't render the scrollbars in the correct place. I also tried saving/restoring AutoScrollPosition but that just left the scroll view in the upper left. I eventually found a Microsoft forum thread somewhere that said AutoScrollPosition is the property to use in this case, but the point set after the reset must be the negation (both X and Y coordinates made negative) of the original point. I'm not sure why that works, but it does.
Sunday, March 17, 2019
When Robolectric tests crash with "window creation failed"
Some students found that Robolectric tests on their Android project crashed with "window creation failed." This seems to be caused by trying to use findViewById before the view tree is ready. Most likely that's going on in an initializer on the same line as an instance variable declaration in an activity class.
Subscribe to:
Posts (Atom)