relativeLayout.setBackgroundResource(R.drawable.b);
setContentView(relativeLayout);
img1.setImageResource(R.drawable.d);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(100,100);
img1.setLayoutParams(parms);
setContentView(img1, parms);
(Part of them)
I set a default background and want to change this b.png after 5 seconds. These lines are in after 5 secs handler method. The above 2 lines make me see new b.png at background. But there is an error when I set imageview over it. I also tried ImageView by adding on xml at origin and to appear as view.visible. But it can't. Only background image is available and no overlapping image on it is denied.
Try this
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout relativeLayout = new RelativeLayout(this);
ImageView img1 = new ImageView(this);
relativeLayout.setBackgroundResource(R.drawable.b);
setContentView(relativeLayout);
img1.setImageResource(R.drawable.d);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(100,100);
img1.setLayoutParams(parms);
relativeLayout.addView(img1, parms);
}
Related
I'm creating the above popup, the content of which consists of rows of horizontal LinearLayout views within a main vertical LinearLayout. Each horizontal LinearLayout contains one ImageView and one TextView.
I'm creating this within a PopupWindow, and doing so programmatically so that I can change the ImageView source as required.
As you can see the first icon seems to take up a lot of space, despite having the same code generating it as the other icons.
Below is the code:
LinearLayout verticalLayout = new LinearLayout(context);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams mainLayoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
verticalLayout.setLayoutParams(mainLayoutParams);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams iconParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams textParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//History row
LinearLayout historyLayout = new LinearLayout(context);
historyLayout.setLayoutParams(layoutParams);
historyLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView historyIcon = new ImageView(context);
historyIcon.setImageResource(R.drawable.small_book_grey);
historyIcon.setAdjustViewBounds(true);
historyIcon.setLayoutParams(iconParams);
historyLayout.addView(historyIcon);
TextView historyText = new TextView(context);
historyText.setLayoutParams(textParams);
historyText.setText("History");
historyLayout.addView(historyText);
verticalLayout.addView(historyLayout);
//Exam row...
//... (duplicate of history row)
I've tried playing around with the layout parameters, even creating a mock xml layout that displays the content as I'd like, to match the parameters to.
If anyone can give some advice on making that book icon the same size as the others, I'd be grateful.
Add a scaleType to ImageView of fitCenter
Write this code under historyIcon.setAdjustViewBounds(true);
historyIcon.setWidth()`
And put width according to your layout.
Although I didn't figure out why the first image was scaling differently to the other images, I did find another solution: Using compound left drawables.
historyText.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Drawable img = m_context.getResources().getDrawable(
R.drawable.small_book_grey);
img.setBounds(0, 0, img.getIntrinsicWidth() * historyText.getMeasuredHeight() / img.getIntrinsicHeight(), historyText.getMeasuredHeight());
historyText.setCompoundDrawables(img, null, null, null);
historyText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
Manually setting the bounds to match the TextView worked. Seems clunky, but it was the only way I could get it to do what I was aiming for.
In the following code , Why I can not set a custom size for my relativeLayout object? The resolution of my device is 240 * 320.
RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
relativeLayout = new RelativeLayout(this);
relativeLayout.getLayoutParams().width=240;
relativeLayout.getLayoutParams().height=320;
setContentView(relativeLayout);}
You should try to use a layout parameter like this:
RelativeLayout relativeLayout = new RelativeLayout(this);
//You can put the background in another color to check were is the layout
//relativeLayout.setBackgroundResource(android.R.color.black);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(240, 320);
relativeLayout.setLayoutParams(lp);
setContentView(relativeLayout);
I have an ImageView which I want it to enlarge once the app is launched
So, firstly I did this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.screenshot198);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
layout.addView(imageView);
imageView.setPivotX(1.0f);
imageView.setScaleX(1.5f);
imageView.setScaleY(1.5f);
}
It works great!
Then I try to set it at bottom by adjusting the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.screenshot198);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
imageView.setLayoutParams(params);
layout.addView(imageView);
imageView.setPivotX(1.0f);
imageView.setPivotY(1.0f);
imageView.setScaleX(1.5f);
imageView.setScaleY(1.5f);
}
Hey! What's going on with my tree!?
The only difference between these two paragraphs of code is that, I've added [params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);] in order to put image at bottom, and added [setPivotY(1.0f);] so the image should scale upward from bottom.
But it seems the [setPivotY(1.0f);] doesn't work properly. Anything I'm missing? Thanks!
Comment by #rupps surprisingly helped me. Changing to setPivotY(view.getHeight()) fixed my case.
When using setPivotY(view.getBottom()) I was getting an unexpected offset/margin when using setScaleY().
I've RelativeLayout created in layout/activity.xml
And i want to add some elements there programmatically by following way:
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rlayout.addView(CustomView,p);
And it works, but elements which were added doesn't fill all view, but i need it.
and also i want to add such elements in square (width=height), how can I do it?
To fill all view Use LayoutParams.MATCH_PARENT instead of LayoutParams.WRAP_CONTENT.
To make layout as square just create int width,height = 300; and then :
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(width, height);
or pass LayoutParams.WRAP_CONTENT into RelativeLayout.LayoutParams and change height and width of your custom view.
To make view square :
Button customView = new Button(this);
customView.setLayoutParams(new RelativeLayout.LayoutParams(200, 200));
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
rlayout.addView(customView);
to make fill on all view you can use this:
Button customView = new Button(this);
customView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
rlayout.addView(customView);
Also you can add rules for your layout items like this :
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Best wishes.
I'm creating buttons dynamically in my class, I try to position them using 'offsetLeftAndRight()' or '.leftMargin' and '.topMargin' as follows,
public class instruction extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instruct);
final Button btn = new Button(this);
RelativeLayout.LayoutParams paramsd2 =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsd2.leftMargin = 500;
paramsd2.topMargin = 500;
paramsd2.height = 60;
paramsd2.width = 200;
btn.offsetLeftAndRight(300);
btn.setLayoutParams(paramsd2);
addContentView(btn, paramsd2);
}
But the button always stays in the top left corner, how can I position it, what am I doing wrong?
AddContentView() is not the proper way to add a view in an already set layout.
make your main layout a RelativeLayout (check this in the instruct.xml layout file)
use its id to retreive a reference on it in your onCreate() method using
myRelativeLayout = (RelativeLayout) this.findViewById(R.id.itsId)
then add your button to this layout :
myRelativeLayout.addView(myButton);
the layout params of your button seems fine for positioning so it should work.
Set margin on button rather then layout
MarginLayoutParams marginParams = new MarginLayoutParams(backToMainScreenImageView.getLayoutParams());
marginParams.setMargins(0, 0, (int) UIUtil.getRadialButtonMainMargin(this), 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
backToMainScreenImageView.setLayoutParams(layoutParams);
Try something like this :
paramsd2.setMargin(500, 500, 0, 0);
btn.setLayoutParams(paramsd2);