Android extending linear layout and inflate - android

I'm extending a linear layout wich contains 2 TextView.
The TextViews are inflated from template layouts I created.
My problem is that the backgrund that I set is covering the entire raw, instead only the part that contains text. But, when I'm setting the same layout not in the dynamic way then it is all working and clipping well.
final LayoutInflater inflater = LayoutInflater.from(this.getContext());
//The title.
title = (TextView)inflater.inflate(R.layout.text_view_day_title, null);
title.setText("bla bla");
//The info.
info = (TextView)inflater.inflate(R.layout.text_view_day_info, null);
info.setText("bla bla 2");
this.addView(title);
this.addView(info);
Thanks for helping.

Make a linear layout inside your xml and addview on it.
Make sure that inflated xml has no background background and it has margins between them to show the background of the main xml layout.

Related

How to handle dynamically inflated layout content (Same Layout inflated multiple times)

I have a Linear layout and inside which there are few dropdown and text views,now i want to inflate the layout each time on click of "add more" button, below is the code for inflating the layout. the problem i am facing is to assign the id to each inflated layout content .Inorder to handle the content the inlfated layout i need the unique id for each content for each time i am inflating the layout.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row_view;
row_view = (View)inflater.inflate(R.layout.inflatefloor,null);
inflatefloordetails.addView(row_view);
here inflatefloor is the layout to inflate and inflatefloordetails is the main root layout inside which layout is to be inflated and i am inflating the inflatefloor layout each time on button click.
New to android pardon if anyone finds the question silly.
Note:Check out link below if anyone is facing similar problem, I found this link useful.
How to set Id of dynamic created layout?
I think it will be helpful.
you can put any object when set the tag to be unique:
yourView.setTag(Object object);
and to retrive it:
object.indexOf(yourView.getTag())
Otherwise you have to use custom view.

How to inflate a layout?

I've tabs as activities. In one of the tabs I have a search dialog. When I inflate the search result my tabs disappear. How can I inflate the result while preserving the tabs?
Here's how I inflate the result:
LinearLayout layout = (LinearLayout) findViewById(R.id.quick_search);
if(data==null) {
LinearLayout empty = (LinearLayout) mInflater.inflate(R.layout.empty, null);
layout.addView(empty);
}
else {
LinearLayout workRequest = (LinearLayout) mInflater.inflate(R.layout.quick_search, null);
layout.addView(workRequest);
}
Actually I want to display the searched result in place of LinearLayout 'quick_search' and make sure that the view above 'quick_search' is not lost. My current method inflates the result to occupy the whole screen. How can I avoid that?
See the below link
https://stackoverflow.com/a/2336047/1441666
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child);
item.addView(child);
If quick_search or empty have layout attributes of layout_width and/or layout_height set to match_parent, then they will occupy the whole screen when you inflate them. Either set them to wrap_content or some fixed value to avoid this.
You need a ActivityGroup within the Tab where you want to change the Activity. If you just go on and create a new Activity and display it, your Tab Layout is no longer visible.
See this: http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity

set LayOut Dynamically

i want to add Layout Dynamically on Add button click and on Dynamic Layout show Datepicker,Timepicker Dialog and set value in given Edit Text. show in image on Date Click set Date Right side . Here problem Start when Add Second Same layout and set date it set only on newly created layout
For example you need to create xml layout file with ScrollView and LinearView inside.
Then in your Activity class:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = inflater.inflate(R.layout.your_layout, null);
setContentView(main);
LinearLayout linear = (LinearLayout)main.findViewById(R.id.linear_layout);
and then in onClick method just:
View yourView = inflater.inflate(R.layout.yourView, null);
// Do whatever you want with your View, set up some variables etc.
and to add your view to main view:
linear.addView(yourView);
I know that this is not a direct answer to your question, but maybe will help you with dynamically adding Views.

How to add a view programmatically to an xml layout in android?

Hiiii there, I have an xml layout and when a button is clicked I need to add a TableLayout view to that xml layout. I know how to do this with simple stuff like text views but tables would seem to be a lot harder to define programmatically, Im not quite sure what inflating is but is that what I should be using? Thanks.
You can define the layout in an xml file and then simply inflate that layout using the LayoutInflater. This will give you a View (in your case this will be a TableLayout), then add this view and you are done.
Reference: LayoutInflater
In your xml layout, let's say you want to add the TableLayout inside a RelativeLayout with an id of relativeLayout1.
To add it, you would do the following code:
TableLayout table = new TableLAyout();
// Code to add rows and views to the table
R.findViewById("relativeLayout1").add(table);

How to inflate another view from another view in Android

I know in my onCreate() I can inflate a view from XML by something like:
loadingScreen = (RelativeLayout) findViewById(R.id.loadingScreen);
But how could I do this from another view? Im trying to call up a loading screen by setting its visibility from GONE to VISIBLE but cant seem to figure out how to do this from my glSurfaceView
If you want to inflate a layout the code looks like this:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(context);
View itemView = inflater.inflate(R.layout.layout_details, myRoot);
Here you first create a new LinearLayout an then inflate the layout with id R.layout.layout_details into it. The inflate method then returns the myRoot view.
Here is a tutorial about the LayoutInflater:
Layout resources in Android
Thats actually not inflating. Inflating is the process that parses a XML layout file and creates a structure of View and ViewGroup class instances out of it (setContentView() does this for you in the background for example).
What you do is getting a reference to a view in code that you have defined in your XML layout file. To change the visibility of your GLSurfaceView you have to reference it like you did above. But remember that the View (GLSurfaceView in this case) has to be defined in your layout file.
After referencing you have to call GLSurfaceView.setVisibility() to change it's visibility.
Here's an example:
GLSurfaceView glsurface = (GLSurfaceView) findViewById(R.id.myglsurfaceid);
glsurface.setVisibility(View.VISIBLE);
Of course you can use View.INVISIBLE or View.GONE either, depending on what you want to do.
If you reference a layout (such as a RelativeLayout), you may find children of this layout with the findViewById() of your RelativeLayout instance:
RelativeLayour rl = (RelativeLayout) findViewById(R.id.mylayout);
(Button) mybutton = (Button) rl.findViewById(R.id.mybutton);
But thats usually not neccessary (at least when you just started with Android) because the activities findViewById() finds all Views that are displayed, even in sublayouts. You only have to use it if you have duplicate ids in your ui structure (tbh I never had that case yet) and want to specifiy where to look for your particular View.
You can't get a reference to a View that's doesn't exists in your current Layout, or your current View, (your current Activity content) , but you can create a new View from another XML layout, using LayoutInflater from current Activity.
you can add to you current Activity content, a new View, that's what you mentioned as " loading screen ", even by showing it as a Dialog or by creating View and then add it to root layout in your Activity
I hope I helped you
If I correctly understood what you wanna do:
Supposing you have a glSurfaceView object and you wanna grab a view that's inside that one.
You'll do just the same thing you did for you normal view. Let's say a button:
Button button = (Button) glSurfaceView.findViewById(R.id.buttonid);
If you meant something different let me know in the comments.
EDIT: And then you can just set the button's visibility:
button.setVisibility(Button.GONE)

Categories

Resources