Reputation: 45
I'm trying to create an App with fragments and in fragments I want to add buttons. Here is the code
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import com.example.lightcontrolsystem.R;
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTabContent();
}
private void setTabContent() {
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
mTabHost.addTab(
mTabHost.newTabSpec("Pattern list").setIndicator("Pattern List",
getResources().getDrawable(android.R.drawable.star_on)),
PatternListFragment.class, null);
}
}
Here is the fragment I want to add buutons:
public class PatternListFragment extends Fragment {
Button Theme1, Theme2, Theme3;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.patterns_list, container,false);
//Theme1 = (Button)findViewById(R.id.theme1);
// Theme1.setOnClickListener(buttonTheme1tOnClickListener);
return view;
}
}
I tried to add a buuton, But I can see some errors in findViewById(R.id.theme1)
`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/theme1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="theme1" />
</LinearLayout>
`
`<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="@+id/tabFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost >
`
How Can I add button handlers in Fragment ? is it possible ?
Thanks
Upvotes: 0
Views: 146
Reputation: 10274
In fragment you are inflating a view,so you have to get all the id with the refernce of that view.below is the code sample.
public class PatternListFragment extends Fragment implements OnClickListener {
Button Theme1, Theme2, Theme3;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.patterns_list, container,false);
Theme1 = (Button)view.findViewById(R.id.theme1);
Theme1.SetOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Theme1:
break;
// same way for other buttons
default:
break;
}
}
}
Upvotes: 1
Reputation: 133560
I can see some errors in findViewById(R.id.theme1)
Use
Theme1 = (Button)view.findViewById(R.id.theme1);
// use the view object to find views
http://developer.android.com/reference/android/view/View.html#findViewById(int)
or in onActivityCreated
Theme1 = (Button)getView().findViewById(R.id.theme1);
Also follow java naming conventions.
Upvotes: 0