I want to create a dynamic layout but I had error
The specified child already has a parent. You must call removeView() on the child's parent first.
I found something on Stackoverlow but most of all suggest to use this line that I already use:
View rootView = inflater.inflate(R.layout.fragment_item, container, false);
Any solution??
ItemFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_items, container, false);
layout = (TableLayout) rootView.findViewById(R.id.itmes_fragment_layout);
item_folder_button = new Button(getActivity());
item_folder_button = (Button) rootView.findViewById(R.id.items_button_layout);
return rootView;
}
private void createItemsButtons() {
layout.removeAllViews();
if (itemscontainer.length == 0) {
TableRow row = new TableRow(myContext);
TextView no_items = new TextView(myContext);
no_items.setText(getResources().getString(R.string.no_items));
no_items.setTextSize(35);
row.addView(no_items);
layout.addView(row);
} else {
for (int i = 0; i <= itemscontainer.length; ) {
TableRow tableRow = new TableRow(getActivity());
TableRow.LayoutParams param = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
tableRow.setLayoutParams(param);
tableRow.setGravity(Gravity.CENTER_VERTICAL);
for (int y = 0; (y < 3) && (i <= itemscontainer.length); y++) {
item_folder_button.setText(("Text " + i));
item_folder_button.setVisibility(View.VISIBLE);
item_folder_button.setGravity(Gravity.CENTER);
TableRow.LayoutParams par = new TableRow.LayoutParams(y);
item_folder_button.setLayoutParams(par);
int id = i;
item_folder_button.setId(id);
item_folder_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int btn_selected = item_folder_button.getId();
Fragment fragment = new ItemFragment();
if (fragment != null) {
FragmentManager fragmentManager = myContext.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment).addToBackStack(null);
fragmentTransaction.commit();
}
}
});
tableRow.addView(item_folder_button);
i++;
}
layout.removeView(layout.getRootView());
layout.addView(tableRow);
}
}
}
return rootView;
}
fragment_items.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/items_fragment_scrollview_layout"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:id="#+id/items_fragment_linearlayout"
>
<TableLayout
android:id="#+id/items_fragment_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:stretchColumns="*"
android:gravity="center_vertical" />
<Button
android:id="#+id/items_button_layout"
android:layout_width="300dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="40"
android:drawableLeft="#drawable/folder"
android:paddingLeft="10dp"
android:text="#string/text"
android:background="#drawable/item_button"
android:drawablePadding="-75dp"
android:visibility="gone"/>
</LinearLayout>
Related
Here is my code:
DetailsFragment fragment = new DetailsFragment();
Transition changeTransform = TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_transform);
Transition explodeTransform = TransitionInflater.from(getActivity()).
inflateTransition(android.R.transition.explode);
setSharedElementReturnTransition(changeTransform);
setExitTransition(explodeTransform);
// Setup enter transition on second fragment
fragment.setSharedElementEnterTransition(changeTransform);
fragment.setEnterTransition(explodeTransform);
FragmentManager manager = ((Activity)mContext).getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.layoutView, fragment).addToBackStack(fragment.getClass().getSimpleName())
.addSharedElement(view.findViewById(R.id.itemView), "view");
fragment.commit();
In DetailsFragment giving the same transition name("view" as Assign a Common Transition Name) in Layout for ImageView but I not able to see any transition on Android Lollipop.
I have already declared the manifests file style for (Enable Window Content Transitions) -
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowContentTransitions">true</item>
</style>
I have searched a lot but didn't get answer.
First need to Create
transition directory under project res directory.
Create change_image_trans.xml under transition directory.
<?xml version="1.0" encoding="utf-8"?>
<transitionSet>
<changeTransform/>
<changeBounds/>
</transitionSet>
Activity like this -
public class DemoActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
ListFragment listFragment = new ListFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, listFragment)
.commit();
}
}
My Activity fragment like this -
activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
here is my fragment to open -
ListFragment.java
public class ListFragment extends Fragment implements AbsListView.OnItemClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_list, container, false);
ListView listView = (ListView) view.findViewById(R.id.listView);
String [] strings = {"First Element", "Second Element", "Third Element"};
MyListAdapter myListAdapter = new MyListAdapter(getActivity(), R.layout.list_item, strings);
listView.setAdapter(myListAdapter);
listView.setOnItemClickListener(this);
return view;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String imageTransitionName = "";
String textTransitionName = "";
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView textView = (TextView) view.findViewById(R.id.textView);
ImageView staticImage = (ImageView) getView().findViewById(R.id.imageView);
LastFragment lastFragment = new LastFragment();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setSharedElementReturnTransition(TransitionInflater.from(
getActivity()).inflateTransition(R.transition.change_image_trans));
setExitTransition(TransitionInflater.from(
getActivity()).inflateTransition(android.R.transition.fade));
lastFragment.setSharedElementEnterTransition(TransitionInflater.from(
getActivity()).inflateTransition(R.transition.change_image_trans));
lastFragment.setEnterTransition(TransitionInflater.from(
getActivity()).inflateTransition(android.R.transition.fade));
imageTransitionName = imageView.getTransitionName();
textTransitionName = textView.getTransitionName();
}
Bundle bundle = new Bundle();
bundle.putString("TRANS_NAME", imageTransitionName);
bundle.putString("ACTION", textView.getText().toString());
bundle.putString("TRANS_TEXT", textTransitionName);
bundle.putParcelable("IMAGE", ((BitmapDrawable) imageView.getDrawable()).getBitmap());
lastFragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, lastFragment)
.addToBackStack("Payment")
.addSharedElement(imageView, imageTransitionName)
.addSharedElement(textView, textTransitionName)
.addSharedElement(staticImage, getString(R.string.fragment_image_trans))
.commit();
}
}
//MyListAdapter for creating list item
class MyListAdapter extends ArrayAdapter<String> {
public MyListAdapter(Context context, int resource, String[] objects) {
super(context, resource, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.list_item, null);
}
String listItem = getItem(position);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(listItem);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.logo);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textView.setTransitionName("trans_text" + position);
imageView.setTransitionName("trans_image" + position);
}
return view;
}
}
fragment_list.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="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:src="#drawable/logo"
android:transitionName="#string/fragment_image_trans"/>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/imageView">
</ListView>
</RelativeLayout>
LastFragment.java
public class LastFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = getArguments();
String actionTitle = "DUMMY ACTION";
Bitmap imageBitmap = null;
String transText = "";
String transitionName = "";
if (bundle != null) {
transitionName = bundle.getString("TRANS_NAME");
actionTitle = bundle.getString("ACTION");
imageBitmap = bundle.getParcelable("IMAGE");
transText = bundle.getString("TRANS_TEXT");
}
getActivity().setTitle(actionTitle);
View view = inflater.inflate(R.layout.fragment_last, container, false);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.findViewById(R.id.listImage).setTransitionName(transitionName);
view.findViewById(R.id.textView).setTransitionName(transText);
}
((ImageView) view.findViewById(R.id.listImage)).setImageBitmap(imageBitmap);
((TextView) view.findViewById(R.id.textView)).setText(actionTitle);
return view;
}
}
R.layout.fragment_last
<?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="match_parent"
android:padding="24dp"
android:orientation="vertical">
<ImageView
android:id="#+id/listImage"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="#drawable/logo"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Sample"
android:textSize="24sp"/>
<ImageView
android:id="#+id/otherImage"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="24dp"
android:src="#drawable/logo"
android:transitionName="#string/fragment_image_trans"/>
</RelativeLayout>
When I run my program I get the error E/AndroidRuntime(27275): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. where is the problem??
My list fragment class
package com.zaa.yhgj;
public class HomeFragment extends ListFragment {
private List<ListViewItem> mItems; // ListView items list
View myFragmentView;
ArrayList<View> chart_view = new ArrayList<View>();
List<Integer> onedimen = new ArrayList<Integer>();
List<List> aa = new ArrayList<List>();
///
List<List> statuslistoflist = new ArrayList<List>();
List<String> statusonedimen = new ArrayList<String>();
private List<DialogInterface.OnClickListener> nItems;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.dashboardlist, container, false);
HashMap<String,Integer> myhash=new HashMap<String, Integer>() ;
myhash.put("Pending",0);
myhash.put("In Progress",0);
myhash.put("Limitation",0);
myhash.put("Needs Research",0);
myhash.put("In Testing",0);
myhash.put("Issue Not Clear",0);
myhash.put("Unassigned",0);
int mainclasssize = Constants.listuserprojectMain.getMainListClass().size();
int[] listsize = new int[mainclasssize];
String[] projectname = new String[mainclasssize];
int[] openissuecount = new int[mainclasssize];
for (int k = 0; k < mainclasssize; k++) {
listsize[k] = Constants.listuserprojectMain.getMainListClass().get(k).getProjectIssue().size();
projectname[k] = Constants.listuserprojectMain.getMainListClass().get(k).getName();
openissuecount[k] = Constants.listuserprojectMain.getMainListClass().get(k).getOpenIssueCount();
}
for (int w = 0; w < mainclasssize; w++) {
onedimen = new ArrayList<Integer>();
for (int x = 0; x < listsize[w]; x++) {
myhash.put(Constants.listuserprojectMain.getMainListClass().get(w).getProjectIssue().get(x).getStatus(),Constants.listuserprojectMain.getMainListClass().get(w).getProjectIssue().get(x).getIssueCount());
}
Set keys = myhash.keySet();
Iterator itr = keys.iterator();
String key;
int value;
while(itr.hasNext())
{
key = (String)itr.next();
onedimen.add((Integer)myhash.get(key));
value = (Integer)myhash.get(key);
// System.out.println(key + " - "+ value);
}
aa.add(onedimen);
statuslistoflist.add(statusonedimen);
}
for (int v = 0; v < aa.size(); v++) {
chart_view.add(openChart(aa.get(v)));
}
mItems = new ArrayList<ListViewItem>();
nItems = new ArrayList<DialogInterface.OnClickListener>();
Resources resources = getResources();
for (int f = 0; f < aa.size(); f++) {
mItems.add(new ListViewItem(projectname[f], openissuecount[f], chart_view.get(f)));
}
nItems.add(new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
// initialize and set the list adapter
setListAdapter(new ListViewDemoAdapter(getActivity(), mItems));
return myFragmentView;
}
private View openChart(List<Integer> chartvaluedd) {
return v;
}
}
my adapter class is
public class ListViewDemoAdapter extends ArrayAdapter {
public ListViewDemoAdapter(Context context, List<ListViewItem> items) {
super(context, R.layout.dashboardlistview_item, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder= new ViewHolder();
if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.dashboardlistview_item, parent, false);
// initialize the view holder
// viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.graphView = (LinearLayout) convertView.findViewById(R.id.ll_chart);
viewHolder.tvCreateIssue = (TextView) convertView.findViewById(R.id.tvCreateissue);
viewHolder.tvDetails = (TextView) convertView.findViewById(R.id.tv_details);
viewHolder.tvIssue = (TextView) convertView.findViewById(R.id.tv_issues);
viewHolder.tvProjectTitle = (TextView) convertView.findViewById(R.id.tv_projectTitle);
viewHolder.tvOpenIssueCount = (TextView) convertView.findViewById(R.id.tv_openissues);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
// recycle the already inflated view
///error when scrolling graph
viewHolder.graphView.removeAllViews();
}
// update the item view
final ListViewItem item = getItem(position);
viewHolder.tvCreateIssue.setText("Create Issue");
viewHolder.tvProjectTitle.setText(item.title);
viewHolder.graphView.addView(item.chartview);
viewHolder.tvOpenIssueCount.setText("Open Issues "+item.openissuecount);
viewHolder.tvCreateIssue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), item.title, Toast.LENGTH_SHORT).show();
}
});
/////
viewHolder.tvIssue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(),"issue",Toast.LENGTH_LONG).show();
}
});
return convertView;
}
private static class ViewHolder {
// ImageView ivIcon;
LinearLayout graphView;
TextView tvCreateIssue;
TextView tvDetails;
TextView tvProjectTitle;
TextView tvOpenIssueCount;
TextView tvIssue;
}
}
my xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include layout="#layout/top" />
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
</ListView>
</LinearLayout>
my listview item is
<?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="match_parent"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="#color/list_background"
android:padding="5dp"
android:id="#+id/ww"
>
<!-- the innner view - provides the white rectangle -->
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#drawable/frame"
android:id="#+id/jhhh">
<!-- the icon view -->
<TextView
android:id="#+id/tv_projectTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project name"
android:textSize="16dp"
android:textStyle="bold"
android:layout_centerHorizontal="true"
/>
<LinearLayout android:id="#+id/ll_chart"
android:orientation="vertical"
android:layout_below="#+id/tv_projectTitle"
android:layout_width="wrap_content"
android:layout_height="230dp"
android:background="#ffffff"
android:padding="5dp"
android:scaleType="fitXY" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ll_chart"
android:id="#+id/tv_openissues"
android:text="Open Issues 24"/>
<LinearLayout
android:layout_width="wrap_content"
android:id="#+id/ll_textview"
android:padding="5dp"
android:layout_alignParentRight="true"
android:layout_below="#id/tv_openissues"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView android:id="#+id/tvCreateissue"
android:drawableLeft="#drawable/bug6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Issue"
/>
<!-- the description view -->
<TextView android:id="#+id/tv_details"
android:layout_below="#id/tvCreateissue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/bars64"
android:layout_marginLeft="5dp"
android:text="Details"
/>
<TextView android:id="#+id/tv_issues"
android:layout_below="#id/tvCreateissue"
android:layout_marginLeft="5dp"
android:drawableLeft="#drawable/bug6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Issues"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I might be wrong, but the error seems to reside here:
viewHolder.graphView.addView(item.chartview);
Once the view item.chartview is added to graphView, it has a parent. However ListView is known to call some getView on some positions multiple times for caching.
It would seem that the same view is being added to multiple parents which in return throws the error you get. Before adding the chartview you could try removing it from its parent if it exists:
// Remove from parent (if exists)
ViewGroup parent = item.chartview.getParent();
if (parent != null) {
parent.removeView(item.chartview);
}
// Add to another parent
viewHolder.graphView.addView(item.chartview);
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.dashboardlist, container, false);
instead of container use null
(I'm not an android geek but faced the problem before)
I have created dynamically Table Layout. However, my Title Row pushes downward when other rows are dynamically created. I want to make the title stay at the top.
The XML code:
<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"
tools:context=".FirstScreen"
android:orientation="vertical"
android:background="#color/white" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Shopping Cart"
android:textColor="#898989"
android:textSize="17dp"
android:textStyle="bold" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/displayLinear"
android:layout_marginLeft="10dp"
android:background="#ffffff"
android:orientation="vertical" >
</TableLayout>
<Button
android:id="#+id/second"
style="#style/btnStyleShakespeare"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="10dp"
android:text="Checkout"
android:textColor="#ffffff" />
<Button
android:id="#+id/scanMore"
style="#style/btnStyleShakespeare"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="5dp"
android:text="Scan More"
android:textColor="#ffffff" />
</LinearLayout>
This is my dynamically created code.
package info.androidhive.slidingmenu;
public class ScreenFirstFragment extends Fragment {
public ScreenFirstFragment(){}
public static double pFinalPrice;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_firstscreen, container, false);
//final LinearLayout lm = (LinearLayout) rootView.findViewById(R.id.linearMain);
final Button secondBtn = (Button) rootView.findViewById(R.id.second);
final Button scanMoreBtn = (Button) rootView.findViewById(R.id.scanMore);
//Get Global Controller Class object (see application tag in AndroidManifest.xml)
final Controller aController = (Controller) getActivity().getApplicationContext();
/******* Create view elements dynamically and show on activity ******/
//Product arraylist size
int ProductsSize = aController.getProductsArraylistSize();
/******** Dynamically create view elements - Start **********/
TableLayout ll = (TableLayout) rootView.findViewById(R.id.displayLinear);
ll.setStretchAllColumns(true);
ll.setShrinkAllColumns(true);
TableRow rowProdLabels = new TableRow(this.getActivity());
// Product column
TextView prodLabel = new TextView(this.getActivity());
prodLabel.setText("PRODUCT");
prodLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowProdLabels.addView(prodLabel);
// Price column
TextView priceLabel = new TextView(this.getActivity());
priceLabel.setText("PRICE");
priceLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowProdLabels.addView(priceLabel);
// Quantity column
TextView quantityLabel = new TextView(this.getActivity());
quantityLabel.setText("Quantity");
quantityLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowProdLabels.addView(quantityLabel);
// Total column
TextView totalLabel = new TextView(this.getActivity());
totalLabel.setText("Total Price");
totalLabel.setTypeface(Typeface.SERIF, Typeface.BOLD);
rowProdLabels.addView(totalLabel);
ll.addView(rowProdLabels);
for(int j=0;j< ProductsSize;j++)
{
// Get probuct data from product data arraylist
String pName = aController.getProducts(j).getProductName();
double pPrice = aController.getProducts(j).getProductPrice();
int pQuantity = aController.getProducts(j).getProductQuantity();
double pTotalPrice = pPrice * pQuantity;
TableRow row= new TableRow(this.getActivity());
TextView product = new TextView(this.getActivity());
product.setText(pName+" ");
//Add textView to LinearLayout
row.addView(product);
TextView price = new TextView(this.getActivity());
price.setText("RM "+pPrice+" ");
//Add textView to LinearLayout
row.addView(price);
TextView quantity = new TextView(this.getActivity());
quantity.setText(pQuantity+" ");
//Add textView to LinearLayout
row.addView(quantity);
TextView totalprice = new TextView(this.getActivity());
totalprice.setText(String.format("RM %.2f",pTotalPrice));
//Add textView to LinearLayout
row.addView(totalprice);
//Update final price
pFinalPrice += pTotalPrice;
ll.addView(row,j);
}
/******** Dynamically create view elements - End **********/
secondBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.replace(R.id.frame_container, new ScreenSecondFragment(), "Cart");
mFragmentTransaction.commit();
}
});
scanMoreBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
FragmentTransaction mFragmentTransaction = getFragmentManager().beginTransaction();
mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
mFragmentTransaction.replace(R.id.frame_container, new ScanFragment(), "Scan Product");
mFragmentTransaction.commit();
}
});
return rootView;
}
}
(SOLVED) -- Removed layout params on each view ---
I am new to Android and I am trying to add rows in a TableLayout from the items that I entered in text fields.
(EDITED) Here's my layout for this Fragment:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:tag="contacts">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/contactName"
android:hint="Name"
android:paddingTop="5dip" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/contactRelationship"
android:hint="Relationship"
android:paddingTop="5dip" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:ems="10"
android:id="#+id/contactAddress"
android:hint="Address"
android:paddingTop="5dip" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/contactPhoneNo"
android:hint="Phone Number"
android:paddingTop="5dip" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/contactPhoneTypeSpinner"
android:layout_toEndOf="#+id/contactPhoneNo"
android:layout_toRightOf="#+id/contactPhoneNo"
android:paddingTop="5dip" />
</RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="107dp"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/contactSpecialNotes"
android:paddingTop="5dip"
android:hint="Write special notes here" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Contact"
android:id="#+id/addContact" />
<TableLayout
android:id="#+id/contactsTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dip"></TableLayout>
</LinearLayout>
</ScrollView>
(EDITED)Here's the code for this Fragment:
public static class ContactsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_contacts,
container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Spinner phoneTypeSpinner = (Spinner) this.getView().findViewById(R.id.contactPhoneTypeSpinner);
Button contactButton = (Button) this.getView().findViewById(R.id.addContact);
final TableLayout tableLayout = (TableLayout) this.getView().findViewById(R.id.contactsTableLayout);
final EditText txtContactName = (EditText) this.getView().findViewById(R.id.contactName);
final EditText txtContactRelationship = (EditText) this.getView().findViewById(R.id.contactRelationship);
final EditText txtContactAddress = (EditText) this.getView().findViewById(R.id.contactAddress);
final EditText txtContactPhoneNo = (EditText) this.getView().findViewById(R.id.contactPhoneNo);
Spinner contactPhoneTypeSpinner = (Spinner) this.getView().findViewById(R.id.contactPhoneTypeSpinner);
EmployeeDataSource datasource = new EmployeeDataSource(getActivity());
datasource.open();
//--- Gender Spinner ---
List<PhoneType> list = datasource.getPhoneTypeList();
ArrayAdapter<PhoneType> adapter = new ArrayAdapter<PhoneType>(getActivity(),
android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
phoneTypeSpinner.setAdapter(adapter);
phoneTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
contactButton.setOnClickListener(new View.OnClickListener() {
int rowId = 0;
#Override
public void onClick(View v) {
TableRow tableRow = new TableRow(getActivity().getApplicationContext());
tableRow.setId(rowId++);
tableRow.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tvContactName = new TextView(getActivity().getApplicationContext());
tvContactName.setText(txtContactName.getText().toString());
tvContactName.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.addView(tvContactName);
TextView tvRelationship = new TextView(getActivity().getApplicationContext());
tvRelationship.setText(txtContactRelationship.getText().toString());
//tvRelationship.setLayoutParams(new ViewGroup.LayoutParams(
// ViewGroup.LayoutParams.FILL_PARENT,
// ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.addView(tvRelationship);
TextView tvContactNo = new TextView(getActivity().getApplicationContext());
tvContactNo.setText(txtContactPhoneNo.getText().toString());
//tvContactNo.setLayoutParams(new ViewGroup.LayoutParams(
// ViewGroup.LayoutParams.FILL_PARENT,
// ViewGroup.LayoutParams.WRAP_CONTENT));
tableRow.addView(tvContactNo);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
});
}
}
I could see in the debugger that the values from my views were added in my tableRow but I don't see the table appearing in my screen, what am I missing or doing wrong?
Any help would be greatly appreciated.
Thanks!
I guess the height of screen is increased. Try using Scrollview on top of this layout.
I think you have to change RelativeLayout property android:height="wrap_content" instead of android:height="fill_parent" and also do for TableLayout
May this work for you...
Try this:
Edited:
public static class ContactsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_section_contacts,
container, false);
Spinner phoneTypeSpinner = (Spinner) rootView.findViewById(R.id.contactPhoneTypeSpinner);
Button contactButton = (Button) rootView.findViewById(R.id.addContact);
final TableLayout tableLayout = (TableLayout) rootView.findViewById(R.id.contactsTableLayout);
final EditText txtContactName = (EditText) rootView.findViewById(R.id.contactName);
final EditText txtContactRelationship = (EditText) rootView.findViewById(R.id.contactRelationship);
final EditText txtContactAddress = (EditText) rootView.findViewById(R.id.contactAddress);
final EditText txtContactPhoneNo = (EditText) rootView.findViewById(R.id.contactPhoneNo);
Spinner contactPhoneTypeSpinner = (Spinner) rootView.findViewById(R.id.contactPhoneTypeSpinner);
EmployeeDataSource datasource = new EmployeeDataSource(getActivity());
datasource.open();
//--- Gender Spinner ---
List<PhoneType> list = datasource.getPhoneTypeList();
ArrayAdapter<PhoneType> adapter = new ArrayAdapter<PhoneType>(getActivity(),
android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
phoneTypeSpinner.setAdapter(adapter);
phoneTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
contactButton.setOnClickListener(new View.OnClickListener() {
int rowId = 0;
#Override
public void onClick(View v) {
TableRow tableRow = new TableRow(getActivity().getApplicationContext());
tableRow.setId(rowId++);
TextView tvContactName = new TextView(getActivity().getApplicationContext());
tvContactName.setText(txtContactName.getText().toString());
tableRow.addView(tvContactName);
TextView tvRelationship = new TextView(getActivity().getApplicationContext());
tvRelationship.setText(txtContactRelationship.getText().toString());
tableRow.addView(tvRelationship);
TextView tvContactNo = new TextView(getActivity().getApplicationContext());
tvContactNo.setText(txtContactPhoneNo.getText().toString());
tableRow.addView(tvContactNo);
tableLayout.addView(tableRow, new TableLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
});
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
I have an application which has 3 fragments. And it works fine viewpager.
But I need to implement similar view like in Android Play store. Initially they have "Featured" Tab. When you swipe left we can see "Categories" tab.
But half of the screen still filled with "Featured" tab contents. How can I implement that view? Any idea?
Here is the actual answer. Posting after a long time.
Add this method in your adapter:
#Override
public float getPageWidth(int position) {
if (position == 0) {
return(0.5f);
} else {
return (1.0f);
}
}
Check out ViewPagerIndicator.
Try the sample first if you want to see how it looks like.
The one you want is the Titles section.
In order to display two fragment in one view you can add a layout which width & height will be wrap content in one fragment's layout then on your onCreateView add the other fragment using fragment manager.
Code will be like this --
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<LinearLayout
android:id="#+id/ll1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:id="#+id/tb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"/>
</LinearLayout>
And onCreate Will be Like --
View view ;
boolean isVisible = false;
FragmentManager fm;
FragmentTransaction ft;
Fragment menuFragment;
LinearLayout llList;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.main, container, false);
llList = (LinearLayout) view.findViewById(R.id.ll1);
menuFragment = new frg1();
fm = getFragmentManager();
Button tb = (Button) view.findViewById(R.id.tb);
tb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!isVisible) {
isVisible = true;
showList();
}
else {
isVisible = false;
hideList();
}
}
});
return view;
}
private void changeDisplay() {
Display display = getActivity().getWindowManager().getDefaultDisplay();
int width = (int) (display.getWidth() * 0.8 );
int height = (int) (display.getHeight() * 0.8);
if(isVisible)
{
llList.setLayoutParams(new LinearLayout.LayoutParams(width, LayoutParams.FILL_PARENT));
//lllList.setLayoutParams(new LinearLayout.LayoutParams(p))
}
else {
llList.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT));
//llList.scrollTo(0, 0);
}
}
private void showList() {
changeDisplay();
ft = fm.beginTransaction();
ft.add(R.id.ll1, menuFragment);
ft.commit();
}
private void hideList() {
changeDisplay();
ft = fm.beginTransaction();
ft.remove(menuFragment);
ft.commit();
}