Code snippets


Somclass
implements View.OnClickListener
{
  @Override
  public void onClick(View v) 
  {
  }
}//eof-class

@Override
	public void onClick(View v) 
	{
		Button b = (Button)v;
		if (b.getId() == R.id.plusButton)
		{
			
		}
	}

private boolean invalidString(String s)
    {
    	return !validString(s);
    }
    private boolean validString(String s)
    {
    	if (s == null)
    	{
    		return false;
    	}
    	if (s.trim().equalsIgnoreCase(""))
    	{
    		return false;
    	}
    	return true;
    }

You can use this link to see the following sample code

How to create a layout with
  textviews
  buttons
  linear layout 
     vertical
     horizontal
how to gather controls
how to setup buttons
how to respond to buttons
how to read/update edit text controls

Intent i = new Intent(this,ThirdActivity.class);
startActivity(i);

private void gotoPlay(String wordRef)
{
  Intent intent = new Intent(this.ctx,UnscrambleActivity.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  intent.putExtra("neword", wordRef);
  this.ctx.startActivity(intent);
}

Here are the various flags

Go back to the activity in the history and make it the top of the stack by closing every one else above. Also dont' start a new one but invoke the onNewIntent() method on that activity.


<Button
  android:id="@+id/btn_st_animation"
  ...other stuff
  android:onClick="startAnimationCB"
  android:text="Start Animation" />

public void startAnimationCB(View someView)
{
  Log.d(tag,"button clicked");
  animateTv();
}

public void startAnimationCB(Button someButtonView)
{
}

public void showMessage(String tag, String message)
   {
      String s = tag + ":" + message;
      Toast toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
      toast.show();
      Log.d(tag,message);
   }

Understand toast more....

Defining custom views code snippets


<com.androidbook.compoundControls.DurationControl
   android:id="@+id/durationControlId"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />

BaseActivity extends Activity
{
  String tag = null;

  //Default constructor can be denied
  BaseActivity(String inTag){tag=inTag;}

  ...other stuff
}

SomeActivity extends BaseActivity
{
  String tag = "uuuu";
  //satisfy default constructor
  public SomeActivity(){super(tag);}
  ...otherstuff
}

Read about android:launchmode


<activity android:name="com.androidbook.parse.RespondToPushActivity"
            android:launchMode="singleTop"
            android:label="Respond"/>

Remember when singleTop handle the onNewIntent() as onCreate() is skipped


public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.rtp_respond_to_push);
      examineIntent(getIntent());
   }

   
   @Override
   protected void onNewIntent(Intent intent) {
      examineIntent(intent);
   }

Here is how you read from an asset file using application context


private List<String> getDDLStatementsFrom(String assetFilename)  {
       ArrayList<String> l = new ArrayList<String>();
       String s = getStringFromAssetFile(assetFilename);
       for (String stmt: s.split(";"))   {
          l.add(stmt);
       }
       return l;
    }

In an optimized version you want to read the file like a stream and do the splitting on the fly. You may also probably just return the output of split!


public abstract class BaseActivity extends MonitoredActivity
{
   private int menuid = 0;
   private int layoutid = 0;
   
   protected BaseActivity(int defaultLayoutId, int defaultMenuId, String intag)
   {
      super(intag);
      menuid = defaultMenuId;
      layoutid = defaultLayoutId;
   }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          setContentView(layoutid);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu){ 
       super.onCreateOptionsMenu(menu);
          MenuInflater inflater = getMenuInflater();
          inflater.inflate(menuid, menu);
       return true;
    }
    //override this to deal with menus
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
       return super.onOptionsItemSelected(item);
    }
    
    protected void gotoActivty(Class<? extends Activity> activityClass)
    {
      Intent intent = new Intent(this,activityClass);
      startActivity(intent);
    }    
}

protected void gotoActivty(Class<? extends Activity> activityClass)
{
   Intent intent = new Intent(this,activityClass);
   startActivity(intent);
}

@Override
    public boolean onCreateOptionsMenu(Menu menu){ 
       if (menuid > 0)
       {
             MenuInflater inflater = getMenuInflater();
             inflater.inflate(menuid, menu);
          return true;
       }
       else
       {
           return super.onCreateOptionsMenu(menu);
       }
    }

/**
 * Basics
 * ********************
 * Activity name: TestPersitenceDriverActivity
 * Layout file: test_persistence_driver_activity_layout.xml
 * Layout shortcut prefix for ids: tpda_
 * Menu file: No menu file
 * 
 * Stats
 * ********************
 * Retained root object: none
 * Retained Fragment: none
 * Other fragments: None
 * Configuration change: n/a
 * Home and back: n/a
 * Dialongs: none
 * Asynctasks: none
 * 
 * 
 * Primary goal:
 * ***********************
 * 1. Home page
 * 2. Invoke the other activities
 *
 */
public class TestPersitenceDriverActivity extends BaseActivity 
{
   public static String tag = "TestPersitenceDriverActivity";
   public TestPersitenceDriverActivity()
   {
      super(R.layout.test_persistence_driver_activity_layout,
            -1, //no menu file
            tag);
   }
   public void startTestContentProviderActivity(View btn1)
   {
      gotoActivty(ContentProviderTestActivity.class);
   }   
   public void startTestDirectSQLiteStorageActivity(View btn1)
   {
      gotoActivty(DirectSQLitePersistenceTestActivity.class);
   }   
}//eof-class