Aybuke
Aybuke

Reputation: 211

Partially clickable TextView and different text colors for the text -Android

I couldn't do alignment in xml. I do this I can do this

But I wanna do this I wanna do this

My xml codes are;

<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal"
            android:layout_marginTop="25dp"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:gravity="center">
            <TextView
                android:id="@+id/signup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#733E3A"
                android:textSize="15dp"
                android:text="@string/have_account"
                fontPath="fonts/pfdindisplaypro-regular.ttf"
                tools:ignore="MissingPrefix"
                android:layout_gravity="center_horizontal" />
            <TextView
                android:id="@+id/txt_login_go_to_register"
                android:onClick="loginOnClickListener"
                android:clickable="true"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/signup1"
                android:textColor="@color/white"
                android:layout_marginLeft="7dp"
                android:textSize="15dp"
                android:text="@string/txt_register_title"
                fontPath="fonts/pfdindisplaypro-regular.ttf"
                tools:ignore="MissingPrefix"
                android:layout_gravity="center_horizontal" />
        </RelativeLayout>

I have used 2 TextView. One of them is static, but second one is dynamic. When user click second TextView, the fragment will go resister page.

Upvotes: 0

Views: 681

Answers (1)

Dhinakaran Thennarasu
Dhinakaran Thennarasu

Reputation: 3356

You can achieve this with Spannable

TextView layout.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:clickable="true"
    android:background="#BF7366" //Use your Background image or any other color
    android:padding="5dp"
    />

In your activity.java

TextView textView =(TextView) findViewById(R.id.textView);
textView.setMovementMethod(LinkMovementMethod.getInstance());

Spannable word = new SpannableString("Don`t have account yet? ");

word.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.text_color)), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

textView.setText(word);
final Spannable wordTwo = new SpannableString("Sign up here and join our exciting team.");

ClickableSpan myClickableSpan = new ClickableSpan()
{
    @Override
    public void onClick(View widget) {
       // There is the OnCLick. put your intent to Register class here
        widget.invalidate();
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setColor(Color.WHITE);
        ds.setUnderlineText(false);
    }
};
wordTwo.setSpan(myClickableSpan,0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.append(wordTwo);

Screenshot

Upvotes: 2

Related Questions