Getting view from child-layout from tablerow (Android) - java

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.

Related

Android: Can we add recyclerview dynamically in layout

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);

How to remove or change Recyclerview footer?

I am using this EndlessRecyclerView.java class to add footer or load more view to the RecyclerView. I have set a footer using the setProgressView() of the EndlessRecyclerView.java i.e.
mRecyclerView.setProgressView(R.layout.recyclerview_footer_layout);
Now after a specific situation, say after a network timeout, I want to change the TextView value in footer. But I am unable to do so using the follwing code:
View progressView = inflater.inflate(R.layout.recyclerview_footer_layout, null);
progressViewText = (TextView) progressView.findViewById(R.id.loadMoreText);
What is wrong here?

Can't access to a spinner in Java

I've got an activity where there is a button which opens an AlertDialog.
My dialog works and I decided to add a layout to it which contains a spinner.
So I have 3 documents :
mainActivity.java : Its role is to open a Dialog
activity_main.xml
dialog_main.xml : The dialog's layout containing the spinner
I try to retrieve in mainActivity.java the spinner declared in dialog_main.xml (in order to add it an adapter) :
Spinner mySpinner = (Spinner)findViewById(R.id.mySpinner);
However mySpinner = NULL, I can't find mySpinner. What is the problem ?
findViewById
as a method of the Activity class finds Views in your Activity's layout. When you are showing a Dialog, it is not part of your layout, so you have to findViewById in the Dialog's layout.
You are probably inflating a View for the Dialog that you show, you cou can use this view to find your Spinnner.
It probably looks like
View view = layoutInflater.inflate (...);
then you do
view.findViewById(...);
Post the code of how how show the Dialog and I'll show you if you can't do it.

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?

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