Android way to add new xml layout - java

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

Related

there is some way to make my own item in xml?

i work on android-studio project, and my question is about if
there is some way i can define in xml type of some item that contain
few edit text and buttons, and open listview that contain the item i create?
somthing like :
<item
<Editext(some setting)/>
<Edittext 1(some setting)/>
<Button(some setting)/>
/>
and then some adapter that adjust or something like that, that i can add to ListView.
i saw in youtube some videos that try to explain that but i get stock.. i dno't really get this.
This took my a while to figure out, but it works.
Create your Layout file just however you want. Save it as res/layout/something.txt. Example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:text="hello"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:id="#+id/text1"/>
<TextView
android:text="world"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:id="#+id/text2"/>
</LinearLayout>
Use it in your code as follows:
LinearLayout mainLinearLayout = new LinearLayout(getApplicationContext());
// inflate your container
View view = inflater.inflate(R.layout.something, null);
// you can make changes to the elements like this:
TextView txt1 = (TextView) view.findViewById(R.id.text1);
TextView txt2 = (TextView) view.findViewById(R.id.text2);
view.setTag(tag);
// add to main layout
mainLinearLayout.addView(view);
Where tag is an unique integer to identify the View. You don't need to set the tag if you're using the container only once.
I didn't use an Adapter when I made this. I just used a ScrollView to make a continous list of entries. Change this to your needs.

How can I dynamically add layouts and views to an Android XML layout file?

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.

Inflated layout's children not showing

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.

Android: It is possible to change the view programmaticaly

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);

Dynamically including an XML relative layout multiple times in a parent view

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);

Categories

Resources