Android: Can we add recyclerview dynamically in layout - java

Like any other dynamic view we can add that view dynamically into layout, is it possible with recyclerview that we can add/create dynamically and attach into a viewgroup?
Thanks

RecyclerView.LayoutParams lparams = new RecyclerView.LayoutParams(
RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT);
RecyclerView recyclerView = new RecyclerView(this);
recyclerView.setLayoutParams(lparams);
rootview.addView(recyclerView);
Here rootview is linear layout or any other view group of your layout XML file after that you can add Adapter to this recycle view as usual. Here you can add any number of recycle view using rootview.addView(recyclerView);

Related

Using Adapter and Item Holder, Hide views from List View

I want to show my text view as shown in Image Below, for which i have to
remove other Views (as I have taken a Text View, Image View and a Video View
in a single XML). I am using Item Holder to show my Text View, and
want to hide Video View and Image View together.
I m trying to code but it is not working.
itemHolder.textViewMessage = (TextView) convertView
.findViewById(R.id.messageDetail);
if (itemHolder.textViewMessage != null && messageBean.getMessage()!=null)
{
itemHolder.textViewMessage.setText(messageBean.getMessage()); // here i am showing my text messages.
itemHolder.imageview2.setVisibility(View.GONE);
itemHolder.videoView.setVisibility(View.GONE);
}
[1]: http://i.stack.imgur.com/PsKTL.png
[2]: http://i.stack.imgur.com/XYCMb.png
In images given above, I don't need extra space below text.
Here Image View and Video View are showing extra space, which
I want to get Invisible.
Here view.gone not Working

Getting view from child-layout from tablerow (Android)

I have a tablerow in a tablelayout. Now, I can get tablerows without problem. Tablerow contains a linearlayout and the linearlayout contains views that I need. Is there any possibility to get the linearlayout, and then the from it views?
I cannot do this, because View doesn't have getChildAt method:
View layout = row.getChildAt(j);
EditText first = (EditText)layout.getChildAt(0);
I cannot do this either, because it leads to runtime error when converting View to Layout:
LinearLayout layout = (LinearLayout)row.getChildAt(j);
EditText first = (EditText)layout.getChildAt(0);
If someone has better solution then post it and I'll mark it as solution.
The way I solved this was by adding ID to layout before adding it to the tablerow:
layout.setId(id);
row.addView(layout);
And then getting layout by ID:
View layout =row.getChildAt(j);
LinearLayout layoutLL = (LinearLayout) findViewById(layout.getId());
EditText first = (EditText)layout1.getChildAt(0);
I couldn't post XML-file because layout is made programmatically.

Accessing a layout from adapter

I am new with Android development.
My activity_main.xml file contains a Listview and wanna put a TextView each line of list view. Then I added a new layout file under layout/ folder to identify my text.
But, I couldn't access the layout and TextView while writing inflate in Adapter class.
Below the piece of Adapter class code.
View lineView;
lineView = mInflater.inflate(R.layout.line_layout, null);
TextView textView = (TextView) lineView.findViewById(R.id.namesurname);
Andy idea why getting compile error with this code?

access items inside custom NavigationDrawer

I'm an Android beginner. I'm using Android Studio, and I create my activity with the NavigationDrawer.
So I have a ListView, but I want to add some other things to my Drawer.
I created a custom layout and inside NavigationDrawerFragment.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout insideDrawer = (RelativeLayout) inflater.inflate(
R.layout.fragment_fragment_inside_drawer, container, false);
return insideDrawer;
}
And it works.
I have a ListView and a button inside the drawer.
I tried to access the list view with getActivity.findViewById(R.id.mylistview) but it doesn't work.
So how can I access my objects inside the drawer?
Thanks!
You are searching for your view from the activity, but because the view you are looking for is inside a fragment (we know there's a fragment here, because the onCreateView), you must search from the view that you inflate in that method.
After you return a view in onCreateView, you can use the getView() method of the fragment, then call .findViewById().
Or if you need to search for the view before its returned in onCreateView, after inflating it, just call insideDrawer.findViewById() -- since insideDrawer is your relativeLayout you inflate in that method.

Is it possible to set content view of a single view group within the code?

I want to know whether it is possible to use an xml layout file to define the content of a view dynamically from within the code. When we start an activity we pass it the xml layout to use with the method call setContentView(R.layout.main); but is it possible to use an xml layout file to define a dynamically created ViewGroup such as LinearLayout?
I have a layout xml which shows a score table for a game. Each score that is displayed on this screen needs to be dynamically added via code. I know that it is possible within the code to create a ViewGroup for this score and populate it with all the things I need to make a single score, and then do this every time for each score, and then add them all to the existing UI structure, already defined in the xml layout. What I would like to know is if it is possible to use another xml file to do this?
For example, a layout xml file:
<LinearLayout android:id="#+id/top">
<LinearLayout android:id="#+id/column_heading"/>
</LinearLayout>
In another xml layout file is something like:
<LinearLayout android:id="#+id/row">
<TextView/>
<TextView/>
<TextView/>
<TextView/>
</LinearLayout>
Within the code I would like to do something like the following:
LinearLayout top = (LinearLayout)findViewById(R.id.top);
for (int i = 0; i < num_of_rows; i++) {
LinearLayout row = new LinearLayout(this);
row.setContentView(R.layout.row); //where R.layout.row is the second layout above
// ... dynamically change values as needed
top.addView(row);
}
However .setContentView(...) is not a valid method of LinearLayout. Is there another way to do this? I know I could do it all by code, but that's rather messy and this way would seem to be very tidy and rational..
You should use LayoutInflater for this. Here is a short example
LinearLayout top = (LinearLayout)findViewById(R.id.top);
for (int i = 0; i < num_of_rows; i++) {
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout row = (LinearLayout)inflater.inflate(R.layout.row, null);
// ... dynamically change values as needed
top.addView(row);
}
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.row, null);
You can use LayoutInflater's inflate method to inflate an arbitrary layout from resources. If you provide a root view parameter to this method, the inflated layout will be contained within it. This way you can inflate the XML view into your row.

Categories

Resources