Dynamic edit text on button add and remove - android

I already read tun of code here or anywhere else. And i am realy confused. I found on web simple prog on add and remove edittext with remove button. But when i add text view more then ones, it has same id, but i need that text what is wrote inside. And if was possible i need next id with letters, not in number.
here is my code:
public class MainActivity extends AppCompatActivity {
private LinearLayout parentLinearLayout1;
private LinearLayout parentLinearLayout2;
TextView a1;
TextView txt;
#Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
parentLinearLayout1 = (LinearLayout) findViewById (R.id.parent_linear_layout1);
parentLinearLayout2 = (LinearLayout) findViewById (R.id.parent_linear_layout2);
a1 = (TextView) findViewById (R.id.textView5);
txt = (EditText) findViewById (R.id.txt);
}
public void onAddField( View v ) {
LayoutInflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate (R.layout.field, null);
parentLinearLayout1.addView (rowView, parentLinearLayout1.getChildCount () );
Button addbtn = (Button) findViewById (R.id.add_field_button);
int id = addbtn.getId ();
a1 = (TextView) findViewById (R.id.textView5);
a1.setText (id + "");
}
public void onDelete( View v ) {
parentLinearLayout1.removeView ((View) v.getParent ());
Button del = (Button) findViewById (R.id.deletebuton1);
// c=(EditText)findViewById (R.id.number_edit_text);
int id = del.getId ();
a1 = (TextView) findViewById (R.id.textView5);
a1.setText (id + "");
}
}
activity_main xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/parent_linear_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Město" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_linear_layout1"
android:layout_width="374dp"
android:layout_height="120dp"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<EditText
android:id="#+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:inputType="text" />
<Button
android:id="#+id/add_field_button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:background="#555"
android:onClick="onAddField"
android:paddingLeft="5dp"
android:text="Add Field"
android:textColor="#FFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
and field xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/number_edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:inputType="text" />
<Button
android:id="#+id/deletebuton1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"
android:onClick="onDelete">
</LinearLayout>
Its even possible or this code is unseless and i must start again?
Thanks for answer.

Related

How to switch between 2 TextViews places on the layout?

I have an OnClickListener that listens to Button A clicks. I also have 2 TextViews below the Button.
I want that on every click on Button A, the 2 TextViews will switch their places between them, like this:
Before clicking:
TextView A
TextView B
After clicking:
TextView B
TextView A
How can I do it? Is there a special function that meant to do that? or some kind of a trick? Thanks!
actvity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
</RelativeLayout>
MainActivity.java:
txtA = (TextView) findViewById(R.id.txtA);
txtB = (TextView) findViewById(R.id.txtB);
float mAX = txtA.getX();
float mAY = txtA.getY();
float mBX = txtB.getX();
float mBY= txtB.getY();
txtA.setX(mBX);
txtA.setY(mBY);
txtB.setX(mAX);
txtB.setY(mAY);
Trick is changing the x and y axis of both views :
Your xml code:
<?xml version="1.0" encoding="utf-8"?>
<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="fill_parent"
android:gravity="start"
tools:context="com.intap.tof.MainActivity">
<TextView
android:id="#+id/txtA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="20dp"
android:text="A"
android:tag="A"
android:clickable="true"
android:visibility="visible"/>
<TextView
android:id="#+id/txtB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtA"
android:text="B"
android:tag="B"
android:clickable="true"
android:layout_marginTop="83dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="visible"
android:textSize="20dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch"
android:id="#+id/btn1"
android:onClick="onClickButton"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Java Code :
public class MainActivity extends Activity {
TextView txtA,txtB;
boolean _isOnTxtA;
Button btn1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtA = (TextView)findViewById(R.id.txtA);
txtB = (TextView)findViewById(R.id.txtB);
btn1 = (Button)findViewById(R.id.btn1);
}
public void onClickButton(View v)
{
float mPos1x,mPos1y,mPos2x,mPos2y;
mPos1x = txtB.getX();
mPos1y = txtB.getY();
mPos2x = txtA.getX();
mPos2y= txtA.getY();
if(_isOnTxtA)
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA = false;
}
else
{
txtB.setX(mPos2x);
txtB.setY(mPos2y);
txtA.setX(mPos1x);
txtA.setY(mPos1y);
_isOnTxtA= true;
}
}
}
View.bringToFront method may be useful.
Where your layout is like:
<LinearLayout>
<TextView A>
<TextView B>
</LinearLayout>
To call bringToFront on TextView A will bring it front (the last position in the parent LinearLayout).
<LinearLayout>
<TextView B>
<TextView A>
</LinearLayout>
For more detailed example, below is layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/text_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_a" />
<TextView
android:id="#+id/text_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text_b" />
</LinearLayout>
In your OnClickListener (in your Activity) call:
View textViewA = findViewById(R.id.text_a);
textViewA.bringToFront();
This should work.
Toggling behavior can be achieved by this application. For example:
ViewGroup textHolder = (ViewGroup) findViewById(R.id.text_holder);
textHolder.getChildAt(0).bringToFront();
ViewGroup.getChildAt(0) always returns the first child of the ViewGroup. So everytime you call bringToFront on the first child will be bring to front.
All previous answers do not work because you use a relative layout where a linear layout will suffice.
If you have to go RelativeLayout, do the following in your click listener
TextView textA = (TextView) findViewById(R.id.txtA);
TextView textB = (TextView) findViewById(R.id.txtB);
RelativeLayout.LayoutParams textALayoutParams = (RelativeLayout.LayoutParams) textA.getLayoutParams();
textALayoutParams.addRule(RelativeLayout.BELOW, R.id.txtB);
textA.setLayoutParams(textALayoutParams);
RelativeLayout.LayoutParams textBLayoutParams = (RelativeLayout.LayoutParams) textB.getLayoutParams();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
textBLayoutParams.removeRule(RelativeLayout.BELOW);
} else {
textBLayoutParams.addRule(RelativeLayout.BELOW,0);
}
textB.setLayoutParams(textBLayoutParams);
This will place A under B. You can do the same for reversing.

Android: How to inflate different layout from different buttons?

I trying to do making something tabs but using button.
but each time I click on button it add me a row.
I want it to change the all layout to another..
MainActivity
public class MainActivity extends Activity {
Button btn1;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(switchListener);
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(switchListener);
}
private OnClickListener switchListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//ScrollView sv = (ScrollView) findViewById(R.id.MyScrollView);
LinearLayout ll = (LinearLayout) findViewById(R.id.MyLinearLayout);
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
if (btn1 == v) {
View v1 = inflater.inflate(R.layout.firstview, null);
ll.addView(v1);
} else {
View v2 = getLayoutInflater().inflate(R.layout.secondview, null);
ll.addView(v2);
}
}
};
}
and here my Main XML file
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Layout 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Layout 2" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ScrollView
android:id="#+id/MyScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/MyLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</TableRow>
and here my other two layouts that I want to inflate
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="First View"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:background="#android:color/holo_blue_light"/>
and the second
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Second View"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:background="#android:color/holo_red_light"/>
First make match parent your tableview.
and now do below on your code:-
if(ll.getChildCount() > 0) {
ll.removeAllViews();
}
if (btn1 == v) {
View v1 = inflater.inflate(R.layout.firstview, null);
ll.addView(v1);
} else {
View v2 = getLayoutInflater().inflate(R.layout.secondview, null);
ll.addView(v2);
}
the problem is you are just adding child on layout one by one so you need to remove first then add into layout so try above code.
before adding views to your ll linear layout check, as
if(ll.getChildCount() > 0) {
ll.removeAllViews();
}
then perform:
ll.addView(view) v1 or v2 as required.

Custom ListView Item barely visible

I'm having a problem that all my ListViews Items are showing like this:
As you guys can see Hello World! is a TextView and is showing perfectly.
But the ListView itens are showing like they are disabled.
This is my Activity's onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_answers);
ctx = getApplicationContext();
Intent intent = getIntent();
usersurvey_id = intent.getIntExtra("usersurvey_id", -1);
list = (ListView) findViewById(R.id.listview_answers);
getAnswers();
AnswersArrayAdapter adapter = new AnswersArrayAdapter(ctx, answersList);
list.setAdapter(adapter);
list.setClickable(false);
list.setEnabled(true);
}
This is my Custom Adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent){
Answers answerItem = answersList.get(position);
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.listitem_answers, null);
holder = new ViewHolder();
holder.question = (TextView) convertView.findViewById(R.id.tv_texto_pergunta);
holder.answer = (TextView) convertView.findViewById(R.id.tv_texto_resposta);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.question.setText(getQuestionById(answerItem.getAnswer_question_id()));
holder.answer.setText(answerItem.getAnswer_answer());
return convertView;
}
Activity layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.seven.questionario.OpenAnswers">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/listview_answers"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
And ListView Item Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv_pergunta"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Pergunta:"
android:background="#D0D0D0"/>
<TextView
android:id="#+id/tv_texto_pergunta"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_below="#id/tv_pergunta"
android:text="Texto da pergunta"
android:background="#D0D0D0"/>
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:background="#drawable/dotted"
/>
<TextView
android:id="#+id/tv_resposta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:text="Resposta:"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_texto_resposta"
android:layout_width="fill_parent"
android:layout_height="80sp"
android:layout_below="#id/tv_resposta"
android:text="Texto da resposta" />
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:layout_below="#id/tv_texto_resposta"
/>
I now this is a bit old but i just stumbled across this post.
Could it be that you're using the wrong context? I think you should pass the current context of your Activity, like so:
ctx = getContext();
or like this:
AnswersArrayAdapter adapter = new AnswersArrayAdapter(this, answersList);
try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#D0D0D0">
<TextView
android:id="#+id/tv_pergunta"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Pergunta:"/>
<TextView
android:id="#+id/tv_texto_pergunta"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_below="#id/tv_pergunta"
android:text="Texto da pergunta"/>
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:background="#drawable/dotted"
/>
<TextView
android:id="#+id/tv_resposta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_texto_pergunta"
android:text="Resposta:"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_texto_resposta"
android:layout_width="fill_parent"
android:layout_height="80sp"
android:layout_below="#id/tv_resposta"
android:text="Texto da resposta" />
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/back"
android:layout_below="#id/tv_texto_resposta"
/>
Show your theme(application/activity) selected in AndroidManifest.xml. I guess it's the problem.
I followed #Aun tip and solved my problem.
I just had to set manually all my app's colors.
try this line
android:textColor="#000000" in your TextView

Inflate layout programmatically within another layout

I need help with my android app. I need inflate a layout within another layout and I dont know how I do. My xml code is this:
item.xml - I need inflate multiple xml (depending on a variable number)
<RelativeLayout
android:id="#+id/cartel_N1"
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="#color/tabhost_background_pressed"
android:layout_marginRight="22dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_N1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:src="#drawable/mcdonalds_icon" />
<TextView
android:id="#+id/title_N1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="McDonals del CC Alcampo"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textSize="15sp" />
<TextView
android:id="#+id/categoria_N1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="CATEGORIA"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
<RelativeLayout
android:id="#+id/stars_and_distance_N1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/stars_N1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stars_25"
android:layout_marginLeft="15dp"
android:layout_marginTop="7dp" />
<TextView
android:id="#+id/distance_N1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200m"
android:textSize="12sp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:gravity="center_vertical"
android:layout_marginTop="3dp" />
<ImageView
android:id="#+id/icon_pos_N1"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="#drawable/marker_distance"
android:layout_toLeftOf="#id/distance_N1"
android:layout_marginTop="7dp" />
</RelativeLayout><LinearLayout
android:id="#+id/cartel_N1"
android:layout_width="150dp"
android:layout_height="match_parent"
android:background="#color/tabhost_background_pressed"
android:layout_marginRight="22dp"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_N1"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:src="#drawable/mcdonalds_icon" />
<TextView
android:id="#+id/title_N1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="McDonals del CC Alcampo"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textSize="15sp" />
<TextView
android:id="#+id/categoria_N1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="CATEGORIA"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
<RelativeLayout
android:id="#+id/stars_and_distance_N1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/stars_N1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stars_25"
android:layout_marginLeft="15dp"
android:layout_marginTop="7dp" />
<TextView
android:id="#+id/distance_N1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200m"
android:textSize="12sp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:gravity="center_vertical"
android:layout_marginTop="3dp" />
<ImageView
android:id="#+id/icon_pos_N1"
android:layout_width="10dp"
android:layout_height="10dp"
android:src="#drawable/marker_distance"
android:layout_toLeftOf="#id/distance_N1"
android:layout_marginTop="7dp" />
</RelativeLayout>
This is my main xml:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/container_destacado"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- Inflate multiple xml file here -->
</LinearLayout>
</ScrollView>
You could use something like
LayoutInflater inflater = LayoutInflater.from(context);
//to get the MainLayout
View view = inflater.inflate(container_destacado, null);
...
//Avoid pass null in the root it ignores spaces in the child layout
View inflatedLayout= inflater.inflate(R.layout.yourLayout, (ViewGroup) view, false);
containerDestacado.addView(inflatedLayout);
You can implement this like below:
LayoutInflater linf;
LinearLayout rr;
linf = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
linf = LayoutInflater.from(activity.this);
rr = (LinearLayout) findViewById(R.id.container_destacado);
for (int i = 1; i < NoOfTimes; i++) {
final View v = linf.inflate(R.layout.item, null);
rr.addView(v);
}
ll = (LinearLayout) findViewById(R.id.container_destacado); // ll is the layout where your inflated layout will be added
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int pos = 0;
while (pos < noOfTimes)
{
View myView = linflater.inflate(R.layout.item, null); //here item is the the layout you want to inflate
myView.setId(pos);
/*
You can change TextView text and ImageView images here e.g.
TextView tv = (TextView)myView.findViewById(R.id.title_N1);
tv.setText(pos);
*/
pos++;
ll.addView(myView);
}
In some point you should gain access to your inflater inside your activity, you can call it with this code:
LayoutInflater li = LayoutInflater.from(context);
Where context is this or this.getActivity() if it is a Fragment. Then inflate your layour with:
View layout = li.inflate(R.layout.your_layout, null, false);
Then use addView(layout) to your container_destacado:
View containerDestacado = li.inflate(R.layout.container_destacado, null, false);
containerDestacado.addView(layout);
Hope it helps.
Kotlin code to do so:
val layoutToInflate =
this.layoutInflater.inflate(R.layout.ly_custom_layout,
null)
container_destacado.addView(layoutToInflate )
You have the LinearLayout on which you want to inflate other childs:
LinearLayout container = (LinearLayout)parentView.findViewById(R.id.container_destacado);
Once you loaded the item.xml with an inflater, you can just use
container.addView(itemView);
in Kotlin with editable textview elements on item.xml
val item = LayoutInflater.from(this).inflate(R.layout.item,null)
val distance_N1_holder = item.findViewById<TextView>(R.id.distance_N1)
distance_N1_holder.text = //dynamically generated value
main.addView(item)
Below is a working code from one of my projects:
// The parent container
mNotificationOverlay = (LinearLayout) findViewById(R.id.container_destacado);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (Message message : m) {
// Inflate the child layout on the fly
final View notificationContainer = inflater.inflate(R.layout.notification_overlay_linear_layout, null);
notificationContainer.setTag(message.getNotificationId());
// Access children of child container
TextView notificationOverlayTitle = (TextView) notificationContainer.findViewById(R.id.notification_title_overlay);
TextView notificationOverlayBody = (TextView) notificationContainer.findViewById(R.id.notification_body_overlay);
ImageButton notificationOverlayCancelButton = (ImageButton) notificationContainer.findViewById(R.id.notification_cancel_overlay);
// Perform desired operations
notificationOverlayCancelButton.setTag(message.getNotificationId());
notificationOverlayTitle.setText(message.getTitle());
notificationOverlayBody.setText(message.getNotificationBody());
mNotificationOverlay.setVisibility(View.VISIBLE);
// Attach any listeners
attachListenersToCancelView(notificationOverlayCancelButton);
// Add view to parent container
mNotificationOverlay.addView(notificationContainer);
}
Hope that helps!
If someone couldn't get the addView() it's because the view is not a layout.

How to make a view appear below

Hey I want to make a view appear below the last that has been created.
It is showing like this:
I want to make the next view show below the last one, so for each new view added, it shows below. Understand?
Here is my code. The xml file and the java file.
listitem.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/linearId"
android:padding="6dip"
>
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip" android:layout_weight="1"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/txt1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="#+id/txt2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
</LinearLayout>
RatedCalls.java
public class RatedCalls extends Activity {
private static final String LOG_TAG = "RatedCalls";
private TableLayout table;
private CallDataHelper cdh;
private TableRow row;
private TableRow row2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listitem);
Log.i(LOG_TAG, "calling from onCreate()");
cdh = new CallDataHelper(this);
startService(new Intent(this, RatedCallsService.class));
Log.i(LOG_TAG, "Service called.");
Log.i(LOG_TAG, "before call fillList");
List<String> ratedCalls = new ArrayList<String>();
ratedCalls = this.cdh.selectTopCalls();
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
for (int i = 0; i < 1; i++) {
View item = inflater.inflate(R.layout.listitem, null);
TextView x = (TextView) item.findViewById(R.id.txt1);
x.setText(ratedCalls.get(0));
TextView ht = (TextView) item.findViewById(R.id.txt2);
ht.setText(ratedCalls.get(1));
ll.addView(item, ViewGroup.LayoutParams.WRAP_CONTENT);
}
}
Your problem is that you should be using listitem.xml from a list adapter. What you're doing is, first setting your content view to an instance of listitem.xml, then trying to insert an instance of listitem INTO listitem's first LinearLayout. You should use listitem.xml as the view that you inflate in your custom adapter's getView() method. See the QuoteAdapter class from this question for an example, or just Google search "custom ArrayAdapter Android" for tons of results on this topic.
To work so you need to create two different xml for each inflater
In your layout folder,new xml layout/main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:src="#drawable/icon"
/>
<TextView
android:id="#+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:gravity="center_vertical"
android:text="My Application"
/>
<TextView
android:id="#+id/txt2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="marquee"
android:text="Simple application that shows how to use RelativeLayout"
android:textSize="10sp"
/>
</LinearLayout>
Then your layout/main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/linearId"
android:orientation="vertical"
>
<TextView android:id="#+id/rowCount" android:layout_width="wrap_content"
android:layout_height="wrap_content" ></TextView>
<LinearLayout
android:id="#+id/linearId2"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
</LinearLayout>
</LinearLayout>
Then you can set you activity like this
private int ELEMENTINEACHROW=2 ;
private int NOOFROW = 4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout ll = (LinearLayout) this.findViewById(R.id.linearId);
for (int j = 0; j < NOOFROW; j++) {
View itemMain = inflater.inflate(R.layout.main, null, false);
TextView rowCount = (TextView) itemMain.findViewById(R.id.rowCount);
rowCount.setText("Row:"+(j+1));
LinearLayout ll2 = (LinearLayout) itemMain.findViewById(R.id.linearId2);
for (int i = 0; i < ELEMENTINEACHROW; i++) {
View item = inflater.inflate(R.layout.main2, null, false);
TextView x = (TextView) item.findViewById(R.id.txt1);
x.setText("test");
TextView ht = (TextView) item.findViewById(R.id.txt2);
ht.setText("good");
ll2.addView(item, ViewGroup.LayoutParams.FILL_PARENT);
}
ll.addView(itemMain, ViewGroup.LayoutParams.FILL_PARENT);
}
}
Hope it give you the idea.
mmm...I did not understand fully what is required, but may be you need to add android:orientation="vertical" to LinearLayout with id="linearId".
In your xml file, set the android:orientation attribute to vertical on your <LinearLayout/>:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:id="#+id/linearId"
android:padding="6dip"
android:orientation="vertical"/>
Check out the LinearLayout class documentation and the orientation documentation for more information.

Categories

Resources