I have little problem
I write code in java file where under oncreate() method i set the contentview as
setContentView(R.layout.main)
but under this i need to access a imageview from another xml file name listview
like this
ImageView img = (ImageView)findViewbyid(R.id.img)
but this img id not present in main.xml it is present in listview.xml then how i can access it.
please give solution ..... thank you
Inflate the liveview.xml and then find view with inflated layout.
Related
I want solution of following:
I have create xml file in data/data/com.example.file/files/abc.xml so during running of app how I get this abc.xml in res/layout and should display on screen.
Thanks.
In general, the XML vocabulary for declaring UI elements closely
follows the structure and naming of the classes and methods, where
element names correspond to class names and attribute names correspond
to methods.
Courtesy goes to #Mike M : You cant create this .XML files used for layouts are pre-compiled .
I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
the RelativeLayout item will be the container for your child view.
Your question needs to be a bit more specific, but here's a general answer:
If you are using fragments then you should re-create your fragment and then use:
inflater.inflate(R.layout.abc, container, false);
If you are using activities then you should use:
setOnContentView(abc);
I was wondering if anyone knew if it where possible to dynamically display an image through code pulling it straight from the res folder in eclipse for a droid app without setting up an imageview in the xml file.
I'm still fairly new to droid and programming in general and so far all images I have ever displayed I have used imageview in xml to do it. Curious to know if it were possible to leave xml out of it, and any suggestions how.
Thanks!
LinearLayout linear=new LinearLayout(getApplicationContext());
ImageView img=new ImageView(getApplicationContext());
img.setBackgroundResource(R.drawable.ic_launcher);
linear.addView(img);
setContentView(linear);
you can inflate views from Java code using their Java object equivalents. For ImageView:
ImageView imageView = new ImageView(Context);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.image_drawable);
addChildView(imageView); // add it to the screen. if in a fragment, need getView().addChildView(imageView);
1.In xml layout, add id to default layout as android:id="#+id/layout"
2.Now in java file :
RelativeLayout layout =(RelativeLayout)findViewById(R.id.layout); //xml default layout
LinearLayout linear1 = new LinearLayout(getApplicationContext()); //dynamically created layout
linear1.setBackgroundResource(R.drawable.ic_launcher); //adding image to dynamic layout
layout.addView(linear1); //adding dynamically created layout to default xml layout
Here I am using LinearLayout for creating dynamic layout you can use anyone.
I have a LinearLayout defined in a file called intro_step1_activity.xml. I would like to load/reference this file in another xml file so that I do not need to retype the code in the other xml file. Is this possible and if so how can it be done?
At runtime you can use an inflater, for instance:
Inflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.intro_step1_activity, null);
then you can add this view to the current view hierarchy. At compile time you can use the include tag for the xml
<include layout="#layout/intro_step1_activity"/>
Load an existing xml layout file in another xml layout file
to use xml file in other xml file use <include />
for your intro_step1_activity.xml use code
<include layout="intro_step1_activity.xml"/>
Yes, it's possible using <merge> or <include>.
You can read more here and here
first give an id to that LinearLayout and then define a LinearLayout in java and reference it using the findViewById() method.
then use the LinearLayout object you just created to call the addView() method.
if you want an example, comment.
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);
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));