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.
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 creating an app that needs to dynamically generate an XML file. The layout starts essentially empty (other than a linear layout), then I loop through a JSON array and create the XML code shown below for EACH element in the array (the only thing different for each element in the array is the ids for the views).
I am not really understanding how I can use Java to create these layouts, edit their attributes, and add views to them.
I know I can create a GridLayout object and give it the number of rows and columns with
GridLayout doc = new GridLayout(3, 3);
But I can't figure out how to edit all of the specific attributes, and then add views inside of the layout.
What is the best way to create a layout, edit it's attributes, and add views within that layout through Jave code?
Thank you.
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:rowCount="3"
android:layout_marginBottom="10dp"
android:background="#3c37ff00"
android:id="#+id/doctor1"
android:longClickable="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dr. Sam"
android:id="#+id/doctor1_name"
android:layout_row="0"
android:layout_column="0"
android:textStyle="bold"
android:textSize="26dp"
android:typeface="sans" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/doctor1_profile"
android:layout_row="0"
android:layout_column="1"
android:src="#drawable/no_pic"
android:scaleType="fitXY"
android:layout_rowSpan="3"
android:layout_columnSpan="1"
android:layout_gravity="right"
android:background="#00ffffff" />
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/doctor1_action"
android:layout_row="0"
android:layout_column="2"
android:src="#drawable/abc_ic_menu_paste_mtrl_am_alpha"
android:scaleType="centerInside"
android:layout_rowSpan="3"
android:layout_columnSpan="1"
android:background="#47ffffff"
android:clickable="true"
android:onClick="setContentView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Very Close"
android:id="#+id/doctor1_distance"
android:layout_row="1"
android:layout_column="0"
android:textStyle="bold" />
</GridLayout>
You could add your views in any viewGroup by:
YourViewGroup.addView(yourChildView);
But I think ListView will serve you better because:
You don't have to worry about each view's handling;
You will have automatic scroll;
You can simply manage your data via Adapter.
EDIT:
As of most recent APIs, you should use a RecyclerView for various reasons. It works similar to the previous adapters and you can accomplish any list/grid behavior you want.
You should start with a single xml document and a parent layout. Then you can give that an id and call it dynamically. You can add to this layout whatever you want. Heres some code I had from doing it. My parent linear layout is my_ll:
LinearLayout ll = (LinearLayout) myInflatedView.findViewById(R.id.my_ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
// Use this for each image in the list
final ImageView image = new ImageView(getActivity());
image.setAdjustViewBounds(true); // Scales it to the screen
image.setId(i); // Sets each with unique id
ll.addView(image, params); // Adds the ImageView to screen BEFORE adding image (important)
Picasso.with(getActivity()) // THEN you add the image from photosList
.load(photosList.get(i))
.into(image);
You can add onClickListeners to each view also dynamically. The addView is the part that will add it dynamically.
Is it possible to change the view appearance order programmaticaly?
Example.
<EditText
android:id="#+id/edit_txt_address_location_street"
android:layout_width="0dip"
android:layout_height="36dp"
android:layout_marginBottom="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:layout_weight="8"
android:background="#drawable/shape_edit_text"
android:inputType="textNoSuggestions"
android:textColor="#color/black"
android:textCursorDrawable="#null" />
<EditText
android:id="#+id/edit_txt_address_location_street_nr"
android:layout_width="0dip"
android:layout_height="36dp"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="8dp"
android:layout_weight="2"
android:background="#drawable/shape_edit_text"
android:inputType="textNoSuggestions"
android:textColor="#color/black"
android:textCursorDrawable="#null" />
The xml about show the normal appearance order.
1. edit_txt_address_location_street
2. edit_txt_address_location_street_nr
For some conditions i muss invert the appearance order programmaticaly.
E.g.:
1. edit_txt_address_location_street_nr
2. edit_txt_address_location_street
Most, if not all, of the xml commands have their corresponding programmatic calls. Assuming what you mean "change" is adding or removing a view after it is inflated with xml, then the answer is yes, but it is does not mean you should.
If you want to add/remove views dynamically , you would probably just better off creating the whole layout dynamically. Trust me it is not that hard compared to xml layout once you know how. I've read some where that there could be potentially some problems if you mix both xml and dynamic layout, I can't prove that statement, but what I have done is to inflate an empty linearlayout with xml, then dynamically add all the views I want.
EditText location_street = new EditText(this);
location_street.setBackgroundResource(R.drawable.shape_edit_text);
EditText location_street_nr = new EditText(this);
location_street_nr.setBackgroundResource(R.drawable.shape_edit_text);
LinearLayout ll = (LinearLayout)findViewById(R.id.textlayout);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.addView(location_street, lp);
ll.addView(location_street_nr, lp);
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();