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
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 want to get the subtitle´s textview in an android's toolbar to change it's font. Actually i'm doing it with the title, getting it on this way:
Field f = toolbar.getClass().getDeclaredField("mTitleTextView");
f.setAccessible(true);
titleTextView = (TextView) f.get(toolbar);
I've tried with the same code but trying to get "mSubtitleTextView" but that's not the solution.
Thanks!!
You can get the subtitle TextView like this:
View subTitleView = toolbar.getChildAt(1);
If you don't add any views to the toolbar, his default structure is:
[0] - (TextView) title
[1] - (TextView) subtitle
[2] - (ActionMenuView) menu
Hope it helps!
Definitely not the best way, but in a pinch this will work. You will have to figure out a way to implement myTypefaceSpan though; I'm using Calligraphy so it ties together decently for me.
CalligraphyTypefaceSpan myTypefaceSpan = new CalligraphyTypefaceSpan(
TypefaceUtils.load(this.getAssets(), "fonts/custom_font.ttf"));
public static void setToolbarSubtitle(String subtitle, Context context) {
SpannableStringBuilder sBuilder = new SpannableStringBuilder();
sBuilder.append(subtitle);
sBuilder.setSpan(MainActivity.myTypefaceSpan, 0, sBuilder.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
((MainActivity)context).getSupportActionBar().setSubtitle(sBuilder);
}
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 have a view in android that shows a number of check boxes. They are all added dynamically and I set a text for each one in part.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
LayoutInflater inflater = getLayoutInflater();
for (String string : getResources().getStringArray(R.array.string_array)) {
LinearLayout searchField = (LinearLayout) inflater.inflate(R.layout.cb, null);
CheckBox checkBox = (CheckBox) searchField.findViewById(R.id.checkBox1);
checkBox.setText(string);
layout.addView(searchField, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
}
AS you can see from the code, I have an array of strings and for each of the strings in the array I add an check box. When the view is first shown, all the check boxes have the correct text, but after I rotate the device to landscape or portrait mode, all the check boxes have the same text (from the last check box). Any rotations (to redraw the screen) do not affect the text anymore. All of them remain with the text of the last check box.
I have looked in the debugger, the check box object is a new one for each string, so I am not working with the same instance of an object. I am currently out of ideas.
Do you have any idea why this is happening?
When you rotate the screen, runtime Resource is changed, and Activity is relaunched. Two ways to fix your problem:
Hold the value of all your checkbox in OnCheckedChangeListener and reset them back in onResume.
Handle onConfigurationChanged by yourself, according to https://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
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.