Android linearlayout error - android

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

Related

Can images in droid be displayed without the use of imageview or the xml file

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.

Unable to get ImageView from xml in Android

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.

Creating activity with layout defined in runtime in android application

Usually the activity has a predefined layout which is described in the xml file. What if I know the exact number and types of UI elements only during the runtime?(for example, I need to display as many TextBoxes as user defined) Is it possible to create an activity with a layout defined during runtime and if it is, how?
First set an identifier to a view, where you want to insert your views at runtime :
<LinearLayout
android:id="#+id/linear_layout"
... >
Then you can add child views to this LinearLayout programatically, whenever you want :
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
linearLayout.removeAllViews();
// Add a TextView (it could be any kind of View)
TextView textView = new TextView(this);
textView.setText("...");
linearLayout.addView(textView);
setContentView(layout);
This layout you can define during runtime

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

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

Accessing View inside the LinearLayout with code

I have a linearlayout how can i access view inside this layout with the help of
programming.
Sure, say if you have a LineraLayout linearLayout, and in its xml you have a TextView like
<LinearLayout [...]>
<TextView android:id="#+id/textView" [...] />
</LinearLayout>
then you can access that TextView by
final TextView txt = (TextView)linearLayout.findViewById(R.id.textView);
Here you have your LinearLayout defined in an xml resource file.
You must assign an id attribute to your TextView for that to be accessible from your code directly. For this purpose stands there the android:id="#+id/textView".
In the xml file you will need to give the view an id..
android:id="#+id/someRandomID"
Then within your main java file you add this:
LinearLayout layout = (LinearLayout)findViewById(R.id.someRandomID);
(([TYPE]) findViewById(R.id.[NAME]))
For example, setting the text on a button:
((Button) findViewById(R.id.my_button)).setText("New text");
Assuming you have a linearlayout with id "linear1" and within that layout you have a ImageView with id "image1", you can do the following in your onCreate method in your activity class:
public void onCreate(Bundle bundle)
{
setContentView(R.layout.linear1);
ImageView image = (ImageView) findViewById(R.id.image1);
}
This is a very simple example, assuming that you are setting linear1 as the main layout of your activity.

Categories

Resources