0

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!

Odino
  • 141
  • 1
  • 6
  • 17

2 Answers2

2

You are extending FragmentActivity (public class Extra extends FragmentActivity). FragmentActivity is an Activity and there is no onCreateView method for that. If you want to use FragmentActivity, I suggest you put your codes in the onCreate method.

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ImageView fb = (ImageView) findViewById(R.id.fb);
        ImageView tw = (ImageView) 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);
        }
      });
}

if you want Fragment then extend your class to Fragment (public class Extra extends Fragment). Your onCreateView method is fine. just add @Override on top of the method. And make sure you have Activity class that will hold the Fragment. For reference, try this link http://www.tutorialspoint.com/android/android_fragments.htm

Catalina
  • 1,954
  • 1
  • 17
  • 25
Genevieve
  • 450
  • 3
  • 9
  • sorry I had written that I used both extends Fragment that extends FragmentActivity. But the result does not change I added @Override but it does not work anyway – Odino Apr 20 '15 at 15:35
  • Kindly show other codes such as your Activity that holds this fragment and the xml that you've used for your Activity – Genevieve Apr 20 '15 at 15:39
  • @Override just instructs the compiler to error out if method is not actually overridden: https://docs.oracle.com/javase/tutorial/java/IandI/override.html – Finn K Apr 20 '15 at 15:52
  • How come whenever I attempt to write `Intent quizIntent = new Intent(XFragment.this, YActivity.class);` within the `onClick` method I get a "cannot resolve constructor" error? Is there a different way to write Intent within `Fragments`? – AndroidDevBro Mar 18 '18 at 20:45
0

Is your behavior that nothing happens when you click the images? As the "buttons" are defined as imageviews you need to turn on clickable (setClickable(true)).

Also your fragment should extend Fragment class, the FragmentActivity is the activity containing the fragment; see docs otherwise the correct lifecycle methods are not going to be invoked.

Finn K
  • 620
  • 3
  • 8
  • I had used extends Fragment (how edit the Question) with setClickable(true), there are no changes. actually I've never used it, in a normal activity images are clickable simply setting onClickListener() setClickable(true) it is not necessary – Odino Apr 20 '15 at 15:41
  • Please share the relevant code, you need to have a FragmentActivity (or derived), UI definition for said Activity that specifies how to include fragment (could be xml or code), then Fragment class and the fragment UI (above). – Finn K Apr 20 '15 at 15:50