Ahmad
Ahmad

Reputation: 437

Show text like Confide android app

I need to create text display like the Confide app. What I tried is to use FlowLayout but then I'm not able to get the row so that I can hide show row wise. There seems to be various options but kinda confused and not able to think wwat exactly to do... Like break the TextView and show in ListView but then I don't know how to form lines and create adapter.

Kindly help me if anyone knows this thing. I tried to search on Google but nothing fruitful.

At present I'm showing TextView using FlowLayout and showing hiding each TextView.

Upvotes: 0

Views: 59

Answers (1)

Mago
Mago

Reputation: 545

Try creating custom view like this one, lest call it CustomTextView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:layout_marginTop="3dp"
android:background="#FF0000"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="3dp"
android:paddingBottom="3dp" >

<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:maxLines="1"
android:textColor="#FFFFFF" />

</LinearLayout>

java code:

public class CustomTextView extends FrameLayout implements View.OnClickListener {

    public CustomTextView(Context context) {
        super(context);
        init(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.custom_text_view, this);
        setOnClickListener(this);
        mLabelTextView = (TextView) findViewById(R.id.label);
    }

    @Override
    public void onClick(View v) {
        mLabelTextView.setVisibility(View.VISIBLE)
    }
}

To add this views to your container view try something like this:

Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
mContainerView.removeAllViewsInLayout();
mContainerView.removeAllViews();
int maxWidth = display.getWidth() - 20;

LinearLayout.LayoutParams params;
LinearLayout newLL = new LinearLayout(getContext());
newLL.setLayoutParams(new LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
newLL.setGravity(Gravity.LEFT);
newLL.setOrientation(LinearLayout.HORIZONTAL);

int widthSoFar = 0;

for (CustomTextView customTextView : CustomTextViewList) {

    LinearLayout LL = new LinearLayout(getContext());
    LL.setOrientation(LinearLayout.HORIZONTAL);
    LL.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
    LL.setLayoutParams(new ListView.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
    customTextView.measure(0, 0);
    params = new LinearLayout.LayoutParams(customTextView.getMeasuredWidth(), FrameLayout.LayoutParams.WRAP_CONTENT);
    LL.addView(customTextView, params);
    LL.measure(0, 0);
    widthSoFar += customTextView.getMeasuredWidth();
    if (widthSoFar >= maxWidth) {
        mContainerView.addView(newLL);

        newLL = new LinearLayout(getContext());
        newLL.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));
        newLL.setOrientation(LinearLayout.HORIZONTAL);
        newLL.setGravity(Gravity.LEFT);
        params = new LinearLayout.LayoutParams(LL.getMeasuredWidth(), LL.getMeasuredHeight());
        newLL.addView(LL, params);
        widthSoFar = LL.getMeasuredWidth();
    } else {
        newLL.addView(LL);
    }
}
mContainerView.addView(newLL);

Example of CustomTextViewList (to answer your question from comment)

ArrayList<CustomTextView> CustomTextViewList = new  ArrayList<CustomTextView>()
CustomTextViewList.add(new CustomTextView(context));
CustomTextViewList.add(new CustomTextView(context));
CustomTextViewList.add(new CustomTextView(context));

Upvotes: 1

Related Questions