0

OK, I'm trying to recycle advice given in these posts about centering an image programmatically without success. My app is basically working and consists of a spinner with a image and some TextViews below it. The content of these depends on the spinner position. What I would like is the image aligned centrally when the spinner is in its default position and aligned left when something has been selected. The layouts are defined in layout files, one for portrait and another for landscape and here is the part of my code that I'm struggling with:-

@Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {

            // The variable index identifies the position the spinner is in.
            // TextView name, country and description... locks to the
            // TextViews defined in the activity_main.xml layout files.
            int index = parent.getSelectedItemPosition();
            TextView name = (TextView) findViewById(R.id.name);
            TextView country = (TextView) findViewById(R.id.country);
            TextView description = (TextView) findViewById(R.id.description);

            // Now we'll check to see if we're in the None Selected spinner
            // position. If true we'll dump the name, country and
            // description TextViews otherwise these will be shown.

            if (index == 0) {

                image.setImageResource(imgs.getResourceId(
                        spinner1.getSelectedItemPosition(), -1));
                // Try and centre the image when none is selected
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
                        .getLayoutParams();
                lp.addRule(RelativeLayout.CENTER_VERTICAL);
                image.setLayoutParams(lp);

                name.setVisibility(View.GONE);
                country.setVisibility(View.GONE);
                description.setVisibility(View.GONE);

            } else {

                image.setImageResource(imgs.getResourceId(
                        spinner1.getSelectedItemPosition(), -1));
                // Try and left align the image when the spinner is NOT in
                // the None Selected position.
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
                        .getLayoutParams();
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                image.setLayoutParams(lp);

                name.setVisibility(View.VISIBLE);
                country.setVisibility(View.VISIBLE);
                description.setVisibility(View.VISIBLE);

                name.setText(leaders[index]);
                country.setText(states[index]);
                description.setText(descrip[index]);
            }

        }

Here are snippets from my layout files: activity_main.xml portrait view:

<ImageView
            android:id="@+id/leaderPhoto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:contentDescription="@string/accessability"
            android:scaleType="centerInside"
            android:src="@drawable/ic_world1" />

activity_in.xml landscape view:

<ImageView
            android:id="@+id/leaderPhoto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="false"
            android:contentDescription="@string/accessability"
            android:paddingLeft="8dp"
            android:scaleType="center"
            android:src="@drawable/ic_world1" />
Roger W
  • 107
  • 1
  • 4
  • 16

3 Answers3

4

Try changing RelativeLayout.CENTER_VERTICAL to RelativeLayout.CENTER_HORIZONTAL if you want it centered horizontally or to RelativeLayout.CENTER_IN_PARENT to center it in both the axes.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • Welthanks Anup, CENTER_IN_PARENT appears to work when the app first kicks off but after selecting something other than the spinner's default position and then going back to the default position it doesn't i.e. the image starts centrally aligned when the app kicks off, gets moved to left aligned when something has been selected and stays left aligned when you go back to the None Selected position. – Roger W Dec 23 '13 at 09:00
  • that's because you are only adding a new rule when index changes. You have to remove the old rule when you add a new one. See: http://stackoverflow.com/a/5110767/1369222 – Anup Cowkur Dec 23 '13 at 09:09
  • Thanks Anup, not solved yet but you've given me a useful lead. Appreciate that. – Roger W Dec 23 '13 at 09:34
1

i made some changes in your code,to solve your question and to help you:

@Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {

            // The variable index identifies the position the spinner is in.
            // TextView name, country and description... locks to the
            // TextViews defined in the activity_main.xml layout files.
            int index = parent.getSelectedItemPosition();
            TextView name = (TextView) findViewById(R.id.name);
            TextView country = (TextView) findViewById(R.id.country);
            TextView description = (TextView) findViewById(R.id.description);

            // Now we'll check to see if we're in the None Selected spinner
            // position. If true we'll dump the name, country and
            // description TextViews otherwise these will be shown.

            if (index == 0) {

                image.setImageResource(imgs.getResourceId(
                        spinner1.getSelectedItemPosition(), -1));
                // Try and centre the image when none is selected
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
                        .getLayoutParams();
                lp.addRule(0);
                //this will make it center according to its parent
                lp.addRule(RelativeLayout.CENTER_IN_PARENT );

                image.setLayoutParams(lp);

                name.setVisibility(View.GONE);
                country.setVisibility(View.GONE);
                description.setVisibility(View.GONE);

            } else {

                image.setImageResource(imgs.getResourceId(
                        spinner1.getSelectedItemPosition(), -1));
                // Try and left align the image when the spinner is NOT in
                // the None Selected position.
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
                        .getLayoutParams();
                lp.addRule(0);
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                image.setLayoutParams(lp);

                name.setVisibility(View.VISIBLE);
                country.setVisibility(View.VISIBLE);
                description.setVisibility(View.VISIBLE);

                name.setText(leaders[index]);
                country.setText(states[index]);
                description.setText(descrip[index]);
            }

        }

update: according Anup Cowkur words, you should use it like this see my updated code: i hope you problem is solved :)

cheers,

Hamad

Hamad
  • 5,096
  • 13
  • 37
  • 65
  • Many thanks Hamad. It partly works but see the comments I added to Anup Cowkur's post which detail the problem. – Roger W Dec 23 '13 at 09:05
  • ok then remove this parent.getSelectedItemPosition(); and use only position for current selected item,then it will work. – Hamad Dec 23 '13 at 09:15
0

Well thanks with the help of Anup and Hamad I've now solved my problem, pretty impressive after posting my Q less than an hour ago. Here's the Java code that now works:

@Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {

            // The variable index identifies the position the spinner is in.
            // TextView name, country and description... locks to the
            // TextViews defined in the activity_main.xml layout files.
            int index = parent.getSelectedItemPosition();
            TextView name = (TextView) findViewById(R.id.name);
            TextView country = (TextView) findViewById(R.id.country);
            TextView description = (TextView) findViewById(R.id.description);

            // Used to set Layout Params for the ImageView
            RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) image
                    .getLayoutParams();
            // Clears the rules for when index changes
            lp.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
            lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);

            // Now we'll check to see if we're in the None Selected spinner
            // position. If true we'll dump the name, country and
            // description TextViews otherwise these will be shown.

            if (index == 0) {

                name.setVisibility(View.GONE);
                country.setVisibility(View.GONE);
                description.setVisibility(View.GONE);

                image.setImageResource(imgs.getResourceId(
                        spinner1.getSelectedItemPosition(), -1));
                // Try and centre the image when none is selected using
                // Layout Params
                lp.addRule(RelativeLayout.CENTER_IN_PARENT);
                image.setLayoutParams(lp);

            } else {

                image.setImageResource(imgs.getResourceId(
                        spinner1.getSelectedItemPosition(), -1));
                // Try and left align the image when the spinner is NOT in
                // the None Selected position using Layout Params
                lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                image.setLayoutParams(lp);

                name.setVisibility(View.VISIBLE);
                country.setVisibility(View.VISIBLE);
                description.setVisibility(View.VISIBLE);

                name.setText(leaders[index]);
                country.setText(states[index]);
                description.setText(descrip[index]);

            }

        }
Roger W
  • 107
  • 1
  • 4
  • 16