How to access Button inside "include" layout - android

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

Related

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

In Android, how to I edit views in a layout, when my setContentView is not in that layout?

For example, I might be in R.Layout.activity_main ,
but when I onCreate my activity, I want to change a TextView in another layout, so I would create a TextView object and findViewById from that layout, and then change it.
But this will not work unless I set my contentview to that layout, but is it possible to be able to set the TextView in that layout, without having to setContentView to that layout?
This does not work in all situations, but you could try a LayoutInflator.
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.your_layout, null);
Then to find views in that layout, just do...
layout.findViewById(R.id.your_view);
It is better to store a reference of desired view in a class as a variable when you create it's parent layout.Then change it's property when you need.
You can see more details in these two questions:
Android: How to declare global variables?
Android, how to access Activity UI from my class?

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

How to inflate another view from another view in Android

I know in my onCreate() I can inflate a view from XML by something like:
loadingScreen = (RelativeLayout) findViewById(R.id.loadingScreen);
But how could I do this from another view? Im trying to call up a loading screen by setting its visibility from GONE to VISIBLE but cant seem to figure out how to do this from my glSurfaceView
If you want to inflate a layout the code looks like this:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(context);
View itemView = inflater.inflate(R.layout.layout_details, myRoot);
Here you first create a new LinearLayout an then inflate the layout with id R.layout.layout_details into it. The inflate method then returns the myRoot view.
Here is a tutorial about the LayoutInflater:
Layout resources in Android
Thats actually not inflating. Inflating is the process that parses a XML layout file and creates a structure of View and ViewGroup class instances out of it (setContentView() does this for you in the background for example).
What you do is getting a reference to a view in code that you have defined in your XML layout file. To change the visibility of your GLSurfaceView you have to reference it like you did above. But remember that the View (GLSurfaceView in this case) has to be defined in your layout file.
After referencing you have to call GLSurfaceView.setVisibility() to change it's visibility.
Here's an example:
GLSurfaceView glsurface = (GLSurfaceView) findViewById(R.id.myglsurfaceid);
glsurface.setVisibility(View.VISIBLE);
Of course you can use View.INVISIBLE or View.GONE either, depending on what you want to do.
If you reference a layout (such as a RelativeLayout), you may find children of this layout with the findViewById() of your RelativeLayout instance:
RelativeLayour rl = (RelativeLayout) findViewById(R.id.mylayout);
(Button) mybutton = (Button) rl.findViewById(R.id.mybutton);
But thats usually not neccessary (at least when you just started with Android) because the activities findViewById() finds all Views that are displayed, even in sublayouts. You only have to use it if you have duplicate ids in your ui structure (tbh I never had that case yet) and want to specifiy where to look for your particular View.
You can't get a reference to a View that's doesn't exists in your current Layout, or your current View, (your current Activity content) , but you can create a new View from another XML layout, using LayoutInflater from current Activity.
you can add to you current Activity content, a new View, that's what you mentioned as " loading screen ", even by showing it as a Dialog or by creating View and then add it to root layout in your Activity
I hope I helped you
If I correctly understood what you wanna do:
Supposing you have a glSurfaceView object and you wanna grab a view that's inside that one.
You'll do just the same thing you did for you normal view. Let's say a button:
Button button = (Button) glSurfaceView.findViewById(R.id.buttonid);
If you meant something different let me know in the comments.
EDIT: And then you can just set the button's visibility:
button.setVisibility(Button.GONE)

How do I set the behavior for an inflated view?

I have an audio player toolbar activity that has a corresponding layout file.
I need this player to show up at the bottom of another activity. I use a ViewStub and inflate the audio toolbar's layout file in the stub.
How do I access the buttons, etc on this inflated view and how do I set their behavior?
The docs on ViewStub did not mention anything about this (or maybe I totally overlooked something).
I may be doing something fundamentally wrong here so I would appreciate any help I can get :)
Once you've inflated a view, you call findViewById on the view with the ID. e.g.
View v = ...;
TextView textView = (TextView) v.findViewById(R.id.textview);
if in your xml you had a declaration of a TextView with id "#+id/textview"

Categories

Resources