Tuesday, 25 February 2014

Java snippet - remove folders out, gen, .idea and files .iml from many Android projects in one click (intellij)

Here is simple java snippet to remove all not interesting files from Android projects.
This snippet removes, gen, out, .idea folders with files inside and also .iml file
 public class main {  
   private static final String[] FOLDERS = new String[]{"gen", "out", "bin", /*".idea"*/};  
   private static final String[] FILES_EXTENSION_TO_DEL = new String[]{/*".iml"*/};  
   private static final String PATH = "C:\\FolderX\\androidprojects";  
   private static final String[] IGNORE_EXTENSIONS = new String[]{".svn"};  
   private static ArrayList<File> dirToRemowe = new ArrayList<File>();  
   private static ArrayList<File> filesToRemowe = new ArrayList<File>();  
   public static void main(String[] args) {  
     new main().doStuff(PATH);  
   }  
   private void doStuff(String path) {  
       String currentPath = PATH;  
       System.out.println("[PATH] " + currentPath);  
       File file = new File(currentPath);  
       iterateFolders(file, false);  
       deleteElements(filesToRemowe);  
       deleteElements(dirToRemowe);  
   }  
   private void deleteElements(ArrayList<File> filesToRemowe) {  
     Collections.reverse(filesToRemowe);  
     for (File f : filesToRemowe) {  
       System.out.println("f[] To remove: " + f.getAbsolutePath());  
       try {  
         FileInputStream in = new FileInputStream(f);  
         in.close();  
       } catch (FileNotFoundException e) {  
       } catch (IOException e) {  
         e.printStackTrace();  
       }  
       System.out.println(f.delete() + "");  
       try {  
         Thread.sleep(5);  
       } catch (InterruptedException e) {  
         e.printStackTrace();  
       }  
     }  
   }  
   public static void iterateFolders(File file, boolean interesting) {  
     File[] files = file.listFiles();  
     for (File f : files) {  
       String absolutePath = f.getAbsolutePath();  
       //if interesting folder true so add all to remove list  
       if (interesting) {  
         if (f.isFile()) {  
           if (!filesToRemowe.contains(f))  
             filesToRemowe.add(f);  
         } else if (f.isDirectory()) {  
           if (!dirToRemowe.contains(f))  
             dirToRemowe.add(f);  
           iterateFolders(f, true);  
         }  
       } else {  
         //default searching folders  
         //add file only with specific name, adding any file will be called when interesting == true  
         if (f.isFile()) {  
           for (String file_extension : FILES_EXTENSION_TO_DEL) {  
             if (f.getName().endsWith(file_extension)) {  
               if (!filesToRemowe.contains(f))  
                 filesToRemowe.add(f);  
             }  
           }  
         } else if (f.isDirectory()) {  
           interesting = false;  
           for (String s : FOLDERS) {  
             if (f.getName().equals(s))  
               if (!dirToRemowe.contains(f)) {  
                 dirToRemowe.add(f);  
                 interesting = true;  
               }  
           }  
           for (String s : IGNORE_EXTENSIONS)  
             if (f.getName().equals(".svn"))  
               continue;//and dont go inside svn, iterate again next  
           iterateFolders(f, interesting);  
           //resert interesting flag on exit from this folder  
           interesting = false;  
         }  
       }  
     }  
   }  
 }  

Monday, 24 February 2014

Unable to open 'xxx-unaligned.apk' as zip archive

If you found in your logs something like this`Unable to open 'xxx-unaligned.apk' as zip archive` it means that somehow compiler cannot reach to all resources so make sure that none of your project items doesn't have spaces in names.
In my situation I couldn't compile project because folder which I was storing project was named "project_android _version_x". As you see the title had space which shouldn't exist. If not, try select it by mouse and you will discover little space between "android" and "_version". Second important thing is to make sure your path to project isn't too long, sometimes it also makes problems..

Few steps:
  1. close project
  2. delete .idea, .iml, gen and out folders in project, main root
  3. correct names of folders which have android projects
  4. import again project
  5. and try now rebuild

Sunday, 23 February 2014

Windows 8 for developers? Not yet!

My opinion about Windows 8 is not quite positive. Few days ago, maybe 5 days I've installed original Windows 8 and then upgraded to 8.1 The only one thing what I connect with this system is "wtf!". Yes, beware from installing to new system. You will have to prepare for many new shiny BSOD's. Most of drivers doesn't work well and you will only lose so much time for searching solution that so far it is no worth for using Windows 8. But as I said, it's my opinion. The most annoying problem was about using android emulator with HAXM Intel tweaker to speed up booting device. It used to drop BSOD after few minutes when anroid emulator booted. I found on stack solution fix for this problem, but after I downloaded fix I couldn't install it, I don't know why, I even didn't wanted to know. (Ok I knew, it was about that the current version is newer than the fix so there was not way to install fix from Intel). So last thing what I did was to back to Windows 7 what is difference like heaven and hell. Please make sure you really want to join to Windows 8 and it's community or maybe give yourself some time to think about and then prepare for new system, new problems which shouldn't be popped up after few days... Remember, you are developer, you don't waste time being tech guy.

Thursday, 20 February 2014

Reading text from files in android - example

Here is working sample how to read text per line from file/files

 //find all files from folder /assets/txt/  
 String[] elements;
  
 try {  
   elements = getAssets().list("txt");  
 } catch (IOException e) {  
   e.printStackTrace();  
 }  

 //for every files read text per line  
 for (String fileName : elements) {  
   Log.d("xxx", "File: " + fileName);  
   try {  
     InputStream open = getAssets().open("txt/" + fileName);  
     InputStreamReader inputStreamReader = new InputStreamReader(open);  
     BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  
     String line = "";  
     while ((line = bufferedReader.readLine()) != null) {  
       Log.d("xxx", line);     
     }  
   } catch (IOException e) {  
     e.printStackTrace();  
   }  
 }  

Saturday, 15 February 2014

Automatic adb install apks files to your phone if you are tired by do it manually - java snippet

This short snippet will help you a lot (I hope so). If you have apps on hardrive  you can speed up installing your apps and save some time.


 public class Main {  
   public static final int SECOND_DELAY = 10;  
   public static final String path = "C:\\Users\\michal\\Dropbox\\android\\back\\";  
   public static void main(String[] args) throws IOException, InterruptedException {  
     new Main().doStuff();  
   }  
   private void doStuff() throws IOException, InterruptedException {  
     File file = new File(path);  
     Runtime rt = Runtime.getRuntime();  
     for(File f: file.listFiles())  
     {  
       String command = "adb install -r " + "\"" + path + f.getName() + "\"";  
       System.out.println(command);  
       Process pr = rt.exec(command);  
       long timeDelay= SECOND_DELAY * 1000;  
       Thread.sleep(timeDelay);  
     }  
   }  
 }  

As you see this snippet automatic installs your apks from specific path with specific second_delay. Thanks for attention :)

Wednesday, 12 February 2014

Fragment dialog with scroll view doesn't scroll - solution

There is possibility, you will have scrollview inside dialog fragment plus with many buttons and the view won't move.

To fix it you should do the same thing like with activities by adjusting windowSoftInputMode in dialog theme.

You should set custom theme for your fragment dialog by inheriting current android dialog and adding windowSoftInputMode option:

 <style name="DialogFragmentStyle" parent="@android:style/Theme.Dialog">  
     <item name="android:windowSoftInputMode">stateHidden|adjustResize</item>  
 </style>  

Use your theme in your fragment dialog while creating constructor

 Dialog dialog = new Dialog(getActivity(), R.style.DialogFragmentStyle);  

 public class MyFragmentDialog extends DialogFragment{  
   @Override  
   public Dialog onCreateDialog(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     Dialog dialog = new Dialog(getActivity(), R.style.DialogFragmentStyle);  
     //stuff  
     return dialog;  
   }  
   //... other methods  
 }  

Thursday, 6 February 2014

Getting resource name by it's ID

Short code in example:

 getResources().getResourceEntryName(view.getId())  

Will returns something like this:

 myEditText  

EditText hidden / covers by keyboard? Fast solution

My problem was about hidding EditText by keyboard. What is more EditText was in activity with theme for Dialog.

What I did to fix was adding this text before setContentView(...);

 getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);  

How ever even in scrollview you are not able to scroll...

---

You can also move big layout to scrollview and set it to scroll to EditText position. In this example my layout has few EditTexts and my last one at the bottom has problem with keyboard, so I did something like this:

 myEditText.setOnTouchListener(new View.OnTouchListener() {  
       @Override  
       public boolean onTouch(View view, MotionEvent motionEvent) {  
         if(view.getId() == R.id.myEditText)  
         {  
           Handler handler = new Handler();  
           handler.postDelayed(new Runnable() {  
             @Override  
             public void run() {  
               scrollview.scrollTo(0, myEditText.getBottom()+300);  
             }  
           }, 300);  
         }  
         return false;  
       }  
     });  

Wednesday, 5 February 2014

How use custom ActionBar layout - short guide

You need create custom layout first.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
         android:layout_width="match_parent"  
         android:layout_height="match_parent"  
         android:background="@color/aqua"  
         android:orientation="vertical">  
   <TextView  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_centerInParent="true"  
       android:textColor="#ffffff"  
       android:id="@+id/barTitle"  
       android:textSize="25dp"/>  

 </RelativeLayout>  

After this in our SherlockActivity need add some code in onCreate method.

 @Override  
 protected void onCreate(Bundle savedInstanceState) {  
      super.onCreate(savedInstanceState);  

      getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);  
      getSupportActionBar().setCustomView(R.layout.customBar);  

      //element from our custom layout, textview is my component which acts like title  
      ((TextView) findViewById(R.id.barTitle)).setText("This is title"));  
      setContentView(R.layout.l1a2a_has_account);  
      
      //further stuff  

 }