I have a linear layout which has horizontal orientation. What i want to do is, i want to add images at runtime and those images should be equally spaced in the linear layout. For better understanding here is the desired output.
But i am getting both images at the right end one after another.
Here is the code snippets.
1) Parent layout (test.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/test"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
></LinearLayout>`
2)Child Layout (test_image.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testTab"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
>
<ImageView
android:id="@+id/testImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
Here is the Activity where i am adding children in parent layout`
public class Test extends AppCompatActivity {
private LayoutInflater inflater;
@Override
protected void onCreate(Bundle savedInstanceStste)
{
super.onCreate(savedInstanceStste);
setContentView(R.layout.test);
inflater = (LayoutInflater) Test.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout)findViewById(R.id.test);
LinearLayout tab = provideTab(R.drawable.rottentomatoes);
LinearLayout tab1 = provideTab(R.drawable.rottentomatoes);
layout.addView(tab);
layout.addView(tab1);
}
public LinearLayout provideTab(int id)
{
LinearLayout cardView = (LinearLayout)inflater.inflate(R.layout.test_image,null);
ImageView imageView = (ImageView)cardView.findViewById(R.id.testImage);
imageView.setImageResource(id);
return cardView;
}}
What am i missing here.
(When i add child layout in parent layout using include tag images are getting equally spaced).
Thanks in advance
Change the first line on your provideTab
method to
LinearLayout cardView = (LinearLayout)inflater.inflate(R.layout.test_image, layout, false);
where layout
is the parent LinearLayout (returned from (LinearLayout)findViewById(R.id.test)
).
That second param will provide a set of LayoutParams values for root of the returned hierarchy. Read more here.