Code snippets
satya - Friday, September 16, 2011 9:54:09 AM
responding to buttons
Somclass
implements View.OnClickListener
{
@Override
public void onClick(View v)
{
}
}//eof-class
satya - Friday, September 16, 2011 9:57:20 AM
further more
@Override
public void onClick(View v)
{
Button b = (Button)v;
if (b.getId() == R.id.plusButton)
{
}
}
satya - Friday, September 16, 2011 10:05:03 AM
I need this often
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;
}
satya - Friday, September 16, 2011 10:52:47 AM
You can use this link to see the following sample code
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
satya - Thursday, September 29, 2011 5:51:49 PM
quickest way to start an activity
Intent i = new Intent(this,ThirdActivity.class);
startActivity(i);
satya - Friday, September 30, 2011 9:01:21 PM
Another example
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);
}
satya - Saturday, October 01, 2011 11:21:07 AM
Brief meaning of these two 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.
satya - Monday, November 07, 2011 1:42:35 PM
Here is a way to set onClick for a button in xml
<Button
android:id="@+id/btn_st_animation"
...other stuff
android:onClick="startAnimationCB"
android:text="Start Animation" />
satya - Monday, November 07, 2011 1:43:26 PM
Here is the onClick signature
public void startAnimationCB(View someView)
{
Log.d(tag,"button clicked");
animateTv();
}
satya - Monday, November 07, 2011 1:44:17 PM
The following is wrong
public void startAnimationCB(Button someButtonView)
{
}
satya - Wed Aug 22 2012 15:44:07 GMT-0400 (Eastern Daylight Time)
showing a quick message
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);
}
satya - Wed Aug 22 2012 15:45:30 GMT-0400 (Eastern Daylight Time)
Understand toast more....
satya - Tue Oct 30 2012 13:59:42 GMT-0400 (Eastern Daylight Time)
Defining custom views code snippets
satya - Tue Oct 30 2012 14:00:06 GMT-0400 (Eastern Daylight Time)
Here is an example of a custom component
<com.androidbook.compoundControls.DurationControl
android:id="@+id/durationControlId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
satya - 3/5/2013 11:02:49 AM
You can have base activities with local variables
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
}
satya - 5/1/2013 4:11:19 PM
An example
<activity android:name="com.androidbook.parse.RespondToPushActivity"
android:launchMode="singleTop"
android:label="Respond"/>
satya - 5/1/2013 4:14:52 PM
Remember when singleTop handle the onNewIntent() as onCreate() is skipped
Remember when singleTop handle the onNewIntent() as onCreate() is skipped
satya - 5/1/2013 4:18:57 PM
Here is an example in code
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);
}
satya - 8/25/2014 1:59:04 PM
Here is how you read from an asset file using application context
Here is how you read from an asset file using application context
satya - 8/25/2014 2:04:48 PM
A simple way to tokenize in android/java
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!
satya - 9/5/2014 12:59:05 PM
Here is an example of a baseclass
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);
}
}
satya - 9/5/2014 12:59:44 PM
Notice the generic method signature for any Acivity extended object
protected void gotoActivty(Class<? extends Activity> activityClass)
{
Intent intent = new Intent(this,activityClass);
startActivity(intent);
}
satya - 9/5/2014 1:03:46 PM
Probably this is a better abstraction in the base class
@Override
public boolean onCreateOptionsMenu(Menu menu){
if (menuid > 0)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(menuid, menu);
return true;
}
else
{
return super.onCreateOptionsMenu(menu);
}
}
satya - 9/5/2014 1:05:32 PM
Example of extending the base activity
/**
* 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