I have Vertical ScrollView where I am going to add some Views after a buttonClick. Here is the xml of the scroll view
<ScrollView
android:background = "#drawable/border"
android:layout_weight="31"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="0dp" >
<LinearLayout
android:id="#+id/layoutScroll"
android:padding = "10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layoutContainer"
android:padding="5dp"
android:orientation="vertical"
android:background="#drawable/border_2"
android:layout_width="fill_parent"
android:layout_height = "wrap_content">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_height = "wrap_content"
android:layout_width = "fill_parent">
<TextView
android:id="#+id/textWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/word"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textNumber"
android:background = "#drawable/border_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text=" 1 "
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<EditText
android:id="#+id/editWord"
android:layout_marginTop="3dp"
android:layout_gravity="left"
android:inputType="text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:scrollbars="horizontal"/>
<TextView
android:id="#+id/textDefinition"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text = "#string/definition" />
<EditText
android:id="#+id/editDefinition"
android:gravity="top|left"
android:inputType="textMultiLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="3"
android:maxLines="3"
android:scrollbars="vertical"/>
</LinearLayout>
<TextView
android:id="#+id/textEmpty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</ScrollView>
In this function I create the same objects for adding them to the LinearLayout
private void initInterface (){
layoutScroll = (LinearLayout) findViewById (R.id.layoutScroll);
layoutContainer = new LinearLayout(getApplicationContext());
relativeLayout = new RelativeLayout(getApplicationContext());
textWord = new TextView(getApplicationContext());
textDefinition= new TextView(getApplicationContext());
textNumber = new TextView(getApplicationContext());
textEmpty = new TextView(getApplicationContext());
editWord = new EditText(getApplicationContext());
editDefinition = new EditText(getApplicationContext());
layoutContainer.setPadding(5, 5, 5, 5);
layoutContainer.setOrientation(LinearLayout.VERTICAL);
layoutContainer.setBackgroundResource(R.drawable.border_2);
LinearLayout.LayoutParams param1 =
new LayoutParams(LayoutParams.MATCH_PARENT , LayoutParams.WRAP_CONTENT );
layoutContainer.setLayoutParams(param1);
RelativeLayout.LayoutParams param2 =
new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(param2);
textWord.setLayoutParams(param2);
textWord.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textWord.setText("Word:");
RelativeLayout.LayoutParams param3=
new RelativeLayout.LayoutParams
(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
param3.addRule(RelativeLayout.ALIGN_PARENT_TOP);
param3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textNumber.setBackgroundResource(R.drawable.border_1);
textNumber.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textNumber.setLayoutParams(param3);
relativeLayout.addView(textWord);
relativeLayout.addView(textNumber);
LinearLayout.LayoutParams param4 = param1;
param4.setMargins(0, 3, 0, 0);
editWord.setGravity(Gravity.LEFT);
editWord.setTextAppearance(this,android.R.style.TextAppearance_Medium);
editWord.setLines(1);
editWord.setHorizontallyScrolling(true);
editWord.setLayoutParams(param4);
textDefinition.setLayoutParams(param1);
textDefinition.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textDefinition.setText("Definition:");
editDefinition.setGravity(Gravity.TOP | Gravity.LEFT);
editDefinition.setSingleLine(false);
editDefinition.setLayoutParams(param1);
editDefinition.setMaxLines(3);
editDefinition.setLines(3);
editDefinition.setVerticalScrollBarEnabled(true);
editDefinition.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);
textEmpty.setTextAppearance(this,android.R.style.TextAppearance_Medium);
textEmpty.setLines(1);
textEmpty.setLayoutParams(param1);
layoutContainer.addView(relativeLayout);
layoutContainer.addView(editWord);
layoutContainer.addView(textDefinition);
layoutContainer.addView(editDefinition);
}
And then I add them to the Layout like this.
public void onCLick_Add(View v){
layoutScroll.addView(layoutContainer);
layoutScroll.addView(textEmpty);
}
The problem is that It only workd once . When I click the button for the second time the app crashes.
Thank you in advance.
Your log says:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
You are trying to add same layoutContainer and textEmpty multiple times when clicking multiple times. This fails because they are already added and therefore already have a parent. (You cannot add same View to more then one parent)
Solution
I guess you want to create a new View every time you press the button and add this to your layoutScroll. You should call your init-method again for every click:
public void onCLick_Add(View v){
initInterface();
layoutScroll.addView(layoutContainer);
layoutScroll.addView(textEmpty);
}
I would also suggest to divide initInterface() into:
private View createLayoutContainer();
private View createTextEmpty();
Make sure you take initialization of scrollView outside (e.g. onCreate) and you declare views like layoutContainer localy in the createLayoutContainer() instead of globally.
Here a snippet how it would look:
private View createLayoutContainer(){
LinearLayout layoutContainer = new LinearLayout(getApplicationContext());
RelativeLayout relativeLayout = new RelativeLayout(getApplicationContext());
TextView textWord = new TextView(getApplicationContext());
...
return layoutContainer;
}
public void onCLick_Add(View v){
layoutScroll.addView(createLayoutContainer());
layoutScroll.addView(createTextEmptyView());
}
The problem occurs due to the fact that a ScrollView can only have on child View. In other words, a ScrollView can have a LinearLayout or a RelativeLayout or a TextView etc. Update your code such that new views inside are added inside LinearLayout (layoutScroll) instead of ScrollView.
As I pointed out in the comment, the bug is this : Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Checklist for these kind of bugs.
As pointed out in this post the specified child already has a parent
Double check all your addView calls.
Make sure not to add any view more then once. When a View is allready
used (e.g., you got it with findViewById, don't use addView on it.
When you want to add a view, use addView with a NEW view. You can add
several of these new views to one view, but you cannot add that one
view multiple times.
You can't re-use a view simply by changing some
stuff. You CAN re-use a variable, but you need to make a new view if
you want to re-add it using addView.
Related
This is my onBindViewHolder method:
#Override
public void onBindViewHolder(#NonNull final mViewHolder h, int i) {
final JSON data = jdata[i];
if(data .getName() != null && data .getStatus() !=null) {
h.textcontainer.setVisibility(View.VISIBLE);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ ViewGroup.LayoutParams.WRAP_CONTENT,
/*weight*/ 1.0f
);
h.textcontainer.setLayoutParams(params);
h.title.setText(feed.getName());
} else {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ ViewGroup.LayoutParams.WRAP_CONTENT,
/*weight*/ 2
);
h.textcontainer.setVisibility(View.GONE);
h.playerView.setLayoutParams(params);
}
and this my XML:
<LinearLayout
android:id="#+id/videopost_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical"
android:weightSum="2"
>
<LinearLayout
android:id="#+id/video_textcontainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingLeft="#dimen/feed_item_padding_left_right"
android:paddingRight="#dimen/feed_item_padding_left_right"
android:paddingBottom="#dimen/feed_item_padding_top_bottom"
android:paddingTop="#dimen/feed_item_padding_top_bottom"
android:layout_weight="1"
>
<TextView
android:id="#+id/video_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/feed_item_profile_name"
android:textStyle="bold"
android:textColor="#color/background"
android:paddingStart="#dimen/feed_item_profile_info_padd"
android:paddingEnd="#dimen/feed_item_profile_info_padd"
/>
</LinearLayout>
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/exo_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:show_buffering="true"
android:layout_weight="1"
>
</com.google.android.exoplayer2.ui.PlayerView>
My Adapter has a textview in it, when the json data = null, I obviously want to hide that TextView without any spacing left, and when the data (title for the video) isn't null, I want to show the text.
The current codes hides the TextView, but leaves an empty space. If I delete the following two lines from the if-statemant:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ ViewGroup.LayoutParams.WRAP_CONTENT,
/*weight*/ 1.0f
);
h.textcontainer.setLayoutParams(params);
Then there's no empty space left, but the TextView won't display if the data isn't empty. I'm pretty sure this is some stupid error somewhere, but I'm not able to find it. SOS :-)
Firstly, you have two individual layouts LinearLayout and PlayerView sharing a weight sum of two (2) making it a 50:50 distribution. Whether the child view of the LinearLayout is gone the 50% space would still remain until it's given out.
Hence any time you need to hide textview/title completely and remove not-needed space, dynamically set the weight of PlayerView to two (2) and make the visibility of LinearLayout gone.
The LinearLayout around your TextView is redundant. You can simplify this to simply be a single LinearLayout with two children. Set the height to wrap_content and do not specify a weightSum. When the TextView's visibility is set to GONE, PlayerView will be the only visible child and your LinearLayout will match its size.
<LinearLayout
android:id="#+id/videopost_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical">
<TextView
android:id="#+id/video_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/feed_item_profile_name"
android:textStyle="bold"
android:textColor="#color/background"
android:paddingStart="#dimen/feed_item_profile_info_padd"
android:paddingEnd="#dimen/feed_item_profile_info_padd"
/>
<com.google.android.exoplayer2.ui.PlayerView
android:id="#+id/exo_player_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:show_buffering="true"/>
</LinearLayout>
I've looked at this question here, however I still cannot find out what I'm doing wrong. There are no errors in Logcat and there definitely is data being passed to it to be made. Here's my setup:
This is all taking place below manually placed elements that I have placed in Android Studio. I have a ScrollView. Inside that ScrollView, I have a LinearLayout, parentLayout, that get's passed to this class. This method is supposed to add another Horizontal LinearLayout, layout, parentLayout. Then it is supposed to add a TextView, titleDisplay, and two Buttons to layout. So far I have only programmed just layout and titleDisplay. I tested it, and nothing was added. So before I program the other two buttons, I would like to know what I am doing wrong. Here's the Java Code:
public class FollowupOption {
private String displayName;
private JSONObject jsonInformation;
private Context context;
private LinearLayout parentLayout;
private LinearLayout layout;
private TextView titleDisplay;
private Button deleteButton, editButton;
public FollowupOption(String displayName, JSONObject jsonInformation,
Context context, LinearLayout parentLayout){
this.displayName = displayName;
this.jsonInformation = jsonInformation;
this.context = context;
this.parentLayout = parentLayout;
buildLayout();
}
private void buildLayout(){
//Horizontal Linear Layout to hold everything
this.layout = new LinearLayout(context);
this.layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
this.layout.setOrientation(LinearLayout.HORIZONTAL);
this.parentLayout.addView(this.layout);
//Text View Displaying title of followup option.
this.titleDisplay = new TextView(context);
try {
this.titleDisplay.setText(this.jsonInformation.getJSONObject("list").getString("title"));
} catch(JSONException e){ e.printStackTrace(); }
this.titleDisplay.setTextColor(0x8f142a); //Black
this.titleDisplay.setTextSize(18);
this.titleDisplay.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, 1f));
this.layout.addView(this.titleDisplay);
}
}
Here's my XML:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="38dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.22"
android:gravity="center"
android:text="#string/followup_text"
android:textColor="#color/myRed"
android:textSize="18sp" />
<Button
android:id="#+id/followup_add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:text="#string/plus"
android:textAlignment="center"
android:textColor="#android:color/holo_green_dark"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="279dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="7dp">
<LinearLayout
android:id="#+id/followup_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/followup_new_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/new_followup_text" />
</LinearLayout>
</merge>
If someone could let me know what I am doing wrong or a good way to debug something like this, that would be appreciated.
Are you adding this.layout view to some view?
UPD: The problem is with your text color. Consider using the Color class to get color from its hex value or constants from that class.
Does your XML view (without the addition of the dynamic layout) take up the entire screen?
The new LinearLayout view will be added below the button. If the button is at the bottom of the screen, the layout will be added off the screen and therefore not visible.
You should add your new layout to the scroll view instead.
I want to create a scroll view list which would show "boxes" or more accurately custom made layouts (I'm using a custom class that extends a RelativeLayout - basically shows different pieces of information, some are static like places' working hours and some change dynamically and will be pulled from a server).
Though I encountered a problem - I (for the sake of seeing if my solution works) created 5 boxes and added them to the scroll view list but it seems like they are stacked upon each other. What's the proper way to make them appear one under another without manually tweaking their position coordinates? I was using addView() for that purpose but it doesn't work as intended for me or I use it poorly. If you know the answer, please briefly describe how this should be done.
Thanks in advance!
EDIT, here goes the code:
public class RestaurantBox extends RelativeLayout {
RestaurantBox (Context context){
super(context);
this.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
TextView restaurantName = new TextView(context);
restaurantName.setText("Test Restaurant");
RelativeLayout.LayoutParams restNameParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
this.addView(restaurantName, restNameParams);
restaurantName.setTypeface(null, Typeface.BOLD);
TextView freeSpots = new TextView(context);
freeSpots.setText("15/20");
RelativeLayout.LayoutParams freeSpotParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
freeSpotParams.topMargin = restNameParams.bottomMargin + 50;
this.addView(freeSpots, freeSpotParams);
TextView book = new TextView(this.getContext());
RelativeLayout.LayoutParams bookParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
book.setText("Book");
bookParams.setMargins(0,freeSpotParams.bottomMargin + 100,0,0);
this.addView(book, bookParams);
}
}
public class BrowseRestaurants extends AppCompatActivity {
int restaurantCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browse_restaurants);
Intent intent = getIntent();
String login = intent.getStringExtra("LOGIN");
TextView text = (TextView) findViewById(R.id.txtWelcomeUser);
text.setText("Welcome, " + login);
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
RestaurantBox initRBox = new RestaurantBox(this);
initRBox.setTop(0);
initRBox.setBottom(300);
relativeLayout.addView(initRBox);
for(int i=1;i<5;i++){
final View view = relativeLayout.getChildAt(i-1);
RestaurantBox restaurantBox = new RestaurantBox(this);
restaurantBox.setTop(view.getBottom() + 50);
restaurantBox.setBottom(restaurantBox.getTop() + 300);
relativeLayout.addView(restaurantBox);
}
}
}
<!-- activity_browse_restaurants.xml-->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:fillViewport="true"
android:orientation="vertical"
tools:context="com.konrad.rezerwacje1.BrowseRestaurants">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewRestaurants"
android:layout_below="#+id/txtWelcomeUser"
android:layout_alignParentStart="true">
<RelativeLayout
android:id="#+id/relLayoutRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:orientation="vertical">
</RelativeLayout>
</ScrollView>
<TextView
android:id="#+id/txtLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Logout"
android:textColor="#android:color/holo_red_dark"
android:textSize="18sp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="#+id/txtWelcomeUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#android:color/holo_blue_dark"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="9dp" />
</RelativeLayout
>
you are adding view in RelativeLayout so view stacking on each other so change it to LinearLayout in activity_browse_restaurants.xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewRestaurants"
android:layout_below="#+id/txtWelcomeUser"
android:layout_alignParentStart="true">
<LinearLayout
android:id="#+id/relLayoutRestaurants"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
now make changes according to in BrowseRestaurants.class
replace
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relLayoutRestaurants);
with
LinearLayout relativeLayout = (LinearLayout) findViewById(R.id.relLayoutRestaurants);
else will be fine if you want to change variable name its up to u, let me know if any problem
I am adding dynamic imagebuttons to a layout in a Fragment using recyclerview with gridlayoutmanager. The buttons are added as a user performs an action, so they are not all created during onCreateView().
I seem to have to initialize my recycler view and adapter onCreateView() with a an empty imagebutton, when my app starts up. And so I do that, but it creates a little grey square like the image below.
and then when the user performs an action and the real button I want is created, the square is still present over my image button that was just created like you can see in this new image below.
DOES ANYONE KNOW HOW I CAN GET THE INITIAL EMPTY GREY IMAGE BUTTON TO NOT BE THERE AT STARTUP OR OVER MY "DriveDroid" IMAGE BUTTON?
I have tried setting the initial background of my image button to transparent (using background color = #00000000), but this seems to make a transparent image button over my DriveDroid button, so that my onClick listener for DriveDroid no longer works.
Here is how I initialize my recycler view:
public class MyFragment extends Fragment {
private GridLayoutManager lLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
// Create an empty list to initialize the adapter (or else get nullPointerException error)
List<ItemObject> myList = new ArrayList<ItemObject>();
lLayout = new GridLayoutManager(getActivity(), 4, GridLayoutManager.HORIZONTAL, false);
RecyclerView rView = (RecyclerView)view.findViewById(R.id.recycler_view);
rView.setHasFixedSize(true);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(getActivity(),myList);
rView.setAdapter(rcAdapter);
return view;
}
And here is my layout my_fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_margin="5dp"
android:id="#+id/my_fragment"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_name"
android:layout_below="#+id/new_app_button"
android:layout_alignStart="#+id/new_app_button"
android:layout_alignLeft="#+id/new_app_button"
android:gravity="center"
/>
</RelativeLayout>
HERE IS HOW I CHANGED MY LAYOUT TO GET IT TO WORK, IF IT HELPS ANYONE ELSE!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_marginTop="15dp"
android:id="#+id/my_fragment"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_name"
android:gravity="center"
android:layout_below="#+id/new_app_button"
/>
</RelativeLayout>
</LinearLayout>
As mentioned in the comment, that your controls was overlapping each others, you should wrap them inside linear layout or add properties to make them positioned relative to each others.
Regarding the grey button this should be related to "new_app_button"
At the moment everything is done through the java code. I'm creating a relative layout then adding my GL surface view and some text views to it. the problem is I cant align one text view to the top left and the other to the top right. Whats wrong with my code. Also is there a more efficient way of doing this such as doing this through XML instead. Cause I've tried this and it didn't work.
r1 = new RelativeLayout(this);
view = new MyGLSurfaceView(this);
view.setKeepScreenOn(true);//keeps activity from going to sleep
r1.addView(view);
TextView text1 = new TextView(this);
TextView text2 = new TextView(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 50);
RelativeLayout.LayoutParams rp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 200);
lp.addRule(RelativeLayout.ALIGN_RIGHT);
text1.setLayoutParams(lp);
text1.setText("3 Star");
text1.setBackgroundColor(0x4060ff70);
rp.addRule(RelativeLayout.ALIGN_LEFT);
text2.setLayoutParams(rp);
text2.setText("4 Star");
text1.setTextSize(30);
text2.setTextSize(30);
//text2.setBackgroundColor(0x4060ff70);
r1.addView(text1);
r1.addView(text2);
setContentView(r1);
This can be done in XML like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left" />
<TextView
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Right" />
</RelativeLayout>