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);
Related
i made a 2 fragment layout one for the login screen and the other for the Register screen and in the main screen i put a fragment for login screen and a button if the user want to register he click in that button and the register fragment appears to him and the login fragment disappear
the problem is after i press the register button in the main screen the login screen appears to be behind the register screen
here's a photo for it
login behind Register
i don't know what's the problem or i misunderstanding the fragments i don't know
here's the onclick method on the MainActity class
#Override
public void onClick(View view) {
String mytxt = inversebutton.getText().toString();
Fragment fragmentSign = new Signin();
Fragment fragmentregister = new Register();
if(mytxt.equals("Register")){ // call signin frame
android.support.v4.app.FragmentManager fragmentManager= getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragmentplace ,fragmentregister).commit();
inversebutton.setText("SignIn");
}else{ // call register fra
android.support.v4.app.FragmentManager fragmentManager= getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragmentplace ,fragmentSign).commit();
inversebutton.setText("Register");
}
}
});
here's the activitymain.xml
<?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:id="#+id/activity_main"
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.abdelmagied.myapplication.MainActivity"
android:background="#222">
<fragment
android:layout_width="match_parent"
android:layout_height="300dp"
android:name="com.example.abdelmagied.myapplication.Signin"
android:id="#+id/fragmentplace"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/reverse"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="16dp" />
</RelativeLayout>
here's the register_fragment class
public class Register extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_register, container, false);
}
here's the signin fragment class
public class Register extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_register, container, false);
}
here's the fragment_register.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="com.example.abdelmagied.myapplication.Register"
android:background="#090"
android:id="#+id/registerfragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="49dp"
android:id="#+id/textView" />
<TextView
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_marginTop="61dp"
android:id="#+id/textView3" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/Rbutton"
android:layout_alignStart="#+id/Rbutton"
android:layout_marginTop="30dp"
android:id="#+id/registerN" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_below="#+id/registerN"
android:layout_alignRight="#+id/registerN"
android:layout_alignEnd="#+id/registerN"
android:layout_marginTop="36dp"
android:id="#+id/Rpassword" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/RRpassword"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignLeft="#+id/Rbutton"
android:layout_alignStart="#+id/Rbutton" />
<Button
android:text="Register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Rbutton"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp" />
<TextView
android:text="Rpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/textView3"
android:layout_alignStart="#+id/textView3" />
</RelativeLayout>
here's the fragment_signin.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="com.example.abdelmagied.myapplication.Signin"
android:background="#878"
android:id="#+id/signinfragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="55dp"
android:layout_marginStart="55dp"
android:id="#+id/sn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="46dp" />
<TextView
android:text="Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/sn"
android:layout_alignLeft="#+id/sn"
android:layout_alignStart="#+id/sn"
android:layout_marginTop="79dp"
android:id="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_above="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/sname" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/sname"
android:layout_alignStart="#+id/sname"
android:id="#+id/signpassword" />
<Button
android:text="SignIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Sbuton"
android:layout_below="#+id/signpassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp" />
</RelativeLayout>
Rather than taking fragment tag in xml,it would be good for taking just a layout by giving its id and replace it by fragment transaction dynamically
In your layout, change it by taking simple Relative or Linear layout and must give that layout background,remove that fragment tag and replace fragment by java coding according to you need
and then replace it at start of your class in onCreate() method by->`
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentplace, new Your_frag(), Constant.FragmentTags.fragmentTag).commit();
I think you should use DialogFragment which are here for those kind of popups in front of others.
You use it like that
DialogFragment registerFragment = RegisterFragment.newInstance(0);
registerFragment.show(getFragmentManager().beginTransaction(), "RegisterPopup");
And your fragment will be of class DialogFragment. Usually you pop a Dialog starting like that. Then depending on what you want many options are possible.
public class RegisterFragment extends DialogFragment {
...
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
}
}
And you can find the example on the Android Developer
DialogFragment
This is representation of my app layout:
Where should I create onClickListeners for included layouts? I tried inside the fragment but I could not get through with findViewById. So I tried from Main Activity but I'm not sure how to get to included layouts from there.
I also tried this inside the fragment:
public class MainMenu extends Fragment implements View.OnClickListener{
View button_call;
#Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
View myView = inflater.inflate(R.layout.fragment_main_menu, container, false);
button_call = myView.findViewById(R.id.btn_call);
button_call.setOnClickListener(this);
return myView;
}
#Override
public void onClick(View v) {
// implements your things
}
public MainMenu() {
}
}
But then Fragment seems to be empty
Fragment XML:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/mainmenu_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="60dp">
<include android:id="#+id/btn_call"
layout="#layout/call_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"/>
<include android:id="#+id/button2"
layout="#layout/message_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<include android:id="#+id/button3"
layout="#layout/navigate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<include android:id="#+id/button4"
layout="#layout/remind_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</LinearLayout>
</ScrollView>
Steps
Create a xml file you want to inclue (eg. web.xml)
use include tag in fragment's xml and connect your layout you need to include using layout="#layout/
Initialize include tag inside fragment (when you do this make sure about the root tag of your web.xml)
Once initializing include tag is done access views inside the layout that included (web.xml) using include tag
Example
Here in my fragment I have an include tag in it's XML. It connects to my web.xml and ..web.xml's parent tag/ root tag is a FrameLayout ..
Now see how I initialize my include tag..(If you do not use the right root tag to initialize include,it will crash with a null pointer)
I have an id called g_betta in my web.xml( XML layout that I included in my fragment)
see how I access that view..
public class FragmentAbout extends Fragment {
private RelativeLayout relativeLayoutConnectedInsideIncludeTag;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_fragment,container,false);
FrameLayout view =(FrameLayout) rootView.findViewById(R.id.include_tag);
relativeLayoutConnectedInsideIncludeTag = (RelativeLayout) view.findViewById(R.id.g_betta);
relativeLayoutConnectedInsideIncludeTag.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Yes I Found you", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
my fragment xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/mainmenu_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<include
android:id="#+id/include_tag"
layout="#layout/web"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
my include--> web.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/maroon"
android:gravity="center_horizontal">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/g_betta"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="HELLO"
android:textColor="#FFF"
android:textSize="20dp" />
<RelativeLayout
android:id="#+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp"
android:background="#color/colorPrimaryDark"
android:gravity="bottom"
android:paddingLeft="24dp"
android:paddingRight="24dp" />
</RelativeLayout>
<ImageView
android:id="#+id/imageone"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/lin"
android:layout_margin="16dp"
android:background="#drawable/girl"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/imagetwo"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="#+id/imageone"
android:layout_margin="16dp"
android:background="#drawable/amanda"
android:scaleType="fitXY" />
</RelativeLayout>
</ScrollView>
</FrameLayout>
Do like this:
button_call = myView.findViewById(R.id.btn_call);
button_call.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// write your code here...
});
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 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.
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;
}
}