0

I need to place this three text and an image horizontally one after another. How to proceed?

My code is as follows:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_placeorder,
            container, false);
    ScrollView scrl=new ScrollView(getActivity());
     final LinearLayout ll=new LinearLayout(getActivity());
     ll.setOrientation(LinearLayout.VERTICAL);
     scrl.addView(ll);
     for(int i=0;i<3;i++) {
     // TODO Auto-generated method stub
     TextView time=new TextView(getActivity());
     time.setText("07.23"+i);
     ll.addView(time);
    TextView orderId=new TextView(getActivity());
     orderId.setText("ORDER:03987"+i);
     ll.addView(orderId); 
     TextView dollarPrice = new TextView(getActivity());
     dollarPrice.setText("$39.55"+i);
     ll.addView(dollarPrice); 
     ImageView image = new ImageView(getActivity());
     image.setImageResource(R.drawable.arrow_right); 
     ll.addView(image);
     }

Screenshot is as follows:

enter image description here

7
  • change ll.setOrientation(LinearLayout.VERTICAL); to ll.setOrientation(LinearLayout.HORIZONTAL); Commented Jul 24, 2014 at 13:18
  • no this makes to scroll horizontally @NarendraPal Commented Jul 24, 2014 at 13:20
  • You need ScrollView & inside it take linearlayout. Then in that you can add components you want. It means your parent view will be scrollview not linear/relative layout. Commented Jul 24, 2014 at 13:23
  • See concept of horizontal listview. So you will get clear idea of that. Commented Jul 24, 2014 at 13:24
  • you can set image of arrow to second textview i mean you can set it drawableRight to 2nd textview it will be easy to your requirment Commented Jul 24, 2014 at 13:25

3 Answers 3

2

You dont need to create all the views dynamically. Just create xml for items and inflate it inside loop. Like..

MainActivity.java

public class MainActivity extends Activity {

    private LinearLayout layoutmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layoutmain = (LinearLayout) findViewById(R.id.layoutmain);
        for (int i = 0; i <= 3; i++) {
            View viewToLoad = LayoutInflater.from(MainActivity.this).inflate(
                    R.layout.row, null);
            ((LinearLayout) findViewById(R.id.layoutmain)).addView(viewToLoad);
        }

TextView txt = new TextView(MainActivity.this);
    txt.setText("Ship");
    ((LinearLayout) findViewById(R.id.layoutmain)).addView(txt);

    for (int i = 0; i <= 3; i++) {

        View viewToLoad = LayoutInflater.from(MainActivity.this).inflate(
                R.layout.row, null);
        ((LinearLayout) findViewById(R.id.layoutmain)).addView(viewToLoad);
    }
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="PROCESSED" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:id="@+id/layoutmain"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>
</LinearLayout>

row.xml

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

    <TextView
        android:id="@+id/txtfirst"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="07.23" />

    <TextView
        android:id="@+id/txtsecond"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="ORDER:0378945" />

    <TextView
        android:id="@+id/txtthird" android:gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="$36.55" />

</LinearLayout>

OUTPUT :

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

You can use another LinearLayout with orientation Horizontal, and set that to the vertical LinearLayout. So that your scroll won't be affected.

Something like this:

View view = inflater.inflate(R.layout.fragment_placeorder,
            container, false);
ScrollView scrl=new ScrollView(getActivity());
LinearLayout ll=new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);

for(int i=0;i<3;i++) {
    // TODO Auto-generated method stub
    LinearLayout l2=new LinearLayout(getActivity());
    l2.setOrientation(LinearLayout.HORIZONTAL);
    TextView time=new TextView(getActivity());
    time.setText("07.23"+i);
    l2.addView(time);
    TextView orderId=new TextView(getActivity());
    orderId.setText("ORDER:03987"+i);
    l2.addView(orderId);
    TextView dollarPrice = new TextView(getActivity());
    dollarPrice.setText("$39.55"+i);
    l2.addView(dollarPrice);
    ImageView image = new ImageView(getActivity());
    image.setImageResource(R.drawable.arrow_right);
    l2.addView(image);
    l1.addView(l2);
}
scrl.addView(ll);

This will add a new linear layout with 3 textview and imageview in a single line in main outer layout which will be vertical, and scrollable.

Hope this helps.

Comments

0

in your xml layout add horizontal LinearLayout and put your object on that like this :

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal">
    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"/>
    <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />
     <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />
</LinearLayout>

and in your class set this layout horizontal or vertical like that :

LinearLayout m =(LinearLayout) findViewById(R.id.LinearLayout1);
m.setOrientation(LinearLayout.HORIZONTAL);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.