2

I am trying to find out how I can add an item (from xml drawable/workaround_item.xml) in a LinearLayout that is vertically aligned.

<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="100dip"
  android:layout_marginTop="5dip"
  android:paddingTop="5dip"
  android:paddingBottom="5dip"
  android:orientation="horizontal">

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical">            
        <ImageView 
            android:id="@+id/home_image"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" />           
        <TextView
            android:id="@+id/home_team" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" 
            android:gravity="center_horizontal"
            android:textStyle="bold"
            android:textSize="12sp"
            android:textColor="#fff"/>          
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center_vertical"
        android:layout_centerHorizontal="true">
        <TextView 
            android:id="@+id/game_cship" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" 
            android:gravity="center_horizontal|center_vertical"
            android:textStyle="bold"
            android:textSize="10sp"/>               
        <TextView 
            android:id="@+id/game_score" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" 
            android:gravity="center_horizontal"
            android:textStyle="bold"
            android:textColor="#fff" android:textSize="24sp"/>          
        <TextView 
            android:id="@+id/game_date" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" 
            android:gravity="center_horizontal"
            android:textStyle="bold"
            android:textSize="10sp"/>           
    </LinearLayout>

    <LinearLayout
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center_vertical"
        android:layout_alignParentRight="true">                       
        <ImageView 
            android:id="@+id/away_image"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" />           
        <TextView 
            android:id="@+id/away_team" 
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:textStyle="bold"
            android:textSize="12sp"
            android:textColor="#fff" />     
    </LinearLayout>
</RelativeLayout>

At the beginning I was trying to use a ListView but as far I as know there is a bug using listview inside a scrollview. I was doing this because I have more components on the view that should be scrolled together with the list view. So I decided to remove the listview and implement a simple linear layout where i will be adding each element below other and so on.

The layout for the activity is as follow (layout/workaround.xml) and I would like to start adding the elements below the top bar.

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

</LinearLayout>

That's my Java code:

public class DemoActivity3 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workaround);

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearid);

    for(int i = 1; i< 15; i++) {        
        View item = inflater.inflate(R.layout.workaround_item, null);
        TextView x = (TextView) item.findViewById(R.id.game_score);
        x.setText("Score" + i);
        TextView ht = (TextView) item.findViewById(R.id.home_team);
        ht.setText("HomeTeam" + i);
        TextView at = (TextView) item.findViewById(R.id.away_team);
        at.setText("AwayTeam"+i);
        ImageView hi = (ImageView) item.findViewById(R.id.home_image);
        hi.setImageResource(R.drawable.gremio);
        ImageView ai = (ImageView) item.findViewById(R.id.away_image);
        ai.setImageResource(R.drawable.goias);
        ll.addView(item,ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}

That's the result I am getting: http://twitpic.com/3562yu

Does Android allow us to do this? If so, Hoe can I add the elements and set their values? (this layout will be displaying results of soccer games).

1
  • Please post the full XML for your "workaround_item" - what you've posted is invalid XML as you have three LinearLayouts that are not contained in anything. Commented Sep 15, 2019 at 23:40

3 Answers 3

1

You can use the LayoutInflater to create an instance of an xml layout file.

Then just call addView on your LinearLayout.

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

7 Comments

I am inflating my item and adding it 15 times to the linear layout. However, i only see the first one right in the middle of the screen. I was trying to get the "list" of items in the screen, but no success.
You added the same item 15 times? You need to create 15 different objects and add them..
Thats what i am doing. I have a loop for 15 elements and I am adding them. I am trying to paste my code here in the comment section, but no way.
You should be able to edit your question with additional details.
Done. I edited my question adding the java code and the result, as you can see, i see only one result instead of 15.
|
1

There's no "bug" about ListView in ScrollView, this is just nonsense.

There's a feature in ListView that allow you to add header/footer views which would allow you to place anything above and below the regular items of your ListView.

3 Comments

Sorry for saying "bug" but I have seen a lot of threads/posts saying to not use listview inside a header view. Is there any sample code available for adding header to a listview? Thanks
Buy the way, the header will not scroll along with the list itens.
So you just need to define a vertical linear layout containing your Header and then a ListView.
1

As Kevin said you're better to use a list view with a header. The header will scroll with the list.

However if you're programatically adding views to a layout, you're code should work. Try adding them with a LayoutParam object:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ll.addView(item, params);

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.