Categories
discuss

How to set transparent background of JDialog

Hy,..


how can i set the background transparent and “remove” the closeoperation (marked red) ?
I only want to show the card 🙂

alt text

Thanks..!

Answer

yourDialog.setUndecorated(true)should do the trick for the title bar.

For having the Frame transparent. You’ll have to work on the root panel with yourDialog.getRootPane().setOpaque(false)on it.

Categories
discuss

How to change a TextView’s style at runtime

I have an android app on which, when the user taps a TextView, I would like to apply a defined style.

I thought to find a textview.setStyle() but it doesn’t exists. I tried

textview.setTextAppearance();

but it does not work.

Answer

I did this by creating a new XML file res/values/style.xml as follows:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="boldText">
        <item name="android:textStyle">bold|italic</item>
        <item name="android:textColor">#FFFFFF</item>
    </style>

    <style name="normalText">
        <item name="android:textStyle">normal</item>
        <item name="android:textColor">#C0C0C0</item>
    </style>

</resources>

I also have an entries in my “strings.xml” file like this:

<color name="highlightedTextViewColor">#000088</color>
<color name="normalTextViewColor">#000044</color>

Then, in my code I created a ClickListener to trap the tap event on that TextView: EDIT: As from API 23 ‘setTextAppearance’ is deprecated

    myTextView.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view){
                    //highlight the TextView
                    //myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
    if (Build.VERSION.SDK_INT < 23) {
       myTextView.setTextAppearance(getApplicationContext(), R.style.boldText);
    } else {
       myTextView.setTextAppearance(R.style.boldText);
    }
     myTextView.setBackgroundResource(R.color.highlightedTextViewColor);
                }
            });

To change it back, you would use this:

if (Build.VERSION.SDK_INT < 23) {
    myTextView.setTextAppearance(getApplicationContext(), R.style.normalText);
} else{
   myTextView.setTextAppearance(R.style.normalText);
}
myTextView.setBackgroundResource(R.color.normalTextViewColor);
Categories
discuss

Correct way to Convert 16bit PCM Wave data to float

I have a wave file in 16bit PCM form. I’ve got the raw data in a byte[] and a method for extracting samples, and I need them in float format, i.e. a float[] to do a Fourier Transform. Here’s my code, does this look right? I’m working on Android so javax.sound.sampled etc. is not available.

private static short getSample(byte[] buffer, int position) {
  return (short) (((buffer[position + 1] & 0xff) << 8) | (buffer[position] & 0xff));
}

...

float[] samples = new float[samplesLength];
  for (int i = 0;i<input.length/2;i+=2){
    samples[i/2] = (float)getSample(input,i) / (float)Short.MAX_VALUE;
  }

Answer

I had a similar solution, but IMHO a little cleaner. Unfortunately, there’s no good library method as far as I’m aware: *This assumes the even bytes are the lower bytes

private static float[] bytesToFloats(byte[] bytes) {
    float[] floats = new float[bytes.length / 2];
    for(int i=0; i < bytes.length; i+=2) {
        floats[i/2] = bytes[i] | (bytes[i+1] << 8);
    }
    return floats;
}
Categories
discuss

ANT build for Android Proguard obfuscation

Can anyone share with sample/simple obfuscation ANT task for Android? Provided that I do have complete APK and I need just pass *class hru Proguard and then prepare *.dex to build APK

Answer

I have found solution:

  1. Get Proguard – copy proguard.jar into known directory (say MyProject/proguard)
  2. Prepare proguard.cfg -describing what and how optimize/obfuscate. This process thoroughly described in Proguard’s manual
  3. Prepare following ANT’s build.xml (or smth like this one) – great thanx to this guy

UPDATE complete build.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project name="MyProject" default="zipalign" basedir=".">
    <property name="target" value="android-8"/>
    <!--property file="default.properties" /-->
    <property name="encoding" value="UTF-8"/>

    <!-- dirs -->
    <property name="sdk.dir" location="Location of Android SDK"/>
    <property name="proguard.dir" value="proguard" />
    <property name="src.dir" value="src"/>
    <property name="gen.dir" value="gen"/>
    <property name="res.dir" value="res"/>
    <property name="assets.dir" value="assets"/>
    <property name="libs.dir" value="libs"/>
    <property name="out.classes.unoptimized.dir" value="out"/>
    <property name="out.classes.optimized.dir" value="out/optimized"/>

    <!-- files -->
    <property name="manifest.file" value="AndroidManifest.xml"/>
    <property name="signed.apk" value="${ant.project.name}-signed.apk"/>
    <property name="unsigned.apk" value="${ant.project.name}-unsigned.apk"/>
    <property name="final.apk" value="${ant.project.name}.apk"/>
    <property name="android.jar" value="${sdk.dir}/tools/platforms/${target}/android.jar"/>
    <property name="unoptimized" value="unoptimized.jar" />
    <property name="optimized" value="optimized.jar" />
    <property name="proguard.config" value="${proguard.dir}/proguard.cfg"/>

    <!-- tools -->
    <property name="dx.jar" value="${sdk.dir}/platform-tools/lib/dx.jar"/>
    <property name="aapt" value="${sdk.dir}/platforms/${target}/tools/aapt.exe"/>
    <property name="zipalign" value="${sdk.dir}/tools/zipalign.exe"/>
    <property name="jarsign" value="jarsigner.exe location is here"/>
    <property name="keystore" value="Your key store is here"/>
    <property name="keyalias" value="Your key alias is here"/>


    <path id="android.antlibs">
        <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
        <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
        <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
        <pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
        <pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
    </path>

    <taskdef name="setup"
        classname="com.android.ant.SetupTask"
        classpathref="android.antlibs" />
    <setup import="false"/>

    <!--taskdef name="aaptexec"
            classname="com.android.ant.AaptExecLoopTask"
            classpathref="android.antlibs" /-->

    <target name="clean" description="Removes output files created by other targets.">
        <echo>Cleaning...</echo>
        <delete dir="${out.classes.unoptimized.dir}" verbose="true" />
        <delete dir="${out.classes.optimized.dir}" verbose="true" />
    </target>

    <target name="dirs">
        <echo>Creating output directories if needed...</echo>
        <mkdir dir="${out.classes.unoptimized.dir}" />
        <mkdir dir="${out.classes.optimized.dir}" />
    </target>

    <!-- Compiles this project's .java files into .class files. -->
    <target name="compile" depends="dirs"
            description="Compiles project's .java files into .class files">
        <echo>Compiling sources...</echo>
        <javac encoding="${encoding}" target="1.6" debug="true" extdirs=""
                destdir="${out.classes.unoptimized.dir}"
                bootclasspathref="android.target.classpath"
                includeantruntime="true">
            <src path="${src.dir}" />
            <src path="${gen.dir}" />
            <classpath>
                <fileset dir="${libs.dir}" includes="*.jar" />
            </classpath>
        </javac>
    </target>

    <target name="preobfuscate" depends="compile">
        <echo>Preparing to obfuscation...</echo>
    <jar destfile="${unoptimized}"
       basedir="${out.classes.unoptimized.dir}"
       includes="**/**"
       excludes="optimized/**"
       />
    </target>

    <!--  Obfuscation with ProGuard   -->
    <target name="optimize" unless="nooptimize" depends="preobfuscate">
        <echo>Proguard obfuscation...</echo>
        <java jar="${proguard.dir}/proguard.jar" fork="true" failonerror="true">
            <jvmarg value="-Dmaximum.inlined.code.length=16" />
            <arg value="@${proguard.dir}/proguard.cfg" />
            <arg value="-injars ${unoptimized}" />
            <arg value="-outjars ${optimized}" />
            <arg value="-libraryjars ${android.jar}" />
        </java>
        <unzip src="${optimized}" dest="${out.classes.optimized.dir}" />
        <!-- Delete optimized jar (now unzipped into bin directory)  -->
        <delete file="${optimized}"/>
        <delete file="${unoptimized}"/>
    </target>

    <target name="dex" description="Converting JVM bytecodes into Dalvik bytecodes" depends="optimize">
        <echo>Converting bytecodes to Dalvik VM bytecodes...</echo>
        <java jar="${dx.jar}" fork="true">
            <arg line="--dex --verbose --output=${out.classes.optimized.dir}/classes.dex ${out.classes.optimized.dir}"/>
        </java>
    </target>

    <target name="aapt" depends="dex" description="compile resources">
        <echo>Packing resources...</echo>
        <exec executable="${aapt}" logerror="true" osfamily="windows">
            <arg line="p
            -f
            -M ${manifest.file}
            -I ${android.jar}
            -S ${res.dir}
            -A ${assets.dir}
            -F ${out.classes.optimized.dir}/${unsigned.apk}
            -m -J ${gen.dir}"/>
        </exec>
    </target>

    <target name="sign" depends="aapt" description="sign apk">
        <input message="Please enter keystore password (store:${keystore}):"
                       addproperty="keystore.password" />
        <echo>Signing apk...</echo>
        <exec executable="${jarsign}" logerror="true" osfamily="windows">
            <arg line="-verbose
            -keystore ${keystore}
            -storepass ${keystore.password}
            -signedjar ${out.classes.optimized.dir}/${signed.apk}
            ${out.classes.optimized.dir}/${unsigned.apk} ${keyalias}"/>
        </exec>
    </target>

    <target name="zipalign" depends="sign" description="zip align">
        <echo>Aligning apk...</echo>
        <exec executable="${zipalign}" logerror="true" osfamily="windows">
            <arg line="-f
            -v
            4
            ${out.classes.optimized.dir}/${signed.apk}
            ${final.apk}"/>
        </exec>
    </target>

</project>
  1. This ANT task has to be added to Eclipse’s builders (Properties/Builders) tasks after Java builder and before Android package builder.

  2. Press “Build All” (it’s better to off Automatic Build check in Eclipse menu)

Categories
discuss

Android: how to check if a View inside of ScrollView is visible?

I have a ScrollView which holds a series of Views. I would like to be able to determine if a view is currently visible (if any part of it is currently displayed by the ScrollView). I would expect the below code to do this, surprisingly it does not:

Rect bounds = new Rect();
view.getDrawingRect(bounds);

Rect scrollBounds = new Rect(scroll.getScrollX(), scroll.getScrollY(), 
        scroll.getScrollX() + scroll.getWidth(), scroll.getScrollY() + scroll.getHeight());

if(Rect.intersects(scrollBounds, bounds))
{
    //is  visible
}

Answer

Use View#getHitRect instead of View#getDrawingRect on the view you’re testing. You can use View#getDrawingRect on the ScrollView instead of calculating explicitly.

Code from View#getDrawingRect:

 public void getDrawingRect(Rect outRect) {
        outRect.left = mScrollX;
        outRect.top = mScrollY;
        outRect.right = mScrollX + (mRight - mLeft);
        outRect.bottom = mScrollY + (mBottom - mTop);
 }

Code from View#getHitRect:

public void getHitRect(Rect outRect) {
        outRect.set(mLeft, mTop, mRight, mBottom);
}
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..