Android custom text style - android

If I want display TextView as below :
-------------display-------------
The "display" field is a dynamic data, and the View is TextView, include text and lines on both sides, is any suggestion for the xml build? Or maybe can do it programmatically?

You can use string.xml to store the dash line as text and use String.format method to put dynamic text in your textview
Put the below line in string.xml:
<string name="dash_line">-----%1$s-----</string>
Now you can use the above string to set dashed line with dynamic data in your `textview as follows:
String formattedstring = String.format(getResources().getString(R.string.dash_line), yourStringToShow);
yourTextView.setText(formattedstring );

Ya you can do like below code.
In this code, both side line will adjust according to width,
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:orientation="horizontal" >
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"
android:background="#D0D0D0" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:singleLine="true"
android:text="Your text goes here" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_weight="1"
android:background="#D0D0D0" />
</LinearLayout>

If you want to show some drawables, Create a Shape drawable as line. i.e. Name below file as dashed_line.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:dashWidth="3dp" android:dashGap="1dp" android:color="#color/black" android:width="2dp"/>
</shape>
and use it as below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#drawable/dashed_line">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/white"
android:text="#string/hello_blank_fragment"/>
</LinearLayout>
Or If you simply want to put as a string, '------' on both side then just add it dynamically while setting text i.e.
String display="display";
textview.setText("---------"+display+"--------");

Related

Android change text color of specific strings in arraylist

I have an arraylist named incorrectAnswers, I want to change the text color of those strings inside that arraylist.
Here's a snippet of my code:
ArrayList<String> incorrectAnswers = new ArrayList<>();
if(mItemArray.get(0).second.startsWith("In")){
numberOfCorrect++;
numberOfTries++;
} else{
numberOfErrors++;
incorrectAnswers.add(mItemArray.get(0).second);
}
if(mItemArray.get(1).second.startsWith("If")){
numberOfCorrect++;
numberOfTries++;
}
else{
numberOfErrors++;
incorrectAnswers.add(mItemArray.get(1).second);
}
This is the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_layout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="10dp"
android:backgroundTint="#android:color/holo_green_light"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/border_spinner">
<ImageView
android:id="#+id/image"
android:layout_width="1dp"
android:layout_height="1dp"
android:src="#drawable/ic_abrasion"
android:visibility="invisible"/>
<TextView
android:id="#+id/list_text"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="#color/textColor"
android:layout_height="wrap_content"/>
I want to change programmatically the text color of those items I've added inside incorrectAnswers. How can I achieve that?
First of all create a xml in drawables folder like this to setup the color states:
Lets call it text_color.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/holo_red_dark" android:state_selected="true" />
<item android:color="#android:color/holo_green_light" />
</selector>
Then, use this xml as your text color in your layout:
<TextView
android:id="#+id/list_text"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="#drawable/text_color"
android:layout_height="wrap_content"/>
Finally, you have to set only the textviews that will have the incorrect answers as checked like this:
TextView textList = (TextView) findViewById(R.id.list_text);
textList.setSelected(true);

Background drawable doesn't match its size with its parent layout

I am having troubles applying background color onto a list item layout. I am doing a sample app to learn android. My problem is that it shows the colored background, but it is not the size of the list item layout. Its a bit small. Its not working for some reasons, every one here suggested to set a background drawable, set it like this:
view.setBackgroundResource(R.drawable.ic_green);
I am doing this inside CursorAdapter.bindView(View view, Context context, Cursor cursor). Because of course I am dealing with list. Each item has a corresponding color data that I need to reflect on each list item. I need to do this programmatically. Since I must allow my self to change the background color as I please.
It looks like this:
My drawable resources like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFFCC" />
</shape>
And this is how my list item layout looks like:
<?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="wrap_content"
android:layout_gravity="end"
android:layout_margin="5sp"
android:background="#drawable/note_style"
android:padding="5sp"
android:paddingLeft="10sp"
android:paddingRight="10sp" >
<TextView
android:id="#+id/textview_note_short"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:gravity="start"
android:maxLength="18"
android:maxLines="1"
android:text="#string/text_sample_short_note"
android:textSize="18sp" />
<TextView
android:id="#+id/textview_note_remind_every"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/textview_note_short"
android:layout_alignStart="#id/textview_note_short"
android:layout_below="#id/textview_note_short"
android:text="#string/text_sample_remind_every"
android:textSize="12sp" />
<TextView
android:id="#+id/textview_note_date_created"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignTop="#id/textview_note_short"
android:text="#string/text_sample_date"
android:textSize="12sp" />
<ImageView
android:id="#+id/imageview_note_notification_alarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/textview_note_remind_every"
android:layout_alignRight="#id/textview_note_date_created"
android:layout_alignEnd="#id/textview_note_date_created"
android:adjustViewBounds="true"
android:contentDescription="#string/content_description_past_due"
android:maxHeight="20dp"
android:maxWidth="20dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_action_alarm" />
<ImageView
android:id="#+id/imageview_note_notification_due"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/imageview_note_notification_alarm"
android:layout_toLeftOf="#id/imageview_note_notification_alarm"
android:layout_toStartOf="#id/imageview_note_notification_alarm"
android:adjustViewBounds="true"
android:contentDescription="#string/content_description_past_due"
android:maxHeight="20dp"
android:maxWidth="20dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_action_due" />
</RelativeLayout>
It does change the background color at whim but, as you can see on the picture, the green background doesn't hit the edges of layout which should match the width of the screen and height-wrap its content.
Any ideas? Thank you!

AutoCompleteTextView creates a margin for dropdown list

Dropdown list comes with right and left margin even though I didn't set margin in my dropdown list item layout. I want it to match to the autocompletetextview's width. How can I achieve that?
Here is my AutoCompleteTextView xml code:
<LinearLayout
android:id="#+id/headerlogo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="9" >
<AutoCompleteTextView
android:id="#+id/autocomplete_name"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="7"
android:background="#drawable/edittextback"
android:ems="10"
android:textSize="15sp"
android:hint="#string/codehint"
android:textColorHint="#color/hintgrey"
android:paddingRight="30dp"
android:paddingLeft="10dp"
android:singleLine="true"
android:ellipsize="end"
android:completionThreshold="1">
</AutoCompleteTextView>
<Button
android:id="#+id/btnsearch"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#drawable/buttondarkblue"
android:text="X"
android:textColor="#color/white" />
</LinearLayout>
And item layout for dropdown list:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:id="#+id/route"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:singleLine="true"
android:ellipsize="end"
android:padding="5dp"
android:textColor="#color/loginblue"
android:text="ASD"></TextView>
</LinearLayout>
You can use 2 methods
setDropDownHorizontalOffset(int) and setDropDownWidth(int)
For example, you can do something like this to match to autocompletetextview:
input.setDropDownHorizontalOffset(0)
input.setDropDownWidth(input.getWidth())
FYI 1st method can use negative values. So you can set a dropdown layout width more the autocompletetextview width:
int offset = 64
input.setDropDownHorizontalOffset(-1 * offset)
input.setDropDownWidth(input.getWidth() + offset * 2)
I don't know, you've got your answer. But may helps.
If you assign a layout for dropdown list in the custom adapter and set margins for the TextView or paddings for the root view (for example, LinearLayout), it doesn't work. I did as follow:
I set background for the dropdown list in style.xml file:
<item name="android:popupBackground">#drawable/dropdown_list_bg</item>
And then in dropdown_list_bg.xml file i wrote these lines:
<item android:right="1dip" android:left="1dip">
<shape>
<solid android:color="#color/dropdownListBg"/>
<corners android:radius="#dimen/corner"/>
</shape>
</item>
As you can see i set "padding right" and "padding left" with "android:right" and "android:left" attributes in the drawable xml file.

android text not showing in button

This happens very frequently to me when starting to work on android. The text within the button element is not showing fully. What can be wrong? I tried text direction, alignment, gravity but nothing works.
The code behind it:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="#string/latestButtonString"
android:background="#drawable/roundbutton" />
Please help and much appreciated.
Update: This is the roundbutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<!-- you can use any color you want I used here gray color-->
<solid android:color="#ABABAB"/>
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
I had this problem, I solved it by chaning the xml for the button from using tools: to android: for the text attribute. Example below (last line is the line thats changed):
<Button
android:id="#+id/btn_settings"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#F6F6F6"
tools:text="App Settings" />
becomes...
<Button
android:id="#+id/btn_settings"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#F6F6F6"
android:text="App Settings" />
try to replace
android:text="#string/latestButtonString"
and put direct hardcode string here
as,
android:text="hello"
or
try to replace this line in roundbutton.xml:
<android:shape="rectangle" android:padding="10dp">
with
<android:shape="rectangle" android:padding="3dp">
Can you please check by removing android:background="#drawable/roundbutton" from your button attribute. If text shown fully, then you need to modify the #drawable/roundbutton" there may be you have set fixed width thus your text in button is not fully visible.
It seems that you have a paddingLeft on your Button. Can you check if this is present in your styles.xml?
If it is in your styles.xml, it is probably important for the theme of your app.
If you would just want to cancel it in that certain Button, just put
android:paddingLeft="0dp"
Faced a similar problem...following attribute made it work:
android:layout_weight="1"
please remove these line android:background="#drawable/roundbutton" but view not create like you if you want to create same view please try these code :
code
<LinearLayout 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"
android:gravity="center">
<LinearLayout
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="40dp"
android:padding="20dp"
android:background="#drawable/roundbutton">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="latestButtonString"
android:textColor="#000000"
android:textSize="18sp"/>
</LinearLayout>

using xml dotted line in android TextView

I have created an xml dotted line as explained in How do I make a dotted/dashed line in Android?. If I use it as the background of my TextView it shows up.
<TextView
android:id="#+id/segment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/dotted_lines"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />
But if I use it as an accompanying drawable, it does not show up.
<TextView
android:id="#+id/segment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableBottom="#drawable/dotted_lines"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />
Essentially, I couldn't care less either way, except: I need the dotted lines to appear below the text in the TextView. Please help.
Use following code to make textview with dotted line.
Create file in drawable folder named dotted.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:color="#F59C51"
android:width="2dp"
android:dashGap="1dp"
android:dashWidth="2dp"/>
</shape>
Then set in as background of textview like below.
<TextView
android:id="#+id/segment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/dotted"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />
Try this. i think textview height might cause problem for you.
<TextView
android:id="#+id/segment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:drawableBottom="#drawable/dotted_lines"
android:gravity="left"
android:text="First segment"
android:textSize="12sp" />

Categories

Resources