Skip to content

Commit 60c4bc0

Browse files
authored
Merge pull request #6 from gitzain/feature/fragmentactivity
Created an activity for fragments
2 parents 575895f + b227802 commit 60c4bc0

File tree

10 files changed

+204
-24
lines changed

10 files changed

+204
-24
lines changed

app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ dependencies {
4343
compile 'com.flaviofaria:kenburnsview:1.0.7'
4444
compile 'com.jpardogo.materialtabstrip:library:1.1.0'
4545
compile 'com.github.bumptech.glide:glide:3.7.0'
46+
compile 'com.android.support:support-v4:25.3.1'
4647
testCompile 'junit:junit:4.12'
4748
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
4849
}

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,16 @@
2626
</intent-filter>
2727
</activity>
2828
<activity android:name=".activities.AboutActivity" />
29-
3029
<activity android:name=".activities.SettingsActivity" />
31-
3230
<activity
3331
android:name=".activities.CardsActivity"
3432
android:label="@string/title_activity_cards" />
35-
3633
<activity
3734
android:name=".activities.VideoActivity"
3835
android:configChanges="orientation|screenSize" />
3936
<activity android:name=".activities.WelcomeActivity" />
40-
41-
<activity android:name=".activities.SignupActivity"></activity>
37+
<activity android:name=".activities.SignupActivity" />
38+
<activity android:name=".activities.FragmentsActivity"></activity>
4239
</application>
4340

4441
</manifest>

app/src/main/java/com/iamzain/template_android/activities/BaseActivity.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Intent;
44
import android.os.Bundle;
5+
import android.support.v4.app.FragmentActivity;
56
import android.support.v4.widget.DrawerLayout;
67
import android.support.v7.app.ActionBarDrawerToggle;
78
import android.support.v7.app.AppCompatActivity;
@@ -97,6 +98,7 @@ private void createNavigationDrawer(AccountHeader account_header)
9798
PrimaryDrawerItem item5 = new PrimaryDrawerItem().withIdentifier(3).withName(R.string.drawer_item_login).withIcon(ic_dashboard);
9899
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(4).withName(R.string.drawer_item_cards).withIcon(ic_dashboard);
99100
PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(5).withName(R.string.drawer_item_video).withIcon(ic_dashboard);
101+
PrimaryDrawerItem item6 = new PrimaryDrawerItem().withIdentifier(6).withName(R.string.drawer_item_fragments).withIcon(ic_dashboard);
100102

101103

102104
SecondaryDrawerItem settings = new SecondaryDrawerItem().withIdentifier(10).withName(R.string.drawer_item_settings).withIcon(ic_settings);
@@ -115,6 +117,7 @@ private void createNavigationDrawer(AccountHeader account_header)
115117
item5,
116118
item3,
117119
item4,
120+
item6,
118121
new DividerDrawerItem(),
119122
settings,
120123
feedback,
@@ -153,6 +156,11 @@ public boolean onItemClick(View view, int position, IDrawerItem drawerItem)
153156
startActivity(intent);
154157
break;
155158

159+
case 6:
160+
intent = new Intent(getApplicationContext(), FragmentsActivity.class);
161+
startActivity(intent);
162+
break;
163+
156164
case 10:
157165
intent = new Intent(getApplicationContext(), SettingsActivity.class);
158166
startActivity(intent);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.iamzain.template_android.activities;
2+
3+
import android.net.Uri;
4+
import android.support.v4.app.FragmentTransaction;
5+
import android.support.v7.app.AppCompatActivity;
6+
import android.os.Bundle;
7+
import android.view.View;
8+
import android.widget.RelativeLayout;
9+
10+
import com.iamzain.template_android.R;
11+
import com.iamzain.template_android.fragments.TestFragment;
12+
13+
public class FragmentsActivity extends BaseActivity implements TestFragment.OnFragmentInteractionListener {
14+
15+
@Override
16+
protected void onCreate(Bundle savedInstanceState) {
17+
super.onCreate(savedInstanceState);
18+
//setContentView(R.layout.activity_fragments);
19+
20+
RelativeLayout da_layout_main = (RelativeLayout) findViewById( R.id.baseactivity);
21+
View view_child = getLayoutInflater().inflate( R.layout.activity_fragments, null);
22+
da_layout_main.addView(view_child);
23+
24+
25+
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
26+
27+
TestFragment fragment = new TestFragment();
28+
fragmentTransaction.add(R.id.fragment_container, fragment);
29+
fragmentTransaction.commit();
30+
}
31+
32+
@Override
33+
public void onFragmentInteraction(Uri uri) {
34+
35+
}
36+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.iamzain.template_android.fragments;
2+
3+
import android.content.Context;
4+
import android.net.Uri;
5+
import android.os.Bundle;
6+
import android.support.v4.app.Fragment;
7+
import android.view.LayoutInflater;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
11+
import com.iamzain.template_android.R;
12+
13+
/**
14+
* A simple {@link Fragment} subclass.
15+
* Activities that contain this fragment must implement the
16+
* {@link TestFragment.OnFragmentInteractionListener} interface
17+
* to handle interaction events.
18+
* Use the {@link TestFragment#newInstance} factory method to
19+
* create an instance of this fragment.
20+
*/
21+
public class TestFragment extends Fragment {
22+
// TODO: Rename parameter arguments, choose names that match
23+
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
24+
private static final String ARG_PARAM1 = "param1";
25+
private static final String ARG_PARAM2 = "param2";
26+
27+
// TODO: Rename and change types of parameters
28+
private String mParam1;
29+
private String mParam2;
30+
31+
private OnFragmentInteractionListener mListener;
32+
33+
public TestFragment() {
34+
// Required empty public constructor
35+
}
36+
37+
/**
38+
* Use this factory method to create a new instance of
39+
* this fragment using the provided parameters.
40+
*
41+
* @param param1 Parameter 1.
42+
* @param param2 Parameter 2.
43+
* @return A new instance of fragment TestFragment.
44+
*/
45+
// TODO: Rename and change types and number of parameters
46+
public static TestFragment newInstance(String param1, String param2) {
47+
TestFragment fragment = new TestFragment();
48+
Bundle args = new Bundle();
49+
args.putString(ARG_PARAM1, param1);
50+
args.putString(ARG_PARAM2, param2);
51+
fragment.setArguments(args);
52+
return fragment;
53+
}
54+
55+
@Override
56+
public void onCreate(Bundle savedInstanceState) {
57+
super.onCreate(savedInstanceState);
58+
if (getArguments() != null) {
59+
mParam1 = getArguments().getString(ARG_PARAM1);
60+
mParam2 = getArguments().getString(ARG_PARAM2);
61+
}
62+
}
63+
64+
@Override
65+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
66+
Bundle savedInstanceState) {
67+
// Inflate the layout for this fragment
68+
return inflater.inflate(R.layout.fragment_test, container, false);
69+
}
70+
71+
// TODO: Rename method, update argument and hook method into UI event
72+
public void onButtonPressed(Uri uri) {
73+
if (mListener != null) {
74+
mListener.onFragmentInteraction(uri);
75+
}
76+
}
77+
78+
@Override
79+
public void onAttach(Context context) {
80+
super.onAttach(context);
81+
if (context instanceof OnFragmentInteractionListener) {
82+
mListener = (OnFragmentInteractionListener) context;
83+
} else {
84+
throw new RuntimeException(context.toString()
85+
+ " must implement OnFragmentInteractionListener");
86+
}
87+
}
88+
89+
@Override
90+
public void onDetach() {
91+
super.onDetach();
92+
mListener = null;
93+
}
94+
95+
/**
96+
* This interface must be implemented by activities that contain this
97+
* fragment to allow an interaction in this fragment to be communicated
98+
* to the activity and potentially other fragments contained in that
99+
* activity.
100+
* <p>
101+
* See the Android Training lesson <a href=
102+
* "http://developer.android.com/training/basics/fragments/communicating.html"
103+
* >Communicating with Other Fragments</a> for more information.
104+
*/
105+
public interface OnFragmentInteractionListener {
106+
// TODO: Update argument type and name
107+
void onFragmentInteraction(Uri uri);
108+
}
109+
}

app/src/main/res/layout/activity_base.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
xmlns:tools="http://schemas.android.com/tools"
33
android:layout_width="match_parent"
44
android:layout_height="match_parent"
5-
tools:context="com.iamzain.template_android.activities.BaseActivity">
5+
tools:context="com.iamzain.template_android.activities.BaseActivity"
6+
android:id="@+id/baseactivity">
67

78
<android.support.v7.widget.Toolbar
89
android:id="@+id/toolbar"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context="com.iamzain.template_android.activities.FragmentsActivity">
8+
9+
<android.support.v7.widget.Toolbar
10+
android:id="@+id/toolbar"
11+
android:layout_width="match_parent"
12+
android:layout_height="?attr/actionBarSize"
13+
android:background="?attr/colorPrimary"
14+
android:elevation="4dp"
15+
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
16+
17+
<!-- As the main content view, the view below consumes the entire
18+
space available using match_parent in both dimensions. -->
19+
<FrameLayout
20+
android:id="@+id/fragment_container"
21+
android:layout_width="fill_parent"
22+
android:layout_height="fill_parent"
23+
android:layout_below="@+id/toolbar">
24+
</FrameLayout>
25+
26+
27+
</android.support.constraint.ConstraintLayout>

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,4 @@
33
android:layout_width="match_parent"
44
android:layout_height="match_parent">
55

6-
<android.support.v7.widget.Toolbar
7-
android:id="@+id/toolbar"
8-
android:layout_width="match_parent"
9-
android:layout_height="?attr/actionBarSize"
10-
android:background="?attr/colorPrimary"
11-
android:elevation="4dp"
12-
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
13-
14-
15-
<!-- As the main content view, the view below consumes the entire
16-
space available using match_parent in both dimensions. -->
17-
<FrameLayout
18-
android:id="@+id/fragment_container"
19-
android:layout_width="fill_parent"
20-
android:layout_height="fill_parent"
21-
android:layout_below="@+id/toolbar">
22-
</FrameLayout>
23-
246
</RelativeLayout>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
tools:context="com.iamzain.template_android.fragments.TestFragment">
6+
7+
<!-- TODO: Update blank fragment layout -->
8+
<TextView
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"
11+
android:text="@string/hello_blank_fragment"
12+
android:textAlignment="viewStart"
13+
android:textSize="100sp" />
14+
15+
</FrameLayout>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<string name="drawer_item_about">About</string>
3131
<string name="drawer_item_login">Login Activity</string>
3232
<string name="drawer_item_welcome">Welcome Activity</string>
33+
<string name="drawer_item_fragments">Fragements Activity</string>
3334
<!-- Navigation drawer feedback button text -->
3435
<string name="mail_feedback_email">Feedback</string>
3536
<string name="mail_feedback_subject">Feedback</string>
@@ -66,5 +67,8 @@
6667
<string name="play_again_desc">To see the welcome slider again, goto Settings -> apps -> welcome slider -> clear data</string>
6768
<string name="play_again">Play Again</string>
6869

70+
<!-- TODO: Remove or change this placeholder text -->
71+
<string name="hello_blank_fragment">Hello blank fragment</string>
72+
6973

7074
</resources>

0 commit comments

Comments
 (0)