I want to draw a black stroke on my text in Android.
I have seen this example:
How do you draw text with a border on a MapView in Android?
Where the solution overrides onDraw() to create the stroke.
The problem is, I'm still relatively starting out in Android, and I have no idea how to transition to using that solution.
In my onCreate I set the text typeface (it's custom):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeatures();
// Set content view and component listeners
setContentView(R.layout.meme_maker);
setListeners();
context = this;
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Impact.ttf");
TextView mmt = (TextView) findViewById(R.id.meme_maker_title);
TextView ttc = (TextView) findViewById(R.id.top_text_canvas);
TextView tbc = (TextView) findViewById(R.id.bottom_text_canvas);
ttc.setTypeface(tf);
tbc.setTypeface(tf);
mmt.setTypeface(tf);
}
And I have an onClickListener where I change the text content of the TextView, based on the user writing the text he/she wants in a TextEntry and clicking a button afterwards.
ImageView ii = (ImageView) findViewById(R.id.insert_image);
ii.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText tt = (EditText) findViewById(R.id.top_text_text);
EditText bt = (EditText) findViewById(R.id.bottom_text_text);
TextView ttc = (TextView) findViewById(R.id.top_text_canvas);
TextView btc = (TextView) findViewById(R.id.bottom_text_canvas);
ttc.setText(tt.getText().toString().toUpperCase());
btc.setText(bt.getText().toString().toUpperCase());
}
});
It's pretty straightforward so far. My question is: how to insert the stroke of the text? Where? Do I need to create a Canvas and Paint objects?
The simplest way to get a shadow for text rendered in a TextView is to set up a style as described in this answer. That requires very little work and sounds like it will work fine in your situation.
Using the technique you link to involves extending an existing View class, overriding onDraw(), and using Canvas.drawText() on the canvas passed to onDraw() to render the text yourself. That can be exactly what you need in some situations, but sounds like overkill for your current situation. If you want to look into it further, the Android dev guide on the subject is a good read.
Related
I'm new to Android Studio.
I made a TextView and a Button in Android Studio.
when I click button it supposed to trigger this method:
public void click (View view)
{
TextView tex = (TextView) findViewById(R.id.text_view);
tex.setText("Hello");
}
The code works in this way .
but when I made the method like this:
public void click (View view)
{
TextView tex = new TextView(this);
findViewById(R.id.text_view);
tex.setText("Hello");
}
the code doesn't do what it supposed to do. I mean nothing happen to the TextView text.
Can anyone explain to me what's the difference? and why this happens or what's wrong about the second case?
TextView tex = new TextView(this);
That code creates new textview instance but this textview has no connection with your view.
But TextView tex = (TextView) findViewById(R.id.text_view); finds the first descendant view with the given ID and assigns it to your local variable so you have connection.
More info https://developer.android.com/reference/android/view/View#findViewById(int)
public void click (View view)
{
TextView tex = new TextView(this);
findViewById(R.id.text_view);
tex.setText("Hello");
}
In this section, tex is a textView object but it does not have any field on the view.
TextView tex= findViewById(R.id.textView)
this line assigns a view to tex object.
The reason is quite obvious.
it seems to me that you are fairly new to programming and doesn't understand java very well, and doesn't have any programming experience of some sort. You see, Semi colons are used to terminate a line of code. So, thats one of the things you missed. Second, this:
TextView tex = (TextView) findViewById(R.id.text_view);
tex.setText("Hello");
gets a reference from a view on the first line and sets the text on the second line. While this:
TextView tex = new TextView(this);
findViewById(R.id.text_view);
tex.setText("Hello");
Creates a new view on the first line. Gets a reference of the view on the second whilst not using its reference. And sets the text on the third line to the new view that was created on the first line.
Suggestion
OOP is not a really hard subject, but, if you really want to code, better know a little more about simple programming before diving in to OOP.
https://developer.android.com/reference/android/view/View.html#findViewById(int)
findviewbyid returns a View.
TextView tex = new TextView(this);
findViewById(R.id.text_view);
when you do this, you created a new TextView but you never actualy did anything with the View returned by findViewById since you ignored it.
There are two good ways to this as listed below
setOnClickListner
Using android view binding library (Maybe in combination with ViewModel and difficult, research nice and slow ! :p)
using android:onClick="methodName()" attribute, (I personally don't prefer this, AFAIK doesn't work in fragment)
Here is the Easiest one!
Put this code into onCreate after setContentView is called
Button btn = findViewById(R.id.your_btn_id);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//handle triggner here i.e set hello to your textView
}
});
Happy coding!
You need add tex into your layout to see it:
((ViewGroup) findViewById(R.id.some_container_id)).addView(tex)
When you call
findViewById(R.id.text_view);
and ignore returning value, this function have no effect
I am working on Android and have being working on OpenGL ES. I have a xml layout as follows (I have took some things out to show only therelative content:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/myText"/>
<MyGLSurfaceView
android:id="#+id/mySurface"/>
Then in my GlSurfaceView I am trying to access the button that is in the same layout. However this is were I am having a problem.
I have tried the following:
View v = (View) getParent();
myTextViewString = (TextView) v.findViewById(R.id.myText);
this
myTextViewString = (TextView) findViewById(R.id.myText);
and this
myTextViewString = (TextView) ((RelativeLayout) getParent()).findViewById(R.id.myText);
I cannot seem to figure out how to access this button outside of the GLSurfaceView but is in the same activity inside my GLSurfaceView.java.
I do know that it has something to do with not being able to get the parent( I am assuming because it does not extend Activity). I have looked around and I cannot find a way to achieve this.
One clean and straightforward way to do this is to pass the button view to the GLSurfaceView. Aside from avoiding navigation through the view hierarchy, this also makes your view code more generic, since it will not have to know the id of a specific button.
In the onCreate() method of the activity, after calling setContentView(), you could have this:
MyGLSurfaceView glView = (MyGLSurfaceView) findViewById(R.id.mySurface);
TextView textView = (TextView) findViewById(R.id.myText);
glView.setTextView(textView);
In MyGLSurfaceView:
private TextView mTextView;
void setTextView(TextView textView) {
mTextView = textView;
}
Then, in other methods of MyGLSurfaceView, you can use mTextView anytime you need access to the button.
I want to implement a feature that allows user to change the textSize of a textView in another view inside the app,
So I have a button with its "onClick" property set to:
Class mainActivity
public void increaseFont(View view)
{
MainViewPager.changeTextViewTextSize(mTextSize);
}
Class MainViewPager
static public void changeTextViewTextSize(int aTextSize)
{
View detailView = (View) LayoutInflater.from(mContext).inflate(R.layout.details, null);
TextView description = (TextView) detailView.findViewById(R.id.story_description);
description.setTextSize(aTextSize);
}
QUESTIONS is the textSize can't be changed when clicking the button. So how to?
The text size can changed at run time of course. You issue is related to the method changeTextViewTextSize. Using the inflater you are creating a new instance of R.layout.details, and through it, you are looking for the TextView you want to change the text size. But that layout is not at screen. It is not what you are seeing.
I am usin the CardsUI library taken from here: http://www.androidviews.net/2012/12/cardsui/
and trying to change the textview size for all the cards by pressing a button.
At the moment i haven't used any buttons, just wanted to check if my approach is correct by setting a fixed textsize.
I have created:
MainActivity.java
MyCard androidViewsCard = new MyCard("Title","Description");
androidViewsCard.setSize(20.0f);
MyCard.java
public void setSize(float f) {
View view = LayoutInflater.from(MainActivity.getAppContext()).inflate(R.layout.card_layout, null);
TextView tv = (TextView) view.findViewById(R.id.description);
tv.setTextSize(f);
}
The text does not change size though. Am i missing something?
Just add a unit with it.
You need to use the function setTextSize(unit, size) with unit SP like this,
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, f);//tv is textView
I am trying to make an android app for my local fence painting business and I simply want it to calculate quotes and payments. My problem is figuring out how to get a simple input box for the square feet of the fence (edittext) with the help of a button to display a that input to a textview. Will you help me?
Also if there is a different way to do this I would love to hear it.
Thank you!
My problem is figuring out how to get a simple input box for the
square feet of the fence (edittext) with the help of a button to
display a that input to a textview.
Pretty simple. Create a TextView, a Button and an EditText object:
TextView txtView;
Button btn;
EditText edtText;
Initialize them:
txtView = (TextView) findViewById(R.id...);
btn = (Button) findViewById(R.id...);
edtText = (EditText) findViewById(R.id...);
This is how you get the input of the EditText by clicking the Button and set the input to the TextView:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String inputText = edtText.getText().toString(); // Get the input
txtView.setText(inputText); // Set the text to the TextView
}
});