Android multiple layout inflater, i can work only in the first - android

i have several layout inflater in my code, which is necessary:
for (String valori : timers_valori){
LayoutInflater inflater=(LayoutInflater)getSystemService(context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.elemento_statistica, contenitore_statistiche, true);
TextView nome=(TextView)menuLayout.findViewById(R.id.timer_testo_1);
nome.setText(x+"");
menuLayout.invalidate();
x=x+1;
}
(the code is trimmed)
it works, but it continue to edit the first layout, for example:
OUTPUT
ciao
...
...
INSTEAD I NEED
hello
hola
ciao
any idea? i think it's not linkin correctly the textview.
thanks!

It seems that you need to iterate the id R.id.timer_testo_1 on each loop, as it is overriding the content of same TextView. Another tip is to get LayoutInflater outside of the for.

I think you should add each view that generates the main view,
Example:
addView (nome);
You should do this in every cycle of the loop.
Your code would look like: :
for (String valori : timers_valori){
LayoutInflater inflater=(LayoutInflater)getSystemService(context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.elemento_statistica, contenitore_statistiche, true);
TextView nome=(TextView)menuLayout.findViewById(R.id.timer_testo_1);
nome.setText(x+"");
menuLayout.invalidate();
x=x+1;
addView(nome);
}

Solved by simply adding
nome.setId(x);
right after TextView declaration.

Related

How to getText from another Layout from editText Android

I am making an activity in which an alertDialog will appear. The dialog will have a view which contains:
Linear layout with two editText (Numeric texts) and then setHeight(wrapContent). You can see below.
Hope you are understanding now what i am doing. Here the view is a xml file which i created and it is not the activity's xml file. Now I want that when a user put the pin in the editText then i can get it and confirm it or whatever i want. But the problem is that i am not getting anything when i call getText(). Here is my code.
LayoutInflater factory = getLayoutInflater();
View view = factory.inflate(R.layout.zalert_pass_enable, null); // zalert_pass.. is the xml file
EditText passInput1 = view.findViewById(R.id.inputPass);
EditText passInput2 = view.findViewById(R.id.inputPassConfirm);
String value = passInput1.getText().toString().trim();
I edited my code to so that you can understand, this is not the actual code but it can easily tell my problem. When i toast the value String then i get nothing. Like there are 0 charaters. But i am putting 4 charaters at least each time. Now i know that the problem is in the process of linking the xml file to inflator.
infalInflater.inflate(R.layout.list_item, parent, false);
See the above line of code. This a well upvoted answer of stackOverflow. which tells us that we should not null the inflator. We should give it parent. But what will be the parent in my case.
You can see the picture and that is my problem and i want a solution for that, please help.
Add this line Dialog dialog = new Dialog(context); after you are inflating view and use it to get edittext object like this
LayoutInflater factory = getLayoutInflater();
View view = factory.inflate(R.layout.zalert_pass_enable, null);
Dialog dialog = new Dialog(context); // add this line
EditText passInput1 = dialog.findViewById(R.id.inputPass);
EditText passInput2 = dialog.findViewById(R.id.inputPassConfirm);
String value = passInput1.getText().toString().trim();

Dynamically add a button to a view from a listview Adapter GetView method

As a little eperiment, I'm trying to do the following.
I have an AXML describing a vertical linear layout which contains a listview (only filling 200dp of the vertical linear layout ). The AXML is inflated when the activity starts with SetContentView. Then the listview is correctly populated with values using its Adapter.
In the GetView method of the listview Adapter, I am trying to also dynamically create a button and add it to the linear layout, but for some reason the button is not added.
If I try to add the button in the constructor method of the Adapter instead, it is correctly added.
Can you tell me what could be possibly going wrong?
Let me add some code:
class TracksAdapter : BaseAdapter<string> {
Activity context;
List<Dictionary<string,string>> trackList;
// constructor
public TracksAdapter (Activity context, List<Dictionary<string,string>> trackList) {
this.context = context;
this.trackList = trackList;
// Just as a little test, if I create the button from here it will be correctly added to linear layout:
var ll = context.FindViewById<LinearLayout>(Resource.Id.linLayForResultsActivity);
Button b1 = new Button(context);
b1.Text = "Btn";
ll.AddView(b1);
}
public override View GetView(int position, View oldView, ViewGroup parent) {
// if I create the button from here it will not be added to the layout
var ll = context.FindViewById<LinearLayout>(Resource.Id.linLayForResultsActivity);
Button b1 = new Button(context);
b1.Text = "Btn";
ll.AddView(b1);
// this other code is working
View view = context.LayoutInflater.Inflate(Resource.Layout.ResultItem, null);
var artistLabel = view.FindViewById<TextView>(Resource.Id.resultArtistNameTextView);
artistLabel.Text = trackList[position]["trackArtistName"];
return view;
}
}
Update: adding some more context information because I know this can be a bit weird to understand without it:
In GetView, I don't need to return the new button I am trying to create there. GetView only need to return a listview view item, but, along its execution, GetView also has to create and add a button to the linear layout containing the listview.
The real code is much more complex than that. I have simplified it in the question. In the real code, the listview items are made of text and a button. The GetView also attaches event handlers to the buttons. Then what I need is, when a user clicks a button in any of the listview items, another button is added below the listview. So I need the code for adding another button to be in GetView, and the button needs to be added outside of the listview, ie. to the linear layout containing the listview.
Use the LayoutInflator to create a view based on your layout template, and then inject it into the view where you need it.
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
// insert into main view
ViewGroup insertPoint = (ViewGroup) findViewById(R.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
I looked in you code, you are returning view, while you add the button to ll, you should return ll
what you return in getView() is what you see in the list item layout, since you're adding the button to ll and returning view, the button won't appear.
you can add the button to view as you implementation
Also check this:
Try using boolean addViewInLayout (View child, int index, ViewGroup.LayoutParams params)
http://developer.android.com/reference/android/view/ViewGroup.html#addViewInLayout(android.view.View, int, android.view.ViewGroup.LayoutParams)
It's working... Without making any changes now it's working as it should... ! Ugh!
I really don't know what I was doing wrong here... probably it was because of some sort of caching of older version of the installed APK.. ? I know this sort of stuff can happen, and that's why I've always been uninstalling the app before deplyoing the new version to the device... but still...!

what happens when a view id is used for setContentView()

The Android developer guide seems to suggest that Activity.setContentView() can only be called with a layout ID (R.layout.*). However, I can see view IDs (R.id.*) being used to call the method. For example, in org/xbmc/android/widget/slidingtabs/SlidingTabActivity.java of XBMC, I can see the following code:
private void ensureTabHost() {
if (mTabHost == null) {
this.setContentView(R.id.slidingtabhost);
}
}
So, what does it mean to call setContentView() with a view ID? Thanks!
Additional question based on comment - is "setContentView(viewId);" equivalent to "View v = findViewById(viewId); setContentView(v);"?
Not
that Activity.setContentView() can only be called with a layout ID (R.layout.)
Just any view id can be called by the setContentView().
And layout is also a view!
I think the document should say:Set the activity content from a view(not only a layout) resource. The resource will be inflated, adding all top-level views to the activity. Actually ,it works like this: If you make a setConentView(R.layout.my_layout); then android os will do the following works:
LayoutInflater inflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.my_layout, null);
setConentView(layout);
if you make a setContentView(R.id.myview);it is also the same way to inflate.
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myview = inflater.inflate(R.id.myview, null);
setConentView(myview); `
So I say they are the same.

Problem in Listview Custom adapter class

I am explaining my issue straightaway. Please refer the following code snippet.
#Override
public View getView(int index, View convertView, ViewGroup parent) {
Comments comment = comments.get(index);
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflator.inflate(R.layout.comment_row, null);
}
TextView tvAuthor = (TextView) convertView.findViewById(R.id.commentAuthor);
System.out.println("tvAuthor"+tvAuthor);
tvAuthor.setText(comment.getAuthor());
convertView.setTag(comment);
return convertView;
}
I get null for tvAuthor . As a result, in the immediate next line where I try to setText, I get null pointer exception.
I have declared commentAuthor in the xml correctly. I cannot trace, from where this error pops up. Experts, kindly help.
ny help in this regard is well appreciated.
Look forward,
Regards,
Rony
Make sure that View.findViewById() is called after View.onCreate() since this is when XML is parsed.
Preferably you'd put .findViewById() inside .onCreate() and save the result in a field for use elsewhere.
I think the problem is in the line:
System.out.println("tvAuthor"+tvAuthor);
As you have declared tvAuthor as TextView, this variable refers to the textview, not its value, so while displaying value from the textview to the console, you needs to write:
if(! tvAuthor.getText.toString.equalsIgnoreCase(""))
{
System.out.println("tvAuthor"+ tvAuthor.getText.toString());
}
so that it will take a value from the tvAuthor TextView and then display on the console.

Android: findviewbyid: finding view by id when view is not on the same layout invoked by setContentView

I have an activity MyActivity that extends from MapActivity. In the .xml file containing the layout I can only include the MapView
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/trail_map_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="key"
/>
However I do need to find another view that is located in another .xml file.
Unfortunately, findViewById returns null.
How can I get the view I am looking for?
Thanks a lot!
Thanks for commenting, I understand what you mean but I didn't want to check old values. I just wanted to get a pointer to that view.
Looking at someone else's code I have just found a workaround, you can access the root of a layout using LayoutInflater.
The code is the following, where this is an Activity:
final LayoutInflater factory = getLayoutInflater();
final View textEntryView = factory.inflate(R.layout.landmark_new_dialog, null);
landmarkEditNameView = (EditText) textEntryView.findViewById(R.id.landmark_name_dialog_edit);
You need to get the inflater for this context, access the root view through the inflate method and finally call findViewById on the root view of the layout.
Hope this is useful for someone! Bye
I have changed in my activity but effected.
Here is my code:
View layout = getLayoutInflater().inflate(R.layout.list_group,null);
try {
LinearLayout linearLayout = (LinearLayout) layout.findViewById(R.id.ldrawernav);
linearLayout.setBackgroundColor(Color.parseColor("#ffffff"));
}
catch (Exception e) {
}
}
try:
Activity parentActivity = this.getParent();
if (parentActivity != null)
{
View landmarkEditNameView = (EditText) parentActivity.findViewById(R.id. landmark_name_dialog_edit);
}
Another way to do this is:
// inflate the layout
View myLayout = LayoutInflater.from(this).inflate(R.layout.MY_LAYOUT,null);
// load the text view
TextView myView = (TextView) myLayout.findViewById(R.id.MY_VIEW);
It's impossible. You can only find and access views that are currently running. If you want to check the value of ex. TextView used in previus activity you must save the value is SharedPreferences, database, file or pass by Intent.
I used
View.inflate(getContext(), R.layout.whatever, null)
The using of View.inflate prevents the warning of using null at getLayoutInflater().inflate().
In main Activity just create Static Variable Like below.
//Create Static variable
public static View mainView;
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.activity_home, null);
mainView = view;
//Now just need to Call MainActivity.mainView to Access your home view or Something else.

Categories

Resources