12

How can I use CSS in my Android app?

Christian Strempfer
  • 6,937
  • 5
  • 43
  • 73
Android
  • 8,412
  • 9
  • 64
  • 107

2 Answers2

22

Native Apps

If you want to style a native android app, there is no support for CSS. Instead android has it's own mechanism.

Here's an introduction: http://developer.android.com/guide/topics/ui/themes.html

The complete list of options is only available at the source code.

Native App with local HTML

You may use WebViews in your native Android app, to display HTML which is packaged with the app, then you can use CSS like in any HTML site.

Using special frameworks

There are frameworks which enable you to implement mobile apps with HTML, CSS and JavaScript. They compile it into native apps to allow usage of phone features like gyroscope.

Web Apps

Web Apps are HTML sites, which are optimized for mobile phones. Of course you can use CSS for your site. An example for mobile optimizations is an offline mode, which uses HTML5's storage mechanisms to bridge connection gaps.

Community
  • 1
  • 1
Christian Strempfer
  • 6,937
  • 5
  • 43
  • 73
  • 2
    I would discourage people from trying to bring literal html or css into a android environment, simply because if you have arrived at android development (or java), learn the android way ... then in the future, when you are an expert in android ways, and you find yourself in a need to import another 'language', you will be more than qualified to do so .. – dsdsdsdsd May 12 '16 at 15:21
2

If you want CSS-like style guiding for Android native apps, consider using Scaloid library, which is I wrote :D

For example:

new SVerticalLayout {
  style { 
    case b: SButton => b.textColor(Color.GREEN).onClick(toast("Bang!"))
    case t: STextView => t.textSize(17 dip)
    case v => v.backgroundColor(Color.BLUE)
  }

  STextView("I am 17 dip tall")
  STextView("Me too")
  STextView("Oh, I am taller than you").textSize(24 dip) // overriding
  SEditText("Am I blue?")
  SButton("I am a green monster!")
}

It is simple, expressive and type-safe. This example came from the Scaloid blog:

http://blog.scaloid.org/2013/01/a-css-like-styling-on-android.html

pocorall
  • 1,007
  • 1
  • 8
  • 20