I want to do something like this:
View v1= new View(this);
v1.setBackgroundResource(R.drawable.pic1);
View v2 = new View(this);
v2.setBackgroundResource(R.drawable.pic2);
v1.addView(v2);
RelativeLayout.LayoutParams params;
rl = (RelativeLayout) findViewById(R.id.activity_main);
rl.addView(v1,params);
I know the code is wrong. It just show how I want to do.
Some websides said that viewGroup may help me to achieve this.
I had tried but never can v2 be shown on the screen.
Does someone can tell me how to achieve this?
Views cannot contain other Views. It simply doesn't work this way in Android.
If you want to place a View inside a different View, the containing View must extend the ViewGroup class.
There are several classes that can help you achieve this:
LinearLayout - if you want your views to be aligned vertically or horizontally.
RelativeLayout - if you want your views to be positioned relative to each-other and/or the container
There a many more.
Hope this helps.
Looks ok but the problem might be that you create a View with no params in a relativelayout. Also I don't know if you can create a View object think it have to be TextView, ImageView or something like that :)
Related
I am creating custom layout (extends FrameLayout). I have a bunch of view defined in xml(it doesn't matter now).
What I need to do. My custom layout has custom defined attributes, let's assume that it named footer_banner_type.
I have different Banners classes some of them I quite different from one another, so I cannot place some base banner in xml. So I have to add some banner based on attribute value.
I am extending FrameLayout . I am newbie and this is my first custom layout.
I don't know how to improve performance.
As I understand Layout iterating and inflating all child views. But if I need to add view in runtime.I don't want to make layout reiterate view hierarchy, because it will be performance issue.
My question is how to implement my task in better way.
//First create your view:
View wonderfulView = new View(this.getApplicationContext());
//Then, create its LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
//Set those layout params in your view
wonderfulView.setLayoutParams(params);
//finaly, add the view to your ViewGroup
yourLayout.addView(wonderfulView);
That's it.
If you want to change the view container, you'll have to remove it form the previous parent like this:
View movingThing = ((YourLayoutClass)findViewById(R.id.currentContainer)).getChildAt(WhereYourViewis);
((YourLayoutClass)findViewById(R.id.currentContainer)).removeView(movingThing);
((YourLayoutClass)findViewById(R.id.newContainer)).addView(movingThing);
I have a title as processed with three textview and an images
and then another title with same three textview.
How to create this in dynamic layout? Since I need this to generate in dynamic in android.
I am new to this. please give me an idea.
Thanks
LinearLayout mContainer;
public void onCreate(Bundle) {
setContentView(R.layout.container);
mContainer = (LinearLayout) findViewByid(R.id.coainter);
View view = layoutInflater.inflate(R.id.inflaterView);
view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mContainer.addView(view,1);
Info -
create the XML you need to add the Views inside.
create the view you need to Dyanmiclly add (inflate or create it)
set the LayoutParams according to the Conatiner.
add the View to the Conatiner the number 1 is the location inside the Container.
of crouse this is an Example i just followed your image.
you can use mConatiner.addView(view) this will insert them according to the Container Choice
Hope it Helps.
I want to create this sort of a view where the cross should be separate image view as I want it to be clickable. How can I achieve this ?
It would be great If I can create this view programatically as I am a dynamic list of images and I am programatically creating the image Views. All I need now is to add the overlapping imageview as well.
Thanks in advance
Use FrameLayout and you can overlay views on top of each other
Ex:
FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
ImageView image = new ImageView(this);
iv.setImageResource(R.drawable.image);
ImageView cross = new ImageView(this);
i.setImageResource(R.drawable.cross);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.RIGHT | Gravity.TOP;
i.setLayoutParams(layoutParams);
frame.addView(image);
frame.addView(cross);
Create a RelativeLayout programmatically, which contains two ImageViews. Place your image in the first one, and your second image in the second one. Adjust the margins accordingly to place it in the top right corner.
First create a completely new layout to use as an placeholder for example "partial_layout.xml". Then in your code first make a LayoutInflater with something like this:
LayoutInflater mInflater = (LayoutInflater) this.getSystemService(this.LAYOUT_INFLATER_SERVICE);
then try to get a fresh copy of this layout with something like this:
View convertView = mInflater.inflate(R.layout.partial_layout, null);
now put your current data to this view, and finally add this view to your content view.
If you create a list of views, you can still use XML by inflating it only when needed - just watch the lecture "the world of listView" in order to do it correctly. Using ListView is much more efficient than creating as many views as the number of items to show (for example, if there are 100 images to show, only those that are on screen need to be created).
Anyway, the xml can contain either FrameLayout or RelativeLayout as the root view, and you just put the 2 imageViews according to the right position you wish to have. You can use the UI designer for this, or edit the XML itself by adding margin-top for the larger image. also, make sure the larger image is written there before the small one.
as for the clicking, you can use setOnClickListener on the small imaveView.
BTW, if it's just images, you should probably use GridView instead of ListView.
How can I retain attributes of the children in a RelativeLayout when dynamically adding ImageViews?
I have a custom ImageView I want to add at runtime to an empty RelativeLayout (nothing inside in XML), I can add the first, then move, scale and rotate them, it works fine.
When I add another ImageView all previously added instances loose their position and their size, but when I touch them they get back just their size, not the position.
In my ImageView I'm overriding onDraw and onTouch, do I need to override something else?
Maybe I have to write my own RelativeLayout implementation? I wouldn't!
This is the pseudocode for adding new ImageView:
create new instance of ImageView
set bitmap
set scaletype
add imageview to the RelativeLayout container
I even tried to set a standard layout parameter for the brand new added ImageView with the same result.
I tried to get the margins for every children, based on its position, and the re set the layout with ImgeView.setLayout(l, t, r, b);
no success...
Here's the XML RelativeLayout container, pretty simple, maybe too much?
<RelativeLayout
android:id="#+id/face_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
Do you need more details to help me? Please ask, I can post the code too, but I have to clean it up a bit before, I can do it tomorrow, in the meantime please give me some advice.
In a RelativeLayout all view positions are related to the others views. So, as you do in xml, you should provide for each view the relative parameters.
Before add imageView to the RelativeLayout call setLayoutParams(layoutParams):
// Create a new RelativeLayout.LayoutParams instance
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// add roules
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
layoutParams.addRule(.........
layoutParams.addRule(.........
//add params to view
imageView.setLayoutParams(layoutParams);
//add imageview to the RelativeLayout container
yourRelativeLayout.addView(imageView);
Look here for all rules.
I have a custom view which will be jar'ed up and added into another project. In the view I want to give an option of a button.
Here is what I have in the CustomView class.
final CustomView currentView = (CustomView) findViewById(this.getId());
RelativeLayout.LayoutParams params = (new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT ));
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
closeButton.setLayoutParams(params);
currentView.addView(closeButton);
This is all wrapped in a RelativeLayout Tag as well as the other objects of the application
Everything compiles however in the CustomView the Button is aligning left instead of right.Any Ideas???
I would guess the problem is your CustomView. It probably doesn't take the entire width of the window, and is just wide enough to fill its children (which, in your case, is the close button). Make sure your CustomView has a fill_parent horizontal layout.
Since your CustomView extends WebView, which, in turn, extends AbsoluteLayout, you can't expect it to handle RelativeLayout's parameters. Instead, it's best you put your customview and your close button inside a RelativeLayout and position them properly.
When adding your closeButton to your currentView you need to supply the LayoutParams as an argument as well in order for them to take effect.
Basically, switch
currentView.addView(closeButton);
with
currentView.addView(closeButton, params);
Since the width of your button is set to wrap_content, you could also try setting its layout_gravity to right.
params.gravity = Gravity.RIGHT;