By liran bh | 9/5/2016 | Android Development

Android Development Snippets

Common tasks in Android development and the required code snippet  

Print and Debug:

Log.d("Tag", "Message");
Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_LONG).show();


Updating component

TextView t=(TextView)findViewById(;


Start activity

Intent in=new Intent("action name");


Start Activity and send parameters:

Intent in=new Intent("");
in.putExtra("num", 100);
startActivityForResult(in, 0);


Return value from Activity:

Intent res=new Intent();
res.putExtra("res", num+20);
setResult(20, res);
finish(); // To close the Activity - optional


Get the Activity result:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);
	int res=data.getIntExtra("res", 99);


Dialog Example

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are You Sure?")
	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
                            // code for yes
	       .setNegativeButton("No", new  DialogInterface.OnClickListener() {
	           public void onClick(DialogInterface dialog, int id) {
AlertDialog alert = builder.create();;


Creating GUI from XML in runtime:

ToggleButton button = (ToggleButton) act.getLayoutInflater().inflate(
				R.layout.template_my_button, null);

//  The XML:
//  <ToggleButton xmlns:android=""
//                             style="@style/switch_icon_style"
//                             />

{{CommentsModel.TotalCount}} Comments

Your Comment


Recent Stories

Top DiscoverSDK Experts

User photo
Ashton Torrence
Web and Windows developer
GUI | Web and 11 more
View Profile
User photo
Mendy Bennett
Experienced with Ad network & Ad servers.
Mobile | Ad Networks and 1 more
View Profile
User photo
Karen Fitzgerald
7 years in Cross-Platform development.
Mobile | Cross Platform Frameworks
View Profile
Show All

Compare Products

Select up to three two products to compare by clicking on the compare icon () of each product.


Now comparing:

{{product.ProductName | createSubstring:25}} X
Compare Now