Reputation: 30
i've created my first widget but i've a problem when i change the button image. below it's a part of code of java file for widget:
@Override
public void onReceive(Context context, Intent intent)
{
if (mp == null){
mp = MediaPlayer.create(context.getApplicationContext(), R.raw.segno);
}
final String action = intent.getAction();
if (ACTION_WIDGET_RECEIVER.equals(action)) {
if (mp.isPlaying()){
mp.stop();
mp.release();
mp = MediaPlayer.create(context.getApplicationContext(), R.raw.segno);
}
else{
RemoteViews control = new RemoteViews(context.getPackageName(), R.layout.widget);
control.setImageViewResource(R.id.button, R.drawable.pausa);
ComponentName cn = new ComponentName(context, Widget.class);
AppWidgetManager.getInstance(context).updateAppWidget(cn, control);
mp.start();
}
super.onReceive(context, intent);
}
super.onReceive(context, intent);
}
I read in the log this:
02-17 23:46:57.042: W/AppWidgetHostView(946): updateAppWidget couldn't find any view, using error view
I think this is a problem but i don't understand how resolve it. when appear this error the widget say: Problem loading widget.
Thank you all
This is a XML Layout for widget
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="4dp">
<Button
android:id="@+id/button"
android:paddingRight="30dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/vai" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@+id/button"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:src="@drawable/segno" />
</RelativeLayout>
Upvotes: 0
Views: 352
Reputation: 7765
you are trying to change the image of a Button
which doesn't have the properties of Button
according to this setImageViewResource
is equivilant to ImageView
properties so Instead of having a button I suggest you change it either to ImageButton
or to ImageView
that is clickable.
so maybe your XML will look like this:
<ImageButton
android:id="@+id/button"
android:paddingRight="30dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/vai" />
Update
the reason is that android:background
is different than android:src
it doesn't resize the photo it actually make the image as size of the photo (setImageViewResource is same as src) .. so you have either to resize the photo from the beginning or try to add this attribute to your ImageButton
android:scaleType="fitXY"
but I am not sure it will work, haven't tried it.
hope this works for you. try it out and give me a feedback.
Upvotes: 2