How use a button inside a fragment? - android

I'm trying to use 4 fragments
[F1]
[F2][F3][F4]
F1 will have 3 buttons to change between F2,F3 and F4 but i don´t know how to use the buttons properties like in a normal Activity
That's what i have in my F1 code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_01, container, false);
}

Check this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_01, container, false);
// here you have the reference of your button
Button yourButton = (Button)view.findViewById(R.id.your_button_id);
return view;
}

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_01, container, false);
Button firstButton = (Button)view.findViewById(R.id.firstbutton);
Button seconButton = (Button)view.findViewById(R.id.seconbutton);
Button thirdButton = (Button)view.findViewById(R.id.thirdbutton);
return view;
}

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
Button button = (Button) rootView.findViewById(R.id.buttonSayHi);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onButtonClicked(v);
}
});
return rootView;
}
public void onButtonClicked(View view)
{
//do your stuff here..
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frameLayoutFragmentContainer, new FragmentTwo(), "NewFragmentTag");
ft.commit();
ft.addToBackStack(null);
}
}

It's a simple way to reference a button or any widget in a fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);
btnOne = view.findViewById(R.id.btnOne);
return view;

You should get the view first, and then get the button from the view
like this:
View rootView = inflater.inflate(R.layout.fragment_01, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
You should do some tutoriasl first, its not that hard.

Related

Setting onClicklistener() doesn't work in a fragment

Button onClick doesn't work in a fragment. What happens?
The id and onClickListener function has the right syntax. I don't know what is happening.
public class LoginFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Button btn;
View vista = inflater.inflate(R.layout.fragment_login, container, false);
btn = (Button)vista.findViewById(R.id.btn_login);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View vista) {
//cargarWebService();
//name.setText("");
//email.setText("");
Toast.makeText(getContext(), "Se ah registrado exitosamente", Toast.LENGTH_SHORT).show();
}
});
return inflater.inflate(R.layout.fragment_login, container, false);
}
}
change this
return inflater.inflate(R.layout.fragment_login, container, false);
to
return vista;
let me know if this work or not
You don’t need to inflate the view again when returning it, simply inflate it once at the beginner of the method and then return the view at the end of the method.
View view = inflater.inflate(R.layout.fragment_login, container, false);
return view;

Detecting the right View

I need a little bit help, defining my in event_filter.xml declared Views.
It is an child of an AbstractDetailListFragment and implements a DatePicker and a Spinner item.
But I dont know how to acces them in an AbstractDetailFragment.
By defining
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View theView = super.onCreateView(inflater, container, savedInstanceState);
ImageButton searchEnterButton = (ImageButton)getNavigationFragment().getNavigationBar().getRightButton();
View view = getView();
final DatePicker datePicker = (DatePicker) container.findViewById(R.id.datePickerEvent);
Spinner inSpinner = (Spinner) container.findViewById(R.id.category_list);
spinner = inSpinner;
downloadCategories();
the variables view, datePicker, inSpinner always are null!
Any Ideas?
thanks a lot.
That's a thing about fragments. I've been looking for the same error for days, until I read a little bit about fragments (i was lazy and i recommend you not to follow my footsteps ). The way i solved it was by overriding onViewCreated and "catching" views there:
public class MyFragment extends Fragment {
View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_fragment, container,
false);
return rootView;
}
//and you use rootView to call findViewById
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button myButton = (Button) rootView.findViewById(R.id.mybutton);
sendFeedback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
}
}

Inflating a layout in a fragment

So I am finally learning Fragments after learning Activities. I am looking at a video tutorial. All of a sudden he changed the way a Fragment inflates a layout. Why does he have two different ways? The first way in FragmentA is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
Then the other way in FragmentB is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
if (savedInstanceState == null)
{
}
else {
savedInstanceState.getString("text");
TextView myText = (TextView) view.findViewById(R.id.textView);
myText.setText(data);
}
return view;
}
So why did it change from return inflater.inflate to View view = inflater.inflate
Sorry for dumb question, but I can't find this answer on Google.
It's basically the same,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
has to return a View.
And
inflater.inflate(R.layout.fragment_b, container, false)
returns a View as well.
In the second example he assigns the return value of inflater.inflate() to a View object, as he needs to loads some data and set it on this View object... then he returns it.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.lstindicators, container,
false);
And return your view thats all! :D
jejeje... and if you want to change your current fragment for another chechk this:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();

Android ImageButton worked well in Activity. It doesn't work in Fragment

I'm trying to convert a layout so that it will include fragments.
One of the views is an ImageButton that has a listener.
The code worked fine as an Activity but makes trouble as a Fragment...
First problem was that I couldn't use findViewById,
but I was able to find the answer for that here and fixed it with getView().
I can't find an answer to the second problem...
after I declared:
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
I want to set an on click listener:
camBt.setOnClickListener(listener);
But it keeps acting like the ImageButton (camBt) doesn't exist...
I don't even know what's the problem... it worked fine in the Activity...
Here's the full code.
Many thanks!
public class CameraFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
return V;
}
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
}
Move
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
inside onCreateView().
Like
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.camera_fragment, container, false);
// Here it is
ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
camBt.setOnClickListener(listener);
return v;
}
And one more thing you need to understand that variable name should starts with small letter. So you should use View v instead of View V.
You should add
ImageButton camBt = (ImageButton)getView().findViewById(R.id.button1);
camBt.setOnClickListener(listener);
inside onCreateView() like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
ImageButton camBt = (ImageButton)v.findViewById(R.id.button1);
camBt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
return V;
}
public class CameraFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.camera_fragment, container, false);
ImageButton camBt = (ImageButton)V.findViewById(R.id.button1);
ImageButton.OnClickListener listener = new ImageButton.OnClickListener()
{
#Override
public void onClick(View arg0)
{
Camera.open().getParameters();
}
};
camBt.setOnClickListener(listener);
return V;
}
}

How to get an id of a View inside a Fragment

Hi I am trying to get an id of some View in a Fragment from a string, however it doesn't seem to work... Is there a special case for fragments?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View singleplayerView = inflater.inflate(R.layout.singleplayer_tab, container, false);
setupMap();
return singleplayerView;
}
private void setupRank() {
TextView view = (TextView) getByStringId("rank0");
view.setText("hello");
}
private final View getByStringId(final String id) {
return findViewById(getResources().getIdentifier(id, "id", getActivi
ty().getPackageName()));
}
So I am trying to set the textView field rank0 in the layout that is used for my fragment... but right now it gets a NUllPointer because it couldnt find the id
Thank you
you can define your Layout In your onCreateView() inside Fragments like this:
View V = new View(getActivity());
V = inflater.inflate(R.layout.fragment_layout,container, false);
Then
TextView SomeField = (TextView)V.findViewById(R.id.TxtSomeField);
Inside fragment, add getActivity before findViewById:
getActivity().findViewById(...);
Try this,
String id;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extra = getArguments();
id = extra.getString("id", "");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_calendar_details, container, false);
Button b = (Button) view.findViewById(id);
return view;
}

Categories

Resources