My goal is to display a chessboard. I have an Activity which displays a menu and load an instance of "ChessView" which extends GridView. I've created a custom GridView because I want to have a class fully dedicated to its display. So that class have its own layout.xml etc ...
GameActivity.java :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
//init a game
CChess.getInstance().init();
//creating the chessboard
ChessView lChessView = (ChessView) findViewById(R.id.chessView);
}
activity_game.xml :
<RelativeLayout 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:background="#color/background"
tools:context=".GameActivity" >
<project.chess.view.ChessView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ChessView" >
</project.chess.view.ChessView>
</RelativeLayout>
ChessView.java :
public class ChessView extends GridView {
public ChessView(Context pContext, AttributeSet pAttrs) {
super(pContext, pAttrs);
GridView.inflate(pContext, R.layout.view_chess, null);
this.setAdapter(new ChessAdapter(pContext));
}
}
view_chess.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" >
<GridView
android:id="#+id/gridView"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:numColumns="8"
android:gravity="center"
>
</GridView>
</LinearLayout>
ChessAdapter.java :
public class ChessAdapter extends BaseAdapter
{
private Context mContext;
public ChessAdapter (Context pContext)
{
this.mContext = pContext;
}
#Override
public int getCount()
{
return 64;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView lImageView;
if (convertView == null) {
lImageView = new ImageView(mContext);
int size = parent.getWidth()/8;
lImageView.setLayoutParams(new GridView.LayoutParams(size,size));
//background black or white depending of the position
int col = position/8 %2;
if (col == 0)
{
if (position%2 == 0)
lImageView.setBackgroundColor(Color.WHITE);
else
lImageView.setBackgroundColor(Color.BLACK);
}
else
{
if (position%2 == 0)
lImageView.setBackgroundColor(Color.BLACK);
else
lImageView.setBackgroundColor(Color.WHITE);
}
//load images
CPiece p = CChess.getInstance().getPiece(position/8, position%8);
if( p != null)
lImageView.setImageResource(mContext.getResources().getIdentifier(p.toString(), "drawable", mContext.getPackageName()));
} else {
lImageView = (ImageView) convertView;
}
return lImageView;
}
}
The result is : i have just 1 column of 64 squares all with an image (my init() in CChess is ok). To be clear, i want a 8x8 grid like all standards chessboards.
Few days ago, i had all this stuff in one activity without my custom view but just a simple gridView and all was working correctly. I have broken it since i've decided to separate my code in different classes
I'm sure i forgot something like an inflater somewhere but i don't understand how it works and what it does. I've parsed google for hours but i couldn't find the answer.
Can you help me please ? Thanks a lot for reading me and sorry for my english
I Think the problem with this LayoutInflator in ChessView.java
remove this line from that class
No Need to Inflate the GridView in that class
GridView.inflate(pContext, R.layout.view_chess, null);
and set the Column count and gravity in Chess View in activity_game.xml file.
android:layout_width="wrap_content"
android:numColumns="8"
I Think this works for you.
Related
I'm getting a very odd (to me) issue when populating a GridView - when it first loads all looks fine, but scrolling ends up with data from cells moving to other positions. In the following example I just have an ArrayList of the numbers 0-800, and an 8 column grid. When loaded 0-7 is in the first row, 8-15 in the second etc, with a different value in the first cell in each row. But after scrolling the first column value will change. Here's the code - it's driving me crazy!
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<Integer> positions = new ArrayList<Integer>();
for(int i=0;i<800;i++) {
positions.add(i);
}
GridView gvVals = findViewById(R.id.gvVals);
CellAdapter adapter = new CellAdapter(this, new ArrayList<Integer>());
adapter = new CellAdapter(this, positions);
gvVals.setAdapter(adapter);
}
Cell Adapter.java:
public class CellAdapter extends ArrayAdapter<Integer> {
private Context context;
public CellAdapter(Context context, ArrayList<Integer> vals) {
super(context, 0, vals);
this.context = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = LayoutInflater.from(getContext()).inflate(R.layout.activity_cell, parent, false);
} else {
v = (View)convertView;
}
TextView tvDay = v.findViewById(R.id.tvVal);
tvDay.setVisibility(View.VISIBLE);
if(position % 8 == 0) {
tvDay.setText("ST:"+position);
}
else
{
v.findViewById(R.id.btnM).setVisibility(View.VISIBLE);
((Button)v.findViewById(R.id.btnM)).setText(position + " ");
tvDay.setText(" ");
}
return v;
}
ActivityMain.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<GridView
android:id="#+id/gvVals"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:numColumns="8"/>
</android.support.constraint.ConstraintLayout>
ActivityCell.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvVal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:visibility="invisible"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/btnM"
android:layout_width="0dp"
android:layout_weight=".34"
android:layout_height="20dp"
android:text="M"
android:background="#color/colorAccent"
android:minWidth="0dp"
android:visibility="invisible"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
GridView when app first opened
After scrolling down a few rows and then back up
Change this:
TextView tvDay = v.findViewById(R.id.tvVal);
tvDay.setVisibility(View.VISIBLE);
if(position % 8 == 0) {
tvDay.setText("ST:"+position);
}
else
{
v.findViewById(R.id.btnM).setVisibility(View.VISIBLE);
((Button)v.findViewById(R.id.btnM)).setText(position + " ");
tvDay.setText(" ");
}
to this:
boolean firstColumn = position % 8 == 0;
TextView tvDay = v.findViewById(R.id.tvVal);
tvDay.setVisibility(firstColumn ? View.VISIBLE : View.GONE);
Button btnM = v.findViewById(R.id.btnM);
btnM.setVisibility(firstColumn ? View.GONE : View.VISIBLE);
if(firstColumn) {
tvDay.setText("ST:"+position);
}
else
{
btnM.setText(position + " ");
}
It's important when working with RecyclerView to make sure that you always update every view every time. This is because old view holders get recycled, so if you only do something some of the time, it can get overwritten or it can overwrite the "expected" behavior.
Previously, you were making btnM visible if it wasn't the first column, but you weren't making it not visible if it was the first column.
I am working on a WearOS app. I am using a ListView to show a list of strings. Right now, the ListView looks like this:
I want a single row to take up the entire screen. Once a user swipes up, then Row 2 takes up the entire screen. Swipe up again and Row 3 takes up the entire screen, etc. So like this:
I came across this link, which is exactly what I want to do, but the first method doesn't work and the second method suggests a library, but I don't want to use a library for what seems like a pretty simple task. I don't want to use RecyclerView. Thank you for your help.
Here is my XML file for the main activity, which is where the ListView is.
<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout 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"
android:background="#color/dark_grey"
android:padding="#dimen/box_inset_layout_padding"
tools:context=".MainActivity"
tools:deviceIds="wear">
<!-- Change FrameLayout to a RelativeLayour -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/inner_frame_layout_padding"
app:boxedEdges="all">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
</android.support.wear.widget.BoxInsetLayout>
Try the following (The idea here is to programmatically set the height of the textView to the height of the screen):
1) MnnnnnnnActivity.class:----------
public class MnnnnnnnActivity extends AppCompatActivity {
private ListView lv;
private CustomAdapter customAdapter;
private String[] s = new String[10];
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout6);
for (int i = 0; i < 10; i++) {
s[i] = "ROW " + String.valueOf(i + 1);
}
lv = (ListView) findViewById(R.id.lv);
customAdapter = new CustomAdapter(MnnnnnnnActivity.this, s);
lv.setAdapter(customAdapter);
}
public int getScreenHeight() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics.heightPixels;
}
}
2) CustomAdapter.class:--------
public class CustomAdapter extends ArrayAdapter<String> {
private String[] s;
private WeakReference<MnnnnnnnActivity> mActivity;
public CustomAdapter(MnnnnnnnActivity activity1, String[] s) {
super(activity1.getApplicationContext(), R.layout.list_view_item, s);
this.s = s;
mActivity = new WeakReference<MnnnnnnnActivity>(activity1);
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(mActivity != null) {
MnnnnnnnActivity activity = mActivity.get();
if (activity != null) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(activity);
convertView = inflater.inflate(R.layout.list_view_item, null);
holder = new ViewHolder();
holder.tv = (TextView) convertView.findViewById(R.id.tv);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.tv.setText(s[position]);
holder.tv.setHeight(activity.getScreenHeight());
}
}
return convertView;
}
private class ViewHolder {
TextView tv;
}
}
3) layout6.xml:-----------
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/lv">
</ListView>
</android.support.constraint.ConstraintLayout>
4) list_view_item.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">
<TextView
android:id="#+id/tv"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="-"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
5) Note: Although this is tested on phone, the same idea can be used to achieve something similar on a wareable. Also, a better approach would be to set the height of the linearLayout (which contains the textView) to the screen height.
6) Output:
I inflate another layout to appear below some view in my current layout.
This is done like this:
LayoutInflater vi = (LayoutInflater) getActivity().getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rootView = vi.inflate(R.layout.horizontal_scroll_view, null);
horizontalScrollView = (HorizontalScrollView) rootView.findViewById(R.id.hsv_suggestions_scroll_view);
LinearLayout suggestionsContainer = (LinearLayout) horizontalScrollView.findViewById(R.id.ll_suggestions_container);
and I can confirm that it appears in the right place since I add some Views in it after a while and they all appear.
The layout I inflate is :
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/hsv_suggestions_scroll_view"
android:scrollbars="none" android:layout_gravity="center_horizontal"
android:paddingTop="16dp" android:fillViewport="true"
android:layout_width="match_parent" android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/ll_suggestions_container"
android:gravity="center_horizontal" android:orientation="horizontal"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</LinearLayout>
just a HorizontalScrollView with a LinearLayout as a child.
The Views I add later are all of them TextViews.
Now after a user action (write some text on an editText) I'm trying to scroll to that View and highlight it. Highlight works. What does not work is scroll.
I have tried :
horizontalScrollView.postDelayed(new Runnable() {
#Override
public void run() {
horizontalScrollView.smoothScrollTo(scrollTo, 0);
}
}, 300);
where variable scrollTo is what I get when I apply getLeft() to the View I wanna scroll to. I can confirm that it takes various values.
Anyone can help me with that ?
Got a similar issue.
I guess the root cause is that the UI was not yet rendered at that moment, causing the scroll not to work properly.
I just had to wrap my call to smoothScrollTo into .post as follows:
mScrollView.post(new Runnable() {
#Override
public void run() {
mScrollView.smoothScrollTo(xxx, yyy);
}
});
Notice that I do a post directly on the scrollView to make sure it is executed after it is being rendered. For example, doing a getActivity().runOnUIThread() would not work in my case.
Switch to using a RecyclerView with a LinearLayoutManager with orientation set to Horizontal. Like this:
scroll_view.xml
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/scrollView"
android:scrollbars="none"
android:layout_gravity="center_horizontal"
android:paddingTop="16dp"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/scrollContainer"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.RecyclerView>
Note: the layoutManager tag is required here for the layout to inflate. After it's inflated, we're going to set it programatically as well because otherwise we'll get an exception because the RecyclerView basically disposes of it before it's done with it.
OptionAdapter.java
public class OptionAdapter extends RecyclerView.Adapter<OptionAdapter.OptionHolder> {
private Context context;
private LayoutInflater inflater;
private ArrayList<String> options;
public OptionAdapter(Context context) {
this.context = context;
this.inflater = LayoutInflater.from(context);
options = new ArrayList<>();
}
#Override
public OptionHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new OptionHolder(inflater.inflate(R.layout.textview, parent, false));
}
#Override
public void onBindViewHolder(OptionHolder holder, int position) {
String option = options.get(position);
((TextView) holder.itemView).setText(option);
}
#Override
public int getItemCount() {
return options != null ? options.size() : 0;
}
public ArrayList<String> getOptions() {
return options;
}
public void addOption(String option, Integer index) {
if (index != null && index <= options.size()) {
options.add(index, option);
} else {
options.add(option);
}
notifyDataSetChanged();
}
public class OptionHolder extends RecyclerView.ViewHolder {
public OptionHolder(View itemView) {
super(itemView);
}
}
}
text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
Setting everything up
final LayoutInflater inflater = LayoutInflater.from(this);
final RecyclerView scrollView = (RecyclerView) inflater.inflate(R.layout.scroll_view, container, false);
scrollView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
final OptionAdapter adapter = new OptionAdapter(this);
scrollView.setAdapter(adapter);
container here is whatever view is going to be holding the RecyclerView. In my code, I've got it in a LinearLayout.
Adding a view to the list
adapter.addOption("The Added One", null);
Or if you want to add it to a specific position in the list.
adapter.addOption("The Added One", position);
Scrolling to a specific position
scrollView.smoothScrollToPosition(position);
Scrolling to a specific item in the list
scrollView.smoothScrollToPosition(adapter.getOptions().indexOf("ItemText"));
Hope it works for you!
Instead of smoothScrollTo(), try using scrollTo().
Make sure that your getLeft() is really returning a value > 0;
I know there are lots and lots of questions like the one that I'm going to ask, however, none of them can give me a solution. I've read blogs and questions here, but no luck. I have a ListView in Android, and I want to display some news with two types of views/layouts: one for news that have pictures, and another one for news that don't have (only text),so the app can display something like this. But everytime I run my app I am getting an error:
E/AndroidRuntime(3973): FATAL EXCEPTION: main
E/AndroidRuntime(3973): Process: com.example.test23, PID: 3973
E/AndroidRuntime(3973): java.lang.NullPointerException
E/AndroidRuntime(3973): at com.example.test23.ItemAdapter.getView(ItemAdapter.java:80)
E/AndroidRuntime(3973): at android.widget.AbsListView.obtainView(AbsListView.java:2263)
E/AndroidRuntime(3973): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
I am implementing the methods getItemViewType and getViewTypeCount(). I've implemented the famous ViewHolder but no luck either. Right now this is the Adapter class (cause I've changed it plenty of times):
public class ItemAdapter extends BaseAdapter implements OnItemClickListener {
Item item = null;
private ArrayList<Item> news = new ArrayList<Item>();
private static final int NO_PICTURE_VIEW = 0;
private static final int PICTURE_VIEW = 1;
public ItemAdapter(Context context, ArrayList<Item> items) {
super();
news.addAll(items);
}
#Override
public int getCount() {
return news.size();
}
#Override
public Item getItem(int index) {
return getItem(index);
}
#Override
public long getItemId(int index) {
return index;
}
public int getViewTypeCount() {
return 2;
}
public int getItemViewType(int position) {
Item article = news.get(position);
if (article.getImageUrl() != null && !article.getImageUrl().equals("")
&& article.getImageUrl().indexOf("no_pic.png") == -1)
// if there's no pic ?
// TYPE_SEPARATOR
// :
// TYPE_ITEM;
return NO_PICTURE_VIEW;
else
return PICTURE_VIEW;
}
#Override
public View getView(int index, View view, ViewGroup parent) {
item = news.get(index);
int type = getItemViewType(index);
int resource;
if (type == NO_PICTURE_VIEW) {
resource = R.layout.item_no_picture_list;
} else {
resource = R.layout.item_picture_list;
}
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(resource, parent, false);
TextView titleView = (TextView) view
.findViewById(R.id.titleArticle);
titleView.setText(item.getTitle());// this is the line that gives me
// the nullpointerexception
}
return view;
}
#Override
public void onItemClick(...) {
}
}
The layouts of both views are ok, I've tested them and no problem. I don't want like a separator, like I've seen on other blogs, I just want to be able to display both view for the same type of information, which is an array. If you need more info or more pieces of code, let me know. Any help or info is appreciated. Thanks in advance.
UPDATE here are the two layouts, for new with pics:
<ImageView
android:id="#+id/imageItem"
android:layout_width="80dp"
android:layout_height="80dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:contentDescription="DescripciĆ³n del contenido de la imagen"
android:src="#drawable/rr_logo"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/titleArticle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</LinearLayout>
</LinearLayout>
For news with no pics:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/itemNoPicture" >
<TextView
android:id="#+id/titleNoPicture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dp"/>
</LinearLayout>
There are only two reasons you could get the error
1) In either one of the layout files (item_no_picture_list or item_picture_list) there is no child with id titleArticle
2) The item in your arrayList news at index index is null
Check whichever is the case.. You can use Logs.. Write below code above the line which is causing error..
if(item == null)
Log.i("WhoIsNull", "item");
if(titleView == null)
Log.i("WhoIsNull", "titleView");
issue is this:
There are 49 items in the arraylist, and the adapter is stopping at item 3 without any error notification. The app does not crash, and no textviews are displayed. Here is the adapter:
public class ExhibitorObjectAdapter extends ArrayAdapter<ExhibitorObject> {
public ArrayList<ExhibitorObject> exhibitors;
public Activity act;
public TextView tvExhibitorName;
public ImageView ivExhibitorLogo;
public ExhibitorObjectAdapter(Activity a, int layoutResourceId, ArrayList<ExhibitorObject> ex) {
super(a, layoutResourceId,ex);
this.exhibitors = ex;
this.act = a;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return exhibitors.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.single_exhibitor, null);
}
ExhibitorObject ex = exhibitors.get(position);
if (ex != null) {
//System.out.println(ex.companyName);
tvExhibitorName = (TextView) v.findViewById(R.id.textViewListExhibitorName);
tvExhibitorName.setText( ex.companyName );
}
return v;
}
}
EDIT: here is the xml containing the listview:
<?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="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:background="#134882"
android:paddingBottom="15sp"
android:paddingTop="5sp"
android:paddingLeft="5sp"
android:textColor="#FFFFFF"
android:text="Exhibitors" />
<ListView android:id="#+id/listViewExhibitors"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
I have several other adapters that are exactly like this one and they work just fine. I have wasted an entire day on this (it's probably a silly problem), any help would be appreciated. Thanks.
It is bad to save tvExhibitorName in the scope of your adapter. Make it local within getView(), or you subject yourself to all kinds of leaks. That well may be the source of your problem.
Also, it is very strange that you don't inflate your new view under the parent. May be using inflate(layout, parent , false) will help.