I am trying to make menu from the code. I want to know what is the programmatic equivalent of android:showAsAction. I know we can have it in XML.
Call setShowAsActionFlags with one of the constants you want.
See constants section for constants you can pass.
Related
As an Android developer new to iOS/swift, I am trying to implement a global theme/style which every view controller can inherit. I expect one file which I can modify which would affect all View Controllers's UI. This is possible in android. Does iOS have anything similar?
The best match for what you are looking for is probably UIAppearance, although not quite the same.
What it allows you to do is basically setup each component once, and then have that be the default look for all other components.
An example for setting button colors (you could do this in your AppDelegate):
UIButton.appearance().setTitleColor(.white, forState: .normal)
UIButton.appearance().backgroundColor = .blue
Try looking at these tutorials:
https://www.raywenderlich.com/108766/uiappearance-tutorial
http://blog.iseinc.biz/use-uiappearance-create-ios-app-themes
Usually if you want to do something like that, you can go to the AppDelegate File, and add code like the ones below:
[[UITextField appearance] setTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTranslucent:NO];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
For example textfield by default has a bluish tint color, but with the code above, the default tint color becomes red. The single line of code you place will affect all UI related to it. Try it out.
This question already has answers here:
How exactly does the android:onClick XML attribute differ from setOnClickListener?
(17 answers)
Closed 9 years ago.
I'm following Google's Android tutorial and discovered that there are two ways you get widget callbacks as per title (or only onClick - I don't know).
I'm a Senior Java Swing Developer so the inner class approach make me feel at home :) But I understand that the xml approach is newer - so google must have added it for a reason.
What is the reasoning here? Is it "nicer" to do it this way on the android platform, should the inner class approach now be avoided (on versions that support it)?
I am not using the XML onClick attribute because that means the Activity that is inflating the XML must implement the onClick value method. But if you do some refactoring and you change this method, then you'll get runtime exceptions if the changes are not correlated to XML. Or if you want to use some include or merge.
To add more: if you use fragments you have to delegate the click event to the fragment that defined onClick XML attribute.
It's less code indeed, but in order to maintain/refactor such code it makes things difficult and open to errors. So don't use it in production code.
You can define widgets like button both by xml and programatically. Can has given a capabity to add the listener both ways. So there is no advantage of one over another.
If you want to do layout specific work from xml, the android has given you a capability to do that.
But someone may define layout progmatically and then will have to define click listener from code.
But there are people who use a mix of it.
I hope you understand what I mean.
If you do it programatically, you can just write onClick() and iside that write a switch case and based on view ids you can define the behaviour which i personally feel is easier to work with.
If the buttons are going to be there always and same action is going to be executed always, then using declarative event handlers makes sense. Like when you don't even need to do a findViewById() for that button.
If you may want to enable/disable clicking or may be generating buttons dynamically etc etc, then setting up event handlers dynamically in code makes sense.
View.OnClickListener is an interface, which defines the onClick(View) method.
You will implement both the interface and the method in your code.
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.
Is it possible to set tab's indicator not in code behind (by using setIndicator), but in XML?
No, sorry, you can only define tabs (whether TabHost or the action bar) in Java.
No, AFAIK there is no way to set in layout xml. If you want those values dynamic, you may add them in separate xml and parse in program and set.
no,It is not possible
But still check it outTabs tutorial
Does something exist for Android? I am looking for something equivalent to HTML's tag. I see things like NumberPicker, but I can't see any way to have a basic drop down selector.
I am aware of ListPreference, but I want to use it outside the context of a PreferenceActivity and inside arbitrary views. Anyone?
Are you looking for the spinner, maybe?