Categories
discuss

is there a way to disable hardware acceleration only for android 4.0.3?

I have recently stumbled upon an issue with android 4.0.3, where Im getting the following Exception as soon as the application starts (on other android versions it works fine):

java.lang.NullPointerException
at android.view.GLES20RecordingCanvas.drawPatch(GLES20RecordingCanvas.java:97)
at android.graphics.NinePatch.draw(NinePatch.java:125)
at android.graphics.drawable.NinePatchDrawable.draw(NinePatchDrawable.java:189)
at android.widget.ImageView.onDraw(ImageView.java:892)
at android.view.View.draw(View.java:10978)
at android.view.ViewGroup.drawChild(ViewGroup.java:2887)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
at android.view.ViewGroup.drawChild(ViewGroup.java:2885)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489)
at android.view.View.getDisplayList(View.java:10415)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)
at android.view.View.getDisplayList(View.java:10380)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)
at android.view.View.getDisplayList(View.java:10380)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)
at android.view.View.getDisplayList(View.java:10380)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:2597)
at android.view.View.getDisplayList(View.java:10380)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:842)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:1910)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1634)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

This is related to having hardware acceleration enabled, as soon as I disable it on the manifest the application starts working just fine.

By doing a search I found a log (inside that doc search for “drawPatch”) on some conversation of Romain Guy, where he discuss a little bit of what could be causing this, although there is no workaround or fix proposed, I wonder if I should disable hardware acceleration only for this version of android, or if there is a workaround for it?

Thanks for your time.

Answer

So A-C mentions this in his comment but let me elaborate.

You can create a boolean in a values file and stick it in the correct version folder. In your manifest under

Check out this post: https://plus.google.com/+AndroidDevelopers/posts/DcsFABkyuYM.

Looks like your going to want to target values-v15/bools.xml

http://developer.android.com/reference/android/os/Build.VERSION_CODES.html#ICE_CREAM_SANDWICH_MR1

Categories
discuss

replace layout on orientation change

My app has a webview and some buttons inside a LinerLayout.

the problem is, I want the buttons to be on bottom in portrait mode and on left in landscape mode while the webview maintains it’s state.

Two different layout doesn’t work as it force recreation of the activity that refresh the webview. for now I use android:configChanges=”orientation” in activity tag so webview doesn’t get refreshed on orientation change.

Is there anyway to replace the layout of buttons on the change of screen mode?

enter image description here

portrait mode

enter image description here

landscape mode

Answer

I tested fragments, but dealing with fragment makes things much more complex and the fragment itself needs saving and restoring which may not work in a webview which has javascript state, So I searched more and find a nice article somewhere and with some modification I came to a solution which I suggest:

First, add android:configChanges="orientation|screenSize|keyboard|keyboardHidden" so the app handles the config changes instead of android.

Make two different layout for landscape and portrait. In both layouts instead of webview place a FrameLayout which acts as a placeholder for the webview.

Define initUI method like this and put everything related to UI initialization in this method:

public void initui()
{
    setContentView(R.layout.main);
    if (wv == null) wv = new WebView(this);
    ((LinearLayout)findViewById(R.id.webviewPlace)).addView(wv);
    findViewById(R.id.home).setOnClickListener(this);
}

If the webview doesn’t exist yet it will be created and after setContentView(R.layout.main) it will be added to the layout. Any other UI customization you need came after this.

and in onConfigurationChanged:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    ((LinearLayout)findViewById(R.id.webviewPlace)).removeAllViews();
    super.onConfigurationChanged(newConfig);
    initUI();
}

In onConfigChange the webview is removed from old placeholder and initui will be called which will add it back to the new layout.

In oncreate() call initui() so the ui will be initialized for the first time.

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    initUI()
}

I wish it would be helpful for someone.

Categories
discuss

Accessing files in specific folder in classpath using Java

I want to read a bunch of text files in com.example.resources package. I can read a single file using the following code:

InputStream is = MyObject.class.getResourceAsStream("resources/file1.txt")
InputStreamReader sReader = new InputStreamReader(is);
BefferedReader bReader = new BufferedReader(sReader);
...

Is there a way to get the listing of file and then pass each element to getResourceAsStream?

EDIT: On ramsinb suggestion I changed my code as follow:

BufferedReader br = new BufferedReader(new InputStreamReader(MyObject.class.getResourceAsStream("resources")));
String fileName;
while((fileName = br.readLine()) != null){ 
   // access fileName 
}

Answer

If you pass in a directory to the getResourceAsStream method then it will return a listing of files in the directory ( or at least a stream of it).

Thread.currentThread().getContextClassLoader().getResourceAsStream(...)

I purposely used the Thread to get the resource because it will ensure I get the parent class loader. This is important in a Java EE environment however probably not too much for your case.

Categories
discuss

Kendo UI treeview parent node without checkbox

What approach will be to achieve displaying TreeView with checkboxs but I do not need checkbox for parents node only for “items” without children. i.e. I am displaying folder structure but do not want to have checkbox for any folder only for files.

Thanks.

Answer

You should use use checkbox template. If there are not children do not display checkbox input at all.
Here is a JSBIN example what I am talking about.

Categories
discuss

Change Action Bar onPressed color

I cannot figure out how to change the action bar items “onPressed” color on Android. I’m not talking about the action bar background color but about the blue over state. How can I change the color of this or at least get rid of it? For instance the App Icon with the homeAsUpIndicator.

I tried to change the action bar style and also the menu item style but nothing worked.

Thanks

EDIT Here is my implementation so far:

My style xml:

<style name="Theme.AppTheme" parent="@android:style/Theme.Holo">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:selectableItemBackground">@android:color/transparent</item>
    <item name="android:windowEnterAnimation">@anim/fadein</item>
    <item name="android:windowExitAnimation">@anim/fadeout</item>        
    <item name="android:actionBarItemBackground">@android:color/transparent</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">@drawable/actionbarheader</item>
    <item name="android:backgroundStacked">@drawable/actionbarheader</item>
    <item name="android:backgroundSplit">@drawable/actionbarheader</item>
    <item name="android:titleTextStyle">@style/ActionBarTitle</item>
    <item name="android:actionBarItemBackground">@android:color/transparent</item>
    <item name="android:actionButtonStyle">@style/MyActionButtonStyle</item>
    <item name="android:homeAsUpIndicator">@drawable/actionbarlogo</item>
    <item name="android:icon">@drawable/app_icon</item>>
</style>

<style name="ActionBarTitle" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textSize">20sp</item>
</style>

<style name="MyActionButtonStyle" parent="@android:style/Widget.Holo.Light.ActionButton">
    <item name="android:background">@android:color/transparent</item>
</style>

Then I have a drawable for the actionbarogo:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/actionbarlogo_on" android:state_pressed="true"/>
  <item android:drawable="@drawable/actionbarlogo_on" android:state_focused="true"/>
  <item android:drawable="@drawable/actionbarlogo_off"/>
</selector>

My menu xml for the action bar items:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/ContactsMode"
        android:icon="@drawable/actionbarcontacts"
        android:title="@string/contacts"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:background="@android:color/transparent" />
</menu>

and finally, the item’s drawable:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/actionbarcontacts_on" android:state_pressed="true"/>
  <item android:drawable="@drawable/actionbarcontacts_on" android:state_focused="true"/>
  <item android:drawable="@drawable/actionbarcontacts_off"/>
</selector>

Here is how I create the Menu Items for the ActionBar:

public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        actionBar.setIcon(R.drawable.actionbarlogo);
        return true;
    }

    public boolean onPrepareOptionsMenu(Menu menu) {        
         MenuItem item= menu.findItem(R.id.ContactsMode);
         if(ContactsButton)
             item.setVisible(true);      
         else
            item.setVisible(false);

         return true;   
    }

Answer

Fixed it. There was a parent activity changing the theme in the onCreate method…

I didn’t know that you could set the theme programmatically within the activity with:

setTheme(R.style.MyTheme);
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..