How to remove Linear Layout via code in ANDROID - android

I have a Linear Layout with many linear layouts inside. At runtime , I want to remove this layout(along with its child linear layouts) from the view subject to a condition. On removal, I want other layouts to be adjusted accordingly.
I tried this, but the layout remains.
if(getIntent().getStringExtra("ListingType").equals("Brief")){
extraLayout.setVisibility(View.GONE);
extraLayout.removeAllViews();
}
This Activity has 2 main Linear Layouts, 1 is DetailsLayout and 2 is ExtraLayout, both these layouts are relative to each other in a ScrollView. The main(root) layout of the Activity is Relative Layout

Related

Button are getting overlapped on deifferent devices using dp in relative layout

left one is the mobile phone and right one is the device on android studio.
Use linear layout (horizontal) inside your relative layout and add buttons to it
Set linear layout's attribute weight_sum to number of buttons in a row (for example 5)
Set buttons' attribute layout_weight instead of explicitly defining size in dp (for example 1 for each button)
Duplicate your linear layout for all the rows
Profit

Nested linear layouts Vs Single relative layout

I am creating a layout which has two views in same line horizontally on top and below that all views are vertically arranged.I have read that its advisable to use linear layout wherever possible.However in this case if I have to use linear layout then I need to create another Linear Layout inside parent Linear Layout to hold top two views with orientation horizontal and my parent layout will have orientation vertical which will contain all views.
Instead if i use relativelayout as parent layout then all views can be arrange in single parent layout without the need to create any child layout.
Which is a better approach-nested linear layouts or single relative layout?

Edit children in scrollview where scrollview is a child of relativelayout in android studio

In my xml hierarchy, I have the root element as a RelativeLayout and then a Scrollview as a child. That ScrollView then has a RelativeLayout with views inside it.
My problem is that I cannot edit the contents in the ScrollView as the design view of Android Studio doesn't show the entire ScrollView. I've tried the toggle viewport render mode button but that doesn't change anything (note: my ScrollView's fillviewport is true as well.
Is there a way I can see everything in my ScrollView where the ScrollView is a child of a RelativeLayout in Android Studio's design view?
I have this issue as well.
If you have a view hierarchy like...
RelativeLayout (root)
--> ScrollView
--> RelativeLayout
--> Views (e.g. Buttons, TextViews, etc.)
...I change the above layout to something like...
RelativeLayout (root)
--> Include
...where my include tag is referencing another XML layout...
ScrollView
--> RelativeLayout
--> Views (e.g. Buttons, TextViews, etc.)
In Android Studio's preview, the first XML layout with the <include> tag should show (part of the complete) layout on the device.
The second XML layout, with the ScrollView as its root should show you all of the Views.
As far as I know, there is no other way around this, unfortunately - and I would love to see an alternative solution if there is one.

Using addContentView to break my large layout file into mutiple smaller layout files?

I have one main layout file, which has grown excessively large and is getting quite hard to read. I currently just use setContentView(int layoutResID) to inflate the whole layout.
I recently discovered the addContentView(View view, ViewGroup.LayoutParams params) method, and I want some input from others before I waste any time making significant changes.
My main layout file looks something like this at the moment:
frame layout
relative layout
...
linear layout a
...
linear layout b
...
linear layout c
...
linear layout d
...
linear layout e
...
/frame layout
Where some of the linear layouts are very complex.
My question is, if I separate each of the complex linear layouts into their own xml files, then successively inflate each of them using addContentView, will I end up with the same behaviour as my current implementation with setContentView ?
My main motivation is for each separate layout file to contain just one of the complex linear layouts, for easier readability of the xml files.
You could also inflate these different layouts by using Layout Inflater. and add those inflated views in your frame layout dynamically. For eg
View v;
if(condition){
v=inflater.inflate(R.layout.a)
}
else if(condition){
v=inflater.inflate(R.layout.b)
}
FrameLayout f= findviewbyId(R.id.frameLayout)
f.addView(v);

Matrix of linear layout and manipulation in code

Let say that I have an layout in res/values named layout1.xml.
In this layout I have only one linear layout (the black one on the picture)
The thing I want to do is to add an array of linear layouts just like on the picture.
The red ones are linearlayout with horizontal orientation that contains 5 other linear layouts.
I want to do everything in code and I want to set an onclick listener to each of the layouts so when one is clicked I want to hide it. Everything needs to be putted in a function that will return a Layout and the this method should take prams for the rows and cols
public LinearLayout getLayout(int rows,int cols){
return the_layout;
}
You make a LinearLayout with the constructor new LinearLayout(context)
You add layouts with parentLayout.addView(childLayout).
You should be able to do the rest.

Categories

Resources