I have a LinearLayout in my layout that has three ImageViews inside it. They have their layout_weight set to 1 so they should all take equal space. In XML it says this
<LinearLayout
android:id="@+id/buttonContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="@+id/option1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@drawable/m571" />
<ImageView
android:id="@+id/option2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@drawable/m562" />
<ImageView
android:id="@+id/option3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="@drawable/m531" />
</LinearLayout>
I programmatically change the images every 15 seconds. The program loads around 300 entries from SQLite database on the start. It stores the entries in an ArrayList and shuffles it. Then, every 15 seconds the program takes the first entry in the ArrayList and removes it so it can't be used twice. It uses the object from the ArrayList to change the images.
Here is the code.
// Get the first sign in the list and remove it
RoadSign sign = signs.get(0);
signs.remove(0);
// Get the possible options
List<String> options = Arrays.asList(sign.getOptions().split(","));
// Shuffle the list
Collections.shuffle(options, new Random(System.nanoTime()));
// Create a new ArrayList with two wrong choices and the correct one
ArrayList<String> choices = new ArrayList<String>();
choices.add(options.get(0));
choices.add(options.get(1));
choices.add(sign.getNumber());
// Shuffle the list
Collections.shuffle(choices, new Random(System.nanoTime()));
// Get image resources
int option1Image = getResources().getIdentifier("mypackage:drawable/m" + choices.get(0), null, null);
int option2Image = getResources().getIdentifier("mypackage:drawable/m" + choices.get(1), null, null);
int option3Image = getResources().getIdentifier("mypackage:drawable/m" + choices.get(2), null, null);
// Show the next images
option1.setImageResource(option1Image);
option2.setImageResource(option2Image);
option3.setImageResource(option3Image);
// Change the sign in 15 seconds
handler.postDelayed(runnable, 15000);
The problem is that the images aren't shown correctly every time. The images aren't the same in size but they should still take the same width in the layout. In some cases the images are fine and each of them take 33 % in width.
Here is a picture of the correct placement
Sometimes though this happens.
In some cases only two images are shown. Sometimes all the three are shown but there is one really small, one medium sized and one huge one.
What do I need to do to get the images show correctly?