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.
Related
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 have a RelativeLayout with a "search bar" (EditText) and a ListView under it:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/etSearch"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:inputType="text" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:clipToPadding="false"
android:listSelector="#drawable/listview_selector" >
</ListView>
</LinearLayout>
After the user "searches" I want the EditText to animate out of the screen and the ListView to push to the top. I put together a very rough GIF of what I need:
Does anyone have an idea on how I can accomplish this? thanks
UPDATE
I found out how to make the EditText animate out of the screen with this:
slide_out_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="-100%" android:duration="600"/>
</set>
and then using it on the EditText like this:
mSlideOutTop = AnimationUtils.loadAnimation(this, R.anim.slide_out_top);
et.startAnimation(mSlideOutTop);
but then the ListView stays at its current height. What I want is for the ListView to extend to the top as the EditText animates out.
You can add TranslateAnimation to the ViewGroup( which is LinearLayout for you), the y-axis moving distance is the hight of the editText View. Then trigger this animation when you need. (update: just thought this way may create a blank bar at bottom, bad idea)
I have an another tricky idea.
set the EditText view in the ListView with position 0 , then simply call the smoothScrollToPostion(1) method to scroll.
see this may help you
smoothScrollToPositionFromTop() is not always working like it should
At this part in the development I am trying to add Linear Layouts on Screen on a button click. I tried to make it as similar to the XML generated ones but it does not code out the same. Screen Shot here. The EditText does not show and there is not top padding. After the Java is the Layout I am basing it off of
FYI: The top two layouts are what I am aming for with this java
This is My code so far:
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.setLayoutParams(new LinearLayout.LayoutParams((getDP(80f)), LinearLayout.LayoutParams.FILL_PARENT));
TextView numberView = new TextView(this);
LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(getDP(80f),getDP(10f));
numberView.setLayoutParams(tvParams);
numberView.setText(numOfItems+".");
layout.addView(numberView);
EditText optionText = new EditText(this);
LinearLayout.LayoutParams etParams = new LinearLayout.LayoutParams(getDP(80f),getDP(100f));
numberView.setLayoutParams(etParams);
numberView.setHint("List Option "+numOfItems);
layout.addView(optionText);
insideScroll.addView(layout);
And the XML:
<LinearLayout
android:paddingLeft="15dp"
android:weightSum="100"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="horizontal">
<TextView
android:layout_weight="10"
android:layout_width="10dp"
android:layout_height="80dp"
android:text="1."
android:id="#+id/tvItem1"/>
<EditText
android:layout_
weight="90"
android:layout_width="100px"
android:layout_height="80dp"
android:hint="List Item 1"
android:id="#+id/etItem1"
android:paddingTop="50px"/>
</LinearLayout>
Try to stick to having most of your layout as XML.
You can inflate the XML programmatically and then, if needed, change any dynamic content within the newly created layout.
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.
I have an activity with a Save and Cancel button at the bottom.
In AlertDialog, the buttons are displayed inside a styled container view of some sort.
How could I give the buttons in my Activity that same appearance? Specifically, how could I apply the style of the button container view in the AlertDialog to say a LinearLayout in my Activity containing the buttons?
Thanks
There are solutions given elsewhere that work. In short, you can simply use style attributes in your xml to achieve this. For instance, style="?android:attr/buttonBarStyle" and style="?android:attr/buttonBarButtonStyle" will do the job (for API 11+). Here is an example of two buttons horizontally put together.
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:measureWithLargestChild="true"
android:orientation="horizontal"
android:paddingTop="0dip" >
<Button
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text= "Ok" />
<Button
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel" />
</LinearLayout>
The only thing that remains, is that there is a horizontal line right above the buttons in an alertDialog, which the above code will not create. If you want to have that horizontal line, it should be added manually in the xml, above the LinearLayout. This will give you the horizontal line:
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_marginBottom="0dp"
android:background="?android:attr/dividerVertical" />
I do some thing like this:
LinearLayout dialogLayout = (LinearLayout) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.dialog_addeditrecord, null);
I then use the dialogLayout to call findViewById() to pull in the buttons and other views and setup OnClickListeners and such...
then to show the dialog:
builder = new AlertDialog.Builder(this);
builder.setView(dialogLayout);
builder.create().show();