Fragmentation Layout inflater issue - android

hello guys..
i want to add two layout in my fragmentation .one layout already inflated after that second layout inflate on first layout button click...well i tried to remove first layout on button click of first layout button and inflate second layout but my button click does not response any type of reaction..please help me
public static LinearLayout mLL_MyNotes,mLin_myNotes,mLL_UploadNotes;
public static ViewFlipper mVF_MyNotes,mViewUploadMyNotes;
public static View mViewMyNotes;
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
if(mContent.equalsIgnoreCase("My Notes")){
mViewMyNotes = inflater.inflate(R.layout.activity_myspace_mynotes_list, container, false);
mLin_myNotes=(LinearLayout)mViewMyNotes.findViewById(R.id.mainLin_MyNotes);
mVF_MyNotes = (ViewFlipper) mViewMyNotes.findViewById(R.id.vf_MySpace_MyNotes);
mbtn_Upload_MyNotes = (Button) mViewMyNotes.findViewById(R.id.btnUpload_MySpace_MyNotes);
mWV_MyNotes = (WebView) mViewMyNotes.findViewById(R.id.wv_MySpace_MyNotes);
mLV_MyNotes = (ListView) mViewMyNotes.findViewById(R.id.lv_MySpace_MyNotes);
mLV_MyNotes.setAdapter(new MySpace_MyNotesAdapter(getActivity(),R.layout.activity_myspace_mynotes_listitem));
mbtn_Upload_MyNotes.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater Newinflater = (LayoutInflater) getActivity().
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewUploadMyNotes==Newinflater.inflate(R.layout.activity_myspace_mynotes_uploades,null);
mLL_UploadNotes = (LinearLayout) mViewUploadMyNotes.findViewById(R.id.llUpload_MyNotes);
((ViewGroup) mLin_myNotes.getParent()).removeView(mViewUploadMyNotes);
mLin_myNotes.addView(mViewUploadMyNotes);
}
});
return mViewMyNotes;

Related

Add editText to fragment

I'm trying to add editText and textView to a fragment. The code to generate the editText and TExtView is correct because I've used it before, but in this case nothing appears. Any ideas?
Fragment code:
public class MarcadoresFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragment_marcadores, container, false);
return myFragmentView;
}
}
Add the editText:
public void createEditText(int size){
Log.d("det", String.valueOf(size));
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.fragment_marcadores, null);
LinearLayout layout = (LinearLayout) dialogView.findViewById(R.id.marcadoresFrag); // this is whatever view you want to add them to
EditText editText;
TextView textView;
if(layout!=null)
for(int i=0;i<size;i++){
textView = new TextView(dialogView.getContext());
textView.setText("Descrição do marcador " + String.valueOf(i + 1));
textView.layout(0, 5, 0, 5);
// add to the view
layout.addView(textView);
editText = new EditText(dialogView.getContext());
editText.setId(i);
editText.setHint("Ex: Bebedouro ndsfhjdsjvdsbjvjhvjdfjbvfjfvjklbjklfv" + i + 1);
Log.d("det", "jsdgbiusdbgksjd");
allEds.add(editText);
// add to the view
layout.addView(editText);
}
}

adding onclicklistener to linearlayout in fragment

I have a fragment which I am using as a navigational menu between my activities.
Each of the icons is in a seperate linearlayout with its own ID. I'm trying to implement an onclick listener in the fragment for each linearlayout so that when it is clicked the appropriate activity is opened and the background of the linear layout as well as the image in the imageview are changed to signify the click. However when I click on the linearlayout nothing happens. I already set the clickable attribute to true so that is not the issue. This is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_menu, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
LinearLayout betslayout = (LinearLayout) this.getView().findViewById(R.id.betnowlayout);
betslayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout layout = (LinearLayout) v;
layout.setBackgroundResource(R.drawable.selected);
ImageView image = (ImageView) layout.findViewById(R.id.betnowimageview);
image.setImageResource(R.drawable.footballsell);
LinearLayout parent = (LinearLayout) v.getParent();
LinearLayout mybetslayout = (LinearLayout) parent.findViewById(R.id.viewbetslayout);
LinearLayout rankingslayout = (LinearLayout) parent.findViewById(R.id.rankingslayout);
LinearLayout coinfrenzylayout = (LinearLayout) parent.findViewById(R.id.coinfrenzylayout);
LinearLayout shoplayout = (LinearLayout) parent.findViewById(R.id.shoplayout);
mybetslayout.setBackgroundResource(R.drawable.notselected);
rankingslayout.setBackgroundResource(R.drawable.notselected);
coinfrenzylayout.setBackgroundResource(R.drawable.notselected);
shoplayout.setBackgroundResource(R.drawable.notselected);
ImageView mybetsimage = (ImageView) mybetslayout.findViewById(R.id.betnowimageview);
ImageView rankingsimage = (ImageView) rankingslayout.findViewById(R.id.rankingsimageview);
ImageView coinfrenzyimage = (ImageView) coinfrenzylayout.findViewById(R.id.coinfrenzyimageview);
ImageView shopimage = (ImageView) shoplayout.findViewById(R.id.shopimageview);
mybetsimage.setImageResource(R.drawable.chipp);
rankingsimage.setImageResource(R.drawable.medal);
coinfrenzyimage.setImageResource(R.drawable.clover);
shopimage.setImageResource(R.drawable.moneybag);
Intent i = new Intent(getActivity(),AllGameslistActivity.class);
startActivity(i);
getActivity().finish();
}
});
}
First Off All You Should Create Custom Buttons. See this.
and set onClickListener for the Icons.
The problem was in this line of code
return inflater.inflate(R.layout.fragment_menu, container, false);
It should instead be : return myfragment;
This is the proper way to do this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View myfragment = inflater.inflate(R.layout.fragment_menu, container, false);
// TODO: Rename method, update argument and hook method into UI event
LinearLayout betslayout = (LinearLayout) myfragment.findViewById(R.id.betnowlayout);
betslayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("isitworkign", "yesyes");
LinearLayout layout = (LinearLayout) v;
layout.setBackgroundResource(R.drawable.selected);
ImageView image = (ImageView) layout.findViewById(R.id.betnowimageview);
image.setImageResource(R.drawable.footballsell);
LinearLayout parent = (LinearLayout) v.getParent();
LinearLayout mybetslayout = (LinearLayout) parent.findViewById(R.id.viewbetslayout);
LinearLayout rankingslayout = (LinearLayout) parent.findViewById(R.id.rankingslayout);
LinearLayout coinfrenzylayout = (LinearLayout) parent.findViewById(R.id.coinfrenzylayout);
LinearLayout shoplayout = (LinearLayout) parent.findViewById(R.id.shoplayout);
mybetslayout.setBackgroundResource(R.drawable.notselected);
rankingslayout.setBackgroundResource(R.drawable.notselected);
coinfrenzylayout.setBackgroundResource(R.drawable.notselected);
shoplayout.setBackgroundResource(R.drawable.notselected);
ImageView mybetsimage = (ImageView) parent.findViewById(R.id.viewbetsimageview);
ImageView rankingsimage = (ImageView) parent.findViewById(R.id.rankingsimageview);
ImageView coinfrenzyimage = (ImageView) parent.findViewById(R.id.coinfrenzyimageview);
ImageView shopimage = (ImageView) parent.findViewById(R.id.shopimageview);
mybetsimage.setImageResource(R.drawable.chipp);
rankingsimage.setImageResource(R.drawable.medal);
coinfrenzyimage.setImageResource(R.drawable.clover);
shopimage.setImageResource(R.drawable.moneybag);
Intent i = new Intent(getActivity(), AllGameslistActivity.class);
startActivity(i);
getActivity().finish();
}
});
return myfragment;
}

android PopupWindow from a fragment

I have an app that uses fragments, its working ok,
but I have now to implement some pop ups when a button gets tapped,
I am following this tutorial "Example of using PopupWindow"
But I get this errors:
Multiple markers at this line
- LAYOUT_INFLATER_SERVICE cannot be resolved to a variable
- The method getBaseContext() is undefined for the type new
View.OnClickListener(){}
here my .java
public class Tab2HeadH1 extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_2_head_buttons, container,false);
//Buttons
Button buttonNose = (Button) view.findViewById(R.id.button_pop_nose);
buttonNose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//aqui tus tareas,,
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); //ERRORS HERE!!
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
});
Button buttonEye = (Button) view.findViewById(R.id.button_pop_eye);
buttonEye.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
// onLoginClicked(v);
Toast.makeText(getActivity(), "ss9 eye",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TabActivity)getActivity()).setHeader("TAPING APPLICATION");
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
}
}
}
SO, how can I fix this problem??, to show my pop up from tapped button in my fragment??
call
LayoutInflater layoutInflater = (LayoutInflater)**getActivity()**.getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
Atleast it won't show error onBaseContext() as now it take from the associated activity
EDIT
Try this,
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View myFragmentView = inflater.inflate(R.layout.yourlayout, container,false);
//inside button onclick write the code given below
View popupView = inflater.inflate(R.layout.address, null);
}

android what is missing to show my pop up

I have an app with fragments working ok,
now I need to implement some pop up views,
Im following this tutorial Example of using PopupWindow
all seems fine, but The pop up is not showing yet, as I have a doubt on how to show the "popupWindow"
my .java:
public class Tab2HeadH1 extends Fragment implements OnClickListener {
//testeo popero
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_2_head_buttons, container,false);
//Buttons
Button buttonNose = (Button) view.findViewById(R.id.button_pop_nose);
buttonNose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//aqui tus tareas,,
//LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); //old errors
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
});
Button buttonEye = (Button) view.findViewById(R.id.button_pop_eye);
buttonEye.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
// onLoginClicked(v);
Toast.makeText(getActivity(), "ss9 eye",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TabActivity)getActivity()).setHeader("TAPING APPLICATION");
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
}
}
}
So what is missing to show my pop up?
thanks a lot!
If you want to generate Custom pop up window you can do this use AlertDialog.Builder
private AlertDialog.Builder scheduleBuilder;
private AlertDialog dialog_Creater;
LayoutInflater myLayoutInflater = getLayoutInflater();
View v = myLayoutInflater.inflate(R.layout.schedule_creator, null); //Replace your layout R.layout.youLayout
scheduleBuilder = new AlertDialog.Builder(v.getContext());
scheduleBuilder.setTitle("Edit your Schedule Memo");
scheduleBuilder.setView(v); //set view
dialog_Creater = scheduleBuilder.create();
dialog_Creater.show();
While you setup the Popup fine, you aren't actually calling any method to show it anywhere. Call whichever one of the following methods suits your needs to open it:
showAtLocation()
showAsDropDown()
showAsDropDown() (Different from #2)
try this:-
PopupWindow pw;
View layout = inflater.inflate(R.layout.popnumber,
(ViewGroup) findViewById(R.id.lytpopuppoppop));
// your popup layout and id of root layout
pw = new PopupWindow(layout, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, true);
// your main layout where you display popup
pw.showAtLocation(findViewById(R.id.lytboard), Gravity.CENTER,
0, 0);

Inflate a layout with ImageButton inside Fragment programmatically

I am new on android development, and i was currently creating an application using tabhost and a TabActivity to switch between different activities. This application was working fine, but I recently discover that TabActivity are deprecated and I should use fragment instead.
So, I follow this great tutorial explaining how to do so :
http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
Most of it works fine, but there is my problem some of my old activities which are now fragments were inflating ImageButton using this kind of methods :
private void displayLevelButtons() {
LayoutInflater lf = getLayoutInflater();
btns = lf.inflate(R.layout.mallmapactivity_level_buttons, null, false);
mBtnLevelDown = (ImageButton) btns.findViewById(R.id.btn_level_down);
mBtnLevelUp = (ImageButton) btns.findViewById(R.id.btn_level_up);
mBtnLevelDown.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelDown();
updateLevelButtons();
}
});
mBtnLevelUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelUp();
updateLevelButtons();
}
});
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;
addContentView(btns,params);
}
In order to be able to compile this method inside a fragment, I changed:
LayoutInflater lf = getLayoutInflater();
to
LayoutInflater lf = getActivity().getLayoutInflater();
and
addContentView(btns,params);
to
getActivity().addContentView(btns,params);
But using my method displayLevelButtons() like this make my buttons appears in the right bottom of my general view, not in the right bottom of my fragment.
I also tried to specify a root when inflating my layout with image buttons, my all fragment class looking like this :
public class MyFragment extends Fragment {
private View btns;
private ImageButton mBtnLevelUp;
private ImageButton mBtnLevelDown;
private ViewGroup myViewGroup;
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
myViewGroup = container;
v = inflater.inflate(R.layout.mallmaptile, container, false);
return v;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//Do some stuff and then :
displayLevelButtons();
}
private void displayLevelButtons() {
LayoutInflater lf = getActivity().getLayoutInflater();
btns = lf.inflate(R.layout.mallmapactivity_level_buttons, myViewGroup, false);
mBtnLevelDown = (ImageButton) btns.findViewById(R.id.btn_level_down);
mBtnLevelUp = (ImageButton) btns.findViewById(R.id.btn_level_up);
mBtnLevelDown.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelDown();
updateLevelButtons();
}
});
mBtnLevelUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelUp();
updateLevelButtons();
}
});
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;
getActivity().addContentView(btns,params);
}
Doing so my buttons are still display in the bottom of my general view not in the bottom of my fragment, and if I try to change the attachToRoot boolean value to true :
btns = lf.inflate(R.layout.mallmapactivity_level_buttons, myViewGroup, true);
I get the error : java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I am sure, I missed something important but I don't what. Any suggestions would be really appreciated.
Thanks in advance,
If you are facing problem in inflating layout then you can use following code:
View headerView = View.inflate(this, R.layout.layout_name, null);
this will give you complete parent layout then you can use that layout to fetch whatever view you want or you can get layout param and then set layout param to any views (In your case button I think).
I had same problem and find my answer in this link:
problem with inflater
you can do it without any dependency
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.view_with_merge_tag, this);

Categories

Resources