Writting text above textview not inside it in android - android

I have a textview, created inside a shape. I want to write text above this text view not inside it. !
I have added image for it. The outer rectangle displayed is a dialog box and inner rectangle is textview. : http://i.stack.imgur.com/tLUgM.png
My xml looks like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_gravity="top"
android:background="#drawable/mydialogbox"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
Now I want to write text above this inner rectangle and inside outer rectangle which is a dilog box.

You can't do that. You will have to add other TextView dynamically if you don't want to add it in the xml file.
And then set the text to newly added TextView. But the TextView must be there to hold text

use two textview one for shape and other one for text.

Related

Android dynamic views

I have a TextView that displays an error message beside 2 Buttons. They are currently inside a horizontal LinearLayout. The problem is if the TextView is too wide, the 2 Buttons will be pushed off the screen. Is it possible to push the elements downwards in those cases?
If the text is short there are no problems:
(Textview text) (Button1) (Button2)|(Edge of screen)
If the textview is long, I want to push the 2 buttons down a "row"
(Realllllllllllly long text that may|(Edge of screen)
span 2 lines)
(Button1) (Button2)|(Edge of screen)
I think you need to keep one more Linear layout below to your horizontal linear layout and need to check text size runtime if it's width is greater than required two button space then need to hide horizontal linear layout buttons and need to show below layout buttons
to refer how to check text size runtime refer below link :
Refer this link
Try this way: Use FlowLayout
<org.apmem.tools.layouts.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</org.apmem.tools.layouts.FlowLayout>
Inside FlowLayout you can put your view's and it will auto move to next line if not fit.
Yes you can do that, flexbox-layout is the solution.
How to use
Gradle dependency
dependencies {
implementation 'com.google.android:flexbox:0.3.2'
}
And xml code
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch" >
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_alignSelf="flex_end"
/>
</com.google.android.flexbox.FlexboxLayout>
There are few other attributes also [read documentation], which you can try and find what works more suitable in you case.
you can use the TextView predefined method, to gave validation to end user like this
TextView textView=(TextView)findViewById(R.id.text_view);
textView.setError("Your Text is very wide please provide short text");
setError put red mark on textview view, with that we can tell the end user. provided text is wide

Android, Unexpected button left and right paddings

I want to make a button, where left right top and bottom paddings will be same.
But I get something like this:
Here is a layout:
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/people_number_label"
android:text="#string/number_of_people"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/people_number"
android:layout_toRightOf="#+id/people_number_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
So the way a Button works is its a text view with a background drawable. That drawable is the one you're seeing. The text will be centered within that drawable, as you see here. If you want the text to appear in line with the prompt, a Button isn't going to work for you. You're better off just making it a TextView and setting an xml background drawable with a rounded rect so it looks like a button, but it actually isn't.
Another thing you can try to do it align_baseline the two view. That should align the text, but may push the prompt down the screen

changing part of text size with java for android

As I add text to my text view with java, I want some text to be smaller and some to be bigger. Is there a way I can change the text size as I add it with java?
You can Use Spannable String for this purpose and for Tutorial you can see it here
Each text view has a value for the size of the text contained in that view.
Depending on what you are trying to do, you could add several text views in a row with different text sizes to simulate what it sounds like you want.
Use a relative view, add a text view to it and align it however you want with wrap_content for width and then just add the other text views using an alignment toRightOf previous textview.
Use webview and html to make a richtextbox like view. Text widget don't have this feature. Maybe there would be 3rd party widget for this also.
Create 2 separate TextView's, place them next to each other and set different text size on each:
TextView myTextView1 = (TextView) findViewById(R.id.my_text_view_1);
TextView myTextView1 = (TextView) findViewById(R.id.my_text_view_2);
float textSize1 = 20.0;
myTextView1.setTextSize(textSize1);
float textSize2 = 30.0
myTextView2.setTextSize(textSize2);
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/my_text_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test 1"/>
<TextView
android:id="#+id/my_text_view_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test 2"/>
</LinearLayout>

Two TextViews shows exclusively on a line using XML

My ListView item has two TextViews. I want to show one of them at a time using XML. I don't want to do this programmatically. How to do it?
For example:
When TextView A has text, TextView B disappears. When TextView A is empty, TextView B appears.
My code
<?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="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/groupNameTextView"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="TextView"
android:textSize="20dip"
android:textColor="#color/ForestGreen"
/>
<TextView
android:id="#+id/topRatedPlaceNameTextView"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="TextView"
android:textSize="20dip"
android:textColor="#color/Azure"
/>
</LinearLayout>
Sorry, you cant do that with xml, by Programmatically in your getView() of List's Adapter check like
if(textview1.getText().toString.length()>0)
textview2.setVisibilty(View.GONE);
else
textview1.setVisibilty(View.GONE);
TextView A has a text then in TextView B set setVisibility(View.VISIBLE) or setVisibility(View.VGONE) and same for TextView B. you do this in your CustomAdapter Class getView Method.
Thanks
Will you ever have both of them shown with text at the same time ? If not, and if you don't have style difference (text size/color), you could just use a single text view ?
Else, if A and B have different size / color / font / whatever, and assuming they will never be having text at the same time, using wrap_content as you do should ensure that A gets a width of 0 when it has no text, and B will take all the space. Else, if B has no text, A will take as much space as needed by its content.

Overlap views in android?

I have a linearlayout which have a textbox(multiline, nearly 5 lines) and image view. Is it possible to draw a image on textview(overlapping)?
Note: I have to specify the coordinates of the image, which are not static, and may be anywhere above text.
Something like this mockup:
I think it can be achieved using RelativeLayout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/Textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/Text2display"
android:textColor="#EEDCAA" />
<ImageView
android:id="#+id/choose_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="-46dp"
android:adjustViewBounds="true"
android:contentDescription="#string/description_logo"
android:src="#drawable/user2" />
</RelativeLayout>
By placing the TextView block above the ImageView, it ensures that the image view overlaps the TextView. Now, based on your requirements and position, use the following commands from the link :-
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
You can align left right, top and bottom. Use negative values to navigate the ImageView, if ur using align bottom and stuff.. This will make it to overlap. Please let me know if this was helpful
Is there any specific reason for Linear Layout?
You can do this easily using RelativeLayout . You can have an ImageView overlapping TextView Unless there is a specific reason for using LinearLayout .
If you really (really) need to use LinearLayout, you can subclass TextView and override onDraw to draw your image.
In all your xml files, should define the background color for it, it will solve the problem :
Add this android:background="#android:color/black" in to the View tag you defined.

Categories

Resources