Accessing View inside the LinearLayout with code - android

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.

Related

How to add text in dynamically added EditTexts

I want to add text to my dynamically added EditTexts.
As for now its just adding a layout with two EditText in, its getting added to a LinearLayout on button click.
How do I set the text of these Editexts? Because now I can add as many as I like and every EditText has the same id as the have in the layout file.
My mainactivity.java with the inflation upon button clicklooks like this:
final LinearLayout mView = (LinearLayout) getLayoutInflater().inflate(R.layout.more_rest_main, null);
mView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
restLayout.addView(mView);
And here is my layout for more_rest_main.xml
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/added_rest_from"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:hint="#string/time_from"
android:textSize="15dp"
android:focusable="false"/>
<EditText
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="#+id/added_rest_to"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:hint="#string/time_to"
android:textSize="15dp"
android:focusable="false" />
The thing to understand here is that you can look for a child view in any ViewGroup (LinearLayout is a ViewGroup).
You're probably familiar with retrieving a View in an Activity onCreate, where after calling setContentView, you have access to any View with findViewById.
So after inflating your ViewGroup, you can use the same approach you would use in an Activity onCreate, but you need to look for your ViewGroup's child views:
final LinearLayout mView = (LinearLayout) getLayoutInflater().inflate(R.layout.more_rest_main, null);
EditText txtAddedRestFrom = (EditText)mView.findViewById(R.id.added_rest_from);
restLayout.addView(mView);
Once you have your reference you can invoke any method on your EditText, such as setText.
You can create an EditText dynamically like following:
EditText editText = new EditText(myContext);
editText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//add the same logic as in the XML file
mView.addView(editText);
You can also set an ID dynamically. If you are using API 17 and above, do the following:
editText.setId(View.generateViewId())
Otherwise you can set a positive Integer of your choice as an ID, instead of using the:
View.generateViewId()
With the code above and since these will be added dynamically, you don't need the XML file as the EditTexts will be created dynamically.
If you want to keep the existing XML file, you can just call the
setTag()
to each one of the inflated EditTexts and set a unique value in the setTag method. That way you can distinguish the EditTexts. And since you can do that, you can add whatever text you need to each one of them.
To reply to your comment, on how to set an OnCLickListener:
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// here you can get either the Tag or the ID that
// you've set dynamically. And do whatever you like to do
view.getTag()
view.getId()
}
});

Accessing Android Views in Java files

I've designed a RelativeLayout with children elements such as TextView and ImageView. What I want to do is to create a RelativeLayout object from my created RelativeLayout in XML, that way I can access to its children elements and modify them (change image from ImageView and change the text from TextView). How can I do this? It would be kind of like this.
RelativeLayout lay = new "myrelativelayout";
ImageView img = lay.children[0];
TextView txt = lay.children[1];
Thanks!
XML:
<RelativeLayout
android:id="#+id/relative_layout_id"
...>
<TextView
android:id="#+id/textview_id"
.../>
</RelativeLayout>
Activity onCreate:
RelativeLayout lay = (RelativeLayout) findViewById(R.id.relative_layout_id);
To change children use:
TextView child = (TextView) findViewById(R.id.textview_id);
child.setText(text);
or:
View child = lay.getChildAt(i);
To access the layout we use the findViewById method. Therefore to change the image in the ImageView you do not really need access to the RelativeLayout.
The way we access any element through its id is as follows:
View v = (View)findViewById(R.id.view_id);
where view_id is the ID of the view.
To access the RelativeLayout, therefore, the piece of code would be:
RelativeLayout lay = (RelativeLayout) findViewById(R.id.relative_layout_id);
But if you only want to change the text in the TextView, you really don't need the above code. It is sufficient if you do a similar access for the TextView:
TextView textView = (TextView) findViewById(R.id.id_of_textview);
textView.setText(text);
A general method of accessing any resource can be found here.

Is both Static and Dynamic Layout Possible?

I have an XML layout having a single TextView
Now I want to add 50 buttons which I want to add dynamically in my java file !.
Is it possible to add attributes to an XML file via java code ??
Or can an activity have 2 layouts at a time ??
for eg,
public class Options extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
Button but=new Button(this);
but.setText("Wassup");
// How do I add this button to the layout ?
}
}
Is it possible to add attributes to an XML file via java code ??
No, but you can add properties to Views and Layouts as you are doing with setText(). resource files themselves cannot be changed after compiled.
Or can an activity have 2 layouts at a time ??
The simple answer is no but you can inflate another layout and add it to the current layout.
Example of what you can do to add a Button
Inflate your root layout and add the Buttons to it with addView(). Something like
Layoutinflater inflater = (LayoutInflater) getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.layout_file);
Button but=new Button(this);
but.setText("Wassup");
// How do I add this button to the layout ?
ll.addView(but);
LayoutInflater
Or if you want to add it to a layout in the current file you can just use findViewById() and use addView() on that to add your Buttons to.
Considering you have an xml layout as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#id/mainlayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
</LinearLayout>
In your java code after setContentView(R.layout.options); you can do the following:
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.mainlayout);
Button button=new Button(this);
linearLayout.addView(button);
Now you can add as many buttons you like into the linear layout as seen above.
Yes it is possible. After setContentView(R.layout.options); get your buttons container with findViewById(). You will have a reference to a LinearLayout, RelativeLayout or something else. After that use Layout inflater and programmatically you can add other layouts or components.
Hope it helps!
just use layout.addView() where layout is a ViewGroup that you get by calling findViewById(R.id.layoutId)

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

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

Categories

Resources