dynamic display of test without using textview : Android - android

Actually I want to show some text using dynamic variable , along with want to take an another input from user so I want a input text thing & send button too.
I googled but every place i am getting that i need to use this kind of code
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText("score :"+test+"\n x : 3\n y : 8");
setContentView(textView);
But using this full layout becomes textview & i cant add button , if i do , i can't see them on screen .All i see is the text above.

If I'm reading this correctly, you want to have a layout which is something like this:
<RelativeLayout ....>
<EditText .... />
<Button ..... />
<TextView android:id="#+id/scoreDisplay" ..... />
</RelativeLayout>
Then, in your activity's onCreate:
setContentView(R.layout......);
TextView tv = findViewById(R.id.scoreDisplay);
tv.setTextSize(20);
tv.setText("score :"+test+"\n x : 3\n y : 8");
This will have a layout containing 3 elements (an EditText, a Button, and a TextView).

Related

Set gravity to two different texts in one textview Android

I need to set two texts(Text1 and Text2) in one single TextView.Following are my requirements.
Text1 should be bigger than Text2 in font size.
Text2 would below Text1. Both are centrally aligned.
Text1 should be given gravity: center so that it resembles other layout.Text 2 will always be below Text1
I'm still unclear about your question, do you want to display both texts at the same time or not?
If you're only going to display one 'style' at a time, mayo's answer is right. You can use a switch statement to implement it:
switch(textStyle){
case 1: myTextView.setTextAppearance(getApplicationContext(), R.style.styleText1);
break;
case 2: myTextView.setTextAppearance(getApplicationContext(), R.style.styleText2);
break
}
More info here.
But if you want to display both at the same time, a WebView is your only option:
Webview wv;
//Specify Text1 and Text2 as strings.
String text = "<html><body style=\"font-size:25px;\">"+"<p align=\"center\">"+ Text1 +"</p>"+"<style=\"font-size:15px;\">"+"<p align=\"center\">"+ Text2 +"</p>"+"</body></html>";
wv.loadData(""+text, "text/html", "utf-8");
You cannot do that with only one TextView.
Basically one TextView keeps a text and some properties of that text. That properties are applied to the whole TextView, ie, to the whole text.
If you want to have different properties for Text1 and for Text2 you must have two TextViews.
At the end any UI element in android is given by an xml, in this case a TexView is something like:
<TextView android:text="#string/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffd300"
android:gravity="center_horizontal"
android:textSize="40sp"
android:id="#+id/textViewTitle"
android:layout_gravity="center" />
Here in property text you will set your Text1 or your Text2. In this case we are using #string/title which means that we are using an string called "title" from the string resources.
Reference:
http://developer.android.com/reference/android/widget/TextView.html
Combining two texts in a single TextView is not possible. It spoils the respect of the TextView itself.
You will need a Vertical LinearLayout with a background and border resembling to your TextView. Inside this layout, you will need to have two TextView's for text1 and text2 respectively. You can apply your desired properties to the individual TextView's
you can use html code in Textview
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:textSize="25sp" />
and in java file
TextView mTitle = (TextView) toolbar.findViewById(R.id.title);
mTitle.setText(Html.fromHtml("<b>S T A C K </b><font color='white'>O V E R F L O W</font>"));

android EditText not automatically wrapping text

I have looked all over the place and there are a ton of similar questions on this site, but I haven't found one that works and I'm wondering what I'm doing wrong. I have an EditText that I have to add programatically, it typically has only one line of text but I need it to wrap if the text is longer than the width of the view.
the only xml i am using is for the table layout the edit text is eventually added to
<TableLayout
android:id="#+id/LineItemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:stretchColumns="4"
>
</TableLayout>
The edit text is the one in the 4th column but even when I removed the stretchColumns attribute it still wasn't working. (i was thinking maybe the stretching column was messing up the width and it couldn't calculate when to wrap)
Here is the code that gets looped through for each row I add to the table, p is the EditText I cant get working properly. just a bit of explanation, the row contains an EditText for a number (this one is fine, i do not want it multiline), then two Buttons and a TextView, and then the last item is the EditText that stretches the rest of the width and should be wrapping text. i put them all here in case they are causing problems because at this point i have no idea what to try next.
final TableRow l = new TableRow(EnvelopeModify.this);
TextView t = new TextView(EnvelopeModify.this);
t.setText("$" + lineItems.get(x).getTotal() + ", " + lineItems.get(x).getQuantity() + " " + lineItems.get(x).getItemUnits() + "");
t.setPadding(5, 5, 30, 5);
EditText p = new EditText(EnvelopeModify.this);
p.setText(lineItems.get(x).getProposalDesc());
p.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
p.setBackgroundResource(R.color.blueEditText);
p.setMinLines(1);
p.setHorizontallyScrolling(false);
p.setImeOptions(EditorInfo.IME_ACTION_DONE);
//TableLayout.LayoutParams tempParams = new TableLayout.LayoutParams(0,LayoutParams.WRAP_CONTENT,1f);
//p.setLayoutParams(tempParams);
EditText o = new EditText(EnvelopeModify.this);
o.setText(lineItems.get(x)._order);
o.setWidth(50);
o.setInputType(InputType.TYPE_CLASS_NUMBER);
Button d = new Button(EnvelopeModify.this);
d.setText("Remove");
Button e = new Button(EnvelopeModify.this);
e.setText("Edit");
l.addView(o);
l.addView(d);
l.addView(e);
l.addView(t);
l.addView(p);
lineItemLayouts.add(l); // this is the tableLayout in the xml above
supposedly setHorizontallyScrolling(false) will allow wrapping but adding that just made things worse. Not only does the text not wrap, but the text that should be wrapping is not visible because the edittext is only showing the first part of the text (not scrolling). I can still hit enter and a new line will be created as expected but I cannot get any text to wrap.
The problem is with the TableLayout that the EditText is in. The EditText is in the stretchable column, setting the column to also be shrinkable fixed the issue. Here is the change I made to the xml layout.
<TableLayout
android:id="#+id/LineItemLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:stretchColumns="4"
android:shrinkColumns="4"
>
And here is what I have in the coding side that ended up working for me:
EditText p = new EditText(EnvelopeModify.this);
p.setText(lineItems.get(x).getProposalDesc());
p.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
p.setBackgroundResource(R.color.usedOnProposal);
p.setMinLines(1);
You're disabling horizontal scrolling in this line.-
p.setHorizontallyScrolling(false);
As for the multiline issue, you could try.-
p.setSingleLine(false);
p.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);

Align the start of the text in EditText and TextVIew

I have a simple layout in a table that consists of 3 rows. The second column may have either an EditText or a TextView like in the picture below:
Now, my question is how can I align the start of the text in EditText and TextView so that they are visually under each other (the texts, not the controls)? In the image above, the text in the second line starts too much to the left. I don't want to hardcode the padding as it may not work the same way on different devices.
try setting a padding for the second textview.
android:padding="5dp"
Instead of making the TextView match the EditText, make the EditText match the TextView by making its background null:
<EditText ... android:background="#null" ... />
The underline in the EditText then goes away and everything lines up perfectly.

How to put text over an imagebutton not via xml

I'm currently developing an android app with eclipse. Basicaly, I need to put the text over an imagebutton. There are some conditional statements in order to put text over that imagebutton, that's why I can't set the text in xml
This is how I set the imagebutton in xml:
<ImageButton
android:id="#+id/question1"
android:layout_width="150dip"
android:layout_height="50dip"
android:layout_gravity="center_vertical|center"/>
This is how I set the image source and text of the button:
buttonQuestion1 = (ImageButton) findViewById(R.id.question1);
TextView listContent = (TextView)findViewById(R.id.question1);
String question1 = getQuestion.get(0);
buttonQuestion1.setImageResource(R.drawable.button);
listContent.setText(question1);
The code to set the image source works fine. I use TextView and setText in order to put text over the imagebutton but when I try to run it, the app crashes. Anyone know how to set text over an imagebutton in a right way?
This line
TextView listContent = (TextView)findViewById(R.id.question1);
gives error because R.id.question1 is not a TextView, it's an ImageButton.
You can use normal <Button/> instead for layout
<Button
android:id="#+id/question1"
android:layout_width="150dip"
android:layout_height="50dip"
android:layout_gravity="center_vertical|center"/>
When you want to set the text and image resource, you can call
buttonQuestion1.setBackgroundResource(R.drawable.button);
buttonQuestion1.setText(question1);

Android - Add Lines to Textview

Ive got a TextView and some String Arrays. I want to add a Line (trough Java) everytime the next Item is displayed(Ive got a for loop, so it loops until every Item is displayed in the TextView). Does anybody know how to do that?
Thank you
EDIT:
I want It to look like a ListView. But I dont want to use a listView insted because I want to set the Line just for certain events.
for(int i=0;i<lenght;i++)
{
//textview is tv
tv.setText(listOfString[i]);
//view is viewWithHorizontalLine with hight = dp and width = fill_parent and background = #000000
viewWithHorizontalLine = view;
//LinearLayout is ll
ll.addView(tv);
ll.addView(viewWithHorizontalLine);
}
XML
<LinearLayout>
<ScrollView>
<LinearLayout orientation=vertical/>
</ScrollView>
</LinearLayout>
What kind of line? If you just want a vertical line in your text, add "|" between each word. If you want a new line, use "\n".
Have you tried adding a newline (\n) character each time you add one of your strings?

Categories

Resources