Ways to reference an icon in an android app - java

I know you can reference an icon with
<item android:id="#+id/config"
android:icon="#drawable/ic_config"
android:title="#string/config"
app:showAsAction="ifRoom"/>
So this goes in the drawable folder and looks up the ic_config image.
But I recently saw this:
android:id="#+id/config"
android:icon="?iconConfig"
android:title="#string/config"
app:showAsAction="ifRoom"/>
Now I don't understand how the mapping between my config-image and ?iconConfig is working, I can see that 'iconConfig' appears in the R.java and attr.xml files, but nowhere else.
Can anyone explain?

Please go through the following link:
http://www.linuxtopia.org/online_books/android/devguide/guide/topics/ui/themes.html
Edit
for your convenience i am posting the required part from link provided above:
Just like styles, themes are also declared in XML elements,
and are referenced in the same manner. The difference is that you add
a theme to an entire application or activity, via the
and elements in the Android Manifest — themes cannot be
applied to individual Views.
Here's an example declaration of a theme:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomTheme">
<item name="android:windowNoTitle">true</item>
<item name="windowFrame">#drawable/screen_frame</item>
<item name="windowBackground">#drawable/screen_background_white</item>
<item name="panelForegroundColor">#FF000000</item>
<item name="panelBackgroundColor">#FFFFFFFF</item>
<item name="panelTextColor">?panelForegroundColor</item>
<item name="panelTextSize">14</item>
<item name="menuItemTextColor">?panelTextColor</item>
<item name="menuItemTextSize">?panelTextSize</item> </style> </resources>
Notice the use of the at-symbol (#) and the question-mark (?) to
reference resources. The at-symbol indicates that we're referencing a
resource previously defined elsewhere (which may be from this project
or from the Android framework). The question-mark indicates that we're
referencing a resource value in the currently loaded theme. This is
done by referring to a specific by its name value. (E.g.,
panelTextColor uses the same color assigned to panelForegroundColor,
defined beforehand.) This technique can be used only in XML resources.

'?' is used when you need to refer to the drawable while having several themes. It is made to simplify the logic when switching between themes and let the Android automatically decide which resource to use.

Related

in Android, is there any way of use classes to format a group´of views?

I'm beginning in Android development and I got very familiar with the XML notation, since it's pretty similar to HTML.
By the XML we can set ID's to the views for later managing it with Java, as like we do with HTML and JS.
My point is: Can I define classes or something like that to set similiar attributes to a group of views?
Sorry if I was unclear or anything, and thanks for you help!
You can define styles and use those across widgets.
Here are the official docs for it:
http://developer.android.com/guide/topics/ui/themes.html
You can do this using styles.
in res/values/ create a file named styles.xml [Auto generated in Android Studio]
You can define new style using the style tag :
<style name="styleName"></style>
You can also modify an already present style by using :
<style name="styleName2" parent="alreadyExistingStyle"></style>
Than you use it in xml and as well as Java.
In xml :
style="#style/styleName"
In Java :
R.style.styleName
Example :
Button Style :
<style name="AppButtonLight" parent="#android:style/Widget.Button">
<item name="android:textColor">#FF000000</item>
<item name="android:layout_margin">5dp</item>
<item name="android:textSize">16sp</item>
<item name="android:background">#drawable/hw_btn_default</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
</style>
Than use it in xml :
<Button
style="#style/AppButtonLight"
android:layout_weight="4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="#string/btnDownload"
android:id="#+id/btnDownload" />
Hope I was able to help you.

I want to hide my title bar in my android application [duplicate]

I am trying to use a custom title to include an image button to the title bar.
I got a lot of help form this post: android: adding button to the title of the app?, but could not get it work for my ListActivity.
In a nutshell, following is what I have:
I hide the titlebar in the AndroidManifest.xml
The specify a relative layout for the custom title (workorder_list_titlebar.xml)
My Activity Class looks like the following:
public class WorkOrderListActivity extends ListActivity {
String[] orders={"WO-12022009", "WO-12302009","WO-02122010", "02152010"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
setContentView(R.layout.workorder_list);
setListAdapter(new ArrayAdapter(this,R.layout.workorder_list, R.id.label,orders));
}
}
When I ran the app, I got AndroidRuntimeException: You cannot combine custom titles with other title features.
Base on the stack trace, the exception was thrown by com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183), that was triggered by setlistAdapter call.
Does anyone have the same problem with ListActivity?
Also once I manage to get this work, how do I attach listeners to the image button for it to do something?
Thanks in advance.
I had the same issue and I fix it deleting
<item name="android:windowNoTitle">true</item>
from my theme.xml
Make you create custom style in “values” folder. Make sure you code as below.
<style name="CustomTheme" parent="android:Theme">
Don't modify parent parameter.
This did work for me.
Instead of modifying your theme.xml you may also:
create a new XML style file my_theme.xml in values folder like this:
<style name="MyWindowTitleBackground">
<item name="android:background">#444444</item>
</style>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/MyWindowTitleBackground</item>
</style>
You may define other settings as you like in this theme.
Then just use this theme in your manifest within the activity's attributes
android:theme="#style/MyTheme"
Finally set your custom title as always in your activity.java:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if (window.getContainer() == null) {
useTitleFeature = window
.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(R.layout.screen_main);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title);
// Set up the custom title
main_title = (TextView) findViewById(R.id.title_left_text);
main_title.setText(R.string.app_name);
main_title = (TextView) findViewById(R.id.title_right_text);
main_title.setText(R.string.Main_titleInfo);
}
Don't forget to define the custom_title.xml file in your layout folder. For example...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/title_left_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="#+id/title_right_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#fff" />
</RelativeLayout>
I think notenking is right, that this is a problem in activities within tabs. Since some of my activities can either be stand-alone or within a tab, I've found the following helps:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if(window.getContainer() == null) {
useTitleFeature = window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(layoutId);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}
May be you find this problem when use it in tab,for there already have a title and you can not add a custom title again.
you should add this custom title in the activity which you get the Tab
I did exactly as Sunny Dasari did but with one small change I put the # before and android in the parent attribute.
So my code looked like this.
<style name="CustomTheme" parent="#android:Theme">
To avoid crashing, you can simply add
android:theme="#style/android:Theme"
to the <Activity> tag in your AndroidManifest.xml:
<activity
android:name="test.TestActivity"
android:label="#string/app_name"
android:theme="#style/android:Theme">
This is because the styles defined in your default theme conflict with FEATURE_CUSTOM_TITLE (such as the attribute android:windowNoTitle). By using another theme, you can avoid such problems.
However, you might further need to define your own theme to change other attributes, such as android:windowTitleSize, background color, text color and font, etc. In this case, you can derive your theme from an existing theme (e.g., Theme.Light) and modify its attributes:
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
</style>
<style name="CustomTheme" parent="#style/android:Theme.Light">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">60dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
</resources>
Try swapping following lines:
setContentView(R.layout.workorder_list);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
I have run into this issue as well and it looks like it is an issue with what theme is applied to an activity in the AndroidManifest.xml file. If I use a theme like:
android:theme="#android:style/Theme.Holo
Then it will throw the error
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
However if I use a different theme like:
android:theme="#android:style/Theme.Black"
then it will not throw the error and subsequently will not crash. However I am trying to use a theme like Theme.Holo. I'm not sure if there is a way around this.
Since, I was trying to compile my program in android 4.0, I was facing the same problem. None of these solutions helped.So, I copied my style contents from values > styles.xml and pasted it in values-v11 styles.xml file and values-v14 styles.xml file. Bingo, the trick worked.
As a beginner most of the answers didn't help me for my case. So here is my answer.
Go to res/values folder in your android project and check for strings.xml (this file may vary in your case, something like themes.xml)
Inside the file under resource tag check whether you have style tags. If you don't find it, add the code below as mentioned below as a child to resources tag
something like below
<resources>
<style name="SomeNameHere">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
if you already have style tag, just add the code below to your style tag
<item name="android:windowNoTitle">true</item>

No resource found that matches the given name, where I do something wrong?

I can't figure why I get this error :
no resource found that matches the given name
I have a folder called "anim" inside the Resources folder, inside the anim folder I have 2 files: slide_up.xml and slide_right.xml
Here is my style file :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowBackground">#drawable/splash</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowTitleSize">0dp</item>
</style>
<style name="DialogAnimation">
<item name="android:windowEnterAnimation">#anim/slide_up</item>
<item name="android:windowExitAnimation">#anim/slide_right</item>
</style>
</resources>
I am keep getting this error every time I am tiring to build the project, I am 100% sure that the file names and the path are correct, so why the project doesn't recognize the files ?
Edit : This is how I call the animation :
public override void OnActivityCreated (Bundle savedInstanceState)
{
Dialog.Window.RequestFeature (WindowFeatures.NoTitle); //Sets the title bar to invisible
base.OnActivityCreated (savedInstanceState);
Dialog.Window.Attributes.WindowAnimations = Resource.Style.DialogAnimation; //Sets the animation
}
Thank you for helping me out but I have figured out the answer, its was pretty simple but I really have no idea why this happens because in every other place in the code everything works fine.
I found the answer here, by Stephen Wylie and I quote the answer :
"This problem appeared for me due to an error in an XML layout file. By changing #id/meid to #+id/meid (note the plus), I got it to work. If not, sometimes you just gotta go to Project -> Clean"
As soon as I added "+" to the "#anim/slide_up" so now its like "#+anim/slide_up" the code seems to work.
I posted this just in-case someone else will have the same struggle because I was trying to fix this for 5 hours now..
From API guides->animation resources->View animation:
resource reference:In Java:
R.anim.filename
In XML:
#[package:]anim/filename
Maybe try adding the package?

Android Styles For API 10 and API 11

I am trying to do something that I feel to be very simple in concept. I would like my app to be supported all the way back to API 10 (Gingerbread). To make this look good, I need to make a slight change to the color of the text on my buttons when the device is running API 10. Thus, I want to create two styles: one of which will be used when the device is using API 10 (I want the text color of the buttons to be black in this case), and another when the device is using API 11 or above (the text color will be the default ICS grayish in this case). To do this, I am using a values and a values-v11 folder. Inside the values folder is a themes.xml file with the following code:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="buttonColorStyle">
<item name="android:textAppearanceButton">#style/buttonTextColor</item>
</style>
<style name="buttonTextColor">
<item name="android:textColor">#FFFFFF</item>
</style>
</resources>
However, when I load up my app with target SDK set to 10, the text color of the buttons is unchanged from the default grayish. Also, here is the code for one of my buttons which should use this style:
<Button
style="#style/buttonColorStyle"
android:id="#+id/thirdSectionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="sectionButtonClicked"
android:text="Section 3"
android:textSize="11.5sp" />
Anyone have any ideas?
You have to do something like this:
<style name="MyStlye">
<item name="android:textAppearanceButton">#style/BtnText</item>
</style>
<style name="BtnText" parent="android:TextAppearance.Small.Inverse">
<item name="android:textColor">#android:color/black</item> // or whatever color
</style>
why not setting the same nice style for all versions?
you can use this library (also suggested at the android developers blog here) for making all of the devices use the holo theme.
alternatively , just leave the current style , so that the user will feel more "at home" with his current style of the OS (since it can change between companies) .

Android FragmentTransaction setTransitionStyle

I'm trying to customize my FragmentTransaction transitions and I came across the setTransitionStyle method. It takes in an xml resource id for a style, but I have no idea what the xml resource would look like. I know you can define animation styles for activities, and I assume the xml needed for this method is similar, but I can't find any documentation on the required format (e.g. the xml attributes/nodes needed to make this work).
EDIT1 (this is what I'm doing now in my FragmentActivity):
public void pushFolderFrag(Fragment folderFrag, String backStackID) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.SplitView_MasterContainer, folderFrag);
transaction.addToBackStack(backStackID);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
//transaction.setTransitionStyle(arg0);//what does the format for this resource look like??
// Commit the transaction
transaction.commit();
}
I found the answer on this link
https://github.com/kedzie/Support_v4_NineOldAndroids
Transition style resources
Specify transition animations in a style resource.
Create a style resource `res/values/styles.xml'
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Override standard Transitions with a Style -->
<style name="MyTransitionStyle">
<item name="fragmentFadeEnterAnimation">#animator/fade_enter</item>
<item name="fragmentFadeExitAnimation">#animator/fade_exit</item>
<item name="fragmentOpenEnterAnimation">#animator/flip_left_in</item>
<item name="fragmentOpenExitAnimation">#animator/flip_left_out</item>
<item name="fragmentCloseEnterAnimation">#animator/flip_right_in</item>
<item name="fragmentCloseExitAnimation">#animator/flip_right_out</item>
</style>
</resources>
Specify the resource and transition in the transaction
tx.setTransitionStyle(R.style.MyTransitionStyle);
tx.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
I know this does not exactly answers the question, but why don't you use setCustomAnimations() instead?
This call takes property animation resources if you use Android 3+, and view animation resources if you use the Support Package.

Categories

Resources