1

I want to set the opacity of background of LinearLayout, I found this solution that works fine if I use normal layouts, but in the case of android-widgets we have to set layout different I wrote the following code

public class AlphaLayout extends LinearLayout {

public AlphaLayout (Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

public AlphaLayout (Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
protected void onAnimationStart() {
    AlphaAnimation alphax = new AlphaAnimation(0.5F, 0.5F);
    alphax.setDuration(0);
    alphax.setFillAfter(true);
    this.startAnimation(alphax);
    super.onAnimationStart();
}

}

And my corresponding widget xml layout is

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin" >

<com.exercise.HelloWidget.AlphaLayout 
    android:id="@+id/myWidgetLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@drawable/appwidget_dark_bg"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/song_info"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"
        android:text="Hello"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white" />

    <TextView
        android:id="@+id/timeWidget"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:text="03:30"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white" />

    
   
</com.exercise.HelloWidget.AlphaLayout>

after running the project When I add a widget to my homescreen, its displaying an error Problem loading widget

Can you tell me what I am doing wrong ? Your help will be appreciated.

Community
  • 1
  • 1
Muaz Usmani
  • 1,298
  • 6
  • 26
  • 48

2 Answers2

1

As stated in Android documentation: You can NOT extend a class supported by RemoteView. So, you can't use your AlphaLayout class.

For any layout background, there are ten different drawables that you can use. You want your image to be transparent. This (as far as I know) can't be done giving what we have.

However, here is a good start:

  • Use a LinearLayout instead of AlphaLayout.
  • Add a Shape Drawable as its background: android:background="@drawable/widget_background"
  • Then, set the background as you want. Here is a suggestion:

    res/drawable/widget_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Corners">
        <gradient android:startColor="#CC111111" android:endColor="#CC7f7f7f" android:angle="45"/>
        <padding android:left="4dp" android:top="1dp" android:right="4dp" android:bottom="1dp" />
        <corners android:radius="4dp" />
        <stroke android:width="2dp" android:color="#FFAfAfAf"/>
    </shape>
    

I believe this is the standard way of making widgets layouts. Good Luck!

iTurki
  • 16,292
  • 20
  • 87
  • 132
0

You can not have custom views / widgets on the homescreen.

the RemoveViews used in the homescreen widgets has limited number of functionality as compared to a typical view.

Prakash Nadar
  • 2,694
  • 1
  • 19
  • 20