Hello i'm having trouble displaying my toolbar using fragment. I am integrating my app with facebook. I am able to connect to facebook and get basic user profile details such as first name, last name and user profile pic. Now i passed the information from my main activity to my second activity. My second activity will be using fragments. Now i made a fragment that holds toolbar, the user name and pic. But i am having trouble displaying the fragment. Will someone tell me what i'm doing wrong. Thank you. below are my files.
color.xml:
<resources> <color name="toolBar">#00ff80</color> <color name="colorPrimary">#cc3300</color> <color name="colorPrimaryDark">#C51162</color> <color name="textColorPrimaryDark">#FFFFFF</color> <color name="windowBackground">#FFFFFF</color> <color name="navigationBarColor">#808080</color> <color name="colorAccent">#FF80AB</color> <color name="backgroundColor">#003399</color> </resources>
styles.xml:
<resources> <!-- Base application theme. --> <!-- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> --> <!-- Customize your theme here. --> <!-- </style> --> <!-- Base Application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> </style> </resources>
activity_second.xml:
<LinearLayout 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" android:orientation="vertical" android:background="@color/backgroundColor" > <android.support.v4.widget.DrawerLayout android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:layout_width="match_parent" android:layout_height="match_parent" tools:layout="@layout/first_fragment" android:name="edu.sjsu.cmpe277.termproject.Fragments.FirstFragment" android:id="@+id/toolBar_fragment" /> </android.support.v4.widget.DrawerLayout> </LinearLayout>
first_fragment.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"> <include android:id="@+id/toolBar" layout="@layout/toolbar" /> <TextView android:id="@+id/textName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/users_FirstName" android:textSize="@dimen/font_size" android:hint="JJJJ" android:layout_gravity="center_horizontal"/> <com.facebook.login.widget.ProfilePictureView android:id="@+id/profilePic" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" > </com.facebook.login.widget.ProfilePictureView> </LinearLayout>
toolbar.xml:
<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res-auto" android:id="@+id/toolBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="@color/colorPrimary" android:elevation="5dp" android:gravity="top|start"> </android.support.v7.widget.Toolbar>
FirstFragment.java:
public class FirstFragment extends Fragment implements FragmentCommunicator { private ProfilePictureView profilePictureView; private Toolbar toolbar; private TextView textView; private FragmentCommunicator fragmentCommunicator; // private GetDetails getdetails; //private DrawerLayout drawerLayout; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // return super.onCreateView(inflater, container, savedInstanceState); View rootView = inflater.inflate(R.layout.event_fragment, container, false); toolbar = (Toolbar)rootView.findViewById(R.id.toolBar); textView = (TextView)rootView.findViewById(R.id.textName); profilePictureView = (ProfilePictureView)rootView.findViewById(R.id.profilePic); return rootView; } @Override public void onAttach(Context context) { super.onAttach(context); try{ fragmentCommunicator =(FragmentCommunicator)context; } catch(ClassCastException ex) { throw new ClassCastException(ex.toString()); } } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); toolbar = (Toolbar)getActivity().findViewById(R.id.toolBar); profilePictureView = (ProfilePictureView)getActivity().findViewById(R.id.profilePic); } @Override public void setDetails(String userId, String name) { }
secondActivity.java:
public class secondActivity extends AppCompatActivity implements FragmentCommunicator{ private FirstFragment firstFragment; private FragmentTransaction fragmentTransaction; private Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); intent = getIntent(); String firstName = intent.getStringExtra("firstName"); String userId = intent.getStringExtra("Id"); firstFragment = (FirstFragment)getSupportFragmentManager().findFragmentById(R.id.toolBar_fragment); intent = getIntent(); String firstName = intent.getStringExtra("firstName"); String name = intent.getStringExtra("lastName"); String userId = intent.getStringExtra("Id"); @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_second, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void setDetails(String userId, String name) { FirstFragment firstFragment = (FirstFragment)getSupportFragmentManager().findFragmentById(R.id.toolBar_fragment); if(firstFragment !=null) { firstFragment.setDetails(userId, name); } }
}
No comments:
Post a Comment