R.id parent of R.layout : how to do this? - android

how can an id be a parent of a layout folder?
in this code, it seems that R.id.layout_root is a parent of R.layout.custom_dialog, how can i do this in my folder tree?
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
Thanks

I don't know if I understood well your question. Anyway, the inflate method that you are using simply inflates that custom_dialog layout as child of en existent VievGroup. You don't have to do anything in your folders, there is no relationships between that code and the directory hierarchy.
These are the 2 parameters that the method gets (from the doc):
resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root Optional view to be the parent of the generated hierarchy.
This small example maybe clarifies your doubts. This line of code:
LinearLayout lLayout = inflater.inflate(R.layout.buttons, R.id.layout1);
is equivalent to:
Button b = (Button) inflater.inflate(R.layout.buttons, null);
LinearLayout lLayout = (LinearLayout)findViewById(R.id.layout1);
lLayout.addView(b);

Related

Android find a view after it is inflated

I am creating some views programmatically using ViewStubs, as shown below.
RelativeLayout allCommissionsContainer = (RelativeLayout) mBaseLayout.findViewById(R.id.allCommissionsContainer);
ViewStub viewStubCommission = new ViewStub(mActivity);
viewStubCommission.setLayoutResource(R.layout.viewstub_commission_container);
allCommissionsContainer.addView(viewStubCommission);
viewStubCommission.inflate();
But how do I find that inflated view (a RelativeLayout) later in the code? If I use viewStubCommission.setTag("MyTag"); won't it be applied to the actual ViewStub and not the inflated View? I tried this but it gave me a null pointer exception.
I need something like allCommissionsContainer.findViewById(R.id.someId) or allCommissionsContainer.findViewWithTag("MyTag").
After some testing I realized I can use
RelativeLayout myRelativeLayout = (RelativeLayout) viewStubCommission.inflate();
and then set a tag or id
myRelativeLayout.setTag("MyTag");

Show custom view in all activities

I want to show a view that should be shown in all activities. I don't know how to inherit views in android. What i did is below, its showing the view in first activity but not in all activities. This pease of code is form my BaseActivity, please help
LayoutInflater inflater = getLayoutInflater();
View child = inflater.inflate(R.layout.custom_error, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
addContentView(child, params);
You could get an Android specific View in the Activity. For example the following code below will add a TextView to the Activity's content area.
TextView tvSample = new TextView(this);
tvSample.setText("Hello!");
((ViewGroup) hostActivity.findViewById(android.R.id.content)).addView(this);
Whereby hostActivity is your current Activity and android.R.id.content is a specific element (the content area, not including the ActionBar).
Alternatively, as already stated, make use of <merge> and <include> tags in your layout XMLs.
you can do this with two solution
for programmatically
1)After adding child view to you parent View need to call setContentView(parentView) and pass you parent layout to it.
and With XMl
2) You can use include tag. follow this link will help you.
http://developer.android.com/training/improving-layouts/reusing-layouts.html
Have you tried 'include' tag of xml? It will do the job.
<include
android:id="#+id/container_header_lyt"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_above=...
android:layout_toLeftOf=...
layout="#layout/header_logo_lyt" //Name of the xml layout file you want to include
/>
In the layout/xxxx use the name of your layout file that should be repeated.
After use the above code in your xml file like any other widget.
When you want to show it:
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
View.inflate(this, R.layout.overlay_layout, rootLayout);
Then when you want to remove it:
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
rootLayout.removeViewAt(rootLayout.getChildCount()-1);
That's a concise solution, you should remove the View by giving the RelativeLayout an id in the XML file, then remove by: rootLayout.removeView(findViewById(R.id.the_id_of_the_relative_layout));.
Answer by nmw

Android including an xml layout to activity

I've an activity.java file in which my setContentView(R.layout.x); Now,I've an y.xml in which I've an Linear Layout,I've to attach an onclick() method to my view.
Attaching onclick() has to be in my activity.java file, How do I include y.xml.
I tried this,
1. layout = (LinearLayout) findViewById(R.layout.y);
eView = (EditText)layout. findViewById(R.id.editview);
2. eView = (EditText)findViewById(R.id.editview);
but both gives my null pointer exception, How do I include my editText
Update
final LayoutInflater lyInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
showLinearLayout = (LinearLayout) lyInflater.inflate(R.layout.y, null);
showView = (EditView) showLinearLayout.findViewById(R.id.edittext);
You can use inflation as shown below:
final LayoutInflater lyInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout yLayout = (LinearLayout) lyInflater .inflate(
R.layout.y, null);
eView = (EditText)yLayout.findViewById(R.id.editview);
So you won't get exception anymore. Hope it helps.
LayoutInflater is used to instantiate layout XML file into its corresponding View objects.in other words, it takes as input a XML file and builds the View objects from it.
in your scenario, you have to use LayoutInflater. read this article.
Ram.
If you want to include y xml file into your x xml file then follw this steps.
I am assuming that you want to include Linear Layout into your Activity on click of onclick() method of the button or whatever, then add the Linear Layout into your x xml file and add the android:visibility="gone" so at begin you can not show the linearlayout.
<LinearLayout
android:id="#+id/history_value_body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" > <<<<<<<<<<< HERE
----------------------------
-----------------------------
</LinearLayout>
Now, From the java class make it visible when needed, in your case into onclick method.
Like...
linear.setVisibility(View.VISIBLE); // linear is the object of your Linearlayout
If any prob then ask me.
Good Luck.
If I understand the question correctly, your Activity uses x.xml, and you also want to include another layout that is defined in y.xml.
You can do so using the <merge> or <include> tags, as described in the documentation.
Alternately, you can use a ViewStub to conditionally inflate another layout in a given place in a layout. For example, you can include a ViewStub tag in x.xml, and inflate y.xml in the same spot in the view hierarchy. Then, you may attach any click listeners you need (by using findViewById()).
You can use addView method of ViewGroup.
addView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

Android linearlayout error

I am trying to call LinearLayout by its id. When I am trying to do so I am getting NoSuchFieldError.
LinearLayout l1 = (LinearLayout)findViewById(R.id.content2);
setContentView(l1);
The way you are using is not correct.
setContentView(R.layout.main) must set with any layout say main.xml for your case.
and now the main layout is having the LinearLayout with id content2.
Also if you want to use setContentView directly create a dynamic linear layout i.e not in xml.
Linearlayout l1 = new LinearLayout(this);
//Set Layout params and bla bla... as per your need..
now setContentView(l1);
You can find that id, if and only if you have given that id in the xml resource file which you are inflating. Without loading the xml resource file you cannot find the id. YOu can load the xml resource file using the setContentView(R.layout.main); in the Activity onCreate(). The code for finding id will be like LinearLayout l = findViewById(R.id.content);

How to access Button inside "include" layout

Refer to the doc: http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html
I have a button inside the included layout, how can I access the button? I don't know the id! How can I define the OnClickListener...?
Please help...
The id you have with the include tag is assigned to the root View of the included layout.
First get a reference to that View using findViewByid. Then you can call findViewById on that specific View to get a reference to a View inside the layout. So:
View myLayout = findViewById( R.id.cell1 ); // root View id from that link
View myView = myLayout.findViewById( R.id.someinnerview ); // id of a view contained in the included file
All you need is the id of the parent view:
In this example, the LinearLayout is the parent of the TextView I want from the included View.
I can now do this:
View view = findViewById(R.id.include_title_layout);
TextView titleTV = (TextView) view.findViewById(R.id.newsTitleTextView);
titleTV.setText("Whatever");
You do know the id of the include. You can get that complete element, and then get one of its children starting from there.
Actually include tags include all the elements in your root layout. So you can always access them using findViewById on your Activity.
Suppose you have a alayout.xml in which there is a include layout. Now in one of your activity A,inside onCreate you declared setContentView(R.layout.alayout) Now inside your include layout you might have a button with id myBtn. You can access that inside onCreate in the same way you could access it if it were in main layout: findViewById(R.id.myBtn)
i think your mean is NavigationView. you can access to inner layout by this way:
NavigationView ngv= (NavigationView) findViewById(R.id.navigation_id);
View innerview = ngv.getHeaderView(0);
TextView user_view= (TextView)innerview.findViewById(R.id.nav_name); //any you need
user_view.setText(user);
works for me in java
<include
android:id="#+id/layout_infoProfile"
layout="#layout/user_info_doctor_profile_layout"
/>
preferences = mContext.getSharedPreferences(MyConstant.MY_SHARED_PREF,Context.MODE_PRIVATE);
View view = mView.findViewById(R.id.layout_infoProfile);
((TextView)view.findViewById(R.id.tv_name)).setText(preferences.getString(MyConstant.ROLE,MyConstant.NO_VALUE));

Categories

Resources