0

In my android application i am creating textview dynamically and each textview have onclick with respect to the type which have been send from webservice.

enter image description here

last text must be aligned to next line, below i have added my layout details

My adapter class

ArrayList<GroupTitleVo> titlelist = activitylist.get(position)
                .getTitlelist();
        LinearLayout sample_layout = new LinearLayout(_context);
        sample_layout.setLayoutParams(new LinearLayout.LayoutParams(
                0, 0));
        sample_layout.setOrientation(LinearLayout.HORIZONTAL);
        for (int i = 0; i < titlelist.size(); i++) {

            if (titlelist.get(i).getType().equals("user")) {
                TextView user_text = new TextView(_context);
                user_text.setId(i);
                user_text.setTextColor(Color.parseColor("#000000"));
                user_text.setTextSize(12);
                user_text.setTypeface(null, Typeface.BOLD);
                user_text.setText(" "+titlelist.get(i).getName());
                user_text.setTag(titlelist.get(i).getId()+"~"+titlelist.get(i).getName());

                user_text.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String id = (String) v.getTag();
                        Toast.makeText(_context, "user id" + id,
                                Toast.LENGTH_SHORT).show();
                        String[] name = id.split("~");
                        listener.userProfileredirect(name[0],name[1]);
                    }
                });

                holder.horizontaltext.addView(user_text);
            } else if (titlelist.get(i).getType().equals("verb")) {
                TextView verb_text = new TextView(_context);
                verb_text.setId(i);
                verb_text.setText(" "+titlelist.get(i).getName());
                verb_text.setTextSize(10);
                verb_text.setTag(titlelist.get(i).getId());
                holder.horizontaltext.addView(verb_text);
            } else {
                TextView group_text = new TextView(_context);
                group_text.setId(i);
                group_text.setTextColor(Color.parseColor("#000000"));
                group_text.setTextSize(14);
                group_text.setTypeface(null, Typeface.BOLD);
                group_text.setTag(titlelist.get(i).getId() + "~"
                        + titlelist.get(i).getType());
                group_text.setTextSize(12);
                group_text.setText(" "+titlelist.get(i).getName());

                group_text.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        String id = (String) v.getTag();
                        Toast.makeText(_context, "Group ID" + id,
                                Toast.LENGTH_SHORT).show();
                        String idtype[] = id.split("~");
                        if (idtype.length > 1) {
                            listener.userGroupRedirect(idtype[1], idtype[0]);
                        }
                    }
                });

                holder.horizontaltext.addView(group_text);
            }

        }

And XML parent layout

  <LinearLayout
                android:id="@+id/textlinearlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/posted_person_img_view_id"
                android:orientation="horizontal" >
            </LinearLayout>

Kindly help me to align this textviews

Madhu
  • 1,780
  • 23
  • 47
  • Adding a `View` to a horizontal `LinearLayout` is just going to stick that `View` on the end. You could either use a nested vertical `LinearLayout` in your current setup to which to add the `View`, or use a `RelativeLayout` for the item's root `View`, and set `RelativeLayout.END_OF` and `RelativeLayout.BELOW` rules on the new `View`'s `RelativeLayout.LayoutParams`. – Mike M. Dec 09 '15 at 10:50

1 Answers1

0

You need to make nested layouts.

LinearLayout

You have to set horizontally LinearLayout, so you have all items in one horizontal line. You need to put into your existing LinearLayout your ImageView and create into it another LinearLayout set vertically to have two TextView items one below another.

Your view should look like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_gallery"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="First text"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Second text text"/>

    </LinearLayout>
</LinearLayout>

In existing code create another LinearLayout, set orientation of it as vertical and copy into it your two TextViews.

RelativeLayout

You need to declare a position of every item, just like in this example

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_gallery"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="First text"
        android:layout_above="@+id/textView2"
        android:layout_toRightOf="@+id/image"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Second text text"
        android:layout_below="@+id/textView1"
        android:layout_toRightOf="@+id/image"/>

</RelativeLayout>

I used here XML files, because I think it might be more clear to see what attributes and views you would need to use.

EDIT: To create RelativeLayout programmaticaly read:

How to lay out Views in RelativeLayout programmatically?

Hope it help.

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • i want to create dynamically may be textview will increase up to 20, in that case how can i use xml – Madhu Dec 09 '15 at 11:38
  • Madhu, look at your code. You have already `LinearLayout` with three elements. I think the easiest way to you would be change Linear into Relative and add to your views position arguments like `textView1.toTheRightOf(textView2)`. I never used layouts as you, I always define a .xml layout files and then write a code – piotrek1543 Dec 09 '15 at 11:56
  • 1
    Thanks for the link, it gives idea to create textview dynamically, as like mike suggest – Madhu Dec 09 '15 at 12:15
  • @Madhu if you still would have a question, the issue is still open and you haven't accepted my answer as solution :-) – piotrek1543 Dec 09 '15 at 17:00
  • i dont think that your answer is correct, only your link helped me to get some idea in that case how can i accept your answer – Madhu Dec 10 '15 at 05:23