How to get & use Widget Element's Action in fragment android - android

In need of a good Ui design for my app with tab layout, i searched in google and i found one UI which is using View Pager and Action bar Tabs.
Link is:
http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/
i downloaded the template & imported to eclipse.i to tried use that template, don't know where to add my activity coding.
i uploaded one of the fragment below, and i tried to get action of a button. but "it shows create a method finViewById".
public class MoviesFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button bu=(Button) findViewById(R.id.button1);
return rootView;
}
i don't know much about fragment. I searched in google but i am not able get solution related to this. anyone pls can help me how and where to add my activity coding using above UI Template. Thanks in advance.

try this,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
Button bu=(Button) rootView.findViewById(R.id.button1);
return rootView;
}

use this
Button bu=(Button)rootView.findViewById(R.id.button1);

Related

java.lang.RuntimeException: Unable to bind views for fragment-different layout sizes

I'm trying to create my app support for different layouts (eg : layout-large-tvdpi).inside layout-large-tvdpi folder i have inserted tow layout (one layout for activity other for fragment).
when i run my app on Tablet activity layout showing fine.but when i move to fragment, app crashed and show me errorjava.lang.RuntimeException: Unable to bind views.it's show error on ButterKnife.bind(this,v)
This is my Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
ButterKnife.bind(this);
}
This is My Fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v;
v = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this,v);
return v;
}
Log Cat
Fragment layout
Activity Layout
Check if your #InjectViews has correct type. I've used ImageView instead of LinearLayout. That might be your problem too.

Android View Pager set Page within Fragment

I want to build a ViewPager with 2 Pages. On the first page you can choose a category. When a List item is selected the screen swipes to the second page and you can choose some subcategories.
I implemented the clicklistener and so on. But I have no idea how to set the current page from within a fragment. Is it even possible? Or do you have another solution for me? Hope you understand what I want to achieve.
Ok, found a solution for my own. Thanks to Sayem for the hint.
I passed the ID of the ViewPager via bundle to my first fragment. in my fragment i could use then this simple code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
viewPagerId = getArguments().getInt("viewPagerId");
View rootView = (View) inflater.inflate(R.layout.fragment_cat, container, false);
populateListView(rootView);
viewPager = (ViewPager) getActivity().findViewById(viewPagerId);
return rootView;
}
and to switch pages then simply this:
viewPager.setCurrentItem(position);
Thanks for the help
use ((ActivityName)getActivity()).getViewPager().setCurrentItem(position);
note: getViewPager() should return the viewpager instance
try:
mPager.setCurrentItem(position);

How to set the right fragment to replace.

I’m doing a navigation drawer in my android app. On my onNavigationDrawerItemSelected function i try on item select to change the current fragment into a new one. The problem is that when I change the current fragment its take the initial status of it. maybe because i'm doing this way
fragmentManager.beginTransaction()
.replace(R.id.cont, new SessionsFragement())
.commit();
I declare a new instance of my fragment and i think it’s normal, its display me the initial status of the thing. On my fragment i've try to set a textView to see if i will say it when i call the fragement from tha navigation drawer but it's not the case.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_spot, container, false);
tata = (TextView) v.findViewById(R.id.tata);
tata.setText("karim");
return inflater.inflate(R.layout.fragment_spot, container, false);
}
So please can anyone help me to fix this.
In the snippet you posted you are inflating again the view. You should return the one you modified
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_spot, container, false);
tata = (TextView) v.findViewById(R.id.tata);
tata.setText("karim");
return v;
}

how to correctly load webview based on sample of creating swipe view

I am new and learning to create a webview by selecting a tab, based on google's sample at http://developer.android.com/training/implementing-navigation/lateral.html.
I created a class for webview fragment:
public static class WebViewFragment<rootView> extends Fragment {
WebView rootView;
public WebView onCreate(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater.inflate(R.layout.fragment_webview_1, container,false);
rootView.getSettings().setJavaScriptEnabled(true);
rootView.loadUrl("http://www.google.com");
return rootView;
}
}
However, it runs without error but the webview interface never shows up. Appreciate if you can provide any clues.
Above method is actually not called by Android. It is a simple onCreate overloaded method.
Change that method to:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// would be safe to check class
rootView = (WebView)inflater.inflate(R.layout.fragment_webview_1, container,false);
rootView.getSettings().setJavaScriptEnabled(true);
rootView.loadUrl("http://www.google.com");
return rootView;
}

Using Fragments in ActionBar Tabs to have widgets

I made a program that uses FragmentTabs for implementing ActionBar Tabs.
I was wondering how it is possible to add a widget to a inflated Fragments in Tabs. It's more complicated than normal because the fragment is returning only an onCreateView to the TabListener, so how would I add in functioning widgets? I don't need specific code, just the logic please!
Here is a sample Fragment being inflated when a Tab is clicked (inside of which I need widgets):
public class IdFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.id_layout, container, false);
}
}

Categories

Resources