0

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>
2
  • can you add the widget layout? Commented Feb 18, 2014 at 1:34
  • sure! added layout of widget Commented Feb 18, 2014 at 10:13

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

cool!! it works!!!! thank you!!! now the problem is: when the image change the size of new image are wrong.
please check the updated part. if you found my answer useful, I hope you tick my answer as right.
ops, sorry. i'm new to stackoverflow :P
glad I could help and welcome to StackOverFlow ;) happy programming

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.