Why doesn't getResources().getIdentifier() work here (API level 22)? - android

Trying to change the appearance of the AlertDialog, I subclassed DialogFragment with ThemedDialogFragment and I called this lines onStart():
int alertTitleId = getResources().getIdentifier("alertTitle", "id", "android");
TextView alertTitle = (TextView) dialog.findViewById(alertTitleId);
alertTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen._20ssp));
That resulted in a NullPointerException, even though I am sure there is a view with id alertTitle in the window. The HierarchyViewer(*) tool confirms.
Tried to do minor variants
e.g.
int alertTitleId = getResources().getIdentifier("android:id/alertTitle", null, null);
or
TextView alertTitle = (TextView) dialog.getWindow().findViewById(alertTitleId);
or
TextView alertTitle = (TextView) dialog.getWindow().getDecorView().findViewById(alertTitleId);
but none of this works.
Any idea, guys?
(*): HierarchyViewer shows the id I'm looking for exists
P.S.: I solved the AlertDialog styling problem, by operating on the themes.xml, I just want to know why this method didn't work (it should IMO).
Having said so, I will downvote every answer that tells me how to style an AlertDialog because it's off-topic here. Thanks.
P.P.S: As a sidenote, I tried this method compiling agains the sdk level 22 and tested it on an Android emulator running Android Jelly Bean 4.3.1.
P.P.P.S: No luck on an emulator running 5.1.1 either.

It appears that you're trying to access an internal resource ID. In this case the package isn't "android" but "com.android.internal", but I'm not sure if even that would actually return the ID for you.
Android Dialog with modifiable single line title
Unfortunately, I cannot access their R.id.alertTitle, because it is part of com.android.internal.R.

Related

Getting the v7 compat ActionBarContainer

As part of an app I'm working on, I'm attempting to obtain a reference to the ActionBar's container view. I used the answer from this question, and it's working nicely for me as long as the Android device in question is running API level 11 or up. However, the app needs to work as far back as API level 9 (the target API level is 19), and Gingerbread devices are giving me problems. I was originally using Sherlock for the project, but recently made the switch over to the v7 compat library instead. I can see and interact with the action bar in normal ways on Gingerbread devices (buttons work, etc.), but it fails when I attempt to get the container. The code I'm using is this (note - it's running inside a subclass of ActionBarActivity):
private FrameLayout getActionBarContainer() {
FrameLayout result = null;
int resId = getResources().getIdentifier("action_bar_container", "id", "android");
try {
result = (FrameLayout)getWindow().getDecorView().findViewById(resId);
}
catch (Exception e) {
// If we get an exception, just eat it
}
return result;
}
To answer a few questions before they get asked:
resId resolves to a proper ID value on v11 devices and up, but resolves to 0 on pre-v11 devices.
The code that uses this has proper checks to handle a null result, which is why I'm just eating the exceptions. The try/catch block is mostly just there in case, by some freak occurrence, a ClassCastException manages to get thrown (which it never should, since the container is a subclass of FrameLayout).
I've checked and re-checked my imports; all of my ActionBar references (and all things related, like the ActionBarActivity superclass I'm extending) are the v7 compat library versions, not the standard library versions.
The action_bar_container ID should exist within the v7 compat library, if this is any indication.
I'm about out of ideas at this point. Is there something simple I'm missing? Any suggestions will be appreciated, and if you need more context/clarification, let me know.
I think your error is on the last parameter of
int resId = getResources().getIdentifier("action_bar_container", "id", "android");
For api level <11 the package should be your application's package and not the plataform's package "android"

Resource not found exception at setDropDownBackgroundColor

I'm trying to set a DropDown Background Color for a autocompleteTextView, because the standart Values seem to be different during different Android Versions. For example, everything works fine starting with android 3.2, but previous Versions seem to not accept the android:textColor="#color/black" in the xml, cause it is not working for me. Android 2.3 for example just shows the items in the list and the ones I picked in white, which isn't working for me, cause the background is white too. So I decided to change the background Color determing on the android version using this:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB){
// Do something for Honeycomb and above versions
//everything is right
} else{
// do something for phones running an SDK before froyo
box_Kurs.setDropDownBackgroundResource(Color.BLUE);
box_Teacher.setDropDownBackgroundResource(Color.BLUE);
}
But when I start my app it crashes and shows resource not found exception:
11-19 15:40:17.225: E/AndroidRuntime(450): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shr.khg/com.shr.khg.AddActivity}: android.content.res.Resources$NotFoundException: Resource ID #0xff0000ff
What am I doing wrong? I alread cleaned my project since that what other people suggested. Or is there any easier way to deal with my issue?
Thank you!
The problem is that the setDropDownBackgroundResource() is expecting the id of the Color resource Drawable, not an int that defines a Color. If you look at the Exception you can see that Android is trying to find a component with id #0xff0000ff, this is the hex code for a completely opaque blue which is the value of Color.BLUE
Replce these two lines:
box_Kurs.setDropDownBackgroundResource(Color.parseColor("#0000ff"));
box_Teacher.setDropDownBackgroundResource(Color.parseColor("#0000ff"));

Eclipse ADT bug - using wrong ResId

Today I noticed a very strange bug - the system used the wrong ResId in the method SetBackgroundResource(). Sure I thought it was my mistake. I fixed the ResId by copying and pasting it from the R file. After I've re-installed the app many times for testing I noticed that a wrong image was set again so it messed up my app. I went to the corresponding method and found out that Eclipse inserted a very similar (but wrong! ..015 instead of ..016) ResId as a method parameter without any actions from me.
I don't think that's important but this strange thing always happens here:
LinearLayout nlap = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 5, 10, 0);
nlap.setLayoutParams(params);
nlap.setOrientation(LinearLayout.HORIZONTAL);
nlap.setBackgroundResource(0x7f020016);
nlap.getBackground().setAlpha(111);
return nlap;
Just clean all your related projects and build again.
Never hard-code a R.id.
I've seen this before: With old versions of ADT and library projects this would sometimes happen. Haven't seen it with ADT 21.
Recently, for me, Eclipse has started underlining R.id's red as if they are wrong or unknown, but they do not show up in the Problems view, and they are not actually wrong. I'm still looking for a bug report for this.

Android:cannot make a selection after WebView search with highlight

Hello I have a bug in my app and I cannot figure it out.
I want to search for text in my WebView and get the found Text highlighted
for Android 1.5-2.3 this works quite well
public void onClick(View v){
webView1.findNext(true);
int i = webView1.findAll(findBox.getText().toString());
try{
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(webView1, true);
}catch(Exception ignored){}
}
}
for Android 3.0+ I have to use the JavaScript workaround from here, because Google doesn't support the highlighting of searched text for incomprehensible reasons
And now my Bug: After the search on my WebView I get the highlighted Text, and I can't select the Text anymore. The only fix I could use is the JavaScript workaround in older Android versions, too. But the function runs very slow and it takes about 10 seconds until the text gets highlighted. I Hope someone has a better solution/fix :)
Thank you
I use the same for 3.x then it did not work on 4.0.x.
Yesterday I updated to 4.0.4 and now highlight works again.
So the solution can be found in the 4.0.4 sources.
OK I have found a quite good solution.
Here is a JavaScript code for Highlighting, that runs really fast :) http://4umi.com/web/javascript/hilite.php#thescript
Anyway I don't understand, why I can't selecting text after the of the official Webview search
For Android 3.x I used webview.showFindDialog(stringtofind, true);
use findAllAsync() instead , finAll() is deprecated in API 16;

Styling the DatePicker

I have been trying very hard to style the text size and colour on android 3.2. When attempting to override the default style with
<item name="android:numberPickerInputTextStyle">#style/numberPickerInputText</item>
I always get this error:
error: Error: No resource found that matches the given name: attr 'android:numberPickerInputTextStyle'.
Why is this so? I am pretty sure I selected the correct style to override (dug it from the platform 13 API folder)
I looked in these places.. seems like it cant be helped!
http://code.google.com/p/android/issues/detail?id=18659#c8
No resource found that matches the given name '#android:style/AlertDialog' error after the latest android 3.2 sdk update
This blog below said something about importing the styles into my application. How is it supposed to be done? examples?
http://daniel-codes.blogspot.com/2011/08/new-to-android-more-style-restrictions.html
Can someone please point me to the right direction? Thanks!
Quick and dirty way.. apply it after you show the dialog. Not a good solution but it works.. still looking for other solutions :)
//Should'nt be here but well..
public static void changeDialogTextSize(Dialog dialog) {
TextView tvMessage = (TextView)dialog.findViewById(android.R.id.message);
if (tvMessage != null)
{
tvMessage.setTextSize(25f);
tvMessage.setTextColor(Color.WHITE);
}
}

Categories

Resources