Change value of TextView from fragment function - android

I have a fragment called SpeedometerFragment which has a respective XML layout fragment_speedometer
I have then created an independent layout called fragment_distance. There is no activity associated with this XML file.
I want to change the value of a textview "distanceText" in the fragment_distance XML file from a function call in SpeedometerFragment.
The fragment_distance is passed to a frameLayout inside my main activity.
I have tried the following inside onCreateView of SpeedometerFragment but there is no change to the textview:
speedometerFragment
LayoutInflater inflater1 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater1.inflate(R.layout.fragment_distance, container, false);
TextView dis = (TextView) v.findViewById(R.id.distanceText);
dis.setText("Test");

I figured out a solution. I was complicating it in my original question, so there is no need to inflate anything and there is no need for the seperate fragment_distance XML file
I created a public static TextView distance in my main activity.
Then in my fragment, when the value of distance changes, I use Eye_Tracking_Main.distance.setText(new DecimalFormat("#.#").format(distance) + " Km"); to set the value of the TextView
In my main activity , I simply used distance=findViewById(R.id.distancemain);

Related

findViewById() returns null for an inflated layout

I have an Activity and its layout. Now I need to add a LinearLayout from another layout, menu_layout.xml.
LayoutInflater inflater;
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.menu_layout, null);
After this, findViewById() returns null. Is there is any solution for that?
Note: I can't put both XML in one place, and using <include> also is not working.
Explanation
When you inflate a layout, the layout is not in the UI yet, meaning the user will not be able to see it until it's been added. To do this, you must get a hold of a ViewGroup (LinearLayout,RelativeLayout,etc) and add the inflated View to it. Once they're added, you can work on them as you would with any other views including the findViewById method, adding listeners, changing properties, etc
Code
//Inside onCreate for example
setContentView(R.layout.main); //Sets the content of your activity
View otherLayout = LayoutInflater.from(this).inflate(R.layout.other,null);
//You can access them here, before adding `otherLayout` to your activity
TextView example = (TextView) otherLayout.findViewById(R.id.exampleTextView);
//This container needs to be inside main.xml
LinearLayout container = (LinearLayout)findViewById(R.id.container);
//Add the inflated view to the container
container.addView(otherLayout);
//Or access them once they're added
TextView example2 = (TextView) findViewById(R.id.exampleTextView);
//For example, adding a listener to the new layout
otherLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your thing
}
});
Assuming
main.xml contains a LinearLayout with the id container
other.xml is a layout file in your project
other.xml contains a TextView with the id exampleTextView
Try using
layoutObject.findViewById();

How to change properties of a view layout programmatically

I have a custom layout xml which i use as a template for creating items in a list dynamically. However I can't seem to change the text and color of elements within this custom layout properly before I add it to the main layout. I need to do this as each item in the list can be different.
If I have added more than one of these custom layouts to the main layout any changes I make to the TextView object always happen on the first item in the list.
My custom layout has a textview and a check box within a relative layout in a file called 'cat_panel.xml'.
My coding to produce the layout is:
LinearLayout rootEl = (LinearLayout) findViewById(R.id.pageWrapper);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View vw;
vw=inflater.inflate(R.layout.cat_panel, rootEl, false);
catTitleTv = (TextView) findViewById(R.id.catPanelTitle);
catTitleTv.setText("testing 1");
rootEl.addView(vw);
//the above is then repeated
vw=inflater.inflate(R.layout.cat_panel, rootEl, false);
catTitleTv = (TextView) findViewById(R.id.catPanelTitle);
catTitleTv.setText("testing 2");
rootEl.addView(vw);
Thanks in advance
Change
catTitleTv = (TextView) findViewById(R.id.catPanelTitle);
to
catTitleTv = (TextView) vw.findViewById(R.id.catPanelTitle);

Can't get individual references to views when inflating multiple instances

My intent is to inflate numerous instances of the same view on a LinearLayout, and then modify their views accordingly. However, whenever I inflate a view multiple times, I always end up with the same reference to the first inflated view.
Simplified example:
View v = getActivity().getLayoutInflater().inflate(R.layout.inflatable_layout,rootLinearLayout,true);
TextView t = (TextView) v.findViewById(R.id.text1);
t.setText("Foo");
View v2 = getActivity().getLayoutInflater().inflate(R.layout.inflatable_layout,rootLinearLayout,true);
TextView t2 = (TextView) v2.findViewById(R.id.text1);
t2.setText("Bar");
By doing this, I end up with two inflated views. However, the first view's TextView contains "Bar", and the second one unaltered.
Is there a way to be able to maintain reference to the views I inflate individually?
To solve your problem, just change:
View v = inflater.inflate(R.layout.inflatable_layout,rootLinearLayout,true);
to
View v = inflater.inflate(R.layout.inflatable_layout,rootLinearLayout,false);
rootLinearLayout.addView(v1);
Note that you now have to add the view manualy to your layout.
(Do the same for v2)
This way you should receive two different references.
Please try this:
View v = getActivity().getLayoutInflater().inflate(R.layout.inflatable_layout,null); // Note the null replacing the "ViewGroup"
TextView t = (TextView) v.findViewById(R.id.text1);
t.setText("Foo");
View v2 = getActivity().getLayoutInflater().inflate(R.layout.inflatable_layout,null); // Note the null replacing the "ViewGroup"
TextView t2 = (TextView) v2.findViewById(R.id.text1);
t2.setText("Bar");
// Manually append each layout to your linear layout.
I think this should work, as no root is defined

I've a linear layout in a fragment. How to dynamically add a view to it from the activity?

I'm posting data from one fragment and updating the other fragment by adding a view to it. I'm not using listView. I am using LinearLayout.
I've a very simple doubt. How do I get the view and update it by adding another view to it?
Basically I want to inflate the linearlayout that already has the data and add a view to it in the main activity that adds the fragment.
Edit:
LinearLayout ll = (LinearLayout)fragment.getView().findViewById(R.id.commentFragmentLayout);
ViewGroup view = (ViewGroup) ll.getParent();
View customView = view;
TextView commentContext = (TextView) customView.findViewById(R.id.commentText);
commentContext.setText("hello");
view.addView(customView);
I tried to do that in the main activity but my view gave null pointer exception.
Edit 2:
FragmentManager fm = getSupportFragmentManager();
// You can find Fragments just like you would with a View by using FragmentManager.
android.support.v4.app.Fragment fragment = fm.findFragmentById(R.id.fragmentCommentContent);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
View child = inflater.inflate(R.layout.comments_list_item, null);
LinearLayout ll = (LinearLayout)fragment.getView().findViewById(R.id.commentFragmentLayout);
TextView newText = (TextView) child.findViewById(R.id.commentText);
newText.setText("Important text");
ll.addView(newText);
I tried that and got : The specified child already has a parent. Call removeView() on child's parent first. The problem is, if I call remove view, I lose the previous data that I've inserted in the layout.
The code you posted doesn't make any sense. You get the parent of the LinearLayout with the id R.id.commentFragment from the fragment, in that parent you search for a TextView to set some text and then you add the parent to itself.
I've a very simple doubt. How do I get the view and update it by
adding another view to it?
You get the View of a fragment with getView(). You search for the desired component on the view returned by getView() and then add the desired view to that container:
LinearLayout ll = (LinearLayout)fragment.getView().findViewById(R.id.commentFragmentLayout);
TextView newText = new TextView(context);
newText.setText("Important text");
ll.addView(newText);
Basically I want to inflate the linearlayout that already has the data
and add a view to it in the main activity that adds the fragment.
You inflate a layout file not a view that is already present. I really don't understand what you want here try to explain better.
LayoutParams params =new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
lay=(LinearLayout)rootView.findViewById(R.id.Lay);
lay.addView(yourView);

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