I just started to use the Fragment e the Swipe Views. I wanted to redo my project but I have a problem in setting the various interactions with buttons and images via the method onClickListener()
this is the xml code of the fragment/activity with the buttons(activity_extra.xml):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/rhinos" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical"
android:paddingBottom="20dip" >
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/facebook_icon"
android:contentDescription="@string/link_facebook" />
<ImageView
android:id="@+id/tw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/twitter_icon"
android:contentDescription="@string/link_twitter" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
this is the code of the class Extra (the class that should make working the buttons of extra_activity):
public class Extra extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// immagini extra
View view = inflater.inflate(R.layout.activity_extra, container, false);
ImageView fb = (ImageView) view.findViewById(R.id.fb);
ImageView tw = (ImageView) view.findViewById(R.id.tw);
fb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(
Intent.ACTION_VIEW,
Uri.parse("https://www.facebook.com/"));
startActivity(intent);
}
});
tw.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("https://twitter.com/"));
startActivity(intent);
}
});
return view;
/*
* fb.setOnClickListener(this);
* tw.setOnClickListener(this);
*/
}
/*
* @Override public void onClick(View v) { Intent intent = null; switch
* (view.getId()) {
*
* case R.id.fb: intent = new Intent( Intent.ACTION_VIEW,
* Uri.parse("https://www.facebook.com/"));
* case R.id.tw: intent = new Intent(Intent.ACTION_VIEW,
* Uri.parse("https://twitter.com/"));
* }
* startActivity(intent);
* }
*/
}
the part with the * regards the class if built with implements:
public class Extra extends Fragment implements OnClickListener{
I think that something is missing to make it work, but what? reading various guides and tutorials really do not understand what I have to further implement.
add the MainActivity:
public class MainActivity extends FragmentActivity {
Button b1,b2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
Fragment fragment = new Extra();
//Extra fragment = new Extra();
public void onClick(View v) {
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(android.R.id.content, fragment);
transaction.commitAllowingStateLoss();
}
});
b2.setOnClickListener(new OnClickListener() {
Fragment fragment = new FrammentoSinistra();
//FrammentoSinistra fragment = new FrammentoSinistra();
public void onClick(View v) {
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(android.R.id.content, fragment);
transaction.commitAllowingStateLoss();
}
});
}
}
Simply load a frame according to the button that is pushed. In addition to its xml file I have no other code ... So what's missing?
as I wrote above:
That says something my application: ok the fragment is loaded. Now the fragment Has to do what he says the extra.java class!