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, ...)
Related
I have a layout like below ( to play media files ) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/mediabar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnPlay"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.2"
android:text="P">
</Button>
<SeekBar
android:id="#+id/sbProgress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"/>
<TextView
android:id="#+id/txtTimeleft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00:00"
android:textSize="13sp"
android:paddingRight="10dp"/>
</LinearLayout>
</LinearLayout>
I use a class called CustomView which inherits from FrameLayout to contains the above layout.
public class CustomView extends FrameLayout
Then I will add the above layout to the CustomView on startup :
private void initItems(){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = null;
rowView = inflater.inflate(R.layout.shadow_layout,null,false);
btnPlay = rowView.findViewById(R.id.btnPlay);
seekBar = rowView.findViewById(R.id.sbProgress);
tvTime = rowView.findViewById(R.id.txtTimeleft);
//scriptFrame = (LinearLayout) rowView.findViewById(R.id.scriptframe);
this.addView(rowView);
setUpFont();
}
And finally, in my activity, I will create and add the above class to my view like this :
shadowingView = new CustomView(context);
shadowingView.setLayoutParams(
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
scrollView.addView(shadowingView);
But the result is not what I expected, the layout wasn't MATCH_PARENT (Play button's text is not displaying also) :
Please point me where I did wrong. Thank you!
you miss the android:weightSum attribute in your linearlayout
<LinearLayout
//put the weightSum here
android:id="#+id/mediabar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btnPlay"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.2"
android:text="P">
</Button>
<SeekBar
android:id="#+id/sbProgress"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:layout_gravity="center"/>
<TextView
android:id="#+id/txtTimeleft"
android:layout_width="0dip"
android:layout_weight="0.2"
android:layout_height="wrap_content"
android:text="00:00:00"
android:textSize="13sp"
android:paddingRight="10dp"/>
</LinearLayout>
here is the result :
your xml code is working fine here......
SHow me the code of
<FrameLayout
......
......
/>
There might be some error in it.....
try using
shadowingView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT));
Thanks for the answers guys. Unfortunately none of the answers worked. But I have fixed the problem.
For some unknown reason, the scrollview is the problem, all I have to do is change from FrameLayout to ScrollView
public class CustomView extends ScrollView
And here :
shadowingView = new CustomView(context);
shadowingView.setLayoutParams(
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
// scrollView.addView(shadowingView); REMOVE THIS LINE
// topParent.addView(scrollView); AND THIS LINE
topParent.addView(shadowingView)
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
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.
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.
Got my TextView in the xml:
<TextView
android:id="#+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="left"
android:text="TextView"/>
I want to create multiple TextViews but i want to look them the same as this.
So I tried:
TextView newTextView = new TextView(this);
newTextView.setLayoutParams(myTextView.getLayoutParams());
I think this should get all the layout parameters from myTextView straigthlghly(?) from the xml, and pass them to newTextView to set them.
My problem is: Nothing happens. It does not take effect, why?
here's a sample project which shows that it works.
you can see that it works by looking at the preview of the visual editor , which looks different than what is shown at runtime.
i think that your mistake was that you didn't set 0px (or 0dp, zero is still zero) for the weighted views.
main.xml (layout) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/container">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="0px" android:text="TextView1"
android:layout_weight="1" android:background="#ffff0000" />
<TextView android:id="#+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="TextView2"
android:background="#ff00ff00" />
</LinearLayout>
TestActivity.java :
public class TestActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView tv1=(TextView)findViewById(R.id.textView1);
final TextView tv2=(TextView)findViewById(R.id.textView2);
final LayoutParams layoutParams=tv1.getLayoutParams();
tv2.setLayoutParams(layoutParams);
// adding textView programatically:
final TextView tv3=new TextView(this);
tv3.setText("textView3");
tv3.setBackgroundColor(0xff0000ff);
tv3.setLayoutParams(layoutParams);
final ViewGroup root=(ViewGroup)findViewById(R.id.container);
root.addView(tv3);
}
}