Ren Austria
Ren Austria

Reputation: 23

How to go to Another activity when button clicked (button in a tab)

I created a Tab Layout with 3 sections. The first one is for checking the grammar and the second one is for lessons, the second tab is consist of 6 buttons for 6 lessons and I wanted the user to click the button and go to another activity or class. But I didn't get the output that I wanted. I tried different ways to open the activity from a fragment class but nothing happened.

What should I do in order to go to another class with a tab layout?

Here's my code

secondFragment_grammarTips

public class secondFragment_grammarTips extends Fragment {


public secondFragment_grammarTips() {
    // Required empty public constructor
}


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

    View view = inflater.inflate(R.layout.fragment_second_fragment_grammar_tips, container, false);
    Button lesson1 = (Button) view.findViewById(R.id.button1);
    lesson1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent less1 = new Intent(getActivity(), Lesson1_GramarTips.class);
            less1.putExtra("some", "some data");
            startActivity(less1);
        }
    });
    // Inflate the layout for this fragment
    //return inflater.inflate(R.layout.fragment_second_fragment_grammar_tips, container, false);
    return view;
}

}

Lesson1_GrammarTips

public class Lesson1_GramarTips extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lesson1__gramar_tips);
    Toolbar  toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    Bundle bundle = getIntent().getExtras();
    if(bundle != null){
        if(bundle.getString("some") != null){
            Toast.makeText(getApplicationContext(), "data: " + bundle.getString("some"), Toast.LENGTH_SHORT).show();
        }
    }

}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="renelyn.austria.capstonetwoproject">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Lesson1_GrammarTips"></activity>
</application>

</manifest>

fragment_second_fragment_grammar_tips.xml

<FrameLayout 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"
tools:context="layout.secondFragment_grammarTips"
>

<!-- TODO: Update blank fragment layout -->
<!--<TextView-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="match_parent"-->
    <!--android:text="@string/hello_blank_fragment" />-->

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <Button
            android:id="@+id/button1"
            android:layout_width="385dp"
            android:layout_height="110dp"
            android:text="Lesson 1"
            />

        <Button
            android:id="@+id/button2"
            android:layout_width="385dp"
            android:layout_height="110dp"
            android:text="Lesson 2"
            />

        <Button
            android:id="@+id/button3"
            android:layout_width="385dp"
            android:layout_height="110dp"
            android:text="Lesson 3"
            />

        <Button
            android:id="@+id/button4"
            android:layout_width="385dp"
            android:layout_height="110dp"
            android:text="Lesson 4"
            />

        <Button
            android:id="@+id/button5"
            android:layout_width="385dp"
            android:layout_height="110dp"
            android:text="Lesson 5"
            />

        <Button
            android:id="@+id/button6"
            android:layout_width="385dp"
            android:layout_height="110dp"
            android:text="Lesson 6"
            />


    </LinearLayout>


</ScrollView>

</FrameLayout>

Upvotes: 0

Views: 75

Answers (1)

Veeresh P
Veeresh P

Reputation: 446

Whenever you create a fragment ,it(fragment) should have activity hosting it. I didn't find any activity other than "Lesson1_GrammarTips" in Manifest file. And Lesson1_GrammarTips(activity) is not hosting any fragment. Go through this.

Upvotes: 1

Related Questions