I have a question regarding Android Activitys:
An Activity has the Method addContentView(View) while a ViewGroup has a (similar?) addView(View) Method.
Unfortunately its undocumented where the View from addContentView is placed. Is it like a LinearLayout just adding the View to the bottom, or is it more like a FrameLayout, which adds its Views "onTop" ? Does it depend on the ViewGroup set by setContentView?
If I dive into the sources I see that addContentView will call Window's abstract Method addContentView. Unfortunately I cannot see which class is implementing this Method. So whats the behaviour of Activitys addContentView exactly?
The base layout of every activity is a FrameLayout. This means the layout you usually set via setContentView() is a child of this layout. addContentView() adds just another child, therefore it behaves like a FrameLayout (which means it adds new UI elements above existing ones).
You can check this by using a tool called hierachyviewer from your ANDROID_SDK\tools folder. Here are two screenshots:
This is the layout before calling addContentView(), my activity consists of the default FrameLayout, holding a LinearLayout with a Button (my layout here). This is reflected in the bottom row here, the other elements above are the title/statusbar.
After adding a TextView via addContentView() it looks like this. You can see that the base FrameLayout got a new child.
public void addContentView(View view,
LayoutParams params) {
mActivity.addContentView(view, params);
}
//
public static void SetActivityRoot(Activity c) {
View v = ((ViewGroup)c.findViewById(android.R.id.content)).getChildAt(0);
ScrollView sv = new ScrollView(c);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
sv.setLayoutParams(lp);
((ViewGroup) v.getParent()).removeAllViews();
sv.addView((View) v);
c.addContentView(sv, lp);
}
//
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout =
(LinearLayout)findViewById(R.id.mainlayout);
//newButton added to the existing layout
Button newButton = new Button(this);
newButton.setText("Hello");
mainLayout.addView(newButton);
//anotherLayout and anotherButton added
//using addContentView()
LinearLayout anotherLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button anotherButton = new Button(this);
anotherButton.setText("I'm another button");
anotherLayout.addView(anotherButton);
addContentView(anotherLayout, linearLayoutParams);
}
}
Related
The app I am trying to make, has got a lot of similar LinearLayouts and textViews that need to be created programmatically and placed on the screen in a specific order.
So I decided to define a method which returns one element, and for the furthur uses,I will put the method in some loop to produce the others. but when I create a view or layout this way, nothing shows up or sometimes the app crashes, as if it's been sent null to addView(). It only works when I create the View/Layout in onCreate() and then I use it right there afterwards.So , any ideas that I can use the method to creat my Layout/View? Because they are too many and it's not possible to create them one by one in onCreate()
Here's the method:
public LinearLayout createLinearLayout(){
TextView tv_day = new TextView(this);
tv_day.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
tv_day.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
tv_day.setGravity(Gravity.END);
tv_day.setText("27");
LinearLayout ll_horizontal = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams ll_horizontal_params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll_horizontal.setLayoutParams(ll_horizontal_params);
ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
ll_horizontal.addView(tv_day);
return ll_horizontal;
}
and this is onCreate() which doesn't add any linear layouts with a textView in it :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_month_view);
LinearLayout ll= createLinearLayout();
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.activity_month_view);
mainLayout.addView(ll);
}
I think this should help
- add an empty linear layout in XML with some id.
- reference that layout in code
- add elements to that layout dynamically
Hey was just checking your code. Its working perfectly now just try this method.
public LinearLayout createLinearLayout(){
TextView tv_day = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tv_day.setLayoutParams(layoutParams);
tv_day.setGravity(Gravity.CENTER);
tv_day.setText("27");
LinearLayout ll_horizontal = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams ll_horizontal_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll_horizontal.setLayoutParams(ll_horizontal_params);
ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
ll_horizontal.addView(tv_day);
return ll_horizontal;
}
I'm adding multiple Views by code into Layout. I need each new View to be above previous one(top of the parent layout).
EDIT: To be more accurate I'll describe what the app module should does. User start with clean screen and one button at the bottom of the screen. The button adds a View at the top of the screen. Next clicks should add next views above previous ones to make the newest View be on the top of a container. The app saves state and on restart user see views in the same order.
Call the following method from Button's onClick Event.
private final int LAYOUT_TOP_INDEX = 0;
private void addViewOnTop(View view){
if(layout != null && view !=null)
layout.addView(view, LAYOUT_TOP_INDEX);
}
where 'layout' is your Layout (e.g., LinearLayout) to which the View is to be added.
Would really need more information from you to give a more accurate answer, but if you're saying what i think you are then you can just add these views to a LinearLayout with orientation set to vertical.
And assuming you're iterating through a list to dynamically add views, instead of incrementing from 0, increment down from the size of the list.
for(int i = size; i >= 0; i--){
linearLayout.add(new TextView(Context));
}
View positions inside ViewGroups are defined by the LayoutParams
How does this happen? Views pass their LayoutParams to their parent ViewGroups
//100% programatic approach with simple LayoutParams
LinearLayout myLinearLayout = new LinearLayout(this);
//if the **parent** of the new linear layout is a FrameLayout
FrameLayout.LayoutParams layoutParams =
new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
//or if you have the XML file you don't have to worry about this
//myLinearLayout = (LinearLayout)findViewById(R.id.my_simple_linear_layout);
//you could have a LinkedList<TextView>
LinkedList<TextView> textViewList = new LinkedList<>();
//assuming the order is the correct order to be displayed
Iterator<TextView> descendingIterator = textViewList.descendingIterator();
while(descendingIterator.hasNext())
{
//just add each TextView programatically to the ViewGroup
TextView tView = descendingIterator.next();
myLinearLayout.addView(tView);
}
Just like we defined LayoutParams for the LinearLayout we could also define LayoutParams for the TextView
IMPORTANT: when setting LayoutParams you need to be sure they fit the VIEWGROUP, that is the parent of the View being added
private TextView textViewFactory(String myText) {
TextView tView = new TextView(getBaseContext());
//controling the position relatively to the PARENT
//because you are adding the textview to a LINEAR LAYOUT
LinearLayout.LayoutParams paramsExample =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
tView.setLayoutParams(paramsExample);
//configuring the insides of the textview
//you can also do all kinds of stuff programatically
tView.setGravity(Gravity.CENTER_HORIZONTAL);
tView.setPadding(10, 10, 10, 10);
tView.setTypeface(Typeface.DEFAULT_BOLD);// (null, Typeface.BOLD_ITALIC);
tView.setTypeface(Typeface.SANS_SERIF);
tView.setTypeface(null, Typeface.ITALIC);
tView.setTypeface(Typeface.defaultFromStyle(R.style.AppTheme));
tView.setId(R.id.aux_info);
tView.setText(myText);
//.........all kinds of stuff really
return tView;
}
If you mean adding a view programmatically so that the new one is added above the previous one, instead of below it, then I suggest this:
Maintain an ArrayList with the items you want to turn into views
Put them into a ListView
When you want to add a new view that must appear at the top of the list, insert it as the first element of your ArrayList and recreate the ListView from it.
I have created a fragment which should display table of buttons. I used table layout to create a table. But the button table doesn't display in the fragment. What is the problem related to this code and how can I overcome this???
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
ArrayList buttons = new ArrayList();
ScrollView sv = new ScrollView(this.getActivity());
//Set a TableLayout to add buttons
TableLayout tl= new TableLayout(getActivity());
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
tl.setLayoutParams(params);
tl.setOrientation(TableLayout.HORIZONTAL);
for(int i=0;i<5;i++){
TableRow row=new TableRow(this.getActivity());
LayoutParams paramrow = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
row.setLayoutParams(paramrow);
for(int j=0;j<2;j++){
Button button = new Button(getActivity());
button.setText("testing");
button.setPadding(5, 5, 5, 5);
LayoutParams parab = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(parab);
row.addView(button);
buttons.add(button);
}
tl.addView(row);
}
sv.addView(tl);
}
You're using the wrong type of LayoutParams in at least two places. Views need to use the type of LayoutParams that correspond to their container. Therefore Views contained directly in a TableLayout - i.e., the TableRows - should use TableLayout.LayoutParams. The Views contained in TableRows - the Buttons, in this case - should use TableRow.LayoutParams. And finally, the TableLayout is in a ScrollView, so it should use ScrollView.LayoutParams, which is actually FrameLayout.LayoutParams, since ScrollView extends FrameLayout.
It also appears that you're not adding the ScrollView to the Fragment's View anywhere. I would mention, too, that it might be preferable to define the ScrollView and the TableLayout in layout xml, inflate this layout in onCreateView(), and dynamically create only what needs to be - i.e., the TableRows and Buttons.
I want to add a scroll view to all the layouts that I have. But dynamically. Because the app will run in different screen sizes, and when I will get a screen size smaller than a specific size, then I want to show the layout in a scroll view.
So I made this method, it will be called on the check that the screen is small. I will pass my activity and I want to change the root layout to scroll view or just add a ScrollView as the root layout. So if the root layout is a LinearLayout, then I want to put that layout in the ScrollView. And I have not named all the layouts, meaning that I didn't give an ID to the layout, so I cannot use findViewById.
public static void SetActivityRoot(Activity c) {
View v = c.getWindow().getDecorView();
// View v = v.getRootView();
ScrollView sv = new ScrollView(c);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
sv.setLayoutParams(lp);
((ViewGroup)v.getParent()).removeView(v);
sv.addView((View) v);
((ViewGroup)v.getParent()).addView(sv);
}
It's giving me an error saying that "you cannot remove view from null" etc. Or that "you cannot add view to layout as it already has parent view". How can I make this work?
Finally solved my problem.
public static void SetActivityRoot(Activity c) {
View v = ((ViewGroup)c.findViewById(android.R.id.content)).getChildAt(0);
ScrollView sv = new ScrollView(c);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
sv.setLayoutParams(lp);
((ViewGroup) v.getParent()).removeAllViews();
sv.addView((View) v);
c.addContentView(sv, lp);
}
I want to know how to add or link with findviewID a LinearLayout with multiple buttons (programatically added) and space between them. I have tried to create a XML layout in my fragment and link it to a variable of the type LinearLayout and work from there with LayoutParams but I don't get it to work properly. I would like to know if anyone here has any suggestions. below is my code.
private GuiLoader guiloader = new GuiLoader();
private LinearLayout layout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.media_list_fragment, container, false);
layout = (LinearLayout)v.findViewById(R.id.LinearLayout1);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 2.0f);
param.setMargins(100, 100, 100, 100);
layout.setPadding(500, 86, 50, 50);
layout.setLayoutParams(param);
and the button creation throug a class
for (final VideoDevice videoDevice : videoDevices) {
Button myButton = guiloader.createButton(getActivity());
myButton.setBackgroundColor(getResources().getColor(R.color.green));
myButton.setText(videoDevice.description);
layout.addView(myButton, guiloader.buttonWidth, guiloader.buttonHeight);
Thanks in advance!
When you call setPadding and setLayoutParams on your LinearLayout - this deals with the padding/margins around the edges of the LinearLayout - not between the buttons.
It sounds like you would like to have a gap between the buttons. For that, you can:
1) add a margin to each button inside your for loop
2) consider using GridView which allows you to specify the spacing between children using android:horizontalSpacing and android:verticalSpacing