First we will create the activity which contain the listview. It extends ListActivity so we only need to set the main layout and create the adapter for the ListView.
public class CheckboxListActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_checkbox); CheckboxListAdapter adapter = new CheckboxListAdapter( getLayoutInflater()); getListView().setAdapter(adapter); } } |
This is the layout file for the activity. It only contains the ListView.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout> |
This is the layout for every element which will be created inside the adapter for every row in the list. It contains a Textview and our CheckBox. Nothing special here.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="5dp" > <TextView android:id="@+id/textView1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/selected" /> </LinearLayout> |
The class SampleData will be used as container for a name and the state of the CheckBox.
public class SampleData { private String name; private boolean selected; public SampleData(String name, boolean selected) { super(); this.name = name; this.selected = selected; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isSelected() { return selected; } public void setSelected(boolean selected) { this.selected = selected; } } |
The class CheckboxListAdapter contains the code to create the view containing a text and the CheckBox for every row in the list. The layoutinflator is used to inflate element_example which we have created before. The class is implementing OnClickListener to be able to serve as listener for the checkbox clicks.
The information from SampleData will be assigned to the TextView and the Checkbox. The SampleData object is tagged to the Checkbox so it can be retrieved in the onClick function to change the state of “selected”
public class CheckboxListAdapter extends BaseAdapter implements OnClickListener { /** The inflator used to inflate the XML layout */ private LayoutInflater inflator; /** A list containing some sample data to show. */ private List dataList; public CheckboxListAdapter(LayoutInflater inflator) { super(); this.inflator = inflator; dataList = new ArrayList(); dataList.add(new SampleData("Peter", false)); dataList.add(new SampleData("Bob", false)); dataList.add(new SampleData("Sara", true)); dataList.add(new SampleData("Mitch", false)); dataList.add(new SampleData("Tracy", false)); dataList.add(new SampleData("Joe", false)); dataList.add(new SampleData("George", false)); dataList.add(new SampleData("Nancy", false)); dataList.add(new SampleData("Susi", true)); dataList.add(new SampleData("Homer", false)); dataList.add(new SampleData("Lisa", false)); dataList.add(new SampleData("Jack", false)); } @Override public int getCount() { return dataList.size(); } @Override public Object getItem(int position) { return dataList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View view, ViewGroup viewGroup) { // We only create the view if its needed if (view == null) { view = inflator.inflate(R.layout.element_example, null); // Set the click listener for the checkbox view.findViewById(R.id.checkBox1).setOnClickListener(this); } SampleData data = (SampleData) getItem(position); // Set the example text and the state of the checkbox CheckBox cb = (CheckBox) view.findViewById(R.id.checkBox1); cb.setChecked(data.isSelected()); // We tag the data object to retrieve it on the click listener. cb.setTag(data); TextView tv = (TextView) view.findViewById(R.id.textView1); tv.setText(data.getName()); return view; } @Override /** Will be called when a checkbox has been clicked. */ public void onClick(View view) { SampleData data = (SampleData) view.getTag(); data.setSelected(((CheckBox) view).isChecked()); } |
The full example can be get at github
Example of an dialog in android using the class DialogFragment which will close itself after aproximate three seconds. Basicly is just a DialogFragment which starts a thread when it is shown which will sleep for the three seconds and then close the dialog again.
public class AutocloseDialog extends DialogFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.autoclose, container); getDialog().setTitle("Hello"); return view; } @Override public void show(FragmentManager manager, String tag) { Log.i("sample_autoclosedialog", "showing dialog"); new CloseThread(this).start(); super.show(manager, tag); } @Override public int show(FragmentTransaction transaction, String tag) { Log.i("sample_autoclosedialog", "showing dialog"); new CloseThread(this).start(); return super.show(transaction, tag); } class CloseThread extends Thread { private AutocloseDialog dialog; public CloseThread(AutocloseDialog dialog) { super(); this.dialog = dialog; } @Override public void run() { try { sleep(3000); dialog.dismiss(); Log.i("sample_autoclosedialog", "dialog dismissed"); } catch (InterruptedException e) { Log.e("sample_autoclosedialog", "exception", e); } } } } |
You can get the full example project at github.
If you have an ImageView which will has to display an image which has to be downloaded first it is best practise to download the image in a seperate Task.
The following AsyncTask will download an image from an given URL and assign it to a existing ImageView.
package com.example; import java.io.InputStream; import java.lang.ref.WeakReference; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.Log; import android.widget.ImageView; /** * AsyncTask to download an image from an URL and attach it to an ImageView. * * @author Gerrit Schimpf * */ public class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> { /** The url from where to download the image. */ private String url; /** Reference to the view which should receive the image */ private final WeakReference<ImageView> imageRef; /** * Constructor. * * @param imageView * The ImageView which will receive the image. */ public ImageDownloadTask(ImageView imageView) { imageRef = new WeakReference<ImageView>(imageView); } /** * This function will be executed to download the image in a background * process. * */ @Override protected Bitmap doInBackground(String... params) { try { InputStream in = new java.net.URL(url).openStream(); Bitmap bitmap = BitmapFactory.decodeStream(in); return bitmap; } catch (Exception e) { Log.e("ImageDownload", e.getMessage()); } return null; } /** * This function will be called after the image download and attaches * the bitmap to the ImageView. * */ @Override protected void onPostExecute(Bitmap bitmap) { if (isCancelled()) { bitmap = null; } if (imageRef != null) { ImageView imageView = imageRef.get(); if (imageView != null) { imageView.setImageBitmap(bitmap); } } } } |
In addition to my post http://schimpf.es/android-clients-for-mysql/ I made some screenshots of all the existing MySQL clients available in android. Sometimes it is alot easier to decide for or against an application viewing some simple screenshots.
If you want to read more about these MySQL clients, please read my post.
List of MySQL clients available on android.
Selling your application is only one of the possibilities to gain money with your android application. The other way, which works fine for any app which got many downloads is to show publicity and get paid either for impressions of these or for clicks your user do on them. Most of the advertisement provider will show a small banner, which you can place in your app, but there are other forms of publicity like videos, popups, advertising using the notifications and so on.
Here is an overview of the most popular advertising provider.
Today I implemented a sdk for free banner exchange to my free android app Easy light. The banner exchange community is called Tap for Tap and promises that that you get a free impresion on one of your banners for every banner you show in your applicaion. This a complete free service and there is no way to buy credits.
The integration of the sdk is very simple and well documented and worked without any problems.
I will report again as soon as i have some stats of the clicks people made on my banners and the increase of the installation rate of the app I am going to promote.
UPDATE(Jun 27)
The banner exchange Tap for Tap is now running in my little test application for this kind of things for two days. In these two days the application did show 55 banner means it earned 55 credits. Most of the people who got the app installed did not update yet to the new version with the Tap for Tap sdk.
In the same two days I spent nearly 2000 credits from the bonus you get for register and installing the sdk. These 2000 impressions resulted in 29 Taps/Clicks in the banner which is a rate of 1.4% which is not to bad. From the stats of the google market i can see 8 installs in the first of these two days which is are 6 more then the usual 2 install this application has every day. The stats for the second day are still missing.
These numbers are not to bad for a complete free service. I will continue testing the service and report more and soon there are more numbers.
How to catch an uncaught Exception in android and avoid the default popup which normally shows.
Read entire article.