I have a mobile commerce app and I would like to show in the app that it is running in Development mode or Production mode so that there is no chance of a member of our team accidentally placing an order on the production server.
I had the foresight to derive all my Activity class from a super Activity class where I can add code that will run in all the app's activities. I tried to change the theme here and although the theme changes, I don't quite know how best to indicate the difference.
I would change the text in the TitleBar but my app doesn't have one. The next idea was to change background color of the entire app but some activities have a default color in the layout.
Ideally, I would like text somewhere on the screen saying something like 'dev mode' but I realize that this might be pretty hard to do so I'm up for interesting suggestions.
How about using a ViewStub for development mode?
You can place a ViewStub in each of your layouts where you want to indicate development mode. Your ViewStub can just hold a simple TextView of some sort that says "Development Mode", kind of like a Title bar.
In the code-behind get the reference to the ViewStub and pass it into that common method that runs in each activity. If you are in development mode, inflate the ViewStub.
Take a look at this:
http://developer.android.com/resources/articles/layout-tricks-stubs.html
Related
I have the following problem:
There is response with json-array, which contain colors settings from the server. And from difference companies difference settings. There are next colors:
1) A main color, it serves to fill background;
2) A second color, which highlights a text, icons and buttons on the background;
3) The color, the darker the first - used in the allocation;
4) The color that is used for coloring the boundaries of the text and icons, if 1st color matches with the 2nd;
(It is the analogy with colors on iOS, because we write native applications and have on server)
And I do not know whether you can manually set these colors in Theme for the entire application , or this configuration must be stored in the form of a XML-code, i.e. we need to write a parser that generated an xml-string (it is a difficult way for me =))?
In addition - if I want to change the Theme of entire application, how can I do this better programmatically? One way I know - all my activities inherited from class, which extends a class Activity, and in this class prescribe and change the Theme. Maybe there are other ways? But the main question about the generation of style.
Sadly, there is no clean and direct way to do this. The theme is part of the resources which are generated at compile time. And Resources.Theme is a final class, so it can't be overridden to do custom behaviors.
So the options you are left with are all bad:
Brute-force change each and every visual property of every widget affected by the customer theme. Yuck.
Dynamically create HTML pages that can be read into a WebView and used as the UI instead of Android widgets. You will probably have to write a lot of canned JavaScript code that will be part of this web UI.
The cleanest, but nearly impossible way, would go like this: As each customer gets registered in your corporate database (along with their color branding scheme), your build process generates a new expansion APK with a theme for that customer, then uploads it to Google Play. Your app would need to know to look for an updated expansion APK and get the user to download it (which may not always happen), then your JSON response would give you the customer name or whatever you need to determine which theme to pull from the expansion APK. Finally, your app would set the theme on the Activity for your white-label UI. So many things would have to happen correctly that it would be a minor miracle if your user saw your customers' corporate branding colors. But just thought I'd mention the idea for inspiration.
At the moment, I'm stuck with a very annoying kind of "bug" I assume regarding all Pre-Lollipop Android versions. It appears that (support) fragments don't apply the activity's theme they're assigned to. To make my explanations a bit easier, have the following demonstration:
My app runs with a turquoise theme at first. Let's say, the user decided to change the turquoise theme to a red theme. He or she restarts the app and is greeted with the following:
(screenshot taken on an Android 4.4.2 tablet)
Terrible sight, isn't it? However, if I run the same app in an emulator with Android L the whole theme problem doesn't even seem to exist.
There's especially one thing which seems odd about the tablet screenshot. The fragment itself doesn't apply the theme but child components inside the fragment which get added lateron (like the view with the exclamation mark which is hosted by a ViewPager) take and apply the theme as if nothing happened.
I'm not quite sure what the issue might be. I've done everything as stated in every document available. I set the theme before I call setContentView(resource) in the corresponding activity. I tried to do the trick with ContextThemeWrapper but it was no use.
Relevant code:
The activity's onCreate()
The fragment's onCreateView()
The fragment's layout
Attributes
Themes (a lot of them)
I tried to resolve this issue for days now and I still can't find out why this is not working. I haven't found a suitable answer yet and would love some advise.
I've been the victim to my own stupidity. I had another close look on my project setup and found this:
My tablet takes its layout resources from the sw600dp-folder, but I forgot to change the corresponding layout to take attributes instead of hardcoded colors. I think I have to retire after making such a stupid mistake.
I took a look at the preference activity that Android use for settings. i'm looking for something like UISwitch as shown in below image, does Android have one like this.
I can see the nearest control on Android preference settings is "list preference"
There is ToggleButton and CompoundButton (one extends the other).
But those have a different default display widget then what you are looking for. However either could be used along with your own drawable resources to create the "switch" type control that you are after.
But by default no the system does not include a widget that serves this function and looks the way you want it to.
No, In Android its called ToggleButton.
But still it can be developed by extending RadioButton placed in RadioGroup and giving its UI way you wanted.
I have been using the following workaround to center the activity label (without having to resort to a custom title in XML layout):
((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
This works great but every once in a while I see one of the users (on Google Play) getting a
java.lang.ClassCastException:
com.android.internal.widget.ActionBarView cannot be cast to
android.widget.TextView
These are probably tablet users who are running Android 3.x or higher.
Short of implementing my own custom title that would give me direct access to the activity label, can you recommend another way to avoid that ActionBarView to TextView ClassCastException?
Perhaps check for the Android version under which the app currently runs and go though another level of getChildAt(0)?
The action bar is defined in the theme. So if you want to avoid that ever showing up you need to pick different theme.
Alternatively you can detect what version of OS the user is using and in the case of being 11 (Honeycomb) and higher simply skip the action bar and get the next view.
Which would look something like
if (Integer.valueOf(android.os.Build.VERSION.SDK) > 11)
{
((TextView)((FrameLayout)((LinearLayout)((ViewGroup).getWindow().getDecorView()).getChildAt(1)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
}
else
((TextView)((FrameLayout)((LinearLayout)((ViewGroup).getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
I would also like to add like Shark said, this is a total hack and is not very robust code. You should be using findViewById() to locate your views and avoid all of that super ugly casting. You can expect your hack or even the code I just put in to break in the future if you continue down that path.
((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)
Start collecting child views from here, and search for your own TextView... You just need to skip the actionbar, no need to be nice about it when you started off with a hack anyway.
Working with Android for the first time, I've blocked out a layout using the relative layout and laid down some buttons and text widgets how I like them. However when I go back to rename the IDs the layout goes all crazy moving elements around and in general destroying the hours of work I spent laying them out.
Does anyone know how I can rename the widgets without Android destroying the positioning for widgets in the Relative Layout? Is this some "feature" of Android? I can't imagine why it would be hard for the UI builder to handle simple renaming of a widget ID without destroying the positioning information.. Do I have to use an external text editor and modify the XML files directly? Ughh I hope not.. I'm using Eclipse IDE.
You can use find and then replace all to change the names every place that they appear. Shouldn't take anywhere near an hour if you're dealing with a small layout.
In general the graphical UI creator that is currently included with the Android SDK is not so great for creating anything but very simple layouts. In my experiences (which were a long time ago, it may have gotten better since) it was terrible with RelativeLayouts.
If you have not modified your xml directly then it is time that you jump in and start learning to do it that way. You'll find that you have a much greater level of control over your layout, and once you get the basics figured out you'll probably be able to create quicker using raw xml then with the graphical tool anyway. I do wish that there were a nice GUI creator for android out there, the best one that I've ever come across is Droid Draw which I found to be better than the one included with the SDK, but still not as good as I was hoping.
To modify the xml directly you don't need any additional text editors, you do it inside eclipse. Open up your layout file and at the bottom click on the tab that says "Source" when you want to switch back to graphical (good to see the changes that you make to the xml graphically) just click back to the tab that says "Design"