I created a separate layout for tablets in android studio, but I'm unable to run that particular layout, whenever I try to run it, it default runs the normal layout, which is for mobile
I have to agree with the user Ricardo but having said that, I need some more information. First of all how you load the layout, what is the layout name and where did you place these layouts.
Check this before https://developer.android.com/training/multiscreen/screensizes.html and you have to do something like this.
In your manifest
<resources>
<bool name="isTablet">true</bool>
</resources>
<resources>
<bool name="isTablet">false</bool>
</resources>
In your Mainactivity
boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
// do something
} else {
// do something else
}
Related
So I want to make my app name as App5 so I did the following. I went to andriod/app/src/AndriodManifest.xml and edited this file to
android:label="#string/app_name"
And then went to andriod/app/src/res/values/strings.xml and Added my app name as following
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">App<sub><small>5</small></sub></string>
</resources>
But still the subscript isn't working so what did I do wrong
Here your app_name's string is considered to be containing text 'App' and child object sub, that's why it isn't working and you are getting only App instead of App5.
Try instead escaping this <sub><small>5</small></sub> by encapsulating it inside <![CDATA[<sub><small>5</small></sub>]]> so you will have :
<string name="app_name">App<![CDATA[<sub><small>5</small></sub>]]></string>
OR
using html entities and
<string name="app_name">App<sub><small>5</small></sub></string>
(PS : check if your widget accepts and formats html)
UPDATE
I forgot to add < at the beginning and > at the end of ![CDATA[]]
, It has been corrected above
In your activity onCreate method do like this ( using the app_name above)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
//ADD THIS LINE
setTitle(Html.fromHtml( getResources().getString(R.string.app_name)));
...
}
I've added Google Analytics to my mobile application by using the google-services.json file.
Now I've enabled auto activity tracking and I want to provide a screen name for each activity.
Google documents say, I should add
<screenName name=".MyActivity">My activity</screenName>
to my XML configuration file. Where's this coming from? I don't have a xml config file, I have a google-services.json file.
Do I need to create a XML file inside res/xml/ ?
What values are necessary as I am currently using the android default R.xml.global_tracker ?
Or do I need to add these screen-name information to the json file and if yes, whats the structure then?
Thanks in advance.
You could create your own XML. Here is mine, remember to replace your tracking ID:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="ga_trackingId" translatable="false">UA-99999999-1</string>
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- The screen names that will appear in reports -->
<screenName name="com.codylab.squats.MainActivity">MainActivity</screenName>
<screenName name="com.codylab.squats.SettingsActivity">SettingsActivity</screenName>
<screenName name="com.codylab.squats.BreakActivity">BreakActivity</screenName>
<screenName name="com.codylab.squats.FinishedActivity">FinishedActivity</screenName>
<screenName name="com.codylab.squats.InitialActivity">InitialActivity</screenName>
<screenName name="com.codylab.squats.WorkoutActivity">WorkoutActivity</screenName>
</resources>
Create an Application class:
public class YourNameApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
// To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
analytics.newTracker(R.xml.squats_tracker);
}
}
Add a name attribute of application tag in your AndroidMenifest.xml
...
<application
android:name=".YourNameApplication"
...>
...
</application>
Short Question: Resources.Theme.resolveAttibute(attrId, typVal, true) returns typVal.resourceId is 0, cause that exception. This ResourceReader.getResourceId() works well in all places of whole app. But randomly, app update or after several restarts this exception occured because TypedValue.resourceId is 0.
Long Question:
I'm getting ResourcesNotFoundException on app restart unfortunately I can't determine when this error occurs. When I restart (close, kill, restart, update, setup) my app several times in a minute at last my app gives this RNFE on first resource read line.
Stack trace:
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:2331)
at android.content.res.Resources.getColor(Resources.java:2013)
at com.my.app.navi.NavigationFragment.setUp(NavigationDrawerFragment.java:570)
at com.my.app.main.MainActivity.onCreate(MainActivity.java:1071)
It's hard to generate that exception but in Google Play Developer Console I can see a lot of exception generated by other devices.
I looked up a lot answers for that exception but nor rebuild, String.ValueOf(int), resource add solved my problem.
I have several build flavors for different styled and featured app. Because of that I use attr style reference for resources. All flavors has different res folders. main res folders has attrs.xml for reference colors, drawables and images. All the other code and xml layout files has R.attrs references
For explain my situation clearly this is main res attrs.xml
<declare-styleable name="Colors">
<!--<<<<<<<<<<<<<<<<<<<<< COLORS >>>>>>>>>>>>>>>>>>>>>>>>>>>>-->
<attr name="c_pressed_button_back" format="color"/>
<attr name="c_window_back" format="color"/>
<attr name="c_navigation_drawer_back" format="color"/>
flavor_one style.xml
<!--<<<<<<<<<<<<<<<<<<<<<<<<< COLORS >>>>>>>>>>>>>>>>>>>>>>>>>>>>>-->
<item name="c_pressed_button_back">#color/c_theme_pressed_button_back</item>
<item name="c_window_back">#color/c_theme_window_back</item>
<item name="c_navigation_drawer_back">#color/c_theme_navigation_drawer_back</item>
flavor_one colors.xml
<color name="c_theme_pressed_button_back">#color/c_theme_red</color>
<color name="c_theme_window_back">#color/c_theme_green</color>
<color name="c_theme_navigation_drawer_back">#color/c_theme_blue</color>
With these settings I wrote this class for resource reading.
public class ResourceReader
{
public static int getResourceId(Resources.Theme thm, int attrId)
{
int returnValue = 0;
TypedValue typedValue = new TypedValue();
try {
thm.resolveAttribute(attrId, typedValue, true);
returnValue = typedValue.resourceId;
}
catch (Exception ex) // Resource cannot resolved
{
ex.printStackTrace();
}
if(returnValue == 0)
{
Log.e("yucel", "Resource resolved result is 0 #thm:" + thm + " attrid:" + attrId + " typdata:" + typedValue.data + " typtyp:" + typedValue.type + " ");
}
return returnValue;
}
}
And finally I use resources like
int back_color_id = ResourceReader.getResourceId(appData.appContext.getTheme(), R.attr.c_navigation_drawer_back);
int back_color= appData.appRes.getColor(back_color_id);
mDrawerLayout.setColor(back_color);
At the line of getColor(back_color_id) exception will be thrown. Because of back_color_id is equal to 0. In resource reader thm.resolveAttribute(attrId, typedValue, true);
I think whole code statements looks ok. Because whole app and 99 percent of use this structure works well. Problem occurs after several restarts and randomly at resource reading. Something must be changed in that situtation. Is there any method to check is resources in a proper state like Context.isResourceLoading() or Resoureces.isLoaded()?
Is there is any one faced resource reading problem at randomly, after several restart or after updates?
I changed attr.xml reading to direct theme item reading
int shadow_color = appData.appRes.getColor(R.color.c_ld_navigation_shadow_color);
mDrawerLayout.setScrimColor(shadow_color);
This works well. But there is a problem about that. I create the attr.xml references for dynamic theme change and different flavor variables reference.
In a flavor_one desing has open dialog background as a color named = c_open_dialog_backcolor
On the flavor_two design this has a drawable reference as a xml named = d_open_dialog_back_pattern
But this two design has same java code with no difference at set like usign resource resolver with R.attr.dialog_bakground
Seems the problem generated by Resources.Theme.resolveAttribute(int resid, TypedValue outValue, boolean resolveRefs) can't read resources when app restarting or update.
I changed my code and delete all the attr.xml items, its seems works good for now. Thanks.
I'm looking for a way to document a layout file to improve its reusability.
What I'd like is something that produces javadoc in generated R file like this.
I know it is possible to do so when using <declare-styleable>. This :
<declare-styleable name="myStyleable">
<!-- Some comment -->
<attr name="someAttr" format"color" />
</declare-styleable>
Produces this output i'd like to obtain for layout files without success :
public final class R {
/** Some comment */
public static final int someAttr...
}
Does someone know of the mean to achieve this ? I'm interrested in :
Documenting the layout file, so that documentation be available when using R.layout.my_layout
Documenting a particular element of the file, so that the documentation be available when finding it by id f.e. aView.findViewById(R.id.the_id_that_is_documented)
By the way, I found part of the response : for ids it is possible to add comments if the id is defined in a resource file (in res/values) :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- This is a comment -->
<item name="myId" type="id"></item>
</resources>
Will produce this result in R.class :
public final class R {
public static final class id {
/** This is a comment
*/
public static int myId=0x7f050007;
}
This won't work directly in the layout file if you use #+id/some_cute_id
Edit : and here's the answer for layout files. In fact it might work for everything (Found it browsing sdk sources in res/values/public.xml). This :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--
* This layout contains a list that enables to choose a bluetooth device to pair with among paired ones -->
<public type="layout" name="devices_choose" id="0x7f030005" />
</resources>
Produces this output in R :
public final class R {
public static class layout {
/**
* This layout contains a list that enables to choose a bluetooth device to pair with among paired ones
*/
public static final int devices_choose=0x7f030005;
}
}
I am getting this error when calling the setContentView() after
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.maintitlebar);
The code is in the onCreate() for my class which extends ListActivity.
My manifest XML file shows the default AppTheme for the application:
android:theme="#style/AppTheme"
I have updated styles.xml to be:
<resources>
<style name="AppTheme" parent="android:Theme.Light" >
<item name="android:windowNoTitle">true</item>
</style>
</resources>
This seems to be in accordance with the main posts on this error message. I have also cleaned the build, yet I am still getting the above error message. Has anybody any idea what is causing the clash?
I had a similar problem that drove me insane: I have 2 versions of the same app using a shared library project as their common code (over 95% of each app is made of that shared library project): One runs fine, without any problem whatsoever. The other crashes upon start with the same error message & symptoms you describe:
You cannot combine custom titles with other title features
The layout XML files are common as well! So, I couldn't find any explanation for this weird problem.
After much brainstorming between me, myself and I, I discovered that the only difference between the two apps is that the one that runs fine has this in its AndroidManifest.xml:
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />
And the one that crashes has this in its AndroidManifest.xml:
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="13" />
So, you see, the Android UI is touted as being a 4th-generation UI framework, in which the UI is declarative and independently themed, but at the end of the day it's all the same st: Developing in C (for example) for Microsoft Windows is no more time consuming than developing in Java for the Android because the Android development framework is full of landmines like this in which the compiler won't tell you anything at compile time, and the thrown exception won't tell you either. Instead, you have to rely on **luck finding that little snippet of documentation (that may or may not exist), providing you with a hint as to where to look for the root cause of the problem.
I hope that this information will be helpful to many who experience the same problem.
You should use
<item name="android:windowNoTitle">false</item>
in your custom theme
I have the same problem and here is what it worked for me:
AndroidManifest.xml
< application
...
...
android:theme="#style/CustomTheme" >
Styles.xml
< style name="CustomTheme" parent="android:Theme">
< /style>
MainActivity.java
1) super.onCreate(savedInstanceState);
2) requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
3) setContentView(R.layout.activity_main);
4) getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title1);
The order of the codes is important as shown above.
If you have:
1) requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
2) setContentView(R.layout.activity_main);
3) super.onCreate(savedInstanceState);
4) getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title1);
You will get this exception:
android.view.InflateException: Binary XML file line #37: Error inflating class fragment
It matters which parent theme you are using:
If I use parent="android:Theme", then android:windowNoTitle="false" works (as per #Vladimir's answer).
However, if I use parent="android:Theme.Holo.Light", then I need to use android:windowActionBar="false" (as per #Jasper's comment). Here's my working xml using the Holo Light theme:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.ScheduleTimes" parent="android:Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
</style>
</resources>
Maybe this question up to this time is already solved, but i post this answer for those who are having this error...i had a very similar problem, and changing the manifest file didn't work at all, but what it worked for me was go to the gradle Scripts and go to the build.gradle(Module:app) in this file, i changed the minSdkVersion and the targetSdkVersion with the same in both of them in this case for example 7, and the app runs correctly!
I had the same problem. I downloaded an app named BlueToothChat.
So, I add the following code to the string.mxl file within the value folder:
<resources>
<style name="CustomTheme" parent="#android:Theme">
<item name="android:windowNoTitle">false</item>
</style>
<string name="app_name">Bluetooth Chat</string>
...
and then add: {Theme = "#style/CustomTheme"} to the home page of the activity page (BluetoothChat.cs):
namespace BluetoothChat
{
/// <summary>
/// This is the main Activity that displays the current chat session.
/// </summary>
[Activity (Label = "#string/app_name", MainLauncher = true,
Theme = "#style/CustomTheme", ConfigurationChanges
Android.Content.PM.ConfigChanges.KeyboardHidden |
Android.Content.PM.ConfigChanges.Orientation)]
public class BluetoothChat : Activity
However, I did not test variations of this procedure or define any real CustomTheme attributes.