so simple but i don't know how to open navagationview in fragment
it's my fragment code here
public class Main4Fragment extends Fragment {
private ImageButton btn_subMenu;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main4, container, false);
btn_subMenu = (ImageButton) view.findViewById(R.id.btn_subMenu);
btn_subMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
};
return view;
I want open navigationview when click btn_subMenu .
and here my xml code
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/nav_view"
app:headerLayout="#layout/side_menu_header"
app:menu="#menu/side_menu"
android:layout_gravity="start"/>
thank you
Related
I'm making an app that displays the same menus for different users, each menu being a fragment, but depending on the user certain things are visible and others aren't.
However, when I try to set a component's visibility as GONE I get a null pointer exception error, I'm still testing so I'm only using a textView on the fragment xml.
I've already tried initializing both the TextView and the FragmentMenuEncomenda, the class of the fragment I'm using, objects, both inside the if clause before the onCreate() method, and after the setContentView(), like the tabLayout and viewPager.
The if clause was only to make sure the user type was correct and the code in question is as follows:
public class PaginaInicialCliente extends AppCompatActivity {
private SharedPreferences sharedpreferences;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pagina_inicial_cliente);
tabLayout = findViewById(R.id.tabLayoutCliente);
viewPager = findViewById(R.id.viewPagerCliente);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
//fragments a adicionar
adapter.addFragment(new FragmentMenuEncomendas(), "Encomendas");
adapter.addFragment(new FragmentMenuOpcoes(), "Opções");
//inicializar o tab layout
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
sharedpreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
if (sharedpreferences.getInt("tipo",0)==3) {
Log.d("USER_DEBUG","Cliente");
FragmentMenuEncomendas fragmentMenuEncomendas = new FragmentMenuEncomendas();
//THE ERROR IS HERE
fragmentMenuEncomendas.getActivity().findViewById(R.id.textViewEncomendas).setVisibility(View.GONE);
}
}
}
The FragmentMenuEncomenda class:
public class FragmentMenuEncomendas extends Fragment {
View v;
public FragmentMenuEncomendas(){
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.encomendas_fragment, container, false);
return v;
}
}
The xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<TextView
android:id="#+id/textViewEncomendas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
All the answers I've searched didn't work, I don't know if it's because I'm trying to change the visibility inside the on create method.
Because view is not created when you call the function.
public class FragmentMenuEmpresas extends Fragment {
View v;
View textView;
boolean isTextViewShow = false;
boolean isViewCreated = false;
public FragmentMenuEmpresas() {
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
v = inflater.inflate(R.layout.empresas_fragment, container, false);
textView = v.findViewById(R.id.textViewEncomendas);
return v;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (isTextViewShow) {
textView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.GONE);
}
isViewCreated = true;
}
void setIsTextViewShow() {
if (isViewCreated) {
textView.setVisibility(View.VISIBLE);
} else {
isTextViewShow = true;
}
}
void setIsTextViewGone() {
if (isViewCreated) {
textView.setVisibility(View.GONE);
} else {
isTextViewShow = false;
}
}
}
then you can call the setIsTextViewShow or setIsTextViewGone anytime you want
FragmentMenuEncomendas fragmentMenuEncomendas = new FragmentMenuEncomendas();
fragmentMenuEncomendas.setIsTextViewShow();//show
fragmentMenuEncomendas.setIsTextViewGone();//gone
Today was the first day I used ViewPager to swipe across screens in my activity. However, after adding the ViewPager, none of my buttons in any activities seem to work. I can only swipe between my activities. I don't understand as to why this happens so any help would be greatly appreciated. Thank you!
Here is the PageAdapter class code:
PageAdapter.java
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentOne();
break;
case 1:
return new FragmentTwo();
break;
case 2:
return new FragmentThree();
break;
default:
break;
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
Here is the Fragments code:
FragmentOne.java
public class FragmentOne extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_log,container,false);
}
}
FragmentTwo.java
public class FragmentTwo extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_remote_lock,container,false);
}
}
FragmentThree.java
public class FragmentThree extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_my_keys,container,false);
}
}
FragmentActivity.java
public class FragmentActivity extends android.support.v4.app.FragmentActivity {
ViewPager viewPager;
#Override
protected void onResume() {
super.onResume();
viewPager.setCurrentItem(1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
viewPager = (ViewPager) findViewById(R.id.damsara);
PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(adapter);
}
}
FragmentActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/damsara"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.v4.view.ViewPager>
The xml files i added to the fragment classes contain buttons with animations. however none of them work.
Hi guys I have problem.......
I have created fragment activity using viewpager with two tabs,and created listview with two textviews, I want to sent the to texts from tab one to tab two as title and description,I have successfully sent the description but failed to send the title(just I can send only one text) do I need (Getter()/Setter()) ? How to do one??
my fragment_one.java :
public class FragmentOne extends Fragment {
SendMessage SM;
ViewPager viewPager;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_one, container, false);
return rootView;
}
#Override
public void onViewCreated(final View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager = (ViewPager)view.findViewById(R.id.viewPager);
Button btnPassData = (Button) view.findViewById(R.id.btnPassData);
final EditText inData = (EditText) view.findViewById(R.id.inMessage);
btnPassData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SM.sendData(inData.getText().toString().trim());
}
});
}
interface SendMessage {
void sendData(String message);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
SM = (SendMessage) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException("Error in retrieving data. Please try again");
}
}
}
my fragment_two.java :
public class FragmentTwo extends Fragment {
ListView listView;
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> adapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_two, container, false);
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = (ListView) view.findViewById(R.id.list_view);
adapter = new ArrayAdapter<String>(getActivity(), R.layout.single_item,R.id.tvdesc, arrayList);
listView.setAdapter(adapter);
}
protected void displayReceivedData(String message) {
arrayList.add(0,message);
adapter.notifyDataSetChanged();
}
}
my custom_items.xml :
<TextView
android:id="#+id/tvtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_toLeftOf="#+id/tvdate"
android:layout_toRightOf="#+id/image"
android:maxLines="1"
android:text="title"
android:textColor="#android:color/black"
android:textSize="15dip"
android:textStyle="bold" />
<TextView
android:id="#+id/tvdesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvtitle"
android:layout_toRightOf="#+id/image"
android:ellipsize="end"
android:maxLines="3"
android:text="desc"
android:textColor="#android:color/black"
android:textSize="13dip"
android:lines="3" />
<TextView
android:id="#+id/tvdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_marginTop="5dip"
android:text="date"
android:textColor="#android:color/black"
android:textSize="12dip" />
note: I have asked this question many times but nobody has helped me, so you would not put any link to another question if you know the answer helped me or ignore this question.
make your SendMessage interface like this
interface SendMessage {
void sendData(String title,String message);
}
Now send both title and message to another fragment using bundle.
I think it will be helpful to you
I want to add button on fragment layer in the middle of the screen , button added but it is not clicking & goes to the desired activity. this is my code of java & xml:-
xml code:
<?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"
android:background="#drawable/a6">
<Button
android:id="#+id/hardware"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="213dp"
android:background="#android:color/darker_gray"
android:text="Button" />
</RelativeLayout>
Java code
public class SwapFragmentsix extends Fragment {
Button hardware;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_swap_fragmentsix, container, false);
hardware = (Button) view.findViewById(R.id.hardware);
hardware.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), HardwareKeyPoints.class);
startActivity(intent);
}
});
return inflater.inflate(R.layout.activity_swap_fragmentsix, container, false);
}
}
You should try this
public class SwapFragmentsix extends Fragment {
Button hardware;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_swap_fragmentsix, container, false);
hardware = (Button) view.findViewById(R.id.hardware);
hardware.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), HardwareKeyPoints.class);
startActivity(intent);
}
});
return view; //change to this
}
}
It is because of you are returning
inflater.inflate(R.layout.activity_swap_fragmentsix,
container,false); instead of view. That is you are inflating the view again.
I am new to Android Fragments. I have 2 different fragments and want to navigate from one fragment to another using buttons. For example, in fragmentA when i press a button, i go to fragmentB and vise verse.
Here are my simple codes:
FragmentA.java
public class FragmentA extends Fragment {
public FragmentA(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_a, container, false);
return rootView;
}
}
fragmenta.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="670dp"
android:gravity="top"
android:orientation="vertical" >
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:text="Fragment 1"
android:textStyle="bold"
android:textSize="22dp" />
</LinearLayout>
FragmentB.java
public class FragmentB extends Fragment {
public FragmentB(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_b, container, false);
return rootView;
}
}
fragmentb.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="670dp"
android:gravity="top"
android:orientation="vertical" >
<TextView
android:id="#+id/txtLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:text="Fragment 2"
android:textStyle="bold"
android:textSize="22dp" />
</LinearLayout>
I need codes (both java and xml) for buttons. Help from scratch would be appreciated. Thanks in advance!!
Check This Tutorial and Below Code for refrence
Fragment1.java
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
public void onClick(View view) {
Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment1, fragment2);
fragmentTransaction.commit();
}
}
Fragment2.java
public class Fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
Code in Activity
public class MyActivity extends FragmentActivity implements OnListenerChangeFragment {
FragmentA mFragmentA;
FragmentB mFragmentB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
#Override
public void onShowFragmenA() {
//replace mFragmentB by mFragmentA
}
#Override
public void onShowFragmenB() {
//replace mFragmentA by mFragmentA
}
}
//Code in Fragments
public class FragmentA extends Fragment {
private OnListenerChangeFragment mCallback;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
Button mButton = new Button(getActivity());
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mCallback.onShowFragmenB();
}
});
return mButton;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnListenerChangeFragment) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()+ " must implement OnListenerChangeFragment");
}
}
}
public class FragmentB extends Fragment {
private OnListenerChangeFragment mCallback;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Button mButton = new Button(getActivity());
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mCallback.onShowFragmenA();
}
});
return mButton;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallback = (OnListenerChangeFragment) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()+ " must implement OnListenerChangeFragment");
}
}
}