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.
Related
I want to show a view that should be shown in all activities. I don't know how to inherit views in android. What i did is below, its showing the view in first activity but not in all activities. This pease of code is form my BaseActivity, please help
LayoutInflater inflater = getLayoutInflater();
View child = inflater.inflate(R.layout.custom_error, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
addContentView(child, params);
You could get an Android specific View in the Activity. For example the following code below will add a TextView to the Activity's content area.
TextView tvSample = new TextView(this);
tvSample.setText("Hello!");
((ViewGroup) hostActivity.findViewById(android.R.id.content)).addView(this);
Whereby hostActivity is your current Activity and android.R.id.content is a specific element (the content area, not including the ActionBar).
Alternatively, as already stated, make use of <merge> and <include> tags in your layout XMLs.
you can do this with two solution
for programmatically
1)After adding child view to you parent View need to call setContentView(parentView) and pass you parent layout to it.
and With XMl
2) You can use include tag. follow this link will help you.
http://developer.android.com/training/improving-layouts/reusing-layouts.html
Have you tried 'include' tag of xml? It will do the job.
<include
android:id="#+id/container_header_lyt"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_above=...
android:layout_toLeftOf=...
layout="#layout/header_logo_lyt" //Name of the xml layout file you want to include
/>
In the layout/xxxx use the name of your layout file that should be repeated.
After use the above code in your xml file like any other widget.
When you want to show it:
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
View.inflate(this, R.layout.overlay_layout, rootLayout);
Then when you want to remove it:
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
rootLayout.removeViewAt(rootLayout.getChildCount()-1);
That's a concise solution, you should remove the View by giving the RelativeLayout an id in the XML file, then remove by: rootLayout.removeView(findViewById(R.id.the_id_of_the_relative_layout));.
Answer by nmw
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.
I have a custom view (an extension of a TextView) that I want to dynamically add to my Layout (don't want to include it in the main.xml file).
The book says to fetch the RelativeLayout using findViewById() in my java code then create a new instance of my custom view, then use addView on the RelativeLayout to add the new view.
I'm not getting any errors, but when I click my button to add the new view, nothing is happening (view isn't being added). Do I need to set additional properties on my custom view (layout width, layout height for example) in order for it to be shown?
EDIT: adding code
// changed to an imageview as I thought it might be easier to see an image
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rellay);
MyCustomImageView mciv = new MyCustomImageView(null);
mciv.setId(5);
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mciv.setLayoutParams(p);
mciv.setImageResource(R.drawable.someImage);
rel.Addview(mciv);
Please post your code where you add the view.
But yes, you might be missing the params for width and height. Try something like
LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT);
txtView.setLayoutParams(p);
or what you would like the width and height to be. Also in xml layout, layout_width and layout_height are required attributes.
I have a LinearLayout View with a OnClickhandler and I want to add a View after the LinearLayout programatically when the OnClick event is fired.
public void onClick(View view) {
LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout info = (LinearLayout) li.inflate(R.layout.infolayer, null);
// view.addViewAfter(info)
}
info is the View i want to add. view is the View on which the click goes and after which I want to add info.
How can I do that?
IF u want add a view after the current linear layout then first get the id of the parent layout in which the linear layout is .
for example let u have the the Linear Layout with id "ll" in relative layout(having id parentlayout) and on a button click u want add the text view under the liner layout
public void onClick(View view) {
RelativeLayout rl=new RelativeLayout(this);
TextView tv=new TextView(this)
//set param value ur requirement
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW,R.id.ll);
}
Assuming you have a known amount of linearlayouts could you just place them inside the XML resource and mark them as 'GONE'. When the event occurs make them visible. When they are marked as gone they shouldnt be taking any screen space up.
You can insert through coding and you don't need to inflate. You can create a view of any type calling the constructor and passing the context. A reference to the context can be stored on the view as a field, when the view is being constructed. This way you can always create your view on the fly.
To add the view to the LinearLayout, you just need to call addView. And afterwards, if you would want to remove it, just call removeView.
But the onClick event is inside the LinearLayout object? This might be a problem because the views inside the the LinearLayout might consume the event before it reaches your method. See this post to learn about that.
Let's say I have a LinearLayout, and I want to add a View to it, in my program from the Java code. What method is used for this? I'm not asking how it's done in XML, which I do know, but rather, how can I do something along the lines of this sample code?
(One View).add(Another View)
Like one can do in Swing.
Calling addView is the correct answer, but you need to do a little more than that to get it to work.
If you create a View via a constructor (e.g., Button myButton = new Button();), you'll need to call setLayoutParams on the newly constructed view, passing in an instance of the parent view's LayoutParams inner class, before you add your newly constructed child to the parent view.
For example, you might have the following code in your onCreate() function assuming your LinearLayout has id R.id.main:
LinearLayout myLayout = findViewById(R.id.main);
Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
myLayout.addView(myButton);
Making sure to set the LayoutParams is important. Every view needs at least a layout_width and a layout_height parameter. Also getting the right inner class is important. I struggled with getting Views added to a TableRow to display properly until I figured out that I wasn't passing an instance of TableRow.LayoutParams to the child view's setLayoutParams.
The best way I found is to use the inflate static method of View.
View inflatedView = View.inflate(context, yourViewXML, yourLinearLayout);
where yourViewXML is something like R.layout.myView
please notice that you need a ViewGroup in order to add a view (which is any layout you can think of)
so as an example lets say you have a fragment which it view already been inflated and you know that the root view is a layout, and you want to add a view to it:
View view = getView(); // returns base view of the fragment
if (view == null)
return;
if (!(view instanceof ViewGroup))
return;
ViewGroup viewGroup = (ViewGroup) view;
View popup = View.inflate(viewGroup.getContext(), R.layout.someView, viewGroup);
EDIT:
Kotlin code for the example above (view is the getView() of a fragment)
(view as? ViewGroup)?.let {
View.inflate(context, R.layout.add_credit_card, it)
}
To add the view programmatically, you can do:
LinearLayout rlmain = new LinearLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
LinearLayout ll1 = new LinearLayout (this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.logo);
LinearLayout .LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
iv.setLayoutParams(lp);
ll1.addView(iv);
rlmain.addView(ll1);
setContentView(rlmain, llp);
You can also add any number of views.
LinearLayout is a subclass of ViewGroup, which has a method called addView. The addView method should be what you are after.
The idea of programmatically setting constraints can be tiresome. This solution below will work for any layout whether constraint, linear, etc. Best way would be to set a placeholder i.e. a FrameLayout with proper constraints (or proper placing in other layout such as linear) at position where you would expect the programmatically created view to have.
All you need to do is inflate the view programmatically and it as a child to the FrameLayout by using addChild() method. Then during runtime your view would be inflated and placed in right position. Per Android recommendation, you should add only one childView to FrameLayout [link].
Here is what your code would look like, supposing you wish to create TextView programmatically at a particular position:
Step 1:
In your layout which would contain the view to be inflated, place a FrameLayout at the correct position and give it an id, say, "container".
Step 2
Create a layout with root element as the view you want to inflate during runtime, call the layout file as "textview.xml" :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
</TextView>
BTW, set the layout-params of your frameLayout to wrap_content always else the frame layout will become as big as the parent i.e. the activity i.e the phone screen.
android:layout_width="wrap_content"
android:layout_height="wrap_content"
If not set, because a child view of the frame, by default, goes to left-top of the frame layout, hence your view will simply fly to left top of the screen.
Step 3
In your onCreate method, do this :
FrameLayout frameLayout = findViewById(R.id.container);
TextView textView = (TextView) View.inflate(this, R.layout.textview, null);
frameLayout.addView(textView);
(Note that setting last parameter of findViewById to null and adding view by calling addView() on container view (frameLayout) is same as simply attaching the inflated view by passing true in 3rd parameter of findViewById(). For more, see this.)
One more way to add view from Activity
ViewGroup rootLayout = findViewById(android.R.id.content);
rootLayout.addView(view);
You guys should also make sure that when you override onLayout you HAVE to call super.onLayout with all of the properties, or the view will not be inflated!