I'm trying to write a general style.xml for my app.
I want all my EditText views to have certain styling, so I've done something like this:
<!-- Base application theme. -->
<style name="MyAppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:editTextStyle">#style/App_EditTextStyle</item>
</style>
<style name="App_EditTextStyle">
<item name="android:background" >#ff0000</item>
<item name="android:textColor">#00ff00</item>
<item name="android:layout_marginLeft">50dip</item>
<item name="android:height">50dip</item>
</style>
In the manifest i apply the theme:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyAppTheme" >
and here is the sample layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:text="Test" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:text="Test2" />
</LinearLayout>
Everything is working fine except for the margin. For some reason it does not apply and i have to add it manually to the EditText. Am i missing something?
If you want to do it for all EditText widgets, then apply this to the LinearLayout tag:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout"
style="#style/MyAppTheme">
Related
I have an app and in the Login screen I have multiple ways to login like gmail, facebook and manual login. They all are working nicely but the problem arises when I try to hide the top bar which shows the app name. I know this has something to do with the theme in the manifest but I haven't set any. Please have a look at my code and see what I am doing wrong ...
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.spuck"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="23" />
<application
android:allowBackup="true"
android:icon="#drawable/the_spuck_4"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity android:name=".LoginActivity"/>
LoginActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.spuck.LoginActivity"
style="#style/Red">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="94dp">
<Button
android:id="#+id/sign_in"
android:layout_width="260dp"
android:layout_height="35dp"
android:textSize="13dp"
android:text="Sign In"
android:textColor="#color/white"
android:textAllCaps="false"
android:background="#drawable/signin_button_login_activity"
android:gravity="center"
android:layout_below="#+id/login_button"
android:layout_alignLeft="#+id/sign_in_button"
android:layout_alignStart="#+id/sign_in_button"
android:layout_marginTop="33dp" />
<ImageView
android:id="#+id/app_name"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/the_spuck_3_2"
android:background="#drawable/white_with_round"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
<com.google.android.gms.common.SignInButton
android:id="#+id/sign_in_button"
android:layout_width="260dp"
android:layout_height="35dp"
android:layout_marginTop="41dp"
android:layout_below="#+id/app_name"
android:layout_centerHorizontal="true" />
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="255dp"
android:layout_height="60dp"
android:textColor="#ffffff"
android:layout_marginTop="10dp"
android:shadowColor="#000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:layout_below="#+id/sign_in_button"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</RelativeLayout>
AppTheme.NoActionBar.xml
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#ff5050</item>
<item name="colorPrimaryDark">#ff5050</item>
</style>
Change the theme of the activity to NoActionBar :
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#ff5050</item>
<item name="colorPrimaryDark">#ff5050</item>
</style>
As Nika said, his approach is absolutely correct. What you need to see is that there are 2 types of themes made. One for API 21 and above and the other for API 21 and below. What you need to do is to assign the theme he said to both versions since you want your app to work for both API conditions. See the code below and let me know if it helped ...
for API 21 and above:
<resources>
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#ff5050</item>
<item name="colorPrimary">#ff5050</item>
<item name="colorPrimaryDark">#ff5050</item>
</style>
</resources>
for API 21 and below:
<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">#ff5050</item>
<item name="colorPrimaryDark">#ff5050</item>
</style>
Now to access this you need to change your manifest to this:
<activity android:name=".LoginActivity"
android:theme="#style/AppTheme.NoActionBar"/>
P.S if you are not sure on how to see the themes for their respective API hold the ctrl button and click on "AppTheme.NoActionBar" text, it will show you two fields where they are to be found and just edit both of them.
I want to build a theme for my app. But it seems like my LiestView item don't apply the values.
This is the styles.xml:
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:textViewStyle">#style/TextView</item>
</style>
<style name="TextView" parent="android:Widget.TextView">
<item name="android:layout_margin">8dp</item>
<item name="android:padding">0dp</item>
<item name="android:textColor">#87000000</item>
<item name="android:textSize">18sp</item>
</style>
in my activity i use a Listview with Custom Adapter but the TextViews from my item.xml did'nt have a margin:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical"
tools:context="de.resper.enigma2chromecast.mainLive" >
<TextView
android:id="#+id/channelName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="#string/hello_world" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:id="#+id/channelLine1"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray" />
</LinearLayout>
what ever you designed xml, put in android manifest file in application theme
<application android:theme="#style/CustomTheme">
I'm trying to copy this layout out of the beautiful Android Timely app
Specifically, the translucent box that has all the alarm info inside it. Not sure if it's a layout with a slightly different fill color than the background with the alpha value set really high.
I do something like this:
Manifest.xml
<activity
android:theme="#style/PopupTheme"
android:configChanges="orientation|screenSize"
android:name="your.package.Activity">
</activity>
Styles.xml
<style name="PopupTheme" parent="Theme.AppCompat.Base.CompactMenu.Dialog">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:windowIsFloating">true</item>
</style>
Color.xml //You can play with the alpha value
<color name="transparent_black">#A0000000</color>
myLayout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:gravity="center">
<LinearLayout
android:id="#+id/root"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center"
android:background="#color/transparent_black">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/transparent"
android:layout_marginTop="30dp">
<Button
android:id="#+id/guardar"
style="#style/boton_aceptar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/action_save"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
And the result: the white part it's gonna be the current screen in the phone.
I have created a custom title bar with the following.
myapptitle.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTitle"
android:text="#string/app_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/titletextcolor"
android:textSize="14sp"
android:layout_gravity="top"
/>
themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/WindowTitleBackground</item>
<item name="android:windowTitleSize">40dip</item>
</style>
</resources>
manifext.xml
<application
android:label="#string/app_name"
android:theme="#style/customTheme" >
.....
I have created a customList adapater and a layout for that like the one below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/icon"
android:layout_width="22dip"
android:layout_height="22dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="10dip"
android:layout_marginTop="4dip"
android:contentDescription="#string/app_msg_title"
android:src="#drawable/logo_small" >
</ImageView>
<TextView
android:id="#+id/msgDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
/>
<TextView
android:id="#+id/msgTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="12sp" >
</TextView>
</LinearLayout>
In my list activity I have given the below code on onCreate method.
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.myapptitle);
But somehow while running the application I'm getting the title bar without any text in it at all. In my strings.xml for app_name i have My Notifications as text.
Appreciate all your time and help.
Edited
Styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="WindowTitleBackground">
<item name="android:background">#color/titlebackgroundcolor</item>
</style>
</resources>
Change your style.xml like below:
<resources>
<style name="CustomWindowTitleBarBG">
<item name="android:background">#323331</item>
</style>
<style name="TitleBarTheme" parent="android:Theme">
<item name="android:windowTitleSize">35dip</item>
<item name="android:windowTitleBackgroundStyle"> #style/CustomWindowTitleBarBG</item>
</style>
</resources>
Then
In manifest file, use TitleBarTheme in application tag and for activity use like below:
android:theme="#android:style/Theme.NoTitleBar"
and in activity class use your code which is:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.myapptitle);
I want to have a ImageButtons changing the image based on its state. (same as "like" button in nine gag and facebook apps).
I have defined a style for each of them that references a selector as a drawable but when I run the program, images doesn't appear. Can you tell me what's wrong with this approach? (sorry for my bad English)
comment_actions_style in values folder:
<style name="LikeStyle">
<item name="android:src">#drawable/like</item>
</style>
<style name="DislikeStyle">
<item name="android:src">#drawable/dislike_normal</item>
</style>
<style name="ReplyStyle">
<item name="android:src">#drawable/reply_normal</item>
</style>
<style name="ShareStyle">
<item name="android:src">#drawable/share</item>
</style>
</resources>
and like drawable in drawable folder: (other buttons are the same)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/like_clicked" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="#drawable/like_focused" android:state_focused="true"/>
<!-- focused -->
<item android:drawable="#drawable/like_normal"/>
<!-- default -->
</selector>
EDIT:
I forgot to mention, I have a list view that its items are user comments and comments have buttons described above.
here is my items layout (part of it)
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/comment_content"
android:orientation="horizontal"
>
<include
android:id="#+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
style="#style/LikeStyle" />
</LinearLayout>
and comment_actions layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/comment_action"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dip">
<ImageButton
android:id="#+id/comment_action_img"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
/>
<TextView
android:id="#+id/comment_action_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/comment_action_img"
android:gravity="center"
android:layout_marginLeft="5dip"/>
</RelativeLayout>
These buttons also inherit from a layout if that's matter.
Here, your are setting style for comment_actions layout, and it is a Relative Layout, which won't be abled to response to your "android:src" attribute.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/comment_content"
android:orientation="horizontal"
>
<include
android:id="#+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
style="#style/LikeStyle" /> <!--here,you are setting a style for the RelativeLayout -->
</LinearLayout>