how to set textview border dynamically in android - android

I m newbie to android and i am working on an app in which i want to set the border of textview dynamically, i.e by code not by xml. i searched a lot on google but everywhere i find solution by xml. I tried a lot, but didn't find a correct way to implement this.
Like using this url, but i cant get to the result
How to draw borders for TextView created using Code not by XML in Android
please suggest me ideas,how i can achieve this..

The reason why you wont find much documentation about non-XML layouts is because most problems can be solved with the layouts. I would suggest at least defining the textview in the XML, then setting the border later in code. For example:
In the layout.xml file:
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
Then in your code:
TextView text = ((TextView)this.findViewById(R.id.text)); //use id to find view
ViewGroup.LayoutParams textLayout = text.getLayoutParams();
textLayout.topMargin = 10;
textLayout.bottomMargin = 10;
text.setLayoutParams(textLayout);
You can modify any attribute of the object this way. (use ViewGroup and View as a resource)
Note the above examples are just examples (I don't have a compiler in front of me to check syntax and effectiveness)

Related

Can you set graphical layout preview-only text on a TextView?

I have a styled TextView whose real text is populated dynamically at runtime. The Graphical Layout view is very useful for getting a feel on how this component works with others in terms of look and feel, etc. There is no sensible default to this text field and I wish it to be blank before being populated. If I don't specify any text in the TextView declaration then the TextView is blank. I can set the text manually using:
<TextView
...
android:text="Preview text"/>
and then switch to the Graphical Layout. However, I must remember to remove this or risk it being shipped in my production version.
Is there a way to specify text which is only seen in the Graphical Layout preview but not applicable at runtime?
EDIT: I'm using Eclipse ADT.
Yes you can with the design tools extension attributes in Android Studio.
See this page https://developer.android.com/studio/write/tool-attributes.html
Basically you define the tools namespace
xmlns:tools="http://schemas.android.com/tools"
Then use it to set your placeholder text.
<EditText
tools:text="John Doe"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This actually works with most (if not all xml attributes).
e.g
tools:visibility="gone"
would set the preview visibility to "gone" but the runtime visibility would be unchanged.
I don't believe there is, the only possible way is when you declare your TextView, you say after, tv.setText(""); this way you will always find it blank at runtime

What is the difference between this XML layout and corresponding Java layout?

This following XML produces the large bold text that I'm looking for.
<TextView
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#000000"
android:gravity="center"
android:text="Review"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textStyle="bold" />
I would think this Java would do the same, but instead I get non-bold, default sized text. What gives?
LinearLayout container = new LinearLayout(this);
container.setOrientation(LinearLayout.VERTICAL);
container.setBackgroundColor(Style.backgroundColor);
TextView header = new TextView(this);
header.setText("Review");
header.setBackgroundColor(Color.BLACK);
header.setTextColor(Color.WHITE);
header.setGravity(Gravity.CENTER);
header.setTypeface(Typeface.DEFAULT_BOLD);
header.setTextAppearance(this, android.R.attr.textAppearanceLarge);
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 44, getResources().getDisplayMetrics());
container.addView(header, LayoutParams.MATCH_PARENT, (int)px);
(And before you answer "Just use XML", I can't. This needs to be nicely packaged in a jar.)
Try using an alternative constructor:
TextView header = new TextView(this, null, android.R.attr.textAppearanceLarge);
And don't call setTextAppearance later. My guess is that textAppearanceLarge overrides the bold setting that you set right beforehand.
well, using your custom implementation of the view is "almost" the same as using the XML code, however Android has some "bugs" when you try to inflate your custom view by doing it all in Java; I know this for sure since I've tried it before and using my layout in XML works fine and using my custom java layout just didnt load well...
So in other words, there is pretty much no difference but you will have to write down a bit more of code and your app is susceptible to little bugs like this.
here is the link of the thread where I asked this time ago:
Android Developers Google Group
Note: you can just pack your proyect to a jar file but Android apps use APKs to be installed. I'm not sure if you can install jar app files, but I don't think its possible.

Android with eclipse

I've started learning coding for the android, I know the basics of programming in general and thought that android would be fun, Which it has so far.
Now in my exercises in the book I have, It says to add more text to the application. The application is nothing at the moment but 1 string, And I have to add another string.
Now when I have added the string to the strings.xml file and then on the main.xml I type:
android:text="#string/AppName" />
AppName is the new string I made which in the strings.xml it looks like this:
This App is called Droid1
The weird thing is when I type in the main xml to referr to the string, It doesnt even get colour coded when I type the android:text part. The whole line stays as the black text colour. Im sure im not missing anything as the string is all colour coded and so is the last string that I referred to while following the examples in the book which is:
android:text="#string/hello" />
And this is what is confusing me. So please point out the obvious or not so obvious thing that I have done wrong. Any help at all will be appreciated
Android uses the XML format to define interfaces and such. The line
android:text="#string/AppName" />
is not valid XML, and the black syntax is Eclipse's way of showing this. This is probably because you have forgotten to put some lines above it. What you want to have is something like this:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/AppName" />
This tells Android that you would like to put a TextView (or, a text label or whatever) with your string in it. Also note that the android:layout_width and android:layout_height attributes here are always required: without them, you will get an error and you can't build your application.
If you are not already familiar XML, I would highly recommend taking a look at an XML tutorial (for instance at Tizag or W3Schools) and learn the basics of XML, since understanding the XML language simplifies Android programming a lot.
If you want your text to have color, you can set android:textColor="######" in your xml either textview or whatever you used to display the string. ###### is your color code.

Setting background colour of Android layout element

I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.
I have created the layout as shown in the image, and the header is a TextView in a RelativeLayout. Now I wish to change the background colour of the RelativeLayout, however I cannot seem to figure out how.
I know I can set the android:background property in the RelativeLayout tag in the XML file, but what do I set it to? I want to define a new colour that I can use in multiple places. Is it a drawable or a string?
Additionally I would expect there to be a very simple way to this from within the Eclipse Android UI designer that I must be missing?
I am a bit frustrated currently, as this should be an activity that is performed with a few clicks at maximum. So any help is very appreciated. :)
You can use simple color resources, specified usually inside res/values/colors.xml.
<color name="red">#ffff0000</color>
and use this via android:background="#color/red". This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via getResources().getColor(R.color.red).
You can also use any drawable resource as a background, use android:background="#drawable/mydrawable" for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).
The above answers are nice.You can also go like this programmatically if you want
First, your layout should have an ID. Add it by writing following +id line in res/layout/*.xml
<RelativeLayout ...
...
android:id="#+id/your_layout_id"
...
</RelativeLayout>
Then, in your Java code, make following changes.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.your_layout_id);
rl.setBackgroundColor(Color.RED);
apart from this, if you have the color defined in colors.xml, then also you can do programmatically :
rl.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
You can use android:background="#DC143C", or any other RGB values for your color. I have no problem using it this way, as stated here
The
res/values/colors.xml.
<color name="red">#ffff0000</color>
android:background="#color/red"
example didn't work for me, but the
android:background="#(hexidecimal here without these parenthesis)"
worked for me in the relative layout element as an attribute.
If you want to change a color quickly (and you don't have Hex numbers memorized) android has a few preset colors you can access like this:
android:background="#android:color/black"
There are 15 colors you can choose from which is nice for testing things out quickly, and you don't need to set up additional files.
Setting up a values/colors.xml file and using straight Hex like explained above will still work.
4 possible ways, use one you need.
1. Kotlin
val ll = findViewById<LinearLayout>(R.id.your_layout_id)
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
2. Data Binding
<LinearLayout
android:background="#{#color/white}"
OR more useful statement-
<LinearLayout
android:background="#{model.colorResId}"
3. XML
<LinearLayout
android:background="#FFFFFF"
<LinearLayout
android:background="#color/white"
4. Java
LinearLayout ll = (LinearLayout) findViewById(R.id.your_layout_id);
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
Android studio 2.1.2 (or possibly earlier) will let you pick from a color wheel:
I got this by adding the following to my layout:
android:background="#FFFFFF"
Then I clicked on the FFFFFF color and clicked on the lightbulb that appeared.
Kotlin
linearLayout.setBackgroundColor(Color.rgb(0xf4,0x43,0x36))
or
<color name="newColor">#f44336</color>
-
linearLayout.setBackgroundColor(ContextCompat.getColor(vista.context, R.color.newColor))
The answers above all are static. I thought I would provide a dynamic answer. The two files that will need to be in sync are the relative foo.xml with the layout and activity_bar.java which corresponds to the Java class corresponding to this R.layout.foo.
In foo.xml set an id for the entire layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/foo" .../>
And in activity_bar.java set the color in the onCreate():
public class activity_bar extends AppCompatActivty {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foo);
//Set an id to the layout
RelativeLayout currentLayout =
(RelativeLayout) findViewById(R.id.foo);
currentLayout.setBackgroundColor(Color.RED);
...
}
...
}
I hope this helps.

how to find in d.android.com?

Im new to this so much is a bit confusing now. But I see that d.android.com is a goldmine if you know how to use it and find the stuff.
How do I use this resource to find what Im searching for? To explain a bit how I mean. I have read a book with this code:
<LinearLayout...
...
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/on" />
I wanted to see what variables (right name?, like layout_gravity) ImageView could have and its attributes (?, like center_horizontal) so I checked out:
http://d.android.com/reference/android/widget/ImageView.html
but nowhere I could find any of the above variables. So instead I tested to check its parent LinearLayout:
http://d.android.com/reference/android/widget/LinearLayout.html
But there were nothing either of above variables. There was android:gravity thats looks alike tho.
So how should I do to find which variables and its attributes a class (?, like imageview)
can have?? Where/how do I find information like this??
First, everything you can set through XML can be set through code too, so this correspondence can help you.
Second, in the references the attributes (it's the name for XML "variables") are not always shown: only the ones that are particular of that class are, the others are inside an inherited XML attributes expandable section.
As an example, android:id is an attribute in common with every class inheriting from View.
Third, LayoutParams are a kind of their own: programmatically, you set a view's layout params with View.setLayoutParams(LayoutParams), and it's LayoutParams that cointains those members/attributes. In XML this is represented by prepending layout_, but it's only a convention.
The base class for LayoutParams is ViewGroup.LayoutParams. Every layout class adds something by extending it (for example, android:layout_gravity is an attribute added by most of the layouts).

Categories

Resources