I have some button on my HomeFragment i want to to open a Fragment on button click from a fragment . Something like Navigation. I am trying with bellow code but did not worked. Please help ! I am very new to android and trying to learn new things.
Here is My code
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class HomeFragment extends Fragment {
public HomeFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton);
Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton);
Button schemeBtn = (Button) rootView.findViewById(R.id.schemeButton);
Button loanBtn = (Button) rootView.findViewById(R.id.loanButton);
Button serviceBtn = (Button) rootView.findViewById(R.id.serviceButton);
Button devBtn = (Button) rootView.findViewById(R.id.devButton);
aboutBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Launching new Activity on selecting single List Item
Intent i = new Intent(getActivity(), AboutFragment.class);
startActivity(i);
}
});
phonebookBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Launching new Activity on selecting single List Item
Intent j = new Intent(getActivity(), PhoneBookFragment.class);
startActivity(j);
}
});
schemeBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Launching new Activity on selecting single List Item
Intent k = new Intent(getActivity(), HomeFragment.class);
startActivity(k);
}
});
loanBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Launching new Activity on selecting single List Item
Intent l = new Intent(getActivity(), RemittanceFragment.class);
startActivity(l);
}
});
serviceBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Launching new Activity on selecting single List Item
Intent m = new Intent(getActivity(), ServiceFragment.class);
startActivity(m);
}
});
devBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
// Launching new Activity on selecting single List Item
Intent n = new Intent(getActivity(), AboutDeveloper.class);
startActivity(n);
}
});
return rootView;
}
}
I am looking for something like bellow code but don't know how to make the code works with my code.
Button aboutBtn = (Button) v.findViewById(R.id.aboutButton);
aboutBtn.setOnClickListener(this);
Button homeBtn = (Button) v.findViewById(R.id.homeButton);
homeButton.setOnClickListener(this);
Button serviceBtn = (Button) v.findViewById(R.id.serviceButton);
serviceBtn.setOnClickListener(this);
@Override
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case aboutButton:
fragment = new AboutFragment();
break;
case homeBtn:
fragment = new PhonebookFragment();
break;
case serviceBtn:
fragment = new ServiceFragment();
break;
default:
fragment = new HomeFragment();
break;
}
}