I decided to use fragments as an alternative to activities, so I cut up my activity_main.xml into 2 different XML files: activity_main.xml and fragment_main.xml. The problem is that the relativeLayout previously in activity_main that I was referencing in java is no longer working after I moved it to the other XML file. It looks like you need to set the content view of whatever XML file you're using in order to findViewByID, but filling in my graph with barchart data might require both; the fragment_main.xml needing to be set as my content view (it's the blank that I'm filling in with other fragments) and the activity_main needing to be set to display it as a part of my main screen. The fSetGraph(); is using this graph: https://github.com/PhilJay/MPAndroidChart
Out of curiousity: do layout inflaters come into play anywhere in here?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawer;
private Toolbar toolbar;
private RecyclerView recyclerView;
private NavigationView nvDrawer;
FloatingActionButton fab;
FragmentManager manager = getSupportFragmentManager();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
fSetGraph();
fsetFAB();
// setContentView(R.layout.activity_main);
// fSetToolBar();
// fSetDrawer();
// fSetDrawerContent();
// fsetInitialFragment();
}
private void fSetGraph() {
.
. //Lots of code here about setting the graph. Not important.
.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayoutForChart);
rl.addView(MainActivity.barChartGlobal,
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayoutMainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayoutActivity"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#FFFFFF">
<!--Fragments placed here-->
</RelativeLayout>
</LinearLayout>
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/drawer_header">
<!--<include-->
<!--layout="#layout/drawer_header"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="160dp"/>-->
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Here on down is the fragment I'm using in activity_main.xml-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:id="#+id/relativeLayoutForChart"
android:layout_above="#+id/linearLayoutForCenterReference"
android:layout_below="#+id/linearLayoutHeader">
<com.github.mikephil.charting.charts.BarChart
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="false" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/linearLayoutForCenterReference">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/linearLayoutLabels"
android:gravity="center_vertical|center_horizontal"
android:layout_alignTop="#+id/linearLayoutForCenterReference"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_centerHorizontal="true"
android:layout_toRightOf="#+id/relativeLayoutSchedule"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="tv1"
android:id="#+id/textView1"
android:textSize="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="tv2"
android:id="#+id/textView2"
android:textSize="15dp"
android:layout_marginLeft="52dp"
android:layout_marginRight="52dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="tv3"
android:id="#+id/textView3"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:id="#+id/linearLayoutHeader"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:gravity="center_horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="One"
android:id="#+id/textViewOne"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/textViewState"
android:layout_toStartOf="#+id/textViewState" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Two"
android:id="#+id/textViewTwo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Three"
android:id="#+id/textViewThree"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textViewState"
android:layout_toEndOf="#+id/textViewState" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayoutLabels"
android:layout_centerHorizontal="true"
android:id="#+id/linearLayoutActivityFeed"
android:gravity="center_vertical|center_horizontal"
android:paddingTop="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="3dp">
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/imageView2"
android:src="#drawable/ic_assignment_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Activity Feed"
android:id="#+id/textViewActivityFeed"
android:layout_marginBottom="1dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayoutActivityFeed"
android:layout_centerHorizontal="true"
android:id="#+id/relativeLayoutForTabs">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:gravity="right">
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white">
</android.support.v4.view.ViewPager>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_add_black_24dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawer_recyclerView"
android:layout_width="match_parent"
android:layout_gravity="start"
android:layout_height="match_parent"
android:background="#FFFFFF">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayoutForCenterReference"
android:layout_above="#+id/linearLayoutActivityFeed"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="false"
android:id="#+id/relativeLayoutSchedule"
android:layout_alignParentTop="false"
android:gravity="center_vertical"
android:layout_alignTop="#+id/linearLayoutLabels"
android:layout_marginLeft="15dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/imageViewSchedule"
android:src="#drawable/ic_event_black_24dp"
android:contentDescription="Event Icon" />
</RelativeLayout>
</RelativeLayout>
Trying to run that ending part of fSetGraph() with the contentView set to activity_main instead of fragment_main (which the graph is in) gives me the error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
Nav Drawer code
switch (menuItem.getItemId()) {
case R.id.navigation_item_log_in:
DialogPopupSignInFragment alertDialogSignInCustom = new DialogPopupSignInFragment();
alertDialogSignInCustom.show(manager, "DialogSignIn");
break;
case R.id.navigation_item_home:
FragmentMain fragmentMain = new FragmentMain();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.relativeLayoutActivity, fragmentMain, "Home");
transaction.commit();
setTitle("App Home");
break;
case R.id.my_stats:
FragmentMyStats fragmentMyStats = new FragmentMyStats();
FragmentTransaction transactionMyStats = manager.beginTransaction();
transactionMyStats.replace(R.id.relativeLayoutActivity, fragmentMyStats , "MyStats");
transactionMyStats.commit();
setTitle("My Stats");
break;
case R.id.navigation_item_winners:
FragmentWinners fragmentWinners = new FragmentWinners();
FragmentTransaction transactionWinners = manager.beginTransaction();
transactionWinners.replace(R.id.relativeLayoutActivity, fragmentWinners, "Winners");
transactionWinners.commit();
setTitle("Winners");
break;
case R.id.navigation_item_settings:
FragmentSettings fragmentSettings = new FragmentSettings();
FragmentTransaction transactionSettings = manager.beginTransaction();
transactionSettings.replace(R.id.relativeLayoutActivity, fragmentSettings, "Settings");
transactionSettings.commit();
setTitle("Settings");
break;
case R.id.navigation_item_about:
FragmentAbout fragmentAbout = new FragmentAbout();
FragmentTransaction transactionAbout = manager.beginTransaction();
transactionAbout.replace(R.id.relativeLayoutActivity, fragmentAbout, "About");
transactionAbout.commit();
setTitle("About");
break;
}
Setting up my Drawer in MainActivity
private void fSetDrawer() {
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.app_name, R.string.app_name);
mDrawer.setDrawerListener(drawerToggle);
drawerToggle.syncState();
}
Navigation Drawer XML from activity_main
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/drawer_header">
<!--<include-->
<!--layout="#layout/drawer_header"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="160dp"/>-->
</android.support.design.widget.NavigationView>
Navigation Drawer Menu
<group
android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_log_in"
android:icon="#drawable/ic_person_black_24dp"
android:title="Log In">
</item>
<item
android:id="#+id/navigation_item_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="Home">
</item>
<item
android:id="#+id/my_stats"
android:icon="#drawable/ic_developer_board_black_24dp"
android:title="My Stats">
</item>
<item
android:id="#+id/navigation_item_winners"
android:icon="#drawable/ic_wb_iridescent_black_24dp"
android:title="Winners">
</item>
<item
android:id="#+id/navigation_item_settings"
android:icon="#drawable/ic_settings_black_24dp"
android:title="Settings">
</item>
<item
android:id="#+id/navigation_item_about"
android:icon="#drawable/ic_local_library_black_24dp"
android:title="About">
</item>
<!--Recycler View-->
</group>
Yes its to do with layout inflaters. What is happening is when you use findViewById, the activity ONLY checks the layout in its setContectView which in your case is activity_main. Because of this the activity is unable to find r1. The solution to this is the inflate r1 in a fragment and then reference it by your main activity.
import android.support.v4.app.Fragment; //Edit : make sure its this
public class MapFragment extends Fragment{
View v //EDIT
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_main, container, false);
RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.relativeLayoutForChart); //EDIT
rl.addView(barchart,
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
return v;
}
Make a new class with this in it. Then in your main activity type in this code
FragmentTransaction ft = ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction();
MapFragment mapFragment=new MapFragment();
ft.replace(R.id.fragment_map, placeOrderFragment).addToBackStack(null).commit();
In your main_activity xml, add a with id of map_fragment.
Basically the flow will be your map will be put into the fragment xml, this will then be inflated by the fragment class, this will then be used by the main activity.
EDIT : Tweaked code. Do have a look. When a layout is being inflated, to find view groups within that layout, it has to passed as well along with findViewById so a small change is needed. Also when you define the fragment, make sure you import the v4. one so its backward compatible.
Related
I have two fragments, WishlistFragment and GoShoppingFragment and a button "Continue Shopping". On button click i want WishlistFragment replaced with GoShoppingFragment.
This is implementation of onClick.
public void onClickShopNow() {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = new GoShoppingFragment();
fragmentManager
.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
if(fragmentManager.getBackStackEntryCount() > 0) {
fragmentManager.popBackStackImmediate();
}
}
Issue here is, when I click on button "Continue shopping" then WishlistFragment gets relaced GoShoppingFragment but I get output like this. WishlistFragment remains in the background. How do I solve this issue?
GoShopping layout:
<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=".ui.store.goshopping.GoShoppingFragment">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_go_shopping"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_go_shopping"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible" />
<LinearLayout
android:id="#+id/moreLoading"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/transparent"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone">
<ProgressBar
android:id="#+id/moreLoadingIndicator"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="#string/label_loading_more" />
</LinearLayout></RelativeLayout>
Wishlist layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.launchbyte.appio.ui.store.mywishlist.MyWishlistFragment">
<LinearLayout
android:id="#+id/layout_continue_shopping"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/txt_wishlist_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:text="#string/wishlist_empty" />
<TextView
android:id="#+id/txt_wishlist_add_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:text="#string/wishlist_add_items" />
<Button
android:id="#+id/button_continue_shopping"
android:layout_width="150dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginTop="24dp"
android:background="#drawable/primary_color_button_selector"
android:text="#string/continue_shopping" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_my_wishlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadeScrollbars="true"
android:visibility="gone" />
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible" />
</RelativeLayout></FrameLayout>
set background of your fragment and setClickable(true) inside each of your fragment xml.
Setting background color to White in parent layout fixed the issue.
Remove this :
if(fragmentManager.getBackStackEntryCount() > 0) {
fragmentManager.popBackStackImmediate();
}
Also in your GoShopping layout: in root tag add:
<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"
android:clickable="true"
android:background="?android:attr/windowBackground"
tools:context=".ui.store.goshopping.GoShoppingFragment">
public void onClickShopNow() {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = new GoShoppingFragment();
Fragment WishlistFragment =
fragmentManager.getFragmentByTag("TagName");
fragmentManager
.beginTransaction()
.remove(WishlistFragment)
.commit();
fragmentManager
.beginTransaction()
.add(R.id.container, fragment,"TagHere")
.commit();
}
I hope this help you..
I'm trying to use fragments to programatically change the text on a screen. To do this I'm setting up an on click listener on a text view and then if it's clicked starting a fragment manager, replacing the current fragment with the new fragment. However, this causes my app to crash when it's started.
From reading the crash report it seems like the error is happening at tv1.setOnClickLIstener...
Finally, Android Studio keeps giving me a type mismatch when I use fragment or support fragment. That is why you see android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Java Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//begin transaction
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//replace the contents of the container with the new fragment
ft.replace(R.id.placeHolder, new SplashScreenFragment());
ft.commit();
TextView tv1 = (TextView) findViewById(R.id.whatIsHumanTrafficing);
tv1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.placeHolder, new whatIsHumanTrafficing());
ft.commit();
}
});
}
}
XML from activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/placeHolder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
XML displayed before click:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.piatt.worksafe.MainActivity"
android:id="#+id/splashScreenId"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work Safe!"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:paddingBottom="32dp"
android:id="#+id/title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is Human Trafficing?"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/whatIsHumanTrafficing"
android:clickable="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I get safe labor?"
android:layout_below="#+id/whatIsHumanTrafficing"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/howDoIGetSafeLabor"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I check that my job / job offer is legal?"
android:layout_below="#+id/howDoIGetSafeLabor"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:gravity="center"
android:id="#+id/checkLegality"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How can I get help?"
android:layout_below="#+id/checkLegality"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/getHelp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About us"
android:layout_below="#+id/getHelp"
android:layout_centerHorizontal="true"
android:textSize="16sp"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:id="#+id/aboutUs"
/>
</RelativeLayout>
XML to be displayed after click on text view:
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Human Trafficing Is:"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:paddingBottom="32dp"
android:id="#+id/title"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confiscation of travel documents"
android:layout_below="#+id/HTdescription1"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HTdescription1"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unregistered Labor"
android:layout_below="#+id/HTdescription1"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HDdescription2"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Isolation from friends and family"
android:layout_below="#+id/HTdescription2"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HDdescription3"
android:layout_gravity="center"
/>
</LinearLayout>
Fragment named human trafficking that I'm trying to inflate:
public class whatIsHumanTrafficing extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
//inflate the layout for this fragment
return inflater.inflate(R.layout.what_is_human_trafficing, container, false);
}
}
You have used activity_main in your MainActivity as follows
setContentView(R.layout.activity_main);
and you are accessing whatIsHumanTrafficingnamed TextView in the same MainActivity which is not possible as it is not present in activity_main and which is present in another xml. So you are getting NullPointerException there.
I recently managed to have my navigation drawer edited and responding clicks, but after one transition to another activity, navigation drawer is displayed but wont response to any further clicks.
the only way to go back is by pressing android buikt in "back" button
Thanks
In your activity layout you should add NavigationView to the drawerLayout as follows:
<android.support.v4.widget.DrawerLayout>
...
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:itemIconTint="#333"
app:itemTextColor="#333">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="fragments.MenuFragment"
android:id="#+id/fragment"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
In your MenuFragment layout you have something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/header"
layout="#layout/nav_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/menuRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/header"/>
</RelativeLayout>
And in the class of your fragment you can do this:
public class MenuFragment extends Fragment {
RecyclerView recyclerView;
ArrayList<MenuItem> menuItems = new ArrayList<>();
private OnFragmentInteractionListener mListener;
public MenuFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.menu_fragment, container, false);
recyclerView = root.findViewById(R.id.menuRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
recyclerView.addOnItemTouchListener(...);//add the code to handle menu click...
return root;
}
EDIT
nav_header.xml is actually the header of the navigation view and is nothing special:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout2"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:padding="10dp">
<ImageView
android:id="#+id/logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:paddingBottom="20dp"
android:src="#drawable/ic_white"/>
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/logo">
<ImageView
android:id="#+id/account"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="2dp"
android:src="#drawable/ic_account_box_white_48dp"/>
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_toLeftOf="#+id/account"
android:ellipsize="marquee"
android:singleLine="true"
android:text="UserName"
android:textColor="#ffffff"/>
</RelativeLayout>
</RelativeLayout>
The menuRecyclerView is just a recyclerView which is responsible for showing the menu items and you have to add the items to that (You can see this or this for getting started with recyclerView)
Once you added the items you are going to need to add the OnItemClickListener to the recycler view which you can learn here
And after all that is done you can start another activity like this:
Intent newActivity = new Intent(getActivity(), newActivity.class);
startActivity(newActivity);
Im using Caldroid library. I want to use it inside my layout. My layout also includes textview. But when I use Caldroid with framelayout the other views disappear.
Here is XML and Java codes using Caldroid with FrameLayout.
<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"
tools:context="com.birfincankafein.mpandroidchart.CalendarFragment"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/container_caldroid"
android:layout_weight="0.4">
</FrameLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="#+id/imageView_divider"
android:layout_below="#+id/container_caldroid"
android:focusableInTouchMode="false"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="0.1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/imageView_divider"
android:background="#ffff00ba"
android:layout_weight="0.4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView_description"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff00ebff"/>
</RelativeLayout>
Here is java code:
mCaldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
args.putInt( CaldroidFragment.START_DAY_OF_WEEK, CaldroidFragment.MONDAY );
mCaldroidFragment.setArguments( args );
getActivity().getSupportFragmentManager().beginTransaction().replace( R.id.container_calendar , mCaldroidFragment ).commit();
And here what it looks like:
These are codes for using Caldroid with fragment.
<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"
tools:context="com.birfincankafein.mpandroidchart.CalendarFragment"
android:orientation="vertical">
<!-- TODO: Update blank fragment layout -->
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
class="com.roomorama.caldroid.CaldroidFragment"
android:id="#+id/container_caldroid"
android:layout_weight="0.4">
</fragment>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="#+id/imageView_divider"
android:layout_below="#+id/container_caldroid"
android:focusableInTouchMode="false"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="0.1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/imageView_divider"
android:background="#ffff00ba"
android:layout_weight="0.4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView_description"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff00ebff"/>
</RelativeLayout>
And there is no need to java code. It had been initialized. Here is how it looks:
Why this happens? I want to use this library inside the frame layout. Because I have to initialize it manually. I have to set something neccassary.
Ps: The background color is for detect where the layout is.
I'm not able to recreate the same. The below code works fine for me.
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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/cal_container"
android:layout_weight="0.4"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="#+id/imageView_divider"
android:layout_below="#+id/container_caldroid"
android:focusableInTouchMode="false"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="0.1"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/imageView_divider"
android:background="#ffff00ba"
android:layout_weight="0.4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView_description"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ff00ebff"/>
</RelativeLayout>
</LinearLayout>
Fragment:
public class meets extends Fragment {
public meets() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_meets, container, false);
CaldroidFragment mCaldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
args.putInt( CaldroidFragment.START_DAY_OF_WEEK, CaldroidFragment.MONDAY );
mCaldroidFragment.setArguments( args );
getActivity().getSupportFragmentManager().beginTransaction().replace( R.id.cal_container , mCaldroidFragment ).commit();
return root;
}
}
With the layout below the buttons are being shown on top of the page. How can I make them appear below, after the container, down on the page?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/map"
android:onClick="ClickClients"
android:text= "#string/Clients"
>
</Button>
</LinearLayout>
</LinearLayout>
The MainActivity looks as follows and the HomePage.xml is also below
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GetButtonClicked(5);
setContentView(R.layout.activity_main);
}
public void GetButtonClicked(int position)
{
// update the main content by replacing fragments
Fragment fragment = null;
switch (position){
case 0:
fragment= new FirstPageFragment();
break;
default:
fragment = new FirstPageFragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void ClickHomePage(View view){
GetButtonClicked(0);
}
public void ClickClients(View view){
GetButtonClicked();
}
}
<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="com.test.FirstPageFragment" android:background="#drawable/theme">
</FrameLayout>
This is the FirstPage
public class FirstPageFragment extends Fragment {
public FirstPageFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first_page, container, false);
}
}
Use ralative layout insted of linear.
Make a different layout for bottom buttons, and then at the last of your container layout use this:
<include android:id="#+id/bottomLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/bottom_buttons"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="#string/Home" />
<Button
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="#string/Clients" >
</Button>
</LinearLayout>
Try this
Use RelativeLayout with android:layout_alignParentBottom="true" step by step guide is available here.
Your skeleton for layout should look like this
<RelativeLayout
<LinearLayout android:layout_alignParentBottom="true">
<Button
<Button
</LinearLayout>
</RelativeLayout>
Final Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="Home" />
<Button
android:layout_marginLeft="30dp"
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="Clients" >
</Button>
</LinearLayout>
The issue was that I did not include the container in another layout...
<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="com.test.MainPageActivityActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/container">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_alignParentTop="#+id/container">
and insert buttons layout here....
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>