I got a Fragment that need to take a buttonclick.
Problem is that the click it going to the MainActivity hosting all fragments and not to the Fragment itself.
I have tried Loging it and nothing shows in the logs when i click the button.
I will show u the entire onCreateView.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.server_settings, container, false);
final Button loginButton = (Button) view.findViewById(R.id.addServerSettingsbtn);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//were the magic won't happen.
}
});
return inflater.inflate(R.layout.server_settings, container,false);
}
Your problem (I believe) is that you're returning an instance of the inflated view that doesn't have the onClick handler assigned.
Try this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.server_settings, container, false);
final Button loginButton = (Button) view.findViewById(R.id.addServerSettingsbtn);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//were the magic won't happen.
}
});
return view;
}
you have to return view instead of inflate again server_setting. inflate returns everytime a new object
Related
I have DialogFragment within just some text
and I want open it in Fragment
It's Dialog Fragment first
public class subscribe_dialog_frag extends DialogFragment {
#Override
#Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);
}
}
and it's my fragment code
public class Main4Fragment extends Fragment {
private ImageButton btn_money;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main4, container, false);
btn_money = (ImageButton)view.findViewById(R.id.btn_momey);
btn_money.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment subscribe_dialog_frag = new subscribe_dialog_frag();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.btn_momey, subscribe_dialog_frag).commit();
}
});
return view;
}
}
I just open the dialogFragment.
possible?? or i do another way?
first use naming convention for classes/variables... Rename your dialog to:
public class SubscribeDialog extends DialogFragment {
#Override
#Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);
}
}
in your Main4Fragment call something like this:
btn_money.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SubscribeDialog subscribeDialog = SubscribeDialog();
subscribeDialog.show(getChildFragmentManager(),"SubscribeDialog");
}
});
public class Supplier_Home extends Fragment {
private static final String TAG = "Supplier_Home";
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.s_1home, null);
view = inflater.inflate(R.layout.supply__home__po, container, false);
Button pOrders=(Button)view.findViewById(R.id.pOrders);
pOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked login1.");
Intent intent = new Intent(getContext(), Login.class);
}
});
}
}
Do like, Create View object like this. Pass container object while inflating layout
View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.dashbord, container, false);
Button btn=(Button)view.findViewById(R.id.btn)
return view;
}
You are returning the complete view in the starting itself making the code below return unreachable.
You have given two layouts, If I am not wrong s_1home is layout of your activity where view pager is resting and supply__home__po is your fragment layout. Considering my assumptions are correct please change the code as follows
public class Supplier_Home extends Fragment {
private static final String TAG = "Supplier_Home";
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.supply__home__po, container, false);
Button pOrders=(Button) view.findViewById(R.id.pOrders);
pOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked login1.");
Intent intent = new Intent(getContext(), Login.class);
}
});
return view;
}
}
I have a settings fragment with 2 spinners in it. I'm not able to execute code written into the onItemSelected-method. There are no errors or exceptions.
I searched many similiar questions but none of the solutions worked for me. I have no idea what to try next.
public class SettingsFragment extends Fragment implements AdapterView.OnItemSelectedListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Getting the instance of Spinner and applying OnItemSelectedListener on it
View view = inflater.inflate(R.layout.fragment_settings, container, false);
Spinner spinnerBetrag = (Spinner) view.findViewById(R.id.spinnerBetrag);
Spinner spinnerDetails = (Spinner) view.findViewById(R.id.spinnerDetails);
spinnerBetrag.setOnItemSelectedListener(this);
spinnerDetails.setOnItemSelectedListener(this);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_settings, container, false);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//>>Problem: Code within this method is never beeing executed<<
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Change your onCreateView() method to return the view
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Getting the instance of Spinner and applying OnItemSelectedListener on it
View view = inflater.inflate(R.layout.fragment_settings, container, false);
Spinner spinnerBetrag = (Spinner) view.findViewById(R.id.spinnerBetrag);
Spinner spinnerDetails = (Spinner) view.findViewById(R.id.spinnerDetails);
spinnerBetrag.setOnItemSelectedListener(this);
spinnerDetails.setOnItemSelectedListener(this);
return view; // add this instead
}
Also for your onItemSelected() add
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.spinnerBetrag)
{
Log.d("Spinner 1","selected");
}
else if(spinner.getId() == R.id.spinnerDetails)
{
Log.d("Spinner 2","selected");
}
}
Change your return statement in onCreateView to
return view;
Your current return statement is inflating a whole new view
I'm new to Android and have a problem with the Navigation Drawer in Android Studio. I want to create dynamically a TextView in one of the Navigation Drawer's view. I cannot create a new TextView and I cannot search by id a TextView
public class StatoServer extends Fragment {
View myView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView tx = new TextView(this); //doesn't work this
tx.setText("text that change dynamically with a function");
container.addView(tx);
myView = inflater.inflate(statoserver, container, false);
return myView;
}
You don't assign any LayoutParams to your TextView, nor do you add it to the correct ViewGroup. It should be added to the View returned by onCreatView(). The following is tested and will work as an example of how to dynamically add a view:
public class OneFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.one_fragment, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getContext());
textView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
);
((ViewGroup)view).addView(textView);
textView.setText("Some Text");
super.onViewCreated(view, savedInstanceState);
}
}
Inside your onCreateView , here an example code on how you do it!
View v = new View(getActivity());
v = inflater.inflate(R.layout.page2, container, false);
View tv = v.findViewById(R.id.Ausgabe);
((TextView)tv).setText("TestText");
View pl = v.findViewById(R.id.PageLayout);
TextView Paper = new TextView(pl.getContext());
Paper.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Paper.setText("Inserted TestText");
((LinearLayout)pl).addView(Paper);
return v;
Here's my fragment class:
public class FirstFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
EditText newText = new EditText(getActivity());
newText.setText("This is a fragment");
newText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
I am trying to add newText to the fragment being created.
simply store the view that was returned from the inflater.inflate call , and add anything you want to it .
in the end , return the stored view .