How to start an activity from an image button which is there in a fragment of a navigation drawer

946 views Asked by At

I'm have implemented a navigation drawer with fragments, now I'm trying to open an activity from those fragments. There are too many fragments I'm posting one of them over here i.e About us, I just wanna start an activity or a fragment (whichever is more simple) on click of the image button.

Aboutus.java

package hind.jai.com.jaihind;

import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

/**
 * Created by sukhvir on 17/04/2015.
 */
public class about extends android.support.v4.app.Fragment {
    ImageButton Img;

    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {


        return inflater.inflate(
                R.layout.about, container, false);


    }

    @Override
    public void onActivityCreated( Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //get the button view
        Img = (ImageButton) getView().findViewById(R.id.imageButton);
        //set a onclick listener for when the button gets clicked
        Img.setOnClickListener(new View.OnClickListener() {
            //Start new list activity
            public void onClick(View v) {
                Intent mainIntent = new Intent(getActivity(), abcd.class);

            }
        });


    }

}

About.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/about"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton"
        android:background="@drawable/image2"
        android:onClick="Onclick"
        android:layout_gravity="center_horizontal" />


</LinearLayout>

And as I mentioned I trying to start a new activity which is "abcd".

abcd.java

package hind.jai.com.jaihind;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by Rishik on 14-06-2015.
 */
public class abcd extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.abcd);
    }
}

abcd.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView9"
        android:layout_gravity="center_vertical" />
</LinearLayout>

When I run the project, it works without any crashes but when I click on the image, it doesn't start's a new activity.

Thanks a lot in advance.

1

There are 1 answers

2
Christian On BEST ANSWER

In your onClick you are just missing to start the activity by the Intent

Intent mainIntent = new Intent(getActivity(), abcd.class);
startActivity(mainIntent);  // <-- this is missing