how to access views in tab activity?

108 Views Asked by At

I've created an android application to my final project it has a tab activity with three tabs, within each tab there are a lot of components. For example, in the second tab I have a WebView and a button and, when I click the button, the webview must load some data.
How can I handle the code?
In the tab1activity.java is not possible because there is no findViewById() also in mainactivity.java that's make my app crashed what can I do?

2

There are 2 best solutions below

0
VIJAY RAVAL On BEST ANSWER
public class animal_tab1 extends Fragment {

    Button AddDiseases, ViewData;
    EditText name, laxan, upay, news;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Get the view from fragmenttab1.xml
        View view = inflater.inflate(R.layout.animal_tab1, container, false);


        Button AddDiseases = (Button) view.findViewById(R.id.btnAdd);
        Button ViewData = (Button) view.findViewById(R.id.btnView);

    name = (EditText) view.findViewById(R.id.ed_name);
    laxan = (EditText)view.findViewById(R.id.ed_laxan);
    upay = (EditText)view.findViewById(R.id.ed_upay);
    news = (EditText)view.findViewById(R.id.ed_News);
   }
}
0
Zohaib Hassan On

You must be having your button and webview in your tabTwo layout. And also you must be having a class for your TabTwoFragment as well. Put this code in your TabTwoFragment class to access its views:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.tab_two_layout, parent, false);
WebView webView = (WebView) rootView.findViewById(R.id.your_web_view);
Button button = (Button) rootView.findViewById(R.id.your_button);

// Set Your clicklistener on your button here.

return rootView;
}