Inflate a layout with ImageButton inside Fragment programmatically - android

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);

Related

Android - Getting Objects in Multiple RootView

public class OccuMechanical extends android.support.v4.app.Fragment {
private View v_schedule;
private LinearLayout layout_schedule;
private ImageButton iv_add_schedule;
private int i = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_occu_mechanical, container, false);
layout_schedule = (LinearLayout) v.findViewById(R.id.layout_mech_schedule);
iv_add_schedule = (ImageButton) v.findViewById(R.id.iv_add_schedule);
iv_add_schedule.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i++;
v_schedule = LayoutInflater.from(getActivity()).inflate(R.layout.layout_mech_schedule, null);
layout_schedule.addView(v_schedule);
EditText et = (EditText) layout_schedule.getRootView().findViewById(R.id.layout_description);
et.setText("Test: " + String.valueOf(i));
}
});
return v;
}
}
idk if my term is right in the title but here's what im doing...
when I click the 'Add more equipment' it adds layout to my app so
I want to get all the EditText inside the layout i added in the RootView,
I tried setting the text to test it but the first editText of the first rootView is the only one updating. All the codes in my fragment listed above. attached image is the result of this code.
image result
I've done this task by using *RecyclerView as #hjchin suggested. Thank You!
RecyclerView Tutorial # Developer.Android

onClickListener and setOnTouchListener doesn't work in React Native

In my Android Native code I have written an OnClick Listener for a PopUp I need to display in my onCreate().
I want to write this in native rather than JS.
public class MainActivity extends ReactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.pointzipreviewtoolbar,null);
PopupWindow popupWindow = new PopupWindow(
popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup rootLayout = (ViewGroup) findViewById(android.R.id.content);
popupView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Popup was Clicked", "I clicked here");
}
});
}
}
The popup shows, however the onClickListner doesn't get triggered and also the setTouchListener doesn't work either. Am I missing some files??
You have set the root of the inflated popupView layout to null, which means it might not have correct layoutparams which may affect a touch interaction.
Try rearranging it so you can do this:
ViewGroup rootLayout = (ViewGroup) findViewById(android.R.id.content);
View popupView = layoutInflater.inflate(R.layout.pointzipreviewtoolbar, rootLayout, false);
That way the popupView will inherit the layoutparams from rootLayout, but will still not be attached to that view as the final argument (attachToRoot) is false. See if the onClickListener responds after that.
You've also not used setContentView(View view) in onCreate, though maybe that's normal when you inherit from ReactActivity, not sure.
I managed to solve this issue by setting the pointzipreviewtoolbar layout elements to an onClick. Which in my case, inside the pointzipreviewtoolbar had an ImageButton called preViewCapture. I then set this to on OnClickListner and it got triggered.
public class MainActivity extends ReactActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater =
(LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.pointzipreviewtoolbar,null);
PopupWindow popupWindow = new PopupWindow(
popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
ViewGroup rootLayout = (ViewGroup) findViewById(android.R.id.content);
ImageButton image = (ImageButton) popupView.findViewById(R.id.preViewCapture);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.e("Popup was Clicked", "I clicked here");
}
});
}
}

Showing a popup window from Fragment

I want to show a popup window from my PlaceHodler class extending Fragment when the button is clicked. For a test, I wrote this code which really works, but I guess it's ridiculous (using Button object as a parent view and so on... I couldn't find another way to make it work...). Please look at this code and advice me how to improve it. Please don't judge me because I'm a very beginner in programming.
My code:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final Button button1 = (Button)rootView.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Log.i("ilog", "onClick() works");
PopupWindow pw = new PopupWindow(getActivity());
TextView tv = new TextView(getActivity());
LayoutParams linearparams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(linearparams1);
tv.setText("Testing");
pw.setContentView(tv);
pw.setWidth(400);
pw.setHeight(180);
pw.showAtLocation(button1, Gravity.CENTER_HORIZONTAL, 25, 25);
pw.update();
}
});
return rootView;
}
}
popwindow in fragment and activity almost the same except they way you get contrext , in activity this , fragment getActivity()
here is the code to create popWindow
View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_layout, null);
final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
// define your view here that found in popup_layout
// for example let consider you have a button
Button btn = (Button) popupView.findViewById(R.id.button);
// finally show up your popwindow
popupWindow.showAsDropDown(popupView, 0, 0);
reference PopupWindow

Fragmentation Layout inflater issue

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;

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);

Categories

Resources