Error in a dynamic text view based on the xml file - android

i am having a dynamic text view based on the xml file.....Please help me for my code......
my codes
MessageViewPage.java
package com.dpn.pack;
public class MessageViewPage extends Activity {
TextView tv1=new TextView(this);
ScrollView sv;
String nickname,body;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message_view_page);
Bundle b = getIntent().getExtras();
nickname= b.getString("nick");
body=b.getString("body");
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.message_view_page, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.textViewSender);
textView.setText("Sender : "+nickname);
// insert into main view
View insertPoint = findViewById(R.id.textViewSender);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
}
my xml file as follows
message_view_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="#drawable/view">
<TextView
android:id="#+id/textViewSender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="35dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:id="#+id/textViewBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textViewSender"
android:layout_below="#+id/textViewSender"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:textSize="15dp"
android:textColor="#F69DD1" />
<Button
android:id="#+id/buttonReply"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="45dp"
android:layout_marginBottom="50dp"
android:text="Reply" />
<Button
android:id="#+id/buttonListen"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="45dp"
android:layout_marginBottom="50dp"
android:text="LIsten" />
</RelativeLayout>
i am having an error in
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
Please any one tell me the solution.....

Your insertPoint is null. Because you don't set the layout before calling findViewById. So it will not find anything.
You have to call
setContentView(R.layout.message_view_page);
before.

you use a line RelativeLayout item = (RelativeLayout) findViewById(R.id.textViewSender);
in your code but hoe can you get textViewSenderid with out adding your xml file message_view_page.xml
you should add
setContentView(R.layout.message_view_page);
after
super.onCreate(savedInstanceState); this line.

Related

Adding a Textview to a Relative Layout in Android Studio

I have a relative layout in my XML file and it contains a button. Now I want that when I press this button, it creates 2 TextViews. Any help please because I am new to Android Studio? I have tried creating the onClickListener for the button but I am having problems in order to get an object of the current relative layout which I have in the XML.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brush_your_teeth);
Intent i = getIntent();
final Button addAlertButton = (Button)findViewById(R.id.AddAlert);
addAlertButton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
}
});
}
The following is the XML:
<?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"
tools:context="com.example.user.dentalapp.BrushYourTeeth"
tools:showIn="#layout/activity_main">
<!--ALERT 1-->
<TextView
android:id="#+id/Alert1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert 1"
android:textSize="25dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"/>
<TextView
android:id="#+id/Time1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="08:00"
android:textSize="25dp"
android:onClick="showTimePickerDialog"
android:layout_above="#+id/Alert2"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"/>
<!--ALERT 2-->
<TextView
android:id="#+id/Alert2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alert 2"
android:textSize="25dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_below="#id/Alert1"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"/>
<TextView
android:id="#+id/Time2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21:00"
android:textSize="25dp"
android:layout_below="#id/Alert1"
android:layout_marginTop="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"/>
<!--ADD ALERT BUTTON-->
<Button
android:id="#+id/AddAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Alert"
android:textAllCaps="false"
android:textSize="25dp"
android:padding="15dp"
android:layout_below="#id/Alert2"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"/>
</RelativeLayout>
Thanks!!
This link may be useful for you:
displaying a string on the textview when clicking a button in android
It is for one string, you may add your second text view into method below:
private void printmyname(){
System.out.println("coming");
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_brush_your_teeth);
final Button addAlertButton = (Button) findViewById(R.id.AddAlert);
addAlertButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout);
int prevTextViewId = 0;
final TextView textView1 = new TextView(this);
final TextView textView2 = new TextView(this);
textView1.setText("Text 1");
textView2.setText("Text 2");
int curTextViewId = v.getId();
textView1.setId(curTextViewId+1);
textView2.setId(curTextViewId+2);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, v.getId()+1);
textView1.setLayoutParams(params);
params.addRule(RelativeLayout.BELOW, v.getId()+2);
textView2.setLayoutParams(params);
layout.addView(textView1, params);
layout.addView(textView2, params);
}
});
}
Not sure but something like this helps you

Why I am getting NullPointerException on ImageView?

My code is as follows:
activity_for_me.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:id="#+id/recco_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
<ImageView
android:id="#+id/checkin"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:onClick="ClickCheckIn"
android:src="#drawable/checkin" />
</RelativeLayout>
recco_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#layout/nine_patch" >
<ImageView
android:id="#+id/outlet_icon"
android:layout_marginLeft="15dp"
android:layout_height="55dp"
android:layout_width="55dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="15dp"
android:hint="Distance"
/>
<TextView
android:id="#+id/outlet_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_marginLeft = "15dip"
android:hint="outlet"
/>
<TextView
android:id="#+id/reward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/outlet_name"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_marginLeft = "15dip"
android:layout_marginTop="5dp"
android:textColor="#color/abc_search_url_text_holo"
android:hint="Reward"
/>
<View
android:id="#+id/rule"
android:layout_below="#id/reward"
android:layout_toRightOf="#id/outlet_icon"
android:layout_toLeftOf="#id/distance"
android:layout_width="fill_parent"
android:layout_marginLeft = "15dip"
android:layout_height="1dp"
android:background="#c0c0c0"/>
<ImageView
android:id="#+id/favourite_icon"
android:layout_below="#id/rule"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"
android:layout_height="15dp"
android:layout_width="15dp"
android:src="#drawable/heart_empty"/>
<ImageView
android:id="#+id/loc_icon"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_below="#id/rule"
android:layout_marginLeft = "15dip"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/outlet_icon"
android:src="#drawable/map_pointer"/>
<TextView
android:id="#+id/locality"
android:layout_toRightOf="#id/loc_icon"
android:layout_toLeftOf="#id/distance"
android:layout_below="#id/rule"
android:layout_marginRight="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft = "5dip"
android:layout_marginTop="5dp"
android:hint="Locality"
/>
</RelativeLayout>
Activity.java
public class ForMeActivity extends Fragment {
#SuppressLint("ClickableViewAccessibility")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RelativeLayout rootView = (RelativeLayout) inflater.inflate(
R.layout.activity_for_me, container, false);
ImageView favourite_heart = (ImageView) rootView
.findViewById(R.id.favourite_icon);
favourite_heart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(YourActivityName.this,
// "The favorite list would appear on clicking this icon",
// Toast.LENGTH_LONG).show();
System.out.println("Favourite Icon clicked");
}
});
list = (ListView) rootView.findViewById(R.id.recco_list);
ImageView check_in_img = (ImageView) rootView
.findViewById(R.id.checkin);
ImageView outlet_logo = (ImageView) rootView
.findViewById(R.id.outlet_icon);
final String data = getArguments().getString("data");
ForMeFile = new File(getActivity().getExternalFilesDir(filepath),
filename);
final String cookie = getArguments().getString("cookie");
System.out.print(cookie);
check_in_img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getActivity(), CheckInView.class);
startActivity(in);
System.out.println("Checkin Icon clicked");
}
});
}
I want to change image for favourite_icon ImageView when I click on it. But as soon as I click on the icon I get NullPointerException. What is the reason that click of check_in_img is working but click of favourite_icon is not working even though the current layout is the one which contains favourite_icon? Other sources of SO says to check the same.
favourite_icon is a part of recco_list.xml and you are trying to find it in activity_for_me.xml. The view does not exist there and hence, the exception.
R.id.favourite_icon is not there in activity_for_me.xml.
And if you want to access the items in the listview, I suggest you read about ListView and getView().

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.

addView FILL_PARENT not working

I want to put a layout into another one via layoutInflater.
Here's the main_layout.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/ads_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:id="#+id/host_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/navigation_buttons_layout"
android:layout_alignParentLeft="true"
android:layout_below="#+id/ads_image_view" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/navigation_buttons_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
</RelativeLayout>
and here's the vitrin_layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0000"
android:text="smallred" />
and finally the onCreate method for my main activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout);
View view = LayoutInflater.from(getBaseContext()).inflate(
R.layout.vitrin_layout, null);
parent.addView(view, LayoutParams.FILL_PARENT);
setTitle(getString(R.string.title_activity_vitrin));
}
The problem is that the vitrin_layout, does not fit into its parent(host_layout); although I've set fill_parent where ever I could!
I can't figure out why.
Edited: I'm really sorry, but i'd pasted wrong vitrin_layout file, I fixed it.
Use this addView method. By the way don't use getBaseContext() unless you know why you are using it, instead use context of an activity (this).
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
ViewGroup parent = (ViewGroup) findViewById(R.id.host_layout);
View view = LayoutInflater.from(this).inflate(R.layout.vitrin_layout, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
parent.addView(view, params);
setTitle(getString(R.string.title_activity_vitrin));
}
parent.addView(view, LayoutParams.FILL_PARENT);
this method was ViewGroup.addView(View child, int index) invoked actually.
you need to invoke ViewGroup.addView(View child, ViewGroup.LayoutParams params) method to set a LayoutParams.

trying to have a dynamic textview [duplicate]

This question already has answers here:
Dynamic textView error while clicking listView
(2 answers)
Closed 8 years ago.
I'm trying to have a dynamic textview based on the xml file.....
Please help me with my code
CODE
MessageViewPage.java
package com.dpn.pack;
public class MessageViewPage extends Activity {
TextView tv1=new TextView(this);
ScrollView sv;
String nickname,body;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.message_view_page);
Bundle b = getIntent().getExtras();
nickname= b.getString("nick");
body=b.getString("body");
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.message_view_page, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.textViewSender);
textView.setText("Sender : "+nickname);
// insert into main view
View insertPoint = findViewById(R.id.textViewSender);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
}
my XML file as follows
message_view_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp"
android:background="#drawable/view">
<TextView
android:id="#+id/textViewSender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="35dp"
android:textColor="#FFFFFF"
android:textSize="20dp" />
<TextView
android:id="#+id/textViewBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textViewSender"
android:layout_below="#+id/textViewSender"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:textSize="15dp"
android:textColor="#F69DD1" />
<Button
android:id="#+id/buttonReply"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="45dp"
android:layout_marginBottom="50dp"
android:text="Reply" />
<Button
android:id="#+id/buttonListen"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="45dp"
android:layout_marginBottom="50dp"
android:text="LIsten" />
</RelativeLayout>
I am having an error in this code
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
like
Please any one tell me the solution for my problem.
my target output demo like
You have a duplicate parameter:
ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT
Try:
ViewGroup.LayoutParams.FILL_PARENT
I could be mistaken, as you show where the error is but do not say what it actually is.
So try instead of passing v to addView() method, to pass your textView.
Something like this: insertPoint.addView(textView, 0, ...)

Categories

Resources