When I click at the menu items, then layouts not coming to view.
And MainActicity wanna "menu1_Fragment.java" encode with "android.support.v4.app.Fragment"
İf I encode only "Fragment", MainActivity is getting error.
In compatible types.
Required :android.support.v4.app.Fragment
Found :intizamyazilim.navigationdrawernew.menu1_Fragment
Here is menu1_Fragment.java
package intizamyazilim.navigationdrawernew;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Administrator on 01.03.2015.
*/
public class menu1_Fragment extends android.support.v4.app.Fragment {
View rootview;
#Nullable
// #Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootview = inflater.inflate(R.layout.menu1_layout, container, false);
return rootview;
}
}
I Fix it.
I changed this "menu1_Fragment.java" with this codes:
package intizamyazilim.navigationdrawernew;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class menu1_Fragment extends android.support.v4.app.Fragment {
public menu1_Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.menu1_layout, container, false);
return rootView;
}
}
No it is not the proper way to fix it. The proble here is that you are extending your menu1_Fragment using support library and on the other hand you are importing fragment from SDK(import android.app.Fragment;). Make it consistent. Either use fragment from support or from android SDK.
Related
I am trying to use the binding method in android studio to connect two fragments using onCreate and onViewCreated methods. so far i am getting id not resolved error. I have already connected the fragments on the xml graph. Bellow is the code of the settings java file.
package com.mqtt.workactiv.ui.settings;
import static android.os.Build.VERSION_CODES.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.mqtt.workactiv.databinding.FragmentSettingsBinding;
public class SettingsFragment extends Fragment {
private FragmentSettingsBinding binding;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
SettingsViewModel settingsViewModel =
new ViewModelProvider(this).get(SettingsViewModel.class);
binding = FragmentSettingsBinding.inflate(inflater, container, false);
View root = binding.getRoot();
return root;
}
#Override
public void onViewCreated (#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
NavController navController = Navigation.findNavController(view);
Button gateConnButton;
gateConnButton = view.findViewById(R.id.gatewayConnButton);
gateConnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
navController.navigate(R.id.action_navigation_setting_to_gateway4);
}
});
}
}
Make sure to add this in build.gradle(app)
buildFeatures {
viewBinding = true
}
Then give sync project with gradle files.
If this didnt work, try rebuild, invalidate cache/ restart.
Also view.findviewbyid not required if view binding is enabled.
You can access button directly by something like this,
binding.gateConnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
navController.navigate(R.id.action_navigation_setting_to_gateway4);
}
});
You can try code like this.
NavDirections navDirections = SettingsFragmentDirections.actionNavigationSettingToGateway4();
findNavController().navigate(navDirections, null);
I don't see your navigation xml file, so SettingsFragmentDirections.actionNavigationSettingToGateway4() can have another name. Fix those names yourself.
P.S. the versions of libraries which I'm using androidx.navigation:navigation-fragment-ktx:2.4.0, "androidx.navigation:navigation-ui-ktx:2.4.0"
I have here a problem with the onClicklistener method in a fragment. If I click on the button nothing happens and I don't know why. No error and no output.
I've tried to implements the OnClickListener or set a (Button) before the view.findViewById(R.id.button); but nothing helps.
I've seen much questions here in Stack Overflow about this problem but no solution from there helps me :/
Do you have any ideas? Thank You!
package com.christoph.myapplication.ui.home;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.christoph.myapplication.R;
public class HomeFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_home, container, false);
final Button button= view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setBackgroundColor(Color.RED);
System.out.println("Hallo");
}
});
return view;
}
}
You can add click listener rather in onViewCreated function. Try it and let me know if it works. Check android fragment lifecycles documentation for more info.
Cheers
This seems like a common issue with lots of accepted answers but none of them seem to be working for me.
Most answers suggest using this piece of code to add a toolbar into a fragment:
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
if (mToolbar != null) {
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
}
Alas, this still crashes my app even though I'm using AppCompatActivity. I think the error may lie in the fact that my Fragment class extends Fragment and not AppCompatActivity but I don't know enough about android as of yet to be sure of this. It crashes on the setSupportActionBar line.
My Fragment code:
package erikligai.ribbitapplication;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by erik on 2017-06-07.
*/
public class MessageFragment extends Fragment {
Toolbar mToolbar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
// return inflater.inflate(R.layout.message_fragment_layout, container, false);
View rootView = inflater.inflate(R.layout.message_fragment_layout, container, false);
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
if (mToolbar != null) {
((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
}
return rootView;
}
Would appreciate any suggestions.
You need to cast your activity from getActivity() to AppCompatActivity
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle();
Toolbar getting from Activity, not from Fragment
mToolbar = (Toolbar)rootView.findViewById(R.id.toolbar);
I am trying to create edittext using Java in bottom fragment class based on input that is to be passed from top fragment. But when I type
Button add_submit = new Button(this);
I get error for the this for parameter. However I can use this code in MainActivity.java.
Why is this so? What is causing the error and how to fix it?
The following are the complete code for the class
package com.test.gpacalc;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class AddActivityBottom extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_bottom,container,false);
return view;
}
public void createAddInput(String number_of_subjects){
Button add_submit = new Button(this);
}
}
Button constructor requires context as an argument. Fragment doesn't implement it, activity does. try new Button(getActivity())
I want to set wordtoSpan string at OnCreateView.
my code:
package com.tachles;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class History_moadim_a extends Fragment {
TextView horef;
TextView kaiz;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
return rootView;
horef = (TextView) findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
}
}
Eclipse says that I cant use findViewById, so I tried to insert getView() but still the same problem. Please show me my mistake
Thank you
If history_moadim_a is your layout containing the textView3, change your onCreateView() to something like
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
horef = (TextView) rootView.findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
return rootView;
You want to find the TextView in the layout you just inflated (rootView), and return the layout at the end (and not in the middle of the method).
use
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class History_moadim_a extends Fragment {
TextView horef;
TextView kaiz;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.history_moadim_a, container, false);
horef = (TextView) rootView.findViewById(R.id.textView3);
Spannable wordtoSpan = new SpannableString("hgh");
horef.setText(wordtoSpan);
return rootView;
}
}
i can not see a return statement at onCreateView at perfect place so please do return rootView ; inside onCreateView at the end of the logics by means at the end of the onCreateView