Adding TextView to LinearLayout in Android - android

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);

Related

method returning Layout or View

The app I am trying to make, has got a lot of similar LinearLayouts and textViews that need to be created programmatically and placed on the screen in a specific order.
So I decided to define a method which returns one element, and for the furthur uses,I will put the method in some loop to produce the others. but when I create a view or layout this way, nothing shows up or sometimes the app crashes, as if it's been sent null to addView(). It only works when I create the View/Layout in onCreate() and then I use it right there afterwards.So , any ideas that I can use the method to creat my Layout/View? Because they are too many and it's not possible to create them one by one in onCreate()
Here's the method:
public LinearLayout createLinearLayout(){
TextView tv_day = new TextView(this);
tv_day.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
tv_day.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
tv_day.setGravity(Gravity.END);
tv_day.setText("27");
LinearLayout ll_horizontal = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams ll_horizontal_params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll_horizontal.setLayoutParams(ll_horizontal_params);
ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
ll_horizontal.addView(tv_day);
return ll_horizontal;
}
and this is onCreate() which doesn't add any linear layouts with a textView in it :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_month_view);
LinearLayout ll= createLinearLayout();
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.activity_month_view);
mainLayout.addView(ll);
}
I think this should help
- add an empty linear layout in XML with some id.
- reference that layout in code
- add elements to that layout dynamically
Hey was just checking your code. Its working perfectly now just try this method.
public LinearLayout createLinearLayout(){
TextView tv_day = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tv_day.setLayoutParams(layoutParams);
tv_day.setGravity(Gravity.CENTER);
tv_day.setText("27");
LinearLayout ll_horizontal = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams ll_horizontal_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll_horizontal.setLayoutParams(ll_horizontal_params);
ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
ll_horizontal.addView(tv_day);
return ll_horizontal;
}

Making TextView Scrollable in Android programmatically

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);

To add textview dynamic at run time Android

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.

how to align text view to the right of Image view within Linearlayout Dynamically in android

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);

Dynamically Add A TextView - Android

How can I dynamically add a TextView to this? The commented out code doesn't work.
public class myTextSwitcher extends Activity {
private TextView myText;
public myTextSwitcher(String string){
//myText = new TextView(this);
//myText.setText("My Text");
}
}
You're creating a text view and setting its value but you're not specifying where and how it should be displayed. Your myText object needs to have a container of some sort which will make it visible.
What you're trying to do is dynamically layout a view. See here for a good starter article. From the article:
// This is where and how the view is used
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
// this part is where the containers get "wired" together
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
First of all, you shouldn't be adding it in the constructor, non-default constructors are pretty much useless for an Activity. Finally, you are correctly creating a new TextView but you are not adding it anywhere. Get ahold of some layout in your content view (probably with findViewById), and call layout.addView(myText) with it.
Did you add the your text view to the activity using setContentView(myText);
make this
myText = new TextView(this);
myText.setText("foo");
setContentView(myText);
in oncreate() method
final TextView tv1 = new TextView(this);
tv1.setText("Hii Folks");
tv1.setTextSize(14);
tv1.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout ll = (LinearLayout) findViewById(R.id.lin);
ll.addView(tv1);
Your activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
</LinearLayout>

Categories

Resources