In my app, portrait would have a Button where landscape would NOT have a Button, so I use <include> and did this:
layout/activity_main.xml
...
<include layout="#layout/my_button" />
...
values/layout.xml
...
<item name="my_button" type="layout">#layout/my_button_layout</item>
...
values-land/layout.xml
....
<item name="my_button" type="layout">#layout/empty_layout</item>
layout/my_button_layout.xml
...
<Button...... <!-- my button XML -->
...
layout/empty_layout.xml
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
Now, this works, but I don't quite like the idea of having an useless view whenever the app's in landscape.
I tried to do:
<item name="my_button" type="layout">#null</item>
but this would throw a ResourceNotFound Exception
Is there a way around this?
(Yes I know I can do this programmatically, but then it defeats the purpose of Android's layout system)
You could use two styles. One in an xml file in a portrait folder and the other in another xml file in the landscape folder. These styles could then set android:visibility="gone" and android:visibility="visible" then just set the style onto the button using style="#style/MyButtonStyle"
Related
In my current project I have about 10 buttons that are almost the same. I'm looking for a way to reuse the code for these buttons.
I've tried to move part of code to separate layout and reuse it via <include... layout=...>, but no success.
I use binding in the project and the compiler does not allow me to cast this View to TextView for calling it from the code. I've tried to move part of this code and set it as a style. No way :-( I have a few selectors here (for different states including enabled and disabled). However, they don't work.
<TextView
android:id="#+id/tv_next_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:layout_marginTop="20dp"
android:layout_marginBottom="40dp"
style="#style/Myfont_Bold"
android:layout_gravity="center_horizontal"
android:background="#drawable/selector_button_next_bg"
android:drawablePadding="36dp"
android:letterSpacing="-0.01"
android:lineSpacingExtra="3sp"
android:padding="#dimen/padding_15"
android:text="#string/next"
android:textColor="#drawable/selector_button_next_font"
android:textSize="#dimen/text_16"
app:drawableEndCompat="#drawable/selector_button_next_arrow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#id/ll_switchers"/>
Any ideas?
I don't want to copy-paste this code 10 times.
Create a style for reused styled Views and apply it in every layout it used
<style name="MyCustomStyle" parent="DesiredParentStyleFromLibrary">
<item name="attribute1">value</item>
<item name="attribute2">value</item>
<item name="attribute2">value</item>
// and so on
</style>
I am developing a simple app in Android Studio, and I am testing it on a real device (Asus zenfone 5 & Android 4.4.2). In the main activity I have applied this layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="madapps.mysecondapp.MainActivity"
android:id="#+id/activity_main" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"
android:orientation="horizontal"
android:background="#drawable/background_deathstar">
<EditText
android:id="#+id/edit_message"
android:text="#string/message" android:layout_weight="1"
android:layout_width="0dp" android:layout_height="wrap_content"
android:hint="#string/edit_message" android:textColor="#FFF" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
with this style.xml applied to the activity
<resources>
<style name="AppTheme" parent="android:Theme.Holo">
<item name="android:windowActionBarOverlay">true</item>
</style>
</resources>
What I want to do is testing a background image on different screen sizes and densities (dpi).
Now, the problem is that while in my physical device the property
android:windowActionBarOverlay
is applied properly, laying out my action bar on top of my background image, this is not working equally within Android Studio's layout editor, meaning that my image begins at the end of the action bar.
ps. I have also noticed that apart from ignoring the windowActionBarOverlay property, my action bar fails to load my custom action buttons. I think these two problems are somewhat related.
Any suggestions?
Thank you very much
Apparently I should have not expected Android Studio's layout editor to reflect what I'm seeing on devices, because it's only using a subset of the real Android layout system.
Actually my action bar is hidden and shown programmatically, and the layout editor is only supposed to serve as a way to drag-and-drop widgets and preview a layout while editing the XML.
I have a custom EditText declared in an XML file and I'm including it like so:
<include layout="#layout/my_edit_text"
android:id="#+id/passwordField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/passwordHint"
android:inputType="textPassword" />
and here is my_edit_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:textColor="#color/gray"
android:textSize="#dimen/editTextFontSize"
android:padding="#dimen/editTextPadding"
android:background="#drawable/edit_text_background"
android:ellipsize="end" />
However, I can't set the hint or inputType this way, for some reason. If I set it in my_edit_text.xml, it works fine, but I would like to be able to set each reference individually.
The reason that I have the custom EditText is to avoid having to rewrite all of the common values in every one of my EditTexts.
Do I have to do something similar to what this person has? If I do, will I need to actually build a .java subclass and extract the attributes that way? That just seems excessive.
The reason that I have the custom EditText is to avoid having to rewrite all of the common values in every one of my EditTexts.
Step #1: Delete my_edit_text.xml.
Step #2: Delete all references to my_edit_text.xml.
Step #3: Define a style resource (e.g., in res/values/styles.xml) akin to:
<style name="rileyText">
<item name="android:textColor">#color/gray</item>
<item name="android:textSize">#dimen/editTextFontSize</item>
<item name="android:padding">#dimen/editTextPadding</item>
<item name="android:background">#drawable/edit_text_background</item>
<item name="android:ellipsize">end</item>
</style>
Step #4: Add style="#style/rileyText" to all EditText widgets that you want to have those particular attributes applied to.
I have an Android application that uses a MapView with an ImageButton control (to move to the user's current location) I've added in the top right-hand corner. The problem I am having is that the ImageButton control's background is too transparent, but changing it with android:background="#BBFFFFFF" alters both the size of the background and removes the blue "flash" that you normally see when the button is pressed - two qualities I wish to retain.
I start with something like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="my api key"
/>
<ImageButton android:id="#+id/googlemaps_select_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="13dp"
android:layout_marginRight="13dp"
android:src="#drawable/device_access_location_found"/>
</RelativeLayout>
Which achieves something that looks like this:
So then I add:
android:background="#BBFFFFFF"
And I get this:
Note that although this is basically the level of opacity I want, changing the background has affected the padding, and also doesn't display a blue "flash" when pressed (which obviously isn't illustrated in this question).
So my question is, how can I change just the background color/opacity in the non-pressed state, while retaining the other visual qualities of the button? I had a brief read about Android styles and themes, but can't even figure out where this button is getting its style/theme from and how I would go about just overriding the background color/opacity while retaining all of the other visual features.
Issue
When you are assigning a fixed color to the a view background, you are replacing the default background in the view by the fixed color you define.
In reality, the background of a button is not a simple fixed color. It's a state list of color or drawables, which means, depending on button status (focous, selected, pressed, etc.) a different background is used, resulting in the "flash" animation you see when button is pressed. If you replace this state list by a simple fixed color, not depending on buttons status, you get a fixed background (i.e. not changing when button is pressed).
Resolution
There is a xml parameter that can be used to change the image view's alfa (i.e. transparency) which is:
android:alpha="1"
where the value 1 above can be any float between 0 and 1, being 1 the maximum opacy.
However, I believe this is not solving your issue, because you want to change the alfa of background not the image alfa, if I correctly understood your issue. Anyway the default seems to be 1.
One possibility the should work for you is to define a selector to be used as background. The selector will choose the drawable based on his status.
Example
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/darker_gray" />
<item android:drawable="#android:color/white" />
</selector>
Save the xml file above in your drawable-xxxx folder with the name my_selector
In this example I'm using standard android colors, but you can define your own colors. You need to assigne a color for each different button status that you want to have a different color.
Then you need to define your ImageView backgroung to be the selector you defined above:
<ImageButton
android:id="#+id/googlemaps_select_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="13dp"
android:layout_marginTop="13dp"
android:background="#drawable/my_selector"
android:src="#drawable/device_access_location_found" />
With the above changes, the bacground color used by the button will change when the button is pressed and you can have the "flash" effect.
I ended up using a style to inherit the look of Widget.ImageButton with just a few minor tweaks for my purposes:
My /res/values/styles.xml file now looks like:
<resources>
<style name="AppTheme" parent="android:Theme.Light" />
<style name="my_loc_btn_style" parent="#android:style/Widget.ImageButton">
<item name="android:layout_marginTop">8dp</item>
<item name="android:layout_marginRight">8dp</item>
</style>
</resources>
And my layout file has:
<ImageButton android:id="#+id/googlemaps_select_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
style="#style/my_loc_btn_style"
android:src="#drawable/device_access_location_found"/>
This seems to have inherited a background from Widget.ImageButton that seems to be just slightly transparent, which is what I was after anyway, so I don't set the transparency at all now.
I have a ListView, and it works great on a phone. Now I am making a tablet UI, with the ListView on the left and details on the right.
When I touch an item, it flashes blue as long as it is pressed. I want to keep that blue color until another item is selected, just like the Gmail app on the Nexus 7.
What is the cleanest way to achieve that? I'd rather avoid setting backgrounds manually, I assume there is a way to mark an element as the "active" one and theme it accordingly.
What is the cleanest way to achieve that?
What you are looking for is known as the "activated" state. To make this work:
Step #1: In res/values-v11/, have a style resource that implements activated. For example, for a new project that has the AppTheme declaration defined there, go with something like:
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light"></style>
<style name="activated" parent="AppTheme">
<item name="android:background">?android:attr/activatedBackgroundIndicator</item>
</style>
</resources>
Step #2: Define the same style in res/values/ for any older devices, just as a stub style resource, so references to it continue to work:
<resources>
<style name="AppTheme" parent="android:Theme.Light"/>
<style name="activated" parent="AppTheme"/>
</resources>
Step #3: In your layout XML resource for the row in the ListView, add style="#style/activated" to the list of attributes of the root element
Step #4: Set the ListView to be a single-choice list, such as the following line in a ListFragment:
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
You can see this in action in this sample project, this sample project, and this sample project. For more background on those first two samples, see this SO question: Complete Working Sample of the Gmail Three-Fragment Animation Scenario?
Using
android.R.layout.simple_list_item_activated_1
instead of
R.layout.simple_list_item_checkable_1.
Just for somebody checking someday.
after days of search and pulling my hair i just found out that activatedBackgroundIndicator is also available in ActionBarSherlock styling system. Most of the devs which develop backwards compatible apps, use ActionBarSherlock,so using ActionBarSherlock is a good option for most cases. So instead of using android:background="?android:attr/activatedBackgroundIndicator" which will give errors in android versions prior to 11, just use: android:background="?activatedBackgroundIndicator"
here is the example row layout xml code:
<?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"
//note the activatedBackgroundIndicator
android:background="?activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingBottom="2dip"
android:paddingTop="2dip" >
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textSize="15sp" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="5dip"
android:textSize="20dip" />
</LinearLayout>