Android Development Tips

I am going to be adding small Android code snippet here that I think others can benefit from. I will only show Android code that has worked for me. I can not guarantee that it is the best way of doing it, but I know it will work. I will continue to add more in depth code (e.g. classes) in separate posts.

Creating a folder on the SD card and adding files inside

This will create a folder on the SD card and add a empty file in it. This does not check to see if the user has a SD card inserted. I have not seen a problem not checking first to see if the folder or file is already created. I am sure a problem will raise from not checking first.

        /** create the folder **/
        File soundDirectory = new File("/sdcard/customSound2/"); 
        soundDirectory.mkdirs();

        /** create the file **/
        File soundFile = new File("/sdcard/customSound2/" , "RECORDING.3gp");

        OutputStream out = null;
		try {
			out = new BufferedOutputStream(new FileOutputStream(soundFile));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try {
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

Add permission in the manifest file.

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Creating Dynamic Buttons Using an array in Android

I had to trim it down my source code to make it more sense. I might have an extra or missing curly bracket.

import android.widget.Button;
import android.widget.LinearLayout;

public class SoundBoardActivity extends Activity {
	int numberRecording= 5;
	Button[] playButtons = new Button[numberRecording];     
        LinearLayout layoutbase;
        LayoutParams param;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.main);  
        layoutbase =(LinearLayout) findViewById(R.id.mainScreen);
        param = new LinearLayout.LayoutParams(
        		LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);   
    for(int i=-0; i < numberRecording; i++){
    	       playButtons[i] = new Button(this);
	       playButtons[i].setTextColor(Color.parseColor("#000000")); 
	       playButtons[i].setTextSize(20);
	       playButtons[i].setPadding(15, 15, 15, 15);
	       playButtons[i].setLayoutParams(param);	 	    	 
    	       layoutbase.addView(playButtons[i]);   	
   	       playButtons[i].setText(sharedPreferences.getString(Integer.toString(i), "problem"));   	
   	       playButtons[i].setOnClickListener(new MyClickListener()); // i had a custom clickListener
               playButtons[i].setText("add a sound");        	   
           }
    }
}

Graveyard Database Logo

Has everything you want to know about grave yards. Plus has many celebrites and world leaders graves. Pssst it is my other site

Google + Profile
Check out Android Photo Mapping, its a app/site I been working on

  • wholesale nfl jerseys
  • wholesale nfl jerseys china
  • wholesale sports jerseys