Saturday 1 June 2013

CHANGING THE CONTENT OF EditText to String.

String myString;
myString = myEditText.getText().toString();



FUNCTION TO WRITE TEXT TO A FILE


public void writeTextToFile(String textToWrite){
   
    String myString;
    myString= textToWrite;
 
//File myFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"fromedittext.txt");//use this for file in downloads Folder
    //File myFile = new File(getExternalCacheDir(),"fromeedittext.txt"); //for file in cache
   
    try {
FileWriter myFileWriter= new FileWriter (myFile,true);//true to append, false to overwrite
myFileWriter.write(myString);
myFileWriter.write("\n");//newline
myFileWriter.flush();
myFileWriter.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
   
    }



Monday 9 April 2012

Learning Android.

Here are some of the Android Methods I have found useful as a Beginner in App development. Might be useful for some...

1. BUTTON CLICK METHOD (as a method called from onCreate(), a field for the Button must have been declared)


public void onClickButton( )
   
      {
    buttonName = (Button)findViewById(R.id.button1cnd); //initialisation of buttonName.
   
      buttonName.setText("Start Creating Fields");//this text will appear on the button itself.
     
//now what will happen on clicking the button:
      buttonName.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
       
                 //code for action on clicking here
       
       }
          });
        }

2.TOAST

CharSequence text = "My Text";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(getApplicationContext(), text, duration);
        toast.show();

3.LIST VIEW


        setListAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, arrayName));
     
        ListView mLv =getListView();
        mLv.setTextFilterEnabled(true);
        mLv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
           
               // Do on Clicking item on list view
// this is opeing new activity and pasing as extra the text on list view item
            Intent i = new Intent(EditexistingDatabase.this,EditProcess.class);
            i.putExtra("database name", ((TextView)view).getText());
           
                startActivity(i);
            }
          });

4. POPULATE ARRAY FROM FILE    (method)

private String[] populateArrayFromFile(){
File file = new File(getExternalFilesDir(null), "Database Record.txt");
String [] stringArr = new String[numOfLines];
try{
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

int x =0;
while(x < numOfLines){stringArr[x]=br.readLine();x++;}
fr.close();
return stringArr;
}catch (IOException e){

    String [] failed={"No Data Bases"};
    return failed;
}

}

5. CREATING A LAYOUT PROGRAMATICALLY (WITHOUT XML)

// THIS IS A LINEAR LAYOUT WITH A TEXT VIEW AND A BUTTON

LnearLayout myLinearLayout =new LinearLayout(this);
myLinearLayout.setOrientation(LinearLayout.VERTICAL);
TextView myTextView = new TextView(this);
dBName.setText("text in TextView");
myLinearLayout.addView(myTextView);
Button myButton = new Button(this);
addRecord.setText("text in Button");
myLinearLayout.addView(myButton);

setContentView(myLinearLayout);

6.ARRAY OF VIEWS

TextView[] myTextView;
myTextView = new TextView [int numberofArrayMembers]; //initialise array

int x=1;
while( x <= numberofArrayMembers){
     
   
       myTextView[x-1] = new TextView(this); //initialise first View of the Array
       myTextView[x-1].setText(fieldNames[x-1]);// set text TextView in array
               LayoutName.addView(fieldName[x-1]);// add TextView to main Layout
         
           x++ ;
}

7.CHECK STRING ARRAYS FOR EMPTY MEMBERS

private boolean check_fieldValueForEmptyMembers(){

int a =1;
while(a <= numOfFields){

if( "".equals(sTRINGARRAY[a-1]){ return true;}
a++;
}
return false;
}