Snippets

Small pieces of code that can be copy/paste-ed on your project

Snippets, Solutions

Android HttpResponse content(body) to String

Usually I use the following code to convert a Http response content to String:   BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuilder responseBuilder = new StringBuilder(); String line = “”; while ((line = in.readLine()) != null) { responseBuilder.append(line); } in.close(); Log.e(“WS RESPONSE”, responseBuilder.toString()); It is not a bad idea but I found over the internet that […]

, , , ,
Snippets, Solutions

Android ExpandableListView Scroll to Expanded Group Position

Once I had this problem: I had an ExapandableListView with groups and children. When I wanted to expand a group the list was scrolling to the end every time. After a little search I’ve figured it out what the problem was. In my xml file where my ExpandableListView was created, I had this attribute: android:transcriptMode=”alwaysScroll”

, , ,
Snippets

Set the application icon

To set the application icon in Android you have to open the project’s Manifest.xml file and add the following attribute to the application tag: <application android:icon=”drawable resource”> Before you do that make sure you add the icon image in one of your drawable directories.. If you called your icon: app_icon.png then the application tag should look

, ,
Snippets, Solutions

Add image to email intent in Android

How to add image to an email intent in Android: // build a email sending intent Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); emailIntent.setType(“image/png”); // set the email subject emailIntent.putExtra(Intent.EXTRA_SUBJECT, “email subject”); // set the email image path for the attachment emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(“file://” + mEmailImagePath)); // set the body of the email emailIntent.putExtra(Intent.EXTRA_TEXT, “some text message”);

, , , ,
Snippets, Tutorial

Android: Open the Calculator from Android using Intent

If you make an application and you need to open the calculator that comes with Android you can use the code bellow, but be careful because some Android phones might not have the Calculator installed or the name of the package and class might differ. 1. When you make the new project call your main

, ,
Snippets, Tutorial

Android Vibrate on Click

If you want to make the phone to vibrate when you click on a button from your application: 1. You have to declare in your AndroidManifest.xml the permission: <uses-permission android:name=”android.permission.VIBRATE”/> 2. Now go to the class you want to put the vibration and in onCreate put this code: final Vibrator vibe = (Vibrator) yourActivity.this.getSystemService(Context.VIBRATOR_SERVICE); //replace

, ,
Snippets, Tutorial

Android EditText text changes Listener

If you want “listen” to the text changes from an EditText you have to use the TextWatcher. Bellow I will show you an example of TextWatcher: //first, we create an EditText EditText answer = new EditText(this); //second, we create the TextWatcher TextWatcher textWatcher = new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int

, ,
Snippets, Tutorial

Android How To Make an AlertDialog

To make an AlertDialog you have to write this code: AlertDialog.Builder alertDelete = new AlertDialog.Builder(ListsActivity.this); //HERE YOU WILL WRITE THE MESSAGE YOU WANT alertDelete.setMessage(R.string.delete_confirm); //HERE IS THE POSITIVE BUTTON. YOU HAVE TO SET THE BUTTON NAME AND A CLICK LISTENER alertDelete.setPositiveButton(getString(R.string.yes),new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //YOUR ACTION } });

Snippets

How to make a background button selector in XML?

Step 1: Create an XML in the drawable folder and name it selected_item.xml (or any another name you want but to be suggestive). The code in this xml will be: <shape android=”http://schemas.android.com/apk/res/android” shape=”rectangle”> <solid color=”#80000000″></solid> </shape> The color=”#80000000″ is transparent black. Step 2: Create an XML again, in the drawable folder an name it not_selected.xml. The code

, , , ,
Scroll to Top