public class FileFragment extends Fragment implements View.OnClickListener {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_files, container, false);
// Define your ui elements
Button btn_add_company = (Button) view.findViewById(R.id.btn_add_company);
// set listener to (this)
// and use it from onClick method
// using by it's id:
btn_add_company.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_add_company:
// Do something i use a intent to start activity from a fragment
Intent intent = new Intent(getActivity(), AddCompanyActivity.class);
((MainActivity) getActivity()).startActivity(intent); // You can delete MainActivity it should work!
break;
}
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("LOG", "MainActivity On start!");
UTab tab = new UTab(this, R.id.viewPager, R.id.tabLayout);
tab.add(SalesFragment.class, "فروش", R.drawable.ic_add_shopping_cart_black_24dp);
tab.add(FileFragment.class, "پرونده", R.drawable.ic_insert_drive_file_black_24dp);
}
}