I have a parent RelativeLayout, that have other Views and other Relative Layouts inside of it. When I try to make one of these RelativeLayouts have a GONE visibility, it just won't disappear. Any solution for this? I'll post the layout I want GONE.
<RelativeLayout android:id="#+id/relativeLayerA"
android:layout_width="315px"
android:layout_height="35px"
android:background="#drawable/bgnavyblue_abcd"
android:layout_below="#id/relativeConfig1"
android:layout_toRightOf="#id/relativeRecipe"
android:layout_marginLeft="15px"
android:layout_marginTop="20px"
>
<ImageView android:id="#+id/imgIconA"
android:layout_width="33px"
android:layout_height="33px"
android:src="#drawable/icon_a"
android:layout_alignParentLeft="true" <!-- -->
/>
<TextView android:id="#+id/txtLayerA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/layer_a_adjusts"
android:textSize="7pt"
android:textColor="#FEFEFE"
android:typeface="sans"
android:layout_toRightOf="#id/imgIconA"
android:layout_marginLeft="5px"
android:layout_marginTop="8px"
/>
</RelativeLayout>
Here is the whole code: http://pastebin.com/VhpWSa6Z
Please, anyone know whats going on?
EDIT: Someone said that this might be happening because other View is using this View to position itself at the screen. So I tried to set GONE visibility to the RelativeLayerC. And it worked. I don't get it why it works with this View, and not with RelativeLayerA. Here you can see the RelativeConfig4 using RelativeLayerC as reference. http://pastebin.com/uHW6faPy
I have faced the same problem.. I think it's a bug.. :(
You can bypass it though.. try this to make the RelativeLayout invisible.
RelativeLayout rLayout=(RelativeLayout) findViewById(R.id.//your relativeLayout id);
RelativeLayout.LayoutParams rParams = new RelativeLayout.LayoutParams(
0, // its height... set it 0 to make it invisible..
RelativeLayout.LayoutParams.FILL_PARENT // and its width
);
rLayout.setLayoutParams(rParams);
This to make it visible.
RelativeLayout.LayoutParams rParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
rLayout.setLayoutParams(rParams);
Here are the correct parameters for RelativeLayout in xml.
<RelativeLayout
android:id="#+id/rLayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:background="#AA000000" >
Remember! dont use this.
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
or left, right.. otherwise it will not work..!
Hope that helps.
Related
I'm developing a small app as project for school, and I need to dinamically move a FAB.
I have a FloatingActionButton inside a Relative layout, which is actually bottom aligned and horizontally aligned.
<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"
tools:context="com.pagiaro.nicola.quiz2_0.HomeActivity"
android:id="#+id/home_rl" >
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:fabSize="normal"
app:srcCompat="#drawable/ic_quiz"
android:id="#+id/fab_inizia"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
app:backgroundTint="#color/accentColor" />
</RelativeLayout>
The code I'm using for move it is:
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_inizia);
final RelativeLayout.LayoutParams rlp_keyboard_is_open = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp_keyboard_is_open.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlp_keyboard_is_open.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
fab.setLayoutParams(rlp_keyboard_is_open);
But with this code my FAB has not right and bottom margin. The problem comes when I add code to add margins.
rlp_keyboard_is_open.setMargins(0, 0, R.dimen.activity_horizontal_margin, R.dimen.activity_vertical_margin);
or
rlp_keyboard_is_open.rightMargin = R.dimen.activity_horizontal_margin;
rlp_keyboard_is_open.bottonMargin = R.dimen.activity_vertical_margin;
If I add this code, the FAB disappear.
I searched al lot for a solution, but I didn't find it. Any help? Thanks.
Use
getResources().getDimension(R.dimen.activity_horizontal_margin)
or
getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin)
instead of directly using R.dimen.activity_horizontal_margin as it returns the internal ID assigned to R.dimen.activity_horizontal_margin rather than the value you assigned in your XML file.
I have an activity with a layout. After a GET request to a server, I want to dynamically add new elements to that layout.
I want to add those elements multiple times, using a for-structure.
The elements I want to add are the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#drawable/outer_border"
android:padding="2dp"
android:layout_marginTop="20dp">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#color/orange"
android:height="40dp"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="TW"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="70px"
android:width="60dp" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_toLeftOf="#+id/checkBox1"
android:text="inca 6 zile"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
I've tried this:
for(int i = 0; i < homeworkList.size(); i++){
LinearLayout linearLayout = (LinearLayout) currentActivity.findViewById(R.id.linearLayout2);
RelativeLayout newLayout = new RelativeLayout(currentActivity, null, R.style.HomeworkLayout);
TextView text = new TextView(currentActivity);
TextView text1 = new TextView(currentActivity);
text1.setText("da");
text.setText("nu");
newLayout.addView(text1);
newLayout.addView(text);
linearLayout.addView(newLayout, relativeParams);
}
But no result, those textview were added but on top of each other, and the relative layout I just added in that for doesn't have any of the style I added using R.style.HomeworkLayout.
What is the best way to add the elements with so much styling? Why isn't this working?
those textview were added but on top of each other
That's what you told RelativeLayout to do. If you wanted to specify positioning rules, you would have passed instances of RelativeLayout.LayoutParams to addView() when you were adding the TextView widgets.
What is the best way to add the elements with so much styling?
Well, probably, the answer is to use ListView or RecyclerView. That being said, the simplest solution that keeps your vertical LinearLayout would be to inflate the rows:
LinearLayout linearLayout = (LinearLayout) currentActivity.findViewById(R.id.linearLayout2);
for(int i = 0; i < homeworkList.size(); i++){
View row=getLayoutInflater().inflate(R.layout.row, linearLayout, false);
// call findViewById() to retrieve your TextView widgets and fill them in
linearLayout.addView(row);
}
This assumes that the layout you show in your question is named R.layout.row; adjust the inflate() call as needed if that is not the name. This also assumes that the code snippet is in a method on the activity that is hosting this UI.
If you want to use a layout which is repeating why don't you prefer using a custom liner layout.
A simple and basic solution is mentioned on this link
http://android-coding-tuts.blogspot.in/2012/02/custom-listview-with-sliding-view-for.html
You should look up Fragments for this. They have a separate control-view structure and you can just create a new fragment for each subview.
Check it out here:
http://developer.android.com/guide/components/fragments.html
I am using a DraggableGridView from here.
What I did before was add a simple programmatically built ImageViews to the grid. That works perfectly fine.
I am now trying to add a Layout instead. I tried RelativeLayout, Framelayout, FrameLayout inside of a RelativeLayout.
Here is my code as of now:
/**
* Rebuild the grid view, e.g. after the adapter has been filled or a backup is restored
*/
private void renewGrid()
{
dgv.removeAllViews();
for(int i = 0; i < adapter.getCount(); i++)
{
LayoutInflater inflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
dgv = (DraggableGridView) inflater.inflate(R.layout.grid_item_layout, dgv);
FrameLayout fl = (FrameLayout) dgv.findViewById(R.id.grid_images);
ImageView icon = (ImageView) fl.findViewById(R.id.qstile);
icon.setImageDrawable(res.getDrawable(res.getIdentifier("qstile_" + adapter.getItem(i), "drawable", packagename)));
}
}
Following the grid_item_layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/grid_images">
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:src="#drawable/icon_delete"/>
<ImageView
android:id="#+id/qstile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
<TextView
android:id="#+id/invisible_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:visibility="gone" />
</RelativeLayout>
The RelativeLayout gets inflated into the grid nicely. I can observe this via DDMS and the "Dump view hierachy" function. The tree shows all RelativeLayouts inside the grid, but each of it doesn't have any children.
BUT... That is not true. If I step through the code and observe the inflated layouts I can see the children. So they are set up, get added just like told in the XML, but don't get drawn.
The OnItemClick listener on the children of the grid also works...
Any hint about what I'm missing here? I already tried several ways, even creating the complete layout programmatically and then adding it to the grid as children. Still no luck. None of the children get added.
Is that maybe an issue with the used DraggableGridView?
After searching for another few hours I found a fix in this SO thread
Basically I now extend DraggableGridView from FrameLayout rather than ViewGroup. This hasn't had any noticeable side effects to me.
I have a xml that the basic "skeleton" is
<RelativeLayout>
<FrameLayout>
<RelativeLayout>
<TextView>
</TextView>
<TextView>
</TextView>
</RelativeLayout>
<DrawView>
//The view that changes 1
</DrawVIew>
<EditText>
//The view that changes 2
</EditText>
</FrameLayout>
<RelativeLayout>
</RelativeLayout>
</RelativeLayout>
What I want is that using code the DrawView goes up or down, so sometimes you can use the DrawView and in other moments you can use the EditText, but ever the two views are showed.
How can I do that?
Since you are placing both widgets inside a FrameLayout you can change its gravity like below:
FrameLayout.LayoutParams drawViewLayoutParams = drawView.getLayoutParams();
drawViewLayoutParams.gravity = Gravity.BOTTOM;
FrameLayout.LayoutParams editTextLayoutParams = editText.getLayoutParams();
editTextLayoutParams.gravity = Gravity.TOP;
The only way to do this is to remove all of the FrameLayout's children and put them back in the order you need.
Or you can use another RelativeLayout and change the LayoutParams for the two Views
I have an XML RelativeLayout snippet that I would like to include several times (from a loop) in my main View. The problem seems to be -- is there a way to avoid hard-coding the parent of the RatingBar, since each time I include the RelativeLayout snippet my elements will need to have different ids?
As far as I can tell, the recommended way is to get the layout snippet and then override the android:id for each element to be unique, and then override the android:layout_below manually for each element that has relative positioning. This seems a little kludgy -- is there any way to have these bindings get done automatically?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:id="#+id/relativeView">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="Label"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView1" />
</RelativeLayout>
you just need to change the id of the RelativeLayout
like
int BASEID=200;
View v = mLayoutInflator.inflate(R.layout.myRelativeLayout, null);
for (int i;i<10;i++){
v.findViewById(R.id.relativeView).setId(i+BASEID);
}
mRootView.addView(v,...);
then when you need to get the RatingBar for suppose the 4th RelativeLayout you added you can call
RatingBar mRatingBar = (RatingBar)mRootView.findViewById(BASEID+3).findViewById(R.id.ratingBar1);