Creating activity with layout defined in runtime in android application - android

Usually the activity has a predefined layout which is described in the xml file. What if I know the exact number and types of UI elements only during the runtime?(for example, I need to display as many TextBoxes as user defined) Is it possible to create an activity with a layout defined during runtime and if it is, how?

First set an identifier to a view, where you want to insert your views at runtime :
<LinearLayout
android:id="#+id/linear_layout"
... >
Then you can add child views to this LinearLayout programatically, whenever you want :
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
linearLayout.removeAllViews();
// Add a TextView (it could be any kind of View)
TextView textView = new TextView(this);
textView.setText("...");
linearLayout.addView(textView);

setContentView(layout);
This layout you can define during runtime

Related

Clarification regarding setContentView

What is difference between:
setContentView(R.layout.activity_main);
TextView txtView = (TextView)findViewById(R.id.text);
And
TextView textView = new TextView(this);
setContentView(textView);
I found these two pieces of code. In first the setContentView has a I'd passed to it about layout. And in second case it has a view passed as argument. Is textView also an id. I think the difference is that in first case, it is layout of activity_main as described in XML file(which contains textView as well) and in second case it is id of textView. Tell me if I am correct.
Also tell me what does 'this' refer to here. Why we are using findViewById in first case?
In the first code,
setContentView(R.layout.activity_main);
TextView txtView = (TextView)findViewById(R.id.text);
you are setting the content view of the container and then trying to access the view with ID - text.
For ex:
<RelativeLayout android:height="match_parent"
android:width="match_parent">
<TextView
android:id="#+id/text"
android:text="Hello"
android:height="wrap_content"
android:width="wrap_content"/>
</RelativeLayout>
In this layout file TextView has the id - text
So,in order to access the textview programatically, we make use of findViewById() t to get reference to view.
TextView textView = findViewById(R.id.text);
now we can make use of this view reference to make changes to the view.
For example we can change text like,
textView.setText("This is a test");
As far as
TextView textView2 = new TextView(this);
is concerned, you're creating a TextView dynamically. This can be added to the parent container as and when required.
Activity.setContentView() has 2 signatures. One is taking a layout id as parameter, the other is taking a View as parameter. There is actually a third one taking a View and ViewGroup.LayoutParam as input.
All three methods take what they get (a View or a layout to inflate) and set it as their root element. So in short: There is no real difference here. Just a few options the developer can choose from to tell the Activity about its root UI element
Also see: setContentView description
The line TextView txtView = (TextView)findViewById(R.id.text); is then searching for a TextView with the id "text" within the Activitys Content (in that case every view in R.layout.activity_main).
The line TextView textView = new TextView(this); is creating a new TextView programmatically instead of inflating a layout xml. The this parameter is a Context instance. A Context instance is always needed to create a View. An Activity is a Context.
When you're using the following:
setContentView(R.layout.activity_main);
TextView txtView = (TextView)findViewById(R.id.text);
you're using the activity_main layout as the content of the activity. whenever you're trying to bind the view with findViewByid(), it only search for the views inside the layout and will giving you an error if you're trying to bind views outside the layout. See setContentView (int layoutResID) for details.
When you're using the following:
TextView textView = new TextView(this);
setContentView(textView);
You're creating a TextView with the activity (this) as the context with new TextView(this);. Please be noted that you always need a context whenever you're creating a View.
Then with setContentView(textView); you're setting the textView as the sole content of the activity. See setContentView (View view) for details.

How to display dynamic view in android

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.

Show custom view in all activities

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

Dynamiclly add a textview using existing textview layout parameters

I am trying to add a TextView from the activity when a button has been pressed. I have found how to add a new textview from the activity, however instead of coding the required layout parameters is it possible to copy an existing textviews parameters (in the xml layout) to the new textview?
I have tried:
TextView tv1 = new TextView(this);
TextView tv2 = (TextView) findViewById(R.id.basetext);;
// its this line below which doesn't work
tv1.setLayoutParams(tv2.getLayoutParams());
But it doesn't copy any of the layout parameters...
Any ideas?
You can do One thing.
If you are trying to set just the Value of the Same TextValue every time and if the Layout properties of that TextView is same at all the time then follow below steps:
First Create the One Layout that only contain he TextView layout only with your appropriate properties. (Let name it as layout_textView.xml)
Now, do add that layout_textView.xml dynamically to your main view as per your requirement.
How it will Solve your issue.
If any query then let me know.
Did you call requestLayout() after adding the textview?

User Interface Design in Android

I want to design the layouts programatically that means without the xml file as per project requirement.
But the terms used programatically is completely different from xml file.Is their any useful tutorial to learn programatically that means without xml file. guide me!
you can create any view you want
a linear layout
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
A Text View
final TextView tv = new TextView(this);
tv.setBackgroundColor(0xFFFF00FF);
tv.setTextColor(0xFF000000);
tv.setTypeface(null, Typeface.BOLD);
tv.setText("Where is Fred?");
tv.setGravity(Gravity.CENTER_HORIZONTAL);
and anything else.
Source
I advise you to spend some time learning about the View class and its popular subclasses such as LinearLayout, RelativeLayout, and so on. (Also, spend a bit of time looking at Drawables.)
When you're creating a layout using XML, you're using XML to define a hierarchy of View objects which, at runtime, are "inflated" into a real hierarchy of View objects that the XML layout file describes. For example, your first XML layout file might be a simple LinearLayout that contains a TextView (note I'm simplifying it for brevity):
<LinearLayout ... >
<TextView ... />
</LinearLayout>
In your Activity you would use this layout using setContentView().
All that XML file is doing is providing a specification, if you like, about the View structure that the system needs to build (or inflate) for you. The end result is that there will be a real LinearLayout object (which is a subclass of View) that has a reference a child TextView (again a subclass of View) together with suitable layout parameters.
To do the above programmatically (i.e. by creating instances of objects and using their methods, rather than inflating from XML) you might do something like (again simplified):
LinearLayout container = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText("hello");
container.addChild(tv); // Simple example - usually you'd specify layout parameters
setContentView(container);
The basic point I'm making is that, in really simple terms, a layout XML file can be thought of as a kind of 'script' that you can use to tell the system how to create a hierarchy of Views. You can create exactly the same result by programmatically creating instances of View objects and calling appropriate methods on them. Whichever route you take, the end result is the same: a bunch of View objects in memory that represent a View hierarchy.
What you will find is that XML layout attribute names aren't necessarily the same as the corresponding method names, but you can use the relevant API documentation to see what corresponding XML attribute strings are for given methods. For instance, the API documentation for LinearLayout details all of the methods as well as the XML attributes.

Categories

Resources