Home > Mobile >  Create Button In A Fragment
Create Button In A Fragment

Time:10-25

Hello Im new to android i want to ask how do i do intent inside the fragment layout

any answers i would really appreciate

here is the code :

for HomeFragment.java

package com.example.splashscreen;

import android.annotation.SuppressLint;

import android.content.Intent;
import android.net.Uri;
import android.net.wifi.hotspot2.pps.HomeSp;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.denzcoskun.imageslider.ImageSlider;
import com.denzcoskun.imageslider.constants.ScaleTypes;
import com.denzcoskun.imageslider.models.SlideModel;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends Fragment implements View.OnClickListener {
    // Initiate
    Button button;
    View rootView;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment HomeFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static HomeFragment newInstance(String param1, String param2) {
        HomeFragment fragment = new HomeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @SuppressLint("WrongViewCast")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /**Button btnMoveActivity = (Button) rootView.findViewById(R.id.btn1);
        btnMoveActivity.setOnClickListener((View.OnClickListener) this);

         ImageSlider imageSlider = getActivity().findViewById(R.id.image_slider);

        List<SlideModel> slideModel=new ArrayList<>();
        slideModel.add(new SlideModel(R.drawable.kucing2, ScaleTypes.FIT));
        slideModel.add(new SlideModel(R.drawable.kucing1,ScaleTypes.FIT));
        slideModel.add(new SlideModel(R.drawable.kucing2,ScaleTypes.FIT));

        imageSlider.setImageList(slideModel,ScaleTypes.FIT);
**/

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_home, container, false);
        return inflater.inflate(R.layout.fragment_home, container, false);
    }



    public void onViewCreated(View view,Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        button = (Button) rootView.findViewById(R.id.btn1);

        button.setOnClickListener(view1 -> {
            switch (view1.getId()){
                case R.id.btn1:
                    Intent moveIntent = new Intent(MainActivity.this,MoveActivity.class); // my button isn't working here
                    startActivity(moveIntent);
                    break;
                }
            });
}

    @Override
    public void onClick(View view) {

    }
}

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.INTERNET"/>

    <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/Theme.Splashscreen">
        <activity
            android:name=".Splashscreen"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

my button cant do intent and i've try several methods that i cant understand i want to add image slider later but I prefer the button first

thank you for your valuable answer

CodePudding user response:

I'm not sure if this would work in your case, but if you want to access the fragment's host activity and use it for that Intent you can try this

Activity myParentActivity = (MainActivity) getActivity();
Intent moveIntent = new Intent(myParentActivity, MoveActivity.class);

or this

Activity myParentActivity = (MainActivity) requireActivity();
Intent moveIntent = new Intent(myParentActivity, MoveActivity.class);

put this in your AndroidManifest.xml

<activity android:name=".MoveActivity"/>
  • Related