I'm trying to create a new Table row and add it into an existing TableLayout. I have scoured the Internet and it seems like I've tried everything but I cant get the new row to show. If I add a TableRow with an id in the layout and add my custom ImageView to that without adding the new TableRow to the layout the ImageView shows but I need to add a new row dynamically. In my Fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_default_view,
container, false);
final ViewGroup tableLayout = (ViewGroup) rootView.findViewById
(R.id.table_bracket);
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT
);
TableRow tableRow = new TableRow(context);
tableRow.setLayoutParams(layoutParams);
SeparatorImageView separator = new SeparatorImageView(getActivity());
separator.setLayoutParams(layoutParams);
tableRow.addView(separator);
tableLayout.addView(tableRow);
return rootView;
}
here's the layout I'm trying to add to:
<FrameLayout 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"
tools:context="com.mbusby.bracketsandbox.DefaultViewFragment">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/table_bracket"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:stretchColumns="*">
</TableLayout>
add this line after tableLayout.addView(tableRow); line
getWindow().getDecorView().findViewById(android.R.id.content).invalidate();
to refresh tableview newly added row will be visible
Related
I faced with a problem that i cannot put my dataGridView into scrollView and in case I have a lot of columns they just become so thin that it's impossible to see something there. That's why I decided to remake it and create LinearLayout with Vertical Layout for each column and each of them will have another LinearLayout with Horizontal Layout just to simulate GridView. (Hope it's a good idea)
But unfortunately I'm facing some problems during its creation. It is not being created and my application turning off. Ask you for your help
Here is my code: grid_container.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/GridScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/main_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
PageFragment.java (place where LinearLayout should be fill out)
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.grid_container, container, false);
LinearLayout mainLayout = (LinearLayout) view.findViewById(R.id.main_grid_layout);
int colCount = mPage.get(0).split(",").length;
int rowCount = mPage.size();
ArrayList<ArrayList<String>> tempNormList = createNormList(mPage);
for(int currCol=0;currCol<colCount;currCol++){
LinearLayout linearLayout = new LinearLayout(view.getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(llParams);
for(int currRow=0; currRow<rowCount;rowCount++){
TextView textView = new TextView(view.getContext());
textView.setText(tempNormList.get(currCol).get(currRow));
if(currRow==0){
textView.setBackgroundResource(R.drawable.header_borders);
}
linearLayout.addView(textView);
}
mainLayout.addView(linearLayout);
}
return view;
}
Thank you in advance for your help
try my linked Answer, it will provide your solution, but you have do some changes like below
Don't Use Custom Adapter, use for loop with getView() method of
CustomAdapter to Set Data.
Cast your Linearlayout which id is main_grid_layout by `findViewById().
add convertView of getView() method to main_grid_layout
Skip Item Android Grid View in condition
hope it will help you
I have to create a complicated layout (A receipt view) using a lot of
data. Because there can be x amount of charges or payments, this view must be done programmatically.
I am trying to build reusable layouts that I can attache to different textviews or edittexts and have them line up.
Broken down it looks something like this:
Which comes down to 2 basic layouts:
A full width (where the text is centered)
A Split column
a) where one column takes up 3/4's the view on either the left or right.
b) and the second column takes up 1/4 the view.
Here is my attempt. But I don't seem to be getting it right.
Here is my error:
The specified child already has a parent. You must call removeView()
on the child's parent first.
Can anyone help?
private ScrollView mScrollView;
private LinearLayout mLinearLayout;
private ProgressBar mProgressBar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_payment, container, false);
mScrollView = (ScrollView) view.findViewById(R.id.svPayment);
mScrollView.setVisibility(View.GONE);
mProgressBar = (ProgressBar) view.findViewById(R.id.pbPayment);
mLinearLayout = (LinearLayout) view.findViewById(R.id.llPayment);
return view;
}
public void setupGUI() {
//RESUABLE LAYOUT
LinearLayout.LayoutParams paramsFull = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramsFull.setLayoutDirection(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams paramSmall = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramSmall.setLayoutDirection(LinearLayout.HORIZONTAL);
paramSmall.weight = 0.2f;
LinearLayout.LayoutParams paramLarge = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
paramLarge.setLayoutDirection(LinearLayout.HORIZONTAL);
paramLarge.weight = 0.8f;
// HOLDS THE LEFT AND RIGHT COLUMNS
LinearLayout columnShell = new LinearLayout(getContext());
columnShell.setLayoutParams(paramsFull);
//Small column
LinearLayout columnSmall = new LinearLayout(getContext());
columnSmall.setLayoutParams(paramSmall);
//Large column
LinearLayout columnLarge = new LinearLayout(getContext());
columnLarge.setLayoutParams(paramLarge);
//First get rid of all the views, incase of refresh
mLinearLayout.removeAllViews();
TextView textView = new TextView(getContext());
//CUSTOMER
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
textView.setLayoutParams(fullwidth);
textView.setText("Mary Jane");
columnShell.addView(textView);
mLinearLayout.addView(columnShell);
LinearLayout columnRight = new LinearLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
columnRight.setLayoutDirection(LinearLayout.HORIZONTAL);
//LEFT SIDE (1/4)
textView = new TextView(getContext());
textView.setLayoutParams(fullwidth);
textView.setText("PO: ");
columnSmall.addView(textView);
columnShell.addView(columnSmall);
//RIGHT SIDE (3/4)
textView = new TextView(getContext());
textView.setLayoutParams(fullwidth);
textView.setText("4465465456");
columnLarge.addView(textView);
columnShell.addView(columnLarge);
mLinearLayout.addView(columnShell);
}
}
fragment_payment.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:orientation="vertical"
tools:context="com.mycompany.myapp.Views.MasterDetails.PaymentFragment">
<ProgressBar
android:id="#+id/pbPayment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:id="#+id/svPayment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/COLOR_LIGHT_GREY">
<LinearLayout
android:id="#+id/llPayment"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"></LinearLayout>
</ScrollView>
</LinearLayout>
Why use scrollview with linearlayout, you might have to use listview with two textview as chid, and to add new data just add it into list and notify to adpter, your new data automatically added last of the list.
I am trying to inflate a EditText view inside a RelativeLayout in my Activity.
But getting an error says:
The specified child already has a parent. You must call removeView() on the child's parent first.
My code:
RelativeLayout relLayout= new RelativeLayout(insideActivity);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relLayout.setLayoutParams(params);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.holo_edit_text, null);
EditText searchBox = (EditText)view.findViewById(R.id.holo_text);
relLayout.addView(searchBox);
And here is holo_edit_text.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/holo_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true" />
</RelativeLayout>
your holo_edit_text.xml should be
<?xml version="1.0" encoding="utf-8"?>
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/holo_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
And the way you add it to the view should be like this
EDIT (use getLayoutInflater() instead)
View view = getLayoutInflater().inflate(R.layout.holo_edit_text, null);
//EditText searchBox = (EditText)view.findViewById(R.id.holo_text);
//The last sentence is not neccesary
relLayout.addView(view);
Have you tried this while adding to relative layout
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
relLayout.addView(view, params);
Try this..
View child_view = LayoutInflater.from(this).inflate(R.layout.holo_edit_text, null);
relLayout.addView(child_view);
Use a try/catch mechanism.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (rootView != null) {
((ViewGroup) rootView.getParent()).removeView(rootView);
}
try {
rootView = inflater.inflate(R.layout.your_layout_file_here, container,false);
EditText searchBox = (EditText)rootView.findViewById(R.id.holo_text);
relLayout.addView(searchBox);
} catch (InflateException e) {
}
return rootView;
}
I have fixed my issue by simply setting the LayoutParams to searchBox before adding it to the RelativeLayout. And its working fine.
Thanks all for your response.
I am trying to create a fragment. In the fragment I plan to place a text view next to it a Spinner control. I tried using this code. However Spinner always is placed over textview to the left of the screen. Can anyone suggest what could be the problem?
Code:
public class FragmentTest extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
RelativeLayout view = (RelativeLayout)inflater.inflate(R.layout.ll2, null);
RelativeLayout lref = (RelativeLayout)view.findViewById(R.id.ll2ll);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(lref.getLayoutParams());
lp.addRule(RelativeLayout.ALIGN_LEFT);
RelativeLayout rl = new RelativeLayout(getActivity());
TextView tv = new TextView(getActivity());
tv.setId(1);
tv.setLayoutParams(lp);
tv.setText("My ");
rl.addView(tv, lp);
Spinner s = new Spinner(getActivity());
lp.addRule(RelativeLayout.RIGHT_OF, tv.getId());
s.setId(2);
String currencyData[] = {"USD",
"EUR","INR"
};
ArrayAdapter<Object> currencyAdapter; currencyAdapter = new ArrayAdapter<Object>(getActivity(), android.R.layout.simple_spinner_item, currencyData);
currencyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(currencyAdapter);
rl.addView(s,lp);
return rl;
}
}
XML file is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="300dp"
android:layout_height="fill_parent"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:isScrollContainer="true"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/ll2ll"
android:layout_width="140dp"
android:layout_height="50dp"
android:textSize="#dimen/font_size" />
</RelativeLayout>
When a view is inflated from a XML you will not get the LayoutParams properly. Try using new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(lref.getLayoutParams());
your problem is this, you can't use the same layout parameters for adding all the layouts and modifying the lp.
because the lp is used after you have initialized all the layouts, that means you are using same parameters for all the layouts spinner and textview both.
try using different layout parameters for all the layouts
RelativeLayout.LayoutParams lp
RelativeLayout.LayoutParams lptextview
RelativeLayout.LayoutParams lpspinner
for all of them
if your problem is solved mark my answser as correct
I am trying to create a splitView in Android programmatically. This feature is completely new to me. After some research, I realized the Fragment has to be used.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="#+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
I am wondering, is the LinearLayout the best option for a layout? Secondly is it possible to add UI parts like buttons, pickers onto the fragment programmatically as well?
If you dont want to use fragment layout or building application in lower android version which does not have fragment support, in this case you can get the split view by calling two separate XML files in one activity. Just but build two xml as per requirement on left and right. Small example
LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.HORIZONTAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(
R.layout.firstxml, null);
RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(
R.layout.secondxml, null);
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutMain.addView(layoutLeft, 100, 100);
layoutMain.addView(layoutRight, relParam);
}
`
You can use any layout with Fragments, totally depends on your requirement.
Yes you can add views like Buttons etc to Fragment programmatically. Declare the layout of the Fragment to be a Layout.
One example of adding views programmatically --- R.layout.main could be a LinearLayout with no child views
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup mViewGroup = inflater.inflate(R.layout.main, container, false);
Button mButton = new Button(getActivity());
mButton.setLayoutParameters(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
mViewGroup.add(mButton);
}