android - how to inflate a map fragment in an already inflated layout? - java

i am creating a Table Layout in my an activity, adding dynamically table rows in a loop.
What i want to achieve, is to add a Google map fragment in each dynamically created Table row.
This is my Table row Layout:
<?xml version="1.0" encoding="utf-8">
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_rowitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp">
<RelativeLayout
android:id="#+id/inflated_row_relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_poi_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange"
android:textColor="#ffffff"
android:text="Name"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/img_snapshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tv_poi_name"
android:adjustViewBounds="true"
android:src="#drawable/map_snapshot" />
<TextView
android:id="#+id/tv_poi_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#fe8301"
android:layout_below="#+id/img_snapshot"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/distance_layout"
android:layout_below="#+id/tv_poi_address" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/distance_icon" />
<TextView
android:id="#+id/tv_poi_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:text="Distance"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</RelativeLayout>
</TableRow>
And this is the loop that i am creating the Table rows dynamically:
final TableLayout tblAddLayout = (TableLayout) findViewById(R.id.tl_menu_pois);
TableRow inflateRow = (TableRow) View.inflate(Menu_pois.this, R.layout.menu_pois_rowitem, null);
for (int i=0;i<3;i++){
View inflate = (TableRow) View.inflate(Menu_pois.this, R.layout.menu_pois_rowitem, >tl_menu_pois);
RelativeLayout inflated_row_relative = (RelativeLayout) >inflate.findViewById(R.id.inflated_row_relative);
inflated_row_relative.setTag(i);
TextView poi_name = (TextView) inflate.findViewById(R.id.tv_poi_name);
TextView poi_address = (TextView) inflate.findViewById(R.id.tv_poi_address);
poi_name.setText("Name");
poi_address.setText("Address");
//set tag for each TableRow
inflate.setTag(i);
//add TableRows to TableLayout
tblAddLayout.addView(inflate);
//set click listener for all TableRows
inflated_row_relative.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Object tag = v.getTag();
String string_tag = tag.toString();
}
});
}
I want to display a Google map in each Table row like the image bellow (instead of the sample image, having the map):
http://goo.gl/9erYB7
Is there a way to implement this?
Thank you very much in advance.

I found a solution for adding multiple map fragment on the same View, following this example, in case someone has the same problem:
http://saadroid.blogspot.gr/2013/06/multiple-maps-in-same-view.html

Related

Relative Layout adding views programmatically one below other not aligning correctly

I am trying to add Text views programmatically to Relative layout parent one below other. However, the first view is not aligning correctly while the rest of the view got aligned correctly. See attached image.
My code:
private void setupQuestionChoices(int position) {
question.setText(questionList.get(position).question());
choicesTextViewsList.clear();
for (int i = 0; i < questionList.get(position).choices().size(); i++) {
TextView textView = new TextView(getActivity());
textView.setBackgroundResource(R.drawable.qa_button_background);
textView.setMinWidth(300);
textView.setGravity(Gravity.CENTER);
textView.setText(questionList.get(position).choices().get(i));
textView.setId(i);
RelativeLayout.LayoutParams layoutParams =
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i == 0) {
layoutParams.addRule(RelativeLayout.BELOW, question.getId());
} else {
layoutParams.addRule(RelativeLayout.BELOW, choicesTextViewsList.get(i - 1).getId());
}
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutParams.setMargins(20, 20, 20, 20);
relativeLayout.addView(textView, layoutParams);
choicesTextViewsList.add(textView);
choicesTextViewsList.get(i).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
answerSelected = true;
}
});
}
}
Relative layout xml where I add my views to:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/relative_parent" //here is where I add views to
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="40dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginBottom="30dp"
android:gravity="center">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_back"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Back"
android:textColor="#android:color/darker_gray" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/button_next"
style="?android:attr/borderlessButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="30dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="30dp"
android:background="#drawable/rectangle_border"
android:text="Next"
android:textColor="#android:color/darker_gray" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I read some posts related to this issue and followed the guidelines from them but that did not help.
How to lay out Views in RelativeLayout programmatically?
Create a new TextView programmatically then display it below another TextView
I was using the index of the for loop as id for the textViews which I changed to make Android generate Id for it as below and it is working fine now as expected.
I changed the line
textView.setId(i)
to
textView.setId(ViewGroup.generateViewId())
Thanks.

Generated Text Fields Position

Is there a way I can position generated text fields to my layout programmatically. Here is what the generated text field currently looks like:
Here is the code I have for generating these two fields:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingButton);
fab.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
LinearLayout linearLayout = new LinearLayout(AddSongActivity.this);
EditText artist = new EditText(AddSongActivity.this);
EditText songTitle = new EditText(AddSongActivity.this);
artist.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
songTitle.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.addView(artist);
linearLayout.addView(songTitle);
((RelativeLayout)findViewById(R.id.add_song_layout)).addView(linearLayout);
}
});
Here is the add song layout that goes along with the above code:
<?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:id="#+id/add_song_layout"
tools:context="com.example.android.playmymusicapp3.AddSongActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ScrollView>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/floatingButton"
android:src="#mipmap/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick ="onClick"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
app:backgroundTint="#color/colorPrimary">
</com.github.clans.fab.FloatingActionButton>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="82dp"
android:text="Add songs to your event..."
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="37dp"
android:layout_below="#+id/textView3"
android:layout_alignParentStart="true">
<EditText
android:id="#+id/editText8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:ems="10"
android:hint="Artist"
android:inputType="textPersonName" />
<EditText
android:id="#+id/editText7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:ems="10"
android:hint="Title"
android:inputType="textPersonName" />
</LinearLayout>
</RelativeLayout>
Is there a way where I can tell Android to take those two generated text fields and place them exactly like the ones I have that are centered?
You should try new Constraint Layout. You can really good position your elements.
ConstraintLayout Tutorial
https://developer.android.com/training/constraint-layout/index.html
https://developer.android.com/reference/android/support/constraint/ConstraintSet.html
Example:
The following code configures a constraint set in which the left-hand
side of a Button view is connected to the right-hand side of an
EditText view with a margin of 70dp:
set.connect(button1.getId(), ConstraintSet.LEFT,
editText1.getId(), ConstraintSet.RIGHT, 70);
You can do the same for TOP and BOTTOM values.

How to set the margin of a textview dynamiclly?

I will make a timetable for my organizer app. My problem is:
- I have make a first row and make the second and every other row like this, but dynamically.
- I have a layout.xml for this, but the margin between the Views doesn´t work.
I want make the second row like the blue row/ first row. The yellow row should be dynamically (on runtime).
My sourcecode:
public class StundenplanActivity extends AppCompatActivity {
TableLayout tl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stundenplan);
//Find the TableLayout defined in activity_stundenplan
tl = (TableLayout)findViewById(R.id.myTableLayout);
createRow();
}
public void createRow(){
/* Create a new row to be added. */
LayoutInflater inflater = getLayoutInflater();
TableRow tr = (TableRow) inflater.inflate(R.layout.tablerow_tabelle,null);
String[] day = {"Zeit", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag"};
TextView[] spalte = new TextView[8];
for(int i=0; i<=7; i++){
TextView text = (TextView) inflater.inflate(R.layout.text_view_tabelle, null);
text.setText(day[i]);
spalte[i] = text;
}
for(int i=0; i<=7; i++) {
tr.addView(spalte[i]);
}
tl.addView(tr);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_stundenplan, menu);
return true;
}
public boolean onOptionItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.item_einstellungen_stundenplan) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
My layout xml for the dynamically textviews:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#ff0000"
android:background="#f0d000"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop">
</TextView>
Edit: xml for tablerow_tabelle
<?xml version="1.0" encoding="utf-8"?>
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1cf000">
</TableRow>
And the xml for the first Row (not dynamically):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTableLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/mainactivity_horizontal_margin"
>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text0"
android:text="#string/Zeit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text1"
android:text="#string/Wochentag0"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text2"
android:text="#string/Wochentag1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text3"
android:text="#string/Wochentag2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text4"
android:text="#string/Wochentag3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text5"
android:text="#string/Wochentag4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text6"
android:text="#string/Wochentag5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
<TextView
android:id="#+id/text7"
android:text="#string/Wochentag6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00f0c0"
android:padding="#dimen/stundenplanactivity_padding"
android:layout_marginRight="#dimen/stundenplanactivity_marginRight"
android:layout_marginTop="#dimen/stundenplanactivity_marginTop"
/>
</TableRow>
</TableLayout>
Here this code works good to make different coloring for different rows. Well, it may be helps you.
In my code, i was used two rows. The first Table layout was Header row and second table layout is value rows. I was set background of header as background image and value layouts having background color.
In XML Layout:
<LinearLayout
android:id="#+id/gridValue1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableLayout
android:id="#+id/GridFrozenTable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginTop="2dip"
android:stretchColumns="*"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginTop="2dip"
android:layout_toRightOf="#id/GridFrozenTable">
<TableLayout
android:id="#+id/GridContentTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:stretchColumns="*" />
</HorizontalScrollView>
</ScrollView>
</LinearLayout>
In Java code :
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void PopulateMainTable(ActionCodeReport[] content) {
TableRow HeaderRow = new TableRow(this);
HeaderRow.setPadding(0, 0, 0, 2);
TextView HeaderCell1 = new TextView(this);
HeaderCell1.setText("Action code ");
HeaderCell1.setTextColor(Color.BLACK);
HeaderCell1.setHeight(65);
HeaderCell1.setGravity(Gravity.CENTER_HORIZONTAL);
HeaderCell1.setBackgroundResource(R.drawable.lt); //Here i set background image for header
HeaderRow.addView(HeaderCell1);
TextView HeaderCell2 = new TextView(this);
HeaderCell2.setText("NoOfAccnt");
HeaderCell2.setHeight(65);
HeaderCell2.setBackgroundResource(R.drawable.lt); //Same image
HeaderCell2.setGravity(Gravity.CENTER_HORIZONTAL);
HeaderRow.addView(HeaderCell2);
HeaderCell2.setTextColor(Color.BLACK);
TableRow contentRow = new TableRow(this);
contentRow.setPadding(0, 0, 0, 2);
int orientation = this.getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT) {
TextView Cell1 = new TextView(this);
Cell1.setText(r.getAction_Code());
Cell1.setTextColor(Color.BLACK);
Cell1.setGravity(Gravity.LEFT);
Cell1.setBackgroundColor(Color.LTGRAY); //**Here change your background color as you need**
contentRow.addView(Cell1);
TextView Cell2 = new TextView(this);
Cell2.setText(String.valueOf(r.getNoOfAccount()));
Cell2.setTextColor(Color.BLACK);
Cell2.setGravity(Gravity.CENTER_HORIZONTAL);
contentRow.addView(Cell2);
contentTable.addView(contentRow);
}
Surely, i may helps you!!
Happy Coding!!
This can help
TextView txtvew= (TextView) findViewById(R.id.ForgotPasswordText);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0)
txtvew.setLayoutParams(llp);
Setting the margin using layout XML file.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"/>
For changing some particular side margins use this code.
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"

Can't Get TableLayout to Programatically Be Created

I've created what I want in xml but I need to do it programatically and I can't get it to work. It's a table layout with one row. In that row there is two more table layouts. The first of which has one row containing a picture and the second table layout has two rows containing two textviews and a picture. Is there some kind of xml to java converter? I've tried to write the code now for a couple of hours and can't get the layouts to work. I'd post a picture but I need 10 rep points first. The whole xml file is in a linear layout if it helps.
<TableLayout
android:id="#+id/tablemain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="center_horizontal" >
<TableRow
android:id="#+id/TableRow1"
android:background="#drawable/border"
android:weightSum="100" >
<TableLayout
android:id="#+id/Tableout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#drawable/border"
android:gravity="center_horizontal" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:adjustViewBounds="true"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:padding="3dp"
android:src="#drawable/unknown" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/Tableout3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="100"
android:gravity="center_horizontal"
android:weightSum="100" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#drawable/border" >
<TextView
android:id="#+id/Judgename"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="7dp"
android:text="Name"
android:textSize="20sp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tablerowctroom"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="50"
android:background="#drawable/border"
android:weightSum="100" >
<TextView
android:id="#+id/Courtroom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="70"
android:padding="7dp"
android:text="Room"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:layout_marginRight="10dp"
android:layout_weight="30"
android:src="#drawable/umid" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
This is the new code I'm trying to use based on Nathan Walter's advice. However, It'll set the first set text but the rest are just the default value. Although the layout looks right. How can I set the textfields for each individual view when the method is called.
public void Display(String name, String ctroom) {
ViewGroup parent = (ViewGroup) findViewById(R.id.container);
view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
parent.addView(view);
TextView jn = (TextView) findViewById(R.id.JudgenameCourts);
jn.setText(name);
TextView ct = (TextView) findViewById(R.id.Courtroomnew);
ct.setText(ctroom);
view = LayoutInflater.from(this).inflate(R.layout.tablebutton, null);
parent.addView(view);
jn.setText("Name1");
ct.setText("Courtroom1");
}
I think the code you want looks something like this. Let me know if it makes sense.
// Create an array of things to create views from.
// You will probably be getting your array from somewhere else
YourObjectHere[] things = new YourObjectHere[2];
things[0] = new YourObjectHere();
things[1] = new YourObjectHere();
// Get a reference to a LinearLayout to hold your views
LinearLayout list = (LinearLayout) findViewById(R.id.list);
// Loop through your list of objects
for(int i = 0; i < things.length; i++) {
// Inflate an instance of your layout
View listItem = getLayoutInflater().inflate(R.layout.child, null);
// Set the TextViews to the appropriate things
((TextView) listItem.findViewById(R.id.name)).setText(things[i].getName());
((TextView) listItem.findViewById(R.id.type)).setText(things[i].getType());
// Add the inflated view to the list
item.addView(child);
}

Unable to dynamically add EditText to LinearLayout

I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.
<LinearLayout
android:id="#+id/layout_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator"
android:layout_margin="5dp"
android:background="#color/yellow_background"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_check_empty" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tv_add_team_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:text="Add a team member"
android:textColor="#color/text_green"
android:textSize="15dp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_add_team_member"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/iv_create_assignment_attach"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_flag_alt" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="15"
android:text="Due"
android:textSize="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="65"
android:hint="None"
android:selectAllOnFocus="true" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_calendar_empty" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_create_assignment_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:text="Trash" />
<Button
android:id="#+id/btn_create_assignment_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:text="Done" />
</LinearLayout>
</LinearLayout>
Here is the Java code, the onClickListener implementation of the TextView.
tvAddTeamMember.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
EditText et = new EditText(getActivity());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Text");
ll.addView(et);
}
You may proceed this way ! This is not what you're looking for. But your problem will get resolved.
1) By default add the EditText view in your XML and make it invisible.
2) On Clicking the TextView, make it visible.
enjoy !
Hi #Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
Try to change the line :
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
With this:
android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Then do your work
If you know you might be adding a layout, you could always declare ViewStub. After that you simply find the viewStub and call inflate() on it:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
This is the promoted way to do that in Android SDK. Thanks to that you can define the infalte layout in an xml file.
Try with this code.
replace ur line ll.addView(et);
by::
ll.addView(et,p);
and delete ur line et.setLayoutParams(p);
A simple way to do this.
Button add = (Button) findViewById(R.id.add_checkpoint);
final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);
// layout has assigned weight = "1.0" in xml & it's a child of ScrollView
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater oLayoutInflater = getLayoutInflater();
// oView is declared globally & Layout "row_checkpoint" contains a EditText
oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);
final EditText speciesEditText = (EditText) oView
.findViewById(R.id.checkpoint);
speciesEditText.setId(layout.getChildCount());
// Perform whatever required over EditText, same way you can add more elements.
layout.addView(oView);
}
});
Hope it will help.

Categories

Resources