This is my xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/blue"
android:gravity="center">
Is possible to set color from my Activity file?
For example I can do that for a button:
ImageButton x = (ImageButton) this.findViewById(R.id.btn1);
x.setBackgroundColor(color);
Yes, you can do it by
public class MainActivity {
private LinearLayout ll;
#Override
public void onCreate(...) {
super.onCreate(...);
setContentView(R.layout.your_layout_name);
ll = (LinearLayout) findViewById(R.id.linear_layout_id);
// You can set Background Color for your Linear Layout
ll.setBackgroundColor(Color.RED);
// You can set Image Also
ll.setBackgroundResource(R.drawable.imagename);
}
}
In XML file :
<LinearLayout
android:id="+#id/linear_layout_id"
android:width = "fill_parent"
android:height = "fill_parent"
android:orientation = "vertical"
/>
Hmm there is also setter for LinearLayout
setBackgroundColor()
Is inherited from the View class
Just like you did it for the button you can do the same for the the LinearLayout also. Give you Linearlayout an id and set the color:-
((LinearLayout)findViewById(R.id.ll_test)).setBackgroundColor(Color.BLUE);
Related
I'm following the accepted answer to this question:
ArrayList<EditText> myEditTextList = new ArrayList<EditText>();
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
myEditTextList.add( (EditText) myLayout.getChildAt( i ) );
How do you get myLayout, what does it actually represent? In my app I define linear_layout in activitymain.xml but can't seem to get it in MainActivity.java (I tried using R but it didn't work). I'm new to Android development and could really use some high level guidance.
Let Linear Layout be the root of your xml:
<LinearLayout android:id="#+id/my_layout" >
//your views goes here
</LinearLayout>
Then in your Activity you'll have to initialize the view once your layout gets inflated like this
LinearLayout myLayout = (LinearLayout) findViewById(R.id.my_layout);
Hope this helps.
You need to use myLayout as your root layout in your layout.xml file
the myLayout maybe a FrameLayout, LinearLayout, RelativeLayout or ConstraintLayout layout
SAMPLE CODE
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myLayout"
android:orientation="vertical">
<!-- add your view here-->
</LinearLayout>
Now do findViewById in side your actvity
public class MyActivity extends AppCompatActivity {
LinearLayout myLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
myLayout = findViewById(R.id.myLayout);
}
}
You can try below code to get a reference of your LinearLayout in Java code.
LinearLayout myLayout=(LinearLayout) findViewById(R.id.your_linear_layout_id)
assuming your actiivtymain.xml looks like this
<?xml version="1.0" encoding="utf-8"?>
<SomeLayout
android:id="#+id/some_id"
...>
<EditText
...
/>
<EditText
...
/>
.
.
.
</SomeLayout>
mLayout you are referring is the parent of all your edit texts. It may not be the root of your layout but if you want to get a Java instance of it, you need to give it an id. and get the instance by using the code given by #Nilesh Rathod. One more thing to remember is to call the method findViewById(R.id.my_layout); only after setContentView(R.layout.activitymain);.
Simple: I want to inflate parent with 1 child which has 0dp width.
Parent xml:
<com.example.KeyPad // extend LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="4" // for the children
android:layout_weight="1" // this is for its parent
android:clickable="true"
android:background="#color/MidnightBlue" >
The child class:
public class KeyButton extends RelativeLayout implements View.OnClickListener{
public KeyButton(Context c ) {
super(c);
RelativeLayout v = (RelativeLayout) LayoutInflater.from(c).inflate(R.layout.key_button, this, true);
}
}
}
which uses the R.layout.key_button xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="1"
android:layout_width="0dp"
android:background="#android:color/holo_red_dark"
android:layout_height="wrap_content">
<TextView ... />
</RelativeLayout>
And the child is getting added by:
Parent.addView(new KeyButton(context) );
The problem is that android:layout_weight doesn't look to take action and the layout_width of the child stays at "0dp". If I change the width to 50dp I can see the child that has been correct inflated.
Also tried to add the parameters programmatically when getting added:
KeyButton bt = new KeyButton(context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1.0f);
bt.setLayoutParams(lp);
Parent.addView(bt);
How can I inflate the child with 0dp/weight? Of course as you can see I have defined the weight_sum of the parent.
You have used a LinearLayout.LayoutParams but the parent of your button are a RelativeLayout.
Try this :
KeyButton bt = new KeyButton(context);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(0, RelativeLayout.LayoutParams.WRAP_CONTENT,1.0f);
Parent.addView(bt, lp);
You can simply set the visibility property to gone or hidden by following code :
bt.setVisibility(View.GONE);
You can find more information about setting views here
I am having a LinearLayout whose visibility is directly affected by the click of a TextView. This LinearLayout has more TextViews dynamically added inside. My LinearLayout viewQuickLinks starts out with a visibility of gone. In my oncreate I call addQuickLinks which then adds several TextViews to the LinearLayout. None of these TextViews have a set visibility. I click on the TextView to change the LinearLayout to visible and space is added, but there are no TextViews.
My xml file (just to add a note this is all in a scrollview):
<TextView
android:id="#+id/textQuickLinksTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="#drawable/navigation_expand"
android:text="#string/quick_links_title"
android:textSize="25sp"
android:visibility="visible" />
<LinearLayout
android:id="#+id/viewQuickLinks"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:visibility="gone"
android:orientation="vertical" />
Changing the LinearLayout to visible and gone:
private void setUpQuickLinks() {
final TextView quickLinksTitleText = (TextView) findViewById(R.id.textQuickLinksTitle);
quickLinksTitleText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout viewQuickLinks = (LinearLayout) findViewById(R.id.viewQuickLinks);
if (viewQuickLinks.getVisibility() == View.VISIBLE){
viewQuickLinks.setVisibility(View.GONE);
quickLinksTitleText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.navigation_expand, 0);
}
else{
viewQuickLinks.setVisibility(View.VISIBLE);
quickLinksTitleText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.navigation_collapse, 0);
}
}
});
quickLinksClickListeners();
}
Why are the TextViews not appearing when the LinearLayout is Visible?
Thank you for any help!
Try changing android:layout_height to fill_parent. Why is it 1 dip?
I just started android and I tried below questions to get this answer before posting it here:
Android - Adding layout at runtime to main layout
Add button to a layout programmatically
Dynamically adding a child to LinearLayout with getting each child's position
And I am still not able to add a button to linear layout :(
Below is the code for activity, please let me know where I am wrong:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.activity_main, null);
Button btn = new Button(this);
btn.setId(123);
btn.setText("Welcome to WI FI World");
layout.addView(btn);
}
And xml looks like below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
Try assigning an id to your layout then add the button to the layout.
Im pretty sure those 2 layouts are not the same, so you are infact adding a button to a layout that is never displayed.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.lnr_main);
Button btn = new Button(this);
btn.setId(123);
btn.setText("Welcome to WI FI World");
layout.addView(btn);
}
With Layout assigned an id
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lnr_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
Take the Id for LinearLayout in XML and in jave code use that id of LinearLayout from XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linear" >
</LinearLayout>
In onCreate():
LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
//Select widgets
linear.addView()
Give an id to your linearLayout.
Then in your code
LinearLayout layout = (LinearLayout)findViewById(R.id.given_id);
Keep the rest the same, should work.
I have an android xml layout, main.xml. I would like to add controls to this layout at runtime (I would like to add a series of additional linear layouts that contain buttons controls). Can I do that and if yes, how?
Thanks
I see the error u r doing here
LinearLayout mainLayout = (LinearLayout) findViewById(R.layout.main);
You r taking the layout as Linearlayout object, you should take the LinearLayout id
Try this
LinearLayout lnr = (LinearLayout) findViewById(R.id.LinearLayout01);
Button b1 = new Button(this);
b1.setText("Btn");
lnr.addView(b1);
You can add controls programmatically if you want in your code, or even another XML with a View and an Inflater.
Here you can read the basics: http://developer.android.com/guide/topics/ui/declaring-layout.html
Ok, I have got it to work.
The steps are the following:
First inflate the xml layout, ie,
View view = View.inflate(this, R.layout.main, null);
Then instantiate the container object from the xml layout into a ViewGroup class, ie,
ViewGroup container = (ViewGroup) view.findViewById(R.id.myContainer);
Then create a linearLayout object, create and add onto that any controls needed, add the linearLayout to the container object and use setContentView on the view object, ie,
container.addView(buttonsLayout);
this.setContentView(view);
You can do this quite easy by setting an id on the layout on which you want to add views to. Say your main.xml look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/label"
android:layout_width="fill_parent"/>
<LinearLayout android:id="#+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
Lets assume that you want to add your additional views to the LinearLayout with id id/container. In your onCreate method you could retrieve that object for later use:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContainer = (ViewGroup)view.findViewById(R.id.container);
}
Now you are all set to add other views to your container ViewGroup:
LinearLayout theButtons = getButtons()
mContainer.addView(theButtons);
In the getButtons method you need to create your LinearLayout containing the buttons you need. Either you do this programmatically or by inflating a view defined in an XML file. See LayoutInflater.inflate.
just try this:
LinearLayout mainLinearLayout = (LinearLayout) findViewById(R.layout.llmain);
now create button dynamically like this
Button btn1 = new Button(this);
btn1.setText=("Button 1");
mainLinearLayout .addView(btn1);
now if you want to add onether linearlayout then add it below button then
LinearLayout llinner = new LinearLayout(this);
Button btn2 = new Button(this);
btn2.setText=("Button 2");
mainLinearLayout .addView(btn2);
llinner.addView(btn2 );
mainLinearLayout .addView(llinner);
Try this :
LinearLayout ll =(LinearLayout)findViewById(R.id.linlay);
Button b = new Button(this);
b.setText("Hello");
l.addView(b);
This might help you
Here is what I did to display a set of runtime buttons on table layout.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = View.inflate(this, R.layout.activity_main, null);
TableRow myrow = (TableRow) view.findViewById(R.id.myrow);
Button btn1 = new Button(this);
btn1.setText("Button 1");
myrow .addView(btn1);
Button btn2 = new Button(this);
btn2.setText("Button 2");
myrow .addView(btn2);
this.setContentView(view);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="#+id/myview"
android:layout_width="404dp"
android:layout_height="691dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableLayout
android:id="#+id/mylayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow android:id="#+id/myrow"></TableRow>
</TableLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
AVD Pixel4API30
Output screenshot