using 'for' loop I am creating some dynmic layouts but I don't want to code them. I want to take reference from any other layout xml file. Is it possible?
You can do something along the lines of following:
for (some loop) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.some_layout, null);
containerView.addView(view);
}
Related
In Java, building an Android app, I need to create one or more custom views and insert them in a Layout.
Each view can have different width. The heigh is always the same. I would like to place each view side by side and if the right border of the screen is reached, the next one must go to a new line.
In fact, imagine that each view represents a word, all the words are a paragraph. So all my custom view must be placed like the words, side by side and line by line.
Is there a way to do this easily with any existing Android object ?
You can try ChipGroup and Chip. I think it will meet your requirements.
At first add ChipGroup in your view xml like this:
<com.google.android.material.chip.ChipGroup
android:id="#+id/cgWords"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Then, to dynamically add Chip in this, sample code can be as following:
private void showTags(List<String> words) {
for (String word : words) {
Chip chip = new Chip(context);
chip.setText(word);
ChipGroup cgWords = findViewById(R.id.cgWords);
cgWords.addView(chip);
}
}
This is a screenshot after running a sample code:
I want solution of following:
I have create xml file in data/data/com.example.file/files/abc.xml so during running of app how I get this abc.xml in res/layout and should display on screen.
Thanks.
In general, the XML vocabulary for declaring UI elements closely
follows the structure and naming of the classes and methods, where
element names correspond to class names and attribute names correspond
to methods.
Courtesy goes to #Mike M : You cant create this .XML files used for layouts are pre-compiled .
I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
the RelativeLayout item will be the container for your child view.
Your question needs to be a bit more specific, but here's a general answer:
If you are using fragments then you should re-create your fragment and then use:
inflater.inflate(R.layout.abc, container, false);
If you are using activities then you should use:
setOnContentView(abc);
Short Story:
I have a layout "layout.xml", which gets replaced by another layout "success.xml" after a successful web request. Both layouts have an ImageView that provides the backgrounds to the layouts. These 2 backgrounds both need to be the same, and both are dependent on a user preference.
Longer Story: This all happens in a Fragmnet with an AsyncTask replacing the contentView with "success.xml" in onPostExecute after the web request. This happens as follows:
View view = getView();
view = null;
view = View.inflate(context, R.layout.success, null);
What I tried to do is give both ImageViews the following android:id="#+id/background_image" and then call
ImageView background = (ImageView)view.findViewById(R.id.background_image);
background.setImageResource(R.drawable.bg1);
This background-setting works for the initial view (layout.xml), but on trying to change to "success.xml", I get a NullPointException because background is null.
I've checked and the View's id is set to -1 while the original view's background_image id is set to something sensible and valid.
I've also tried setting the second view's background id like this: android:id="#id/background_image", i.e. without the '+', but still no luck.
The added complication is that it's not just 2 layouts, but about 5 that I need to do this for, so it would be really handy to recycle view id's.
Any help would be greatly appreciated.
Your code for replacing the fragment's view will not do what you want, the original view will remain the same as you change only a reference to that view and not the actual object.
To replace the view of the fragment with the new layout you could have another ViewGroup(for example a FrameLayout) in the basic layout (layout.xml) wrapping your current content(don't forget to give it an id) of layouts.xml(as I understand this is the basic layout). Then, when it's time to replace the layout you could simply do:
// remove the current content
((ViewGroup) getView().findViewById(R.id.yourWrapperLayout)).removeAllViews();
// add the new content
View.inflate(context, R.layout.success, ((ViewGroup) getView().findViewById(R.id.yourWrapperLayout)));
You could avoid adding an extra layout if, by any chance, all your five layouts have the same type for the root view(like a LinearLayout etc). In this case you would use the same code as above but you'll modify the other layouts file to use a merge tag. Also, you'll be looking for the id of the root in the layout.xml layout into which you'll add the content of the other files.
Then you could have the same ids, but you'll have to reinitialize any reference to the views(meaning that you'll have to search for the view again if you store a reference to the view(like a Button field in the fragment class)).
I've got a ListView, which is fully defined in the xml file. Now I want to add a footer to thi s list so that the last element of the list became a button.
So firstly I do this: View v = getLayoutInflater().inflate(R.layout.add_new, null);, where add_new is the layout for my footer
Now I know that I need to do smth like this:
listViewInstance.addFooterView(v);
But i dont really have that instance as my ListView is defined in an xml file. So what do I write instead of listViewInstance? Is there a way to get its instance somehow from an xml?
Thanks in advance xxx
Use ListActivity.getListView() if you are using a ListActivity or Activity.findViewById() to get the reference to the list view otherwise.
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);