Reputation: 147
I need to create textview( or similar) dinamicaly, iam a novice and i need your help. these are parameters recivied from a webservice
String jacuzzi=yes
String estacionamiento = no
String peli= yes
String cable=yes
String roomservice= no
i need to create them when the (variable == "yes")
i´ve benn looking for an answer but i don get it
i dont´know if i really need to create a new layout ,
i am really new in android
here i put my android xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:background="#ccccfe" >
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="45dp" >
<ImageView
android:id="@+id/imageView0"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#ffd7bd"
android:orientation="vertical"
android:layout_marginLeft="-200dp "
android:textStyle="bold"
android:textColor="#000000" android:shadowColor="#8a6603"
android:shadowDx="3" android:shadowDy="2" android:shadowRadius="1.8"
android:src="@drawable/muestra2" />
</TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginTop="10dp"
android:text="Motel: "
android:textColor="#303f46"
android:textSize="21sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imagen"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:background="#ccccfe"
android:scaleType="fitXY"
android:padding="1dp"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow >
<TextView />
<TextView />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dirección: "
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:width="90dp" />
<TextView
android:id="@+id/textView2"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737"/>
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:width="90dp"
android:text="Comuna: " />
<TextView
android:id="@+id/textView3"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:width="90dp"
android:text="Descripcion: " />
<TextView
android:id="@+id/desc"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:text="Telefono: "
android:width="90dp" />
<TextView
android:id="@+id/telefono"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737" />
</TableRow>
<TableRow >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:text="Correo: "
android:width="90dp"/>
<TextView
android:id="@+id/correo"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#303f46"
android:textSize="15sp"
android:textStyle="bold"
android:text="SitioWeb:"
android:width="90dp" />
<TextView
android:id="@+id/sitio"
android:text=""
android:textSize="14sp"
android:layout_width="wrap_content"
android:textColor="#373737" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
Upvotes: 0
Views: 1713
Reputation: 2189
This is how to dynamically create your layout using code rather than XML.
First you should give an id to your LinearLayout in your xml:
<LinearLayout android:id="@+id/myLayout">
Then in your code:
LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
if (variable.contentEquals("yes")) {
TextView jacuzzi = new TextView(this);
jacuzzi.setText("jacuzzi");
myLayout.addView(jacuzzi);
}
However avoid using Strings and setting them to yes. Use booleans instead.
Upvotes: 0
Reputation: 3453
Best to create them and drop them if NOT needed rather than the other way round as shown below.
Firstly
boolean jacuzzi = true;
boolean estacionamiento = false;
boolean peli = true;
boolean cable = true;
boolean roomservice = false;
Use booleans rather than Strings. boolean can either be true or false. Easier to work with then having yes or no.
// if you must use strings
Use
if (jacuzzi.contentEquals("yes")
instead of booleans below.
//
I'm going to show you for jacuzzi, use the same technique for all.
TextView tv = (TextView) findViewById(R.id.textView1);
I've called it tv, but you can call it anything. As for the R.id.textView1 - this is referencing to your TextView in your XML code that android automatically called textView1 but you can change this by changing the ID.
if(jacuzzi == false)
{tv.setVisibility(View.GONE);}
This reads if jacuzzi is false then make the TextView disappear. You can also use
if(!jacuzzi)
{tv.setVisibility(View.GONE);}
As they mean the same thing.
Otherwise for dynamically coding, it's a lot harder. You need to reference your linearlayout by findViewById similarly to how I did the textview. Then you need to create a new textview. If you feel you really want to learn how to do this it's harder but just ask.
Upvotes: 1