Hi,

Couldn't you just also add the linearlayout (with its textview) to the 
relative layout in (2) as you did in (1) ?

And specifically, add and remove the linear layout to the collection in 
your instantiate & destroy item methods instead of just the text view.

Regards

On Saturday, November 23, 2013 7:11:43 AM UTC+11, Ab wrote:
>
> I would like to create a ViewPager whose width wrap's to its contents, and 
> is centered horizontally in it's parent.  The first code snippet uses a 
> LinearLayout to create this effect, as shown in the first screenshot.  The 
> second code snippet is my attempt to do this with a ViewPager instead of 
> the LinearLayout, but the result is not the desired behavior, as shown in 
> the second screenshot.
>
> Any suggestions as to how I create the first effect, but using a ViewPager?
>
>       @Override
>         protected void onCreate(Bundle savedInstanceState) 
>         {
>         super.onCreate(savedInstanceState);
>         
>         textView = new TextView(this);
>         textView.setLayoutParams(new 
> ViewGroup.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
> LinearLayout.LayoutParams.WRAP_CONTENT));
>         textView.setText("abcabcabcabcabc");
>         textView.setBackgroundColor(Color.YELLOW);
>         
>         LinearLayout llayout = new LinearLayout(this);
>         llayout.setBackgroundColor(Color.BLUE);
>         RelativeLayout.LayoutParams layoutParams = new 
> RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
> RelativeLayout.LayoutParams.MATCH_PARENT);
>         layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
>         llayout.setLayoutParams(layoutParams);
>         llayout.addView(textView);
>         
>         layout = new RelativeLayout(this);
>         layout.setLayoutParams(new 
> ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
> ViewGroup.LayoutParams.MATCH_PARENT));
>         layout.setBackgroundColor(Color.GREEN);
>         layout.addView(llayout);
>         
>         setContentView(layout);
>         }
>     
>     
>     @Override
>     protected void onCreate(Bundle savedInstanceState) 
>     {
>     super.onCreate(savedInstanceState);
>     
>     textView = new TextView(this);
>     textView.setLayoutParams(new 
> ViewGroup.LayoutParams(ViewPager.LayoutParams.WRAP_CONTENT, 
> ViewPager.LayoutParams.WRAP_CONTENT));
>     textView.setText("abcabcabcabcabc");
>     textView.setBackgroundColor(Color.YELLOW);
>     
>     ViewPager pager = new ViewPager(this);
>     pager.setBackgroundColor(Color.BLUE);
>     RelativeLayout.LayoutParams layoutParams = new 
> RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
> RelativeLayout.LayoutParams.FILL_PARENT);
>     layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
>     pager.setLayoutParams(layoutParams);
>     pager.setAdapter(new ViewPagerAdapter());
>     
>     layout = new RelativeLayout(this);
>     layout.setLayoutParams(new 
> ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
> ViewGroup.LayoutParams.MATCH_PARENT));
>     layout.setBackgroundColor(Color.GREEN);
>     layout.addView(pager);
>     
>     setContentView(layout);
>     }
>     
>     class ViewPagerAdapter extends PagerAdapter
>     {
>             @Override
>             public int getCount() 
>             {
>             return 1;
>             }
>     
>             public Object instantiateItem(ViewGroup collection, int 
> position) 
>             {
>             collection.addView(textView, 0);
>             return textView;
>             }
>     
>             @Override
>             public void destroyItem(ViewGroup collection, int position, 
> Object view) 
>             {
>             collection.removeView((View) view);
>             }
>     
>             @Override
>             public boolean isViewFromObject(View view, Object object) 
>             {
>             return (view==object);
>             }
>     
>             @Override
>             public void finishUpdate(ViewGroup arg0) {}
>             
>     
>             @Override
>             public void restoreState(Parcelable arg0, ClassLoader arg1) {}
>     
>             @Override
>             public Parcelable saveState() 
>             {
>             return null;
>             }
>     
>             @Override
>             public void startUpdate(ViewGroup arg0) {}
>     }
>
> ![enter image description here][1]
>
> ![enter image description here][2]
>
>
>   [1]: 
> http://i.stack.imgur.com/6Ajm7.png<http://www.google.com/url?q=http%3A%2F%2Fi.stack.imgur.com%2F6Ajm7.png&sa=D&sntz=1&usg=AFQjCNHvdfuqP1sArG9Qk5ylvyI24qqx7A>
>   [2]: 
> http://i.stack.imgur.com/qWYQY.png<http://www.google.com/url?q=http%3A%2F%2Fi.stack.imgur.com%2FqWYQY.png&sa=D&sntz=1&usg=AFQjCNHCgTlx4GxeVq7aMXc-Wk4aF21lWg>
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to