Android: How to Update a Imageview/Imagebutton with a Number to Show the Number of New Messages

Android: Is it possible to update a ImageView/ImageButton with a number to show the number of new messages?

I was interested in this SO as I didnt know how to do it neither. So I started looking into it and here is how I did it.

  • I am not a specialist in UI so there may be some stuff useless/wrong in the following XML.
  • From what I said above, I didnt manage to have the count on the bottom right corner of the icon. :)
  • For test, I use the standard icon.png from the sdk

res/drawable/shapecount.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="#ff2233" />
</shape>

res/layout/my_widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RelativeLayout
android:orientation="vertical"
android:background="@null"
android:id="@+id/rlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/icon"
android:src="@drawable/icon"
android:layout_margin="0dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="50" android:textSize="9dp" android:textStyle="bold"
android:background="@drawable/shapecount"
android:textColor="#FFFFFF"
android:paddingLeft="3dp" android:paddingRight="3dp"
android:layout_margin="0dp"
android:layout_alignBottom="@+id/rlayout"
android:id="@+id/txtCount" />

</RelativeLayout>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="My App Name" android:textSize="9dp" android:textStyle="bold"
android:background="@drawable/shapecount"
android:textColor="#FFFFFF"
android:paddingLeft="3dp" android:paddingRight="3dp"
android:layout_margin="0dp"
android:layout_alignBottom="@+id/rlayout"
android:id="@+id/txtAppName" />
</LinearLayout>

An home widget is actually a normal view that you can build in an XML file like you would for an activity. There is some rules to follow though. See this link for guideline

In this link which shows you how to build an AppWidget, you can see the following code:

// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);

where R.layout.appwidget_provider_layout is the layout of your home widget, which will be my_widget_layout.xml

From that views, you can set the value you want for any count text view:

int count = 50;//Or whatever value you set to it.
views.setTextViewText(R.id.txtCount,Integer.toString(count));

and then you just have to update the widget itself:

appWidgetManager.updateAppWidget(appWidgetId, views);

NOTE:

The updatePeriodMillis doesnt allow the widget to request an update more than once every 30 min so I use a combination of BroadcastReceiver and Service

EDIT:

See below a activity test if the count you want should be within an Activity instead of a AppWidget. It uses the same XMLs as above:

public class TestActivity extends Activity {
/** Called when the activity is first created. */

final Random gen = new Random();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final TextView t = (TextView) findViewById(R.id.txtCount);

Timer timer = new Timer();
timer.schedule(new TimerTask() {

@Override
public void run() {
runOnUiThread(new Runnable() {

@Override
public void run() {
int a = gen.nextInt(20);
t.setText(Integer.toString(a));
}
});
}
}
,new Date(), 3000L);
}
}

showing number of new messages on button when new message comes and when no new message it should not show

You will need to implement a 1X1 widget.

Android App Widget Resource

Drawing number badge like iPhone in Android

Look at my answer here for widgets/app.

You can use shapecount.xml

res/layout/tabicon.xml

<RelativeLayout
android:orientation="vertical"
android:background="@null"
android:id="@+id/rlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/icon"
android:src="@android:drawable/ic_menu_mylocation" <!-- Just for test -->
android:layout_margin="0dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="50" android:textSize="12dp" android:textStyle="bold"
android:background="@drawable/shapecount"
android:textColor="#FFFFFF"
android:paddingLeft="3dp" android:paddingRight="3dp"
android:layout_margin="0dp"
android:layout_alignBottom="@+id/rlayout"
android:id="@+id/txtCount" />

</RelativeLayout>

When creating your tab:

LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.tabicon, null);
final TextView txtCount = (TextView) view.findViewById(R.id.txtCount);

spec = tabHost.newTabSpec("artists").setIndicator(view).setContent(intent);

Now you can use txtCount to update the number in the tab's icon. See the post I pointed out above for an example of TimerTask to test this.

You will have to arrange the layout as you wish also

How can I give an imageview click effect like a button on Android?

You can design different images for clicked/not clicked states and set them in the onTouchListener as follows

final ImageView v = (ImageView) findViewById(R.id.button0);
v.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()) {
case MotionEvent.ACTION_DOWN: {
v.setImageBitmap(res.getDrawable(R.drawable.img_down));
break;
}
case MotionEvent.ACTION_CANCEL:{
v.setImageBitmap(res.getDrawable(R.drawable.img_up));
break;
}
}
return true;
}
});

The better choice is that you define a selector as follows

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@drawable/img_down" />
<item android:state_selected="false"
android:drawable="@drawable/img_up" />
</selector>

and select the image in the event:

v.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
v.setSelected(arg1.getAction()==MotionEvent.ACTION_DOWN);
return true;
}
});

Application icon with counter on android

You cannot modify an application icon - the path to icon is stored inside AndroidManifest.xml, both files are signed with your certificate, application cannot write to it's own .apk file. You may try a hack - install one "wrapper" app, and let it install/uninstall bunch of other dummy apps, which will have an icons with different numbers on them. However this will require app install permission, which users will be warned about.

So you have to implement a widget.

Is it possible to show a badge on an image being shown in ListView

try this. You may need to amend the size options depending on the size of your image. I use this for 128x128 images.

public static Bitmap getOverlayedImage(Resources res, Drawable img1, Drawable img2) {
float den = res.getDisplayMetrics().density;
int dip = (int) (80 * den + 0.5f);
int sz = (int) (128 * den + 0.5f);

Drawable[] layers = new Drawable[2];
layers[0] = img1;
layers[1] = img2;

LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(1, dip, dip, 0, 00);

Bitmap b = Bitmap.createBitmap(sz, sz, Bitmap.Config.ARGB_8888);
layerDrawable.setBounds(0, 0, sz, sz);
layerDrawable.draw(new Canvas(b));

return b;
}

call it like this:

getOverlayedImage(getResources(), drawable1, drawable2);


Related Topics



Leave a reply



Submit