I am new to Android and trying to learn on my own. But I am having a tough time with Fragments. I am creating a simple application to learn fragments. I think it may seem si
//sure run it i will also test it
//we make a class that extends with the fragment
public class Example_3_1 extends Fragment implements OnClickListener
{
View vi;
EditText t;
EditText t1;
Button bu;
// that are by defult function of fragment extend class
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,BundlesavedInstanceState)
{
vi=inflater.inflate(R.layout.example_3_1, container, false);// load the xml file
bu=(Button) vi.findViewById(R.id.button1);// get button id from example_3_1 xml file
bu.setOnClickListener(this); //on button appay click listner
t=(EditText) vi.findViewById(R.id.editText1);// id get from example_3_1 xml file
t1=(EditText) vi.findViewById(R.id.editText2);
return vi; // return the view object,that set the xml file example_3_1 xml file
}
@Override
public void onClick(View v)//on button click that called
{
switch(v.getId())// on run time get id what button os click and get id
{
case R.id.button1: // it mean if button1 click then this work
t.setText("UMTien"); //set text
t1.setText("programming");
break;
}
} }
Use your code
public class FragmentOne extends Fragment implements OnClickListener{
View view;
Fragment fragmentTwo;
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
Button buttonSayHi = (Button) view.findViewById(R.id.buttonSayHi);
buttonSayHi.setOnClickListener(this);
return view;
}
But I think is better handle the buttons in this way:
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonSayHi:
/** Do things you need to..
fragmentTwo = new FragmentTwo();
fragmentTransaction.replace(R.id.frameLayoutFragmentContainer, fragmentTwo);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
*/
break;
}
}
This works for me.
private OnClickListener mDisconnectListener;
mDisconnectListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
...
... onCreateView(...){
mButtonDisconnect = (Button) rootView.findViewById(R.id.button_disconnect);
mButtonDisconnect.setOnClickListener(mDisconnectListener);
...
}
You only have to get the view of activity that carry this fragment and this could only happen when your fragment is already created
override the onViewCreated()
method inside your fragment and enjoy its magic :) ..
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button button = (Button) view.findViewById(R.id.YOURBUTTONID);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//place your action here
}
});
Hope this could help you ;
Your fragment class should implement OnClickListener
public class SmartTvControllerFragment extends Fragment implements View.OnClickListener
Then get view, link button and set onClickListener like in example below
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.smart_tv_controller_fragment, container, false);
upButton = (Button) view.findViewById(R.id.smart_tv_controller_framgment_up_button);
upButton.setOnClickListener(this);
return view;
}
And then add onClickListener method and do what you want.
@Override
public void onClick(View v) {
//do what you want to do when button is clicked
switch (v.getId()) {
case R.id.textView_help:
switchFragment(HelpFragment.TAG);
break;
case R.id.textView_settings:
switchFragment(SettingsFragment.TAG);
break;
}
}
This is my example of code, but I hope you understood
While you are declaring onclick in XML then you must declair method and pass View v as parameter and make the method public...
Ex:
//in xml
android:onClick="onButtonClicked"
// in java file
public void onButtonClicked(View v)
{
//your code here
}