I am having the following method which passes View
private void doSomething(View view){ }
The problem am having is how do i call this view in onCreate method in an Activity, i will have to pass the view
For Example
View view;
doSomething(view)
How do is assign view/instantiate view, am using getView() but its not working
Like
view = getView()
For Example in fragments onViewCreatedMethod has an argument view which i can assign to the method when am calling it. Example below
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
doSomething(view);
}
Is it possible to do that inside onCreate method in fragment, can I parse the View arguments in onCreate method in activity
In your onCreate method, once you've called setContentView, you can use findViewById to get whatever View you want. If you want the root view for some reason, you can pass android.R.id.content.
For example:
#Override
public void onCreate(Bundle savedInstanceState) {
// Replace your_layout_id with your Activity layout ID
setContentView(R.layout.your_layout_id);
View rootView = findViewById(android.R.id.content);
// Replace with whatever ID/View type you have in your code
// Button and your_button_id are just examples
Button button = findViewById(R.id.your_button_id);
}
Related
How are these methods different from each other when trying to get the view?
First:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
listView = view.findViewById(R.id.listview);
return view;
}
Second:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = getActivity().findViewById(R.id.listview); }
* some say this is used to get activity views but i used it for getting fragment views(which didn't existed in the activity) and it worked fine.
Third:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = getView().findViewById(R.id.listview);
}
Three methods are good. In the onCreateView you create the view (!), it's the really first time you can use what you inflated. Then onViewCreated is called with the view you returned in the onCreateView, You can directly use the view given as parameter, it is the same the getView() provides. My advice is to initialise your UI variables here
For onActivityCreated, it is the best place to modify your UI elements. It is called when fragment creation is complete and when fragment is re-attached. There you can use the variables you initialised before, without having to get the activity just for that purpose.
I want change an element of a fragment from MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);// => this is the error
...
list.setOnItemClickListener(new Adapter.OnItemClickListener() {//this can't work because list isn't in layout=>activity_main
....
I need manipulate 'lis't from MainActivity, but i can't, because the element "list" is in the fragment container.xml and isn't in activity_main.xml .
I can't move 'list' in activity_main.xml, because i need load into container.xml.
How can i call from MainActivity in the right way?
Why you need to perform action on fragments view in activity?
You can do all this inside your fragment.
Add that fragment to activity.
In fragments onCreateView method get the view and do whatever you want.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflatedView = inflater.inflate(R.layout.your_layout, container, false);
ListView list = (ListView) inflatedView.findViewById(R.id.list);// => this is the error
list.setOnItemClickListener(new Adapter.OnItemClickListener() {});
return inflatedView;
}
Although it is a bad idea and it is better that you find a better solution for your requirement, the answer is that you can make your ListView inside fragment public static, and then perform your action on it like:
YourFragment.list.setOnItemClickListener(new Adapter.OnItemClickListener()...
I want to set text of a TextView that is inside a layout file,
I tried setContentView() but it isnt working since i am using fragments.
I tried using getResources().getLayout(R.layout.abc);
It returns null
I tried setContentView() but it isnt working since i am using
fragments
That's wrong. You have to override onCreateView and inflate and return the layout you want to show, and you can use onCreateView, and use its first parameter, View view, to call findViewById and access the widgets in your layout. You can read more here and here
inside fragment you can set view inside function onCreateView(), use below code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b) {
View view = (LinearLayout) inflater.inflate(R.layout.my_layout, container, false);
return view;
}
here my_layout should be the name of layout file,
now you can get view of it inside function onviewcreated()
public void onViewCreated(View view, Bundle savedInstanceState){
// here you can get your textview and set its value
}
thumbs up, if you find my answer correct
You can inflate your layout like this:
ViewGroup group = LayouInflate.from(context).inflate(R.layout.abc,null);
TextView tv = group.findViewById(R.id.xxx);
you have to inflate the layout through the onCreateView method and then return the View.
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.your_layout, container, false);
TextView yourTextView = (TextView)rootView.findViewById(R.id.yourTextViewId);
return rootView;
}
I have the problem with connect method of button in fragment.
It doesn't work ... my application is always closed when I click a button.
In normal activity it work correctly, but why not in fragments? How can I repare it?
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="green"
android:text="#string/green" />
and
View rootview;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu1_layout, container, false);
return rootview;
}
and my method green
public void green(View v){
//here everything is good
}
import android.view.View.OnClickListener; is marked "is never used"
You cannot do this since there is no context reference to a Fragment without the Activity. So therefore this is only possible in an Activity not a Fragment. Important thing to note here is that your Fragment must call getActivity() to find a reference to the context, since a fragment can be placed in any Activity fragment's themselves really have no context so referencing a Fragment's method in xml will not be possible in this respect.
Directly from Android :
public static final int onClick added in API 4
Name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).
Must be a string value, using '\;' to escape characters such as '\n' or '\uxxxx' for a unicode character.
This may also be a reference to a resource (in the form "#[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.
Constant Value: 16843375 (0x0101026f)
UPDATE
Just Use this then:
Button button = (Button) getView().findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Move your green(View v) method logic here instead of calling green(v)
}
});
UPDATE 2
your fragment code should be doing this before anything else:
public class menu1_fragment extends Fragment {
View rootview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu1_layout, container, false);
return rootview;
}
// Probably safer for you to use onViewCreated(View, Bundle)
#Override
public void onViewCreated(View view, Bundle savedInstanceState){
super.onViewCreated(view, savedInstanceState);
// use rootView or getView()
Button button = (Button) rootView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Move your green(View v) method logic here instead of calling green(v)
}
});
}
}
you cannot reference a UI element before the View is inflated, which was the cause of your error. Alternatively you can use onActivityCreated(Bundle savedInstanceState).
I am new in android and I have been created simple app which have 3 tabs that I build them with Fragment Activity .
in Activities we can use findViewById method . but Fragments don't have this method . how can I access to my views ?
You can call your getAcivity() method within your fragment class to call the context of your activity that is bounded in it and after you can call findViewById();
sample:
public void onViewCreated()
TextView txSample = (TextView)getView().findViewById(R.id.yourid);
TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_simple_lights, container, false);
tv = (TextView) rootView.findViewById(R.id.yourid);
return load;
}
You would better do all the view-related operations in onCreateView() and all the functional operations in onActivityCreated()