Ali Bahraminezhad
Ali Bahraminezhad

Reputation: 326

Customized Android Widget

I need to create a Widget which look like photo below: enter image description here

Upper part of photo resembles normal state of my costume widget and lower part of it resembles highlight state of the widget.

Widget primary features:

  1. Both Normal and Highlighted sates could have a background with image and color at same time.
  2. It could re-size by width easily (for example 200dp or 100dp)

I have 3 questions:

  1. Is there any particular widget which could help me to create something like what I said?
  2. Or should I create a custom widget myself?

Upvotes: 0

Views: 49

Answers (1)

Libin
Libin

Reputation: 17095

Create a custom layout with a Vertical LinearLayout and two child TextView for Title and description.

Set the description TextView visibility as GONE , since you need to show the description only on highlight state.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_highlight"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/image_shape">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Title"
    android:textStyle="bold"
    android:textSize="30dp"
    android:layout_margin="10dp"/>

<TextView
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hello! Iam here to display the description"
    android:textStyle="italic"
    android:textSize="25dp"
    android:layout_margin="5dp"
    android:visibility="gone"/>

</LinearLayout>

Now, on the code change the description TextView visibility as visible on long click.

    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.layout_highlight);

    final TextView descriptionTextView = (TextView)findViewById(R.id.description);

    linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            descriptionTextView.setVisibility(View.VISIBLE);
            return true;
        }
    });

Also , you need to hide back the description when long click is released. so you have to have onTouch listener like this..

linearLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                descriptionTextView.setVisibility(View.GONE);
            }
            return false;
        }
    });

Upvotes: 1

Related Questions