5 Replies - 961 Views - Last Post: 28 January 2016 - 07:14 PM

#1 john_1993   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 148
  • Joined: 16-March 15

How to create fragments in Android studios from existing activities

Posted 26 January 2016 - 05:03 AM

I have searched online and still cant manage to find any help of how to create fragments from existing activities. for example I have a dashboard activity and a menu activity which I would like to create fragments with so that it displays both of the pages on tablets as one screen.

I want to make the menu activity a fragment so then I will have menu fragment then you I can load menu other activities from a fragment container. Could you please how I can carryout this task?.



Menu Actvity. Java

    public class MenuActivity extends ActionBarActivity {
    
    
        private Toolbar toolbar;
    
        ImageButton museummenubtn;
        ImageButton theatremenubtn;
        ImageButton fooddrinkmenubtn;
        ImageButton leisuremenubtn;
        ImageButton shoppingmenubtn;
        ImageButton historicalmenubtn;
        ImageButton parkmenubtn;
        ImageButton familyfunmenubtn;
        ImageButton travelinformenubtn;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_menu);
    
            ImageButton museummenubtn = (ImageButton) findViewById(R.id.museum_icon);
            museummenubtn.setonclickListener(new View.onclickListener() {
                public void onclick(View view) {
                    Intent myIntent = new Intent(view.getContext(), MuseumActivity.class);
                    startActivityForResult(myIntent, 0);
                    finish();
                }
    
            });
    
            ImageButton theatremenubtn = (ImageButton) findViewById(R.id.theatre_icon);
            theatremenubtn.setonclickListener(new View.onclickListener() {
                public void onclick(View view) {
                    Intent myIntent = new Intent(view.getContext(), TheatreActivity.class);
                    startActivityForResult(myIntent, 0);
                    finish();
                }
    
            });
    
            ImageButton fooddrinkmenubtn = (ImageButton) findViewById(R.id.fooddrink_icon);
            fooddrinkmenubtn.setonclickListener(new View.onclickListener() {
                public void onclick(View view) {
                    Intent myIntent = new Intent(view.getContext(), FoodAndDrinksActivity.class);
                    startActivityForResult(myIntent, 0);
                    finish();
                }
    
            });
    
            ImageButton leisuremenubtn = (ImageButton) findViewById(R.id.leisure_icon);
            leisuremenubtn.setonclickListener(new View.onclickListener() {
                public void onclick(View view) {
                    Intent myIntent = new Intent(view.getContext(), LeisureActivity.class);
                    startActivityForResult(myIntent, 0);
                    finish();
                }
    
            });
    
            ImageButton shoppingmenubtn = (ImageButton) findViewById(R.id.shopping_icon);
            shoppingmenubtn.setonclickListener(new View.onclickListener() {
                public void onclick(View view) {
                    Intent myIntent = new Intent(view.getContext(), ShoppingActivity.class);
                    startActivityForResult(myIntent, 0);
                    finish();
                }
    
            });
    
            ImageButton historicalmenubtn = (ImageButton) findViewById(R.id.histroy_icon);
            historicalmenubtn.setonclickListener(new View.onclickListener() {
                public void onclick(View view) {
                    Intent myIntent = new Intent(view.getContext(), HistoricalActivity.class);
                    startActivityForResult(myIntent, 0);
                    finish();
                }
    
            });
    
            ImageButton parkmenubtn = (ImageButton) findViewById(R.id.park_icon);
            parkmenubtn.setonclickListener(new View.onclickListener() {
                public void onclick(View view) {
                    Intent myIntent = new Intent(view.getContext(), ParksActivity.class);
                    startActivityForResult(myIntent, 0);
                    finish();
                }
    
            });
    
            ImageButton familyfunmenubtn = (ImageButton) findViewById(R.id.familyfun_icon);
            familyfunmenubtn.setonclickListener(new View.onclickListener() {
                public void onclick(View view) {
                    Intent myIntent = new Intent(view.getContext(), FamilyFunActivity.class);
                    startActivityForResult(myIntent, 0);
                    finish();
                }
    
            });
    
            ImageButton travelinformenubtn = (ImageButton) findViewById(R.id.travelinfor_icon);
            travelinformenubtn.setonclickListener(new View.onclickListener() {
                public void onclick(View view) {
                    Intent myIntent = new Intent(view.getContext(), TravelInformationActivity.class);
                    startActivityForResult(myIntent, 0);
                    finish();
                }
    
            });
    
    
            toolbar = (Toolbar) findViewById(R.id.tool_bar);
            setSupportActionBar(toolbar);
    
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            int id = item.getItemId();
    
            if (id == R.id.action_user) {
                Intent intent= new Intent(this,DashboardActivity.class);
                startActivity(intent);
                return true;
            }
    
            int id1 = item.getItemId();
    
            if (id1 == R.id.action_setting) {
                Intent intent= new Intent(this,DashboardActivity.class);// Settings Class once it is created.
                startActivity(intent);
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    }


activitymenu.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"
    
        tools:context=".MainActivity">
    
        <include
            android:id="@+id/tool_bar"
            layout="@layout/tool_bar"
            ></include>
    
    
        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:id="@+id/linearLayout"
            android:weightSum="1"
            android:background="#ffffff"
            android:layout_below="@+id/tool_bar">
    
            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/museum_icon"
                android:background="#ffffff"
                android:src="@drawable/museumicon"
                android:scaleType="fitXY" />
    
            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/theatre_icon"
                android:background="#00000000"
                android:src="@drawable/theatreicon"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/museum_icon"
                android:layout_toEndOf="@+id/museum_icon"
                android:scaleType="fitXY" />
    
            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/fooddrink_icon"
                android:src="@drawable/foodicon"
                android:background="#ffffff"
                android:layout_gravity="center_vertical"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/theatre_icon"
                android:layout_toEndOf="@+id/theatre_icon"
                android:scaleType="fitXY" />
    
        </RelativeLayout>
    
        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:layout_below="@+id/linearLayout"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:id="@+id/linearLayout2"
            android:background="#ffffff">
    
            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/leisure_icon"
                android:background="#ffffff"
                android:scaleType="fitXY"
                android:src="@drawable/leisureicon" />
    
            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/shopping_icon"
                android:background="#00000000"
                android:scaleType="fitXY"
                android:src="@drawable/shoppingicon"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/leisure_icon"
                android:layout_toEndOf="@+id/leisure_icon" />
    
            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/histroy_icon"
                android:src="@drawable/historicalicon"
                android:scaleType="fitXY"
                android:background="#ffffff"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/shopping_icon"
                android:layout_toEndOf="@+id/shopping_icon" />
        </RelativeLayout>
        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            android:id="@+id/linearLayout3"
            android:layout_below="@+id/linearLayout2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="#ffffff"
            android:layout_alignParentBottom="true">
    
            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/park_icon"
                android:background="#ffffff"
                android:scaleType="fitXY"
                android:src="@drawable/parkicon" />
    
            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/familyfun_icon"
                android:background="#00000000"
                android:scaleType="fitXY"
                android:src="@drawable/familyfunicon"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/park_icon"
                android:layout_toEndOf="@+id/park_icon" />
    
            <ImageButton
                android:layout_width="125dp"
                android:layout_height="170dp"
                android:id="@+id/travelinfor_icon"
                android:src="@drawable/travelicon"
                android:scaleType="fitXY"
                android:background="#ffffff"
                android:layout_alignParentTop="true"
                android:layout_toRightOf="@+id/familyfun_icon"
                android:layout_toEndOf="@+id/familyfun_icon" />
        </RelativeLayout>
    
    
    </RelativeLayout>


MuseumActvity.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"
    
    tools:context=".MainActivity">
    
    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        ></include>
    
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="260dp"
        android:layout_below="@+id/tool_bar"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:weightSum="1"
        android:id="@+id/linearLayout4">
    
        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/musehead"
            android:scaleType="fitXY"
            android:src="@drawable/museumhead"
            android:background="#ffffff" />
    </LinearLayout>
    
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="260dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/linearLayout4" >
    
        <ImageButton
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageButton"
            android:scaleType="fitXY"
            android:src="@drawable/gallerieshead"
            android:background="#ffffff" />
    </LinearLayout>
    
    </RelativeLayout>


I want to create a fragment from these two activities, so that it fits tablet screen size.

Is This A Good Question/Topic? 0
  • +

Replies To: How to create fragments in Android studios from existing activities

#2 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

Re: How to create fragments in Android studios from existing activities

Posted 26 January 2016 - 03:57 PM

you only need one activity. then you can have as many fragments as you want. and if its a tablet, you can check for it and display two fragments.

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_page);

        if(findViewById(R.id.startpagefragment) != null){
            if(savedInstanceState != null) {

                return;
            }
            StartScreenFragment firstFragment = new StartScreenFragment();

            firstFragment.setArguments(getIntent().getExtras());

            getSupportFragmentManager().beginTransaction().add(R.id.startpagefragment, firstFragment).commit();

        }

    }

this is the actvity_start_page.xml
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/startpagefragment"/>



all you need. then you create a fragment class.


here you create your layout like you do with the activities and inflate it. if you want to change fragment you can do it from this one and add it to the backstack
public class StartScreenFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.start_menu, container, false);
        if(view.findViewById(R.id.start_menu) != null){

            return view;
        }
        return view;
    }
}

Was This Post Helpful? 0
  • +
  • -

#3 john_1993   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 148
  • Joined: 16-March 15

Re: How to create fragments in Android studios from existing activities

Posted 27 January 2016 - 05:39 AM

So do I need just one activity called menu activity and then 9 different fragments for the different cateogries. for example museum, theatre category. Could you give me a tutorial or any advice of how I can create this. ATM I have 10 activities with all the categories and menu activity. I also have 1 fragment called menu fragment. Please advise?.
Was This Post Helpful? 0
  • +
  • -

#4 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

Re: How to create fragments in Android studios from existing activities

Posted 28 January 2016 - 09:47 AM

i did show you have to create one fragment, two should not be a issue after that
Was This Post Helpful? 0
  • +
  • -

#5 john_1993   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 148
  • Joined: 16-March 15

Re: How to create fragments in Android studios from existing activities

Posted 28 January 2016 - 02:37 PM

Ben I really appreciate your help, I am aware of how to create fragments. My issue was that I have menu page with image buttons and then category pages with image buttons as well. I have used fragments, however, I personally feel that fragments are not suitable for this.

I have instead created layout resource folders in order to fit the various different screen sizes. I have seen that fragments work more effective with listview and detail view.

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#6 ben255   User is offline

  • D.I.C Addict

Reputation: 39
  • View blog
  • Posts: 510
  • Joined: 09-September 13

Re: How to create fragments in Android studios from existing activities

Posted 28 January 2016 - 07:14 PM

okay, but i rly dont see the issue in using fragments because of a few image buttons. i use fragments for everything, they are easy to set up and reuse.
But if youre not communicating between those two activites its okay. if you have to communicate between the two activites then fragments are much better i guess
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1