I want to make textView programmatically and make them scrollable. I am calling this method again and again to get textView and adding it to the linearLayout.
TextView textView = addTextView(contents.paragraphs.get(i),false);
linearLayout.addView(textView);
But, it is not scrollable at all. The method is:
private TextView addTextView(String text,boolean type) {
TextView valueTV = new TextView(this);
valueTV.setText(text);
valueTV.setTextColor(getResources().getColor(R.color.colorTextPrimary));
valueTV.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
valueTV.setMaxLines(1000);
valueTV.setVerticalScrollBarEnabled(true);
valueTV.setMovementMethod(new ScrollingMovementMethod());
return valueTV;
}
Change to:
valueTV.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
If you use WRAP_CONTENT the TextView will increase height 'indefinitely'.
You can wrap the TextView into a ScrollView
ViewGroup linearLayout = findViewById(R.id.linear_layout);
TextView textView = addTextView(contents.paragraphs.get(i), false);
ScrollView scroller = new ScrollView(getApplicationContext());
scroller.addView(textView);
linearLayout.addView(scroller);
Related
I am adding a dynamic layout view which looks like this.
I am appending this view on button click. Here is the code for this view on button click
public void addDynamicView() {
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
LinearLayout lHori = new LinearLayout(this);
lHori.setOrientation(LinearLayout.HORIZONTAL);
//lHori.setWeightSum(3);
lHori.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout lVertical = new LinearLayout(this);
lVertical.setOrientation(LinearLayout.VERTICAL);
//lVertical.setWeightSum(2);
lVertical.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
Button button = new Button(this);
button.setLayoutParams(new LinearLayout.LayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)));
button.setVisibility(View.VISIBLE);
button.setText("button");
TextView txtEmpName = new TextView(this);
txtEmpName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
txtEmpName.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txtEmpName.setText("txt1");
TextView txtEmpDropTime = new TextView(this);
txtEmpDropTime.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
txtEmpDropTime.setText("txt2");
lVertical.addView(txtEmpName);
lVertical.addView(txtEmpDropTime);
lHori.addView(lVertical);
lHori.addView(button);
relativeLayout.addView(lHori);
linear.addView(relativeLayout);
}
The problem is I want all the buttons to be placed below the click button.
But I get output like this
How can I place all Button with text Button below the Click button?
If you two do with xml than it's so simple just make a layout which you want after click on click button after that set visibility gone for all layout accept click button next in Java on button click you can call visibility view to make that layout ...
Hope so it will works for you..
You can create a LinearLayout lButton:
LinearLayout lButton = new LinearLayout(this);
lButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
then set its gravity as Gravity.END
lButton.setGravity(Gravity.END)
and add the button into it
lButton.addView(button);
lHori.addView(lVertical);
lHori.addView(lButton);
Hello I am trying to make 2 TextViews apear above each other through code.
My question is:
How can I stack them over each other?
Sample Code
private TextView DurrationView;
private TextView DurrationViewoverall;
DurrationView = new TextView(getContext());
DurrationView.setText("");
addView(DurrationView);
DurrationViewoverall = new TextView(getContext());
DurrationViewoverall.setPadding(5,5,5,5);
DurrationViewoverall.setText("");
addView(DurrationViewoverall);
I am trying to have DurrationView appear above DurrationViewoverall.
The class they are in extends Linear Layout.
Add LayoutParams to the textview and make your linear layout as orientation vertical
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
DurrationView = new TextView(getContext());
LinearLayout.LayoutParams durrationViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurrationView.setLayoutParams(durrationViewParams);
DurrationView.setText("Text1");
linearLayout.addView(DurrationView);
DurrationViewoverall = new TextView(getContext());
LinearLayout.LayoutParams durrationViewoverallParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
DurrationViewoverall.setLayoutParams(durrationViewoverallParams);
DurrationViewoverall.setPadding(5,5,5,5);
DurrationViewoverall.setText("Text2");
linearLayout.addView(DurrationViewoverall);
addView(linearLayout);
Check that the LinearLayout is set to have a vertical orientation. If that is set properly, you may need to set the LayoutParams of the TextViews programatically as well.
I want to add Text-view at run time on Android to display the contents of my database on a activity,which can change every time.
Please Help
You could create a TextView dynamically like this for example:
//create a TextView with Layout parameters according to your needs
LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//if your parent Layout is relativeLayout, just change the word LinearLayout with RelativeLayout
TextView tv=new TextView(this);
tv.setLayoutParams(lparams);
tv.setText("test");
//get the parent layout for your new TextView and add the new TextView to it
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example);
linearLayout.addView(tv);
And in the tv.setText(...); line you could set the text, that your got from your database.
I have 2 TextViews and I want to add those to LinearLayout, but when I ran the project, only one TextView appreared.
Here's my code:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Thank you, Jesus!");
textView.setTextColor(Color.BLACK);
TextView textView2 = new TextView(this);
textView.setText("Dont give up on me!");
textView.setTextColor(Color.BLACK);
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.WHITE);
layout.addView(textView);
layout.addView(textView2);
setContentView(layout);
}
}
After running, textView2 was the only view present in the LinearLayout.
Can someone explain to me what was going on?
Use textView2 for calling setText and setTextColor method for textView2 because currently you are using textView :
TextView textView2 = new TextView(this);
textView2.setText("Dont give up on me!");
textView2.setTextColor(Color.BLACK);
Suggestion also set height/width for all views by calling setLayoutParams method
Another Suggestion : Add Orientation to Linear layout by using: layout.setOrientation(LinearLayout.VERTICAL);
LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout3);
TextView tv = new TextView(this);
ImageButton imb=new ImageButton(this);
ll.addView(tv);
tv.setSingleLine();
tv.setEllipsize(TruncateAt.MARQUEE);
tv.setHorizontallyScrolling(true);
tv.setFocusableInTouchMode(true);
tv.setText(myString1);
This is my code and I'm able to display data in textView now. But I want to put one imageButton to the left of textView so that will work as cancel strip. but I'm having a problem. How can I set imageButton left of text view programatically?
You have two solutions for your question. You can use the below code if you just want to show image on the right of textview.
TextView textView = (TextView) findViewById(R.id.myTxtView);
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon,0, 0,0);
or else
set orientation of your linearlayout as Horizontal and add the imageview first and then textview second. So they will be aligned as your requirement.
Below is the example.
TextView tv = new TextView(this);
tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton imb=new ImageButton(this);
imb.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ll.addView(imb);
ll.addView(tv);