I want to programatically center my progress bars in code. Here is my code:
LinearLayout streamLayout = new LinearLayout(this);
streamLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
streamLayout.setOrientation(LinearLayout.VERTICAL);
streamLayout.setGravity(Gravity.CENTER);
LinearLayout newsLayout = new LinearLayout(this);
streamLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
streamLayout.setOrientation(LinearLayout.VERTICAL);
streamLayout.setGravity(Gravity.CENTER);
mSwipeView.addView(streamLayout);
mSwipeView.addView(newsLayout);
mSwipeView.addView(new LinearLayout(this));
((LinearLayout) mSwipeView.getChildContainer().getChildAt(0)).addView(streamListView);
((LinearLayout) mSwipeView.getChildContainer().getChildAt(0)).addView(streamProgressBar);
((LinearLayout) mSwipeView.getChildContainer().getChildAt(1)).addView(newsListView);
((LinearLayout) mSwipeView.getChildContainer().getChildAt(1)).addView(newsProgressBar);
((LinearLayout) mSwipeView.getChildContainer().getChildAt(2)).addView(chartImageView);
How can I get the progress bar to be in the centered vertically, and horizontally in my layout?
Try using LinearLayout.layoutParams and setting them to the view. they contain a field gravity that you can set to Gravity.CENTER or similar.
See This Link