I have created an LinearLayout object in my .java file and tried to connect it with my .xml layout by using the following code,but it is not responding. Can you please explain to me why and what errors are in this code? I have named the id of the LinearLayout of .xml file as "root".
public class MainActivity extends Activity {
LinearLayout l;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImageView HelloWorldImageView = new ImageView(this);
TextView t=new TextView(this);
t.setText("hello");
HelloWorldImageView.setImageResource(R.drawable.ic_launcher);
l=new LinearLayout(this);
l=(LinearLayout)findViewById(R.id.root);
l.addView(t);
l.addView(HelloWorldImageView);
setContentView(l);
}
}
Do it like this-
setContentView(R.layout.yourxml);
ImageView HelloWorldImageView = new ImageView(this);
TextView t=new TextView(this);
t.setText("hello");
HelloWorldImageView.setImageResource(R.drawable.ic_launcher);
l=(LinearLayout)findViewById(R.id.root);
l.addView(t);
l.addView(HelloWorldImageView);
You have to set that xml file in the setContentView() which has the root linear layout in it.
First off- you don't need the new LinearLayout line. The findViewById() will return an instantiated version. Same for the the other newed Views.
Secondly- you want to do a setContentView() before making any calls to findViewById(). This way you set the layout (by passing it a layout id), Android will instantiate all the Views in your layout, and you can get references to them via findViewById().
Related
I have a dynamically built Layout in Main Activity. Now I want to send this complete Layout to another Activity. How can I send ?
#Milad gh:
Also can make a layout.xml for temp and use it as interface:
my_root = (LinearLayout)findViewById(R.id.my_root);
LinearLayout A = new LinearLayout(this); // is your layout than you want to create
A.setOrientation(LinearLayout.HORIZONTAL);
A.addView(view1);
A.addView(view2);
A.addView(view3);
my_root.addView(A)
Then you can use my_root in other activity or make public static "A" layout and use it to another activity su as that Milad gh said
you can define your layout public static, and get it in other classes or activities,
for example :
public class Main extends Activity {
public static LinearLayout my_root;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
my_root = (LinearLayout) findViewById(R.id.my_root);
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
A.addView(view1);
A.addView(view2);
A.addView(view3);
my_root.addView(A);}
}
and in another activity:
LinearLayout L2 = new LinearLayout(this);
L2.addView(Main.my_root);
I'm completely new to Android and am trying to add a simple button to my view, but my view is completely blank. Here is my setup:
EDIT: I have now changed my code to write this programmatically since I'm used to that with iOS.
I'm now getting the following error:
Error: Attempt to invoke virtual method 'void android.widget.LinearLayout.setOrientation(int)' on a null object reference
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
LinearLayout linearLayout;
public Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
// Create main view
linearLayout = (LinearLayout) findViewById(R.id.linearLayout1);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// Create button
myButton = new Button(this);
myButton.setText("Fire Call");
// Add button to view
linearLayout.addView(myButton);
}
}
You need to setContentView which takes a View or a layout resource id.
Also, you can only call findViewById if you provide a layout resource id, and call setContentView with it. Alternatively, you could inflate it and call view.findViewById on the resulting inflated view.
Here's your code with some changes that should hopefully fix your new issue:
public class MainActivity extends Activity {
LinearLayout linearLayout;
public Button myButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
// Create main view
linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// Create button
myButton = new Button(this);
myButton.setText("Fire Call");
// Add button to view
linearLayout.addView(myButton);
setContentView(linearLayout);
}
}
I cannot pinpoint the error but maybe you can.
Try checking after every line you type and see if the button appears. Also check if the problem is with button only or does it extend to other layouts as well.
Just type and see what happens.
Follow it up with dimensions.
Don not forget to save after each step. Usually all such problems can be solved by detailed debugging.
If none of the layout appears, try and restart the software.
You didn't callsetContentView(R.layout.activity_main) , in your code there, you commented it out, so your layout wasnt inflated so you got a blank white screen. Uncomment that line and you will be fine.
Instead of prefixing id's in xml, is it possible to specify the particular layout in code? For example, if I have 3 layouts, each with a button that has an id of "btn". Is it possible to specify which layout to findViewById(R.id.btn) in?
The basic context is defined via setContentView(R.lyaout.my_layout). If you inflate another layout using LayoutInflater.inflate() you get a layout object, lets call it buttonLayout. You can now differ between this.findViewById(R.id.button) and buttonLayout.findViewById(R.id.button) and both will give you different button references.
findViewById is a method of the View class. You can specify where the view should be searched for like that
final View container = new View(context);
container.findViewById(R.id.btn);
If your content view is a complex hierarchy that has several views with id btn, you will need to navigate to a subtree of the hierarchy and search from there. Suppose you have three LinearLayout views, each with a btn view somewhere in it. If you can first select the correct LinearLayout (by id, tag, position, or some other means), you can then find the correct btn view. If the relevant LinearLayout has id of branch1, for instance:
View parent = findViewById(R.id.branch1); // Activity method
View btn = parent.findViewById(R.id.btn); // View method
if you have your btns inside different Viewgroups, it is possible, but requires to give ViewGroups a different name! Easyiest will be for that purpose to define the Layout of the Button inside its own XML (i.e button_layout.xml)
inside your Activity you can do this:
public MyActivity extends Activity{
Button btn1, btn2, btn3;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
LinearLayout ll = new LinearLayout(this);
setContentView(ll);
btn1 = (Button)inflater.inflate(R.layout.button_layout, ll);
btn2 = (Button)inflater.inflate(R.layout.button_layout, ll);
btn3 = (Button)inflater.inflate(R.layout.button_layout, ll);
}
}
I have this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.main, null);
setContentView(layout);
s = (Spinner) findViewById(R.id.spinner1);
It throws a ClassCastException why is it?
If I do setContentView(R.layout.main) everything goes well. But i need to have the layout in a variable because I need it to use an advertsiment library. Is there a a way to inflate the XML and have the layout in an variable?
Thanks
Which line is throwing the ClassCastException?
Also you can use setContentView(R.layout.main); then still use a normal find view by id call to get a reference to your root layout.
LinearLayout layout = (LinearLayout)findViewById(R.id.yourParentId);
as long as this comes after you've called setContentView() you should get returned a reference to your layout object that you can use however you wish.
On what line does it throw the exception? Maybe the root view of the layout is not really a LinearLayout. Or maybe R.id.spinner1 is not a Spinner.
I'm adding TextViews programmatic using a for-loop and use setId(Int int) to set unique id for that perticular Textview.
But now I want to search textView on the basis of that id.
How can I search?
erorr occurd 'app stopped unexpectedly...' Here is my code.
public class Idtest extends Activity {
TextView tv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll=new LinearLayout(this);
tv=new TextView(this);
tv.setText("Hello");
ll.addView(tv);
tv=new TextView(this);
tv=(TextView)ll.getChildAt(1);
setContentView(tv);
}
}
tv is obviously null at the end (the LinearLayout only has one child, which is at index 0), so you're basically calling setContentView(null) which results in an exception. It's not clear for me what you're trying to do (your code is pretty messed up).
Supposing you are trying to show multiple TextViews in a LinearLayout, here's my suggestion:
public class Idtest extends Activity {
LinearLayout mainLayout;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainLayout = new LinearLayout(this);
setContentView(mainLayout);
for (int i=0; i < 10; i++) {
TextView tv = new TextView(this);
tv.setText("Hello " + i);
mainLayout.addView(tv);
}
}
}
If, at any later point, you need one of the TextViews, do:
TextView tvX = mainLayout.getChildAt(X); // where X is between 0 and 9
Also, please note that creating layout from code is evil. If you can avoid it, please do. For example, if the number of TextViews is dynamic, then it's perfectly normal to create them from code (although you could inflate them). However, it's not advisable to also create the LinearLayout from code. You should have that in an XML. If it's possible for you to also have the TextViews in an XML, that would be even better.
There is another option too...we have setTag() and getTag() methods. While adding a textview give a tag for it and whenever u want to do a search use getTag().
I suppose you are adding your TextView controls to some ViewGroup (e.g. LInearLayout)?
You can iterate through the ViewGroup child views using getChildCount() / getChildAt(index) and compare the child view id with the one you're searching for.