How can I make an ImageView appear in the middle of the screen when the user clicks a button? I have placed the image in the res/drawable-folder.
I've been trying with the code below but I don't know how to make the ImageView appear:
View v = new ImageView(getBaseContext());
ImageView image;
image = new ImageView(v.getContext());
image.setImageDrawable(v.getResources().getDrawable(R.drawable.gameover));
Thanks!
//LinearLayOut Setup
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
//ImageView Setup
ImageView imageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
//make visible to program
setContentView(linearLayout);
int id = getResources().getIdentifier("gameover", "drawable", getPackageName());
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams vp =
new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(vp);
imageView.setImageResource(id);
someLinearLayout.addView(imageView);
If you add to RelativeLayout, don't forget to set imageView's position. For instance:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(200, 200);
lp.addRule(RelativeLayout.CENTER_IN_PARENT); // A position in layout.
ImageView imageView = new ImageView(this); // initialize ImageView
imageView.setLayoutParams(lp);
// imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setImageResource(R.drawable.photo);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
layout.addView(imageView);
Create the ImageView
Use an OnClickListener in the button
Add the ImageView to the layout or set the visibility of the ImageView to VISIBLE
Related
My android project connected to server.And i retrieve data from server if it's 5 then create 5 imageview in linearlayout in other words automatically create imageview, depends on retrieved data.
This code Loops over number of images and creates new ImageView for each time, and then it adds them to the Parent Linear Layout. You can also set the LayoutParams of ImageView dynamically as well.
LinearLayout layout = (LinearLayout)findViewById(R.id.parentLayout);
for(int i=0;i<number_of_images;i++) {
ImageView image = new ImageView(context);
layout.addView(image);
}
For Linear Layout:
LinearLayout linearLayout= new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
for(int i=0;i<Number_Images;i++){
//ImageView Setup
ImageView i mageView = new ImageView(this);
//setting image resource
imageView.setImageResource(R.drawable.play);
//setting image position
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
//adding view to layout
linearLayout.addView(imageView);
}
//make visible to program
setContentView(linearLayout);
Hope this Help You.
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 building an application with the following layout:
A RelativeLayout container has a ImageView child as its background and two other RelativeLayouts as the actual content of the page.
The rules of these views are set up as follows:
// Define rules and params for views in the container
ImageView backgroundImg = new ImageView(this);
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lpImg.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
backgroundImg.setImageResource(R.drawable.baggrund_smartphones_retina);
backgroundImg.setLayoutParams(lpImg);
RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.fragment_main_layout);
RelativeLayout storeLayout = (RelativeLayout)findViewById(R.id.fragment_store_layout);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp2.addRule(RelativeLayout.RIGHT_OF, R.id.fragment_main_layout);
mainLayout.setLayoutParams(lp1);
storeLayout.setLayoutParams(lp2);
root.removeAllViews();
//Add background first to ensure its in the back
snapView.addInfoPage(backgroundImg);
snapView.addInfoPage(mainLayout);
Now here is my problem. But my RelativeLayouts are aligning properly to their parent, but the ImageView is not. Instead the imageview is places like this:
What am I doing wrong?
may be your image dimensions is smaller than the screen dimensions of your parentLayout .
So if you want the image to Fit all the screen use the attribut android:scaleType in your imageView Tag via xml :
<ImageView android:id="#+id/img"
blabla
blabla
android:scaleType="fitXY" />
or programmatically :
imgView.setScaleType(ScaleType.FIT_XY);
change like this
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(screenWidth, LayoutParams.MATCH_PARENT);
In place of
RelativeLayout.LayoutParams lpImg = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
And fix imageview like this
backgroundImg.setLayoutParams(ScaleType.FIT_XY);
I'd like to insert a pause button and a Highscore text for my game with the pause button at the upper left of the gameview and the highscore text at the upper right. I'd like to code it programmaticaly, but the highscore and button are not showing up. Here's the code :
gameView = new SFGameView(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
TextView textBox = new TextView(SFEngine.context);
textBox.setText("HIGH SCORE");
textBox.setId(1);
Button pauseButton = new Button(SFEngine.context);
pauseButton.setText("PAUSE");
pauseButton.setGravity(Gravity.RIGHT);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.RIGHT_OF, textBox.getId());
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(textBox);
layout.addView(pauseButton,lp);
layout.addView(gameView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
setContentView(layout,rlp);
here's the button listener, the button won't respond to the touch event
pauseButton.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e) {
try{
Thread.sleep(1000);
}
catch(InterruptedException ex){
}
return true;
}
});
You need to add the LayoutParams correctly. Each View needs LayoutParams of the type of its parent. E.g. your TextView and Button need RelativeLayout.LayoutParams, and your RelativeLayout needs FrameLayout.LayoutParams (as this is the built-in container for anything you add in setContentView).
For the pauseButton set the RelativeLayout.LayoutParams to WRAP_CONTENT & WRAP_CONTENT, with rules ALIGN_PARENT_TOP & ALIGN_PARENT_RIGHT.
For the textBox add another RelativeLayout.LayoutParams with WRAP_CONTENT & WRAP_CONTENT and rules ALIGN_PARENT_TOP & ALIGN_PARENT_LEFT.
Then the RelativeLayout named layout needs FrameLayout.LayoutParams with MATCH_PARENT & MATCH_PARENT.
Example code:
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
SFGameView game = new SFGameView(this);
game.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Button btn = new Button(this);
btn.setText("||");
RelativeLayout.LayoutParams btnLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnLP.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btnLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
btn.setLayoutParams(btnLP);
TextView tv = new TextView(this);
tv.setText("Some Text");
RelativeLayout.LayoutParams textLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
tv.setLayoutParams(textLP);
layout.addView(game);
layout.addView(btn);
layout.addView(tv);
setContentView(layout);
As for the listener. Use setOnClickListener (not onTouch).
I want my TextView to appear below my ImageView in a RelativeLayout that is going into a GridView. I've tried:
public override View GetView(int position, View convertView, ViewGroup parent)
{
RelativeLayout rl = new RelativeLayout(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.FillParent);
ImageView imageView;
TextView tv;
imageView = new ImageView(context);
imageView.LayoutParameters = new GridView.LayoutParams(250, 250);
imageView.SetScaleType(ImageView.ScaleType.CenterCrop);
imageView.SetPadding(8, 8, 8, 8);
imageView.SetImageResource(thumbIds[position]);
imageView.Id = position;
lp.AddRule(LayoutRules.Below, position);
lp.AddRule(LayoutRules.CenterHorizontal);
tv = new TextView(context);
tv.Text = stringIds[position].ToString();
tv.SetTextSize(Android.Util.ComplexUnitType.Dip, 20);
tv.SetTextColor(Android.Graphics.Color.WhiteSmoke);
tv.LayoutParameters = lp;
rl.AddView(imageView);
rl.AddView(tv);
return rl;
}
But the TextView always shows up on top of the ImageView.
Check out this question.
When you call rl.AddView(tv), you should include the LayoutParams rl.AddView(tv, lp).
You have to use
imageView.setLayoutParams(lp) in order to assign the params to your view.
How do you setLayoutParams() for an ImageView?
Since my content is not dynamic, I worked around the issue by simply editing my image in Paint and placing text below the image. Then I made that the background for the ImageView.