Read XML from URL in Android

Read XML file :

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new URL(url).openStream());

Parse :

File fXmlFile = new File("/Users/mkyong/staff.xml");
	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
	Document doc = dBuilder.parse(fXmlFile);

	//optional, but recommended
	//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
	doc.getDocumentElement().normalize();

	System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

	NodeList nList = doc.getElementsByTagName("staff");

	System.out.println("----------------------------");

	for (int temp = 0; temp < nList.getLength(); temp++) {

		Node nNode = nList.item(temp);

		System.out.println("\nCurrent Element :" + nNode.getNodeName());

		if (nNode.getNodeType() == Node.ELEMENT_NODE) {

			Element eElement = (Element) nNode;

			System.out.println("Staff id : " + eElement.getAttribute("id"));
			System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
			System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
			System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
			System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());

		}

References
https://stackoverflow.com/questions/16958081/java-how-to-read-xml-from-url
https://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

Prevent locale changes on Android application

    public class MyApplication extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        Resources res = base.getResources();

        Locale locale = new Locale("en");
        Locale.setDefault(locale);

        Configuration config = new Configuration();
        config.locale = locale;

        res.updateConfiguration(config, res.getDisplayMetrics());

        super.attachBaseContext(base);
    }
}

Support layout mirroring

<manifest>
    <application
        .
        .
        .
        android:supportsRtl="false"
        tools:replace="android:supportsRtl"
        >
    </application>
</manifest>

 

References
https://stackoverflow.com/questions/37894163/is-there-a-android-attribute-to-disable-locale-change-in-my-app/
https://stackoverflow.com/questions/11013904/disable-localization-in-android-application
https://stackoverflow.com/questions/26450489/disable-automatic-layout-change-on-android

How Libraries can silently add permissions to your Android App

final Manifest file :

app/build/intermediates/manifests/full/debug/AndroidManifest.xml

where did it exactly these permissions came from?

app/build/outputs/logs/manifest-merger-debug-report.txt

So the solution is :

<uses-permission android:name=”android.permission.RECORD_AUDIO” tools:node=”remove” />

Even if another library is asking for this specific permission, the build will be forced to not merge it in your final Manifest file

References
https://medium.com/glucosio-project/how-libraries-can-silently-add-permissions-to-your-android-app-620911d7de6c