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.
Related
I have a custom android widget that abstracts a forum field, RecordDataField, that is based on a RelativeLayout (basically groups a field and it's label together, and takes custom attributes to determine where the label is relative to the field, to show a drop down button for dropdown list's, ...)
public abstract class RecordDataField : RelativeLayout
{
protected TextView _tvLabel;
protected FieldLayout _rlInput;
//protected EditText _etInput;
protected FieldInput _etInput;
protected Button _btnDrop;
protected abstract FieldInput InstantiateInput();
}
FieldInput is a subclassed EditText, nothing really special.
Any child of RecordDataField implements InstantiateInput similar to as follows (_etInput ultimately ends up with the value returned by InstantiateInput)
protected override FieldInput InstantiateInput()
{
View v = _inflater.Inflate(Resource.Layout.RecordDataFields, null);
_fieldInput = ((ViewGroup)v).FindViewById<FieldInput> Resource.Id.RecordDataFieldInput);
return (FieldInput)_fieldInput;
}
This works great for all my RecordDataField variants, but they are all FieldInput (EditText) based. I now need to go even more general, and when I try changing the abstract method InstantiateInput's return type to View, I run into problems in the base class, b/c it set's gravity and other attributes on _etInput, and apparently View doesn't support gravity
public virtual void InitView()
{
_tvLabel = (TextView)_inflater.Inflate(Resource.Layout.RecordDataFieldLabel, this, false);
_tvLabel.Text = _label;
_tvLabel.Gravity = _labelGravity;
ViewGroup.LayoutParams tvLabelLayout = (ViewGroup.LayoutParams)_tvLabel.LayoutParameters;
tvLabelLayout.Width = _labelWidth;
RelativeLayout.LayoutParams lpLabel = new RelativeLayout.LayoutParams(tvLabelLayout);
lpLabel.AddRule(LayoutRules.AlignParentTop);
lpLabel.AddRule(LayoutRules.AlignParentLeft);
AddView(_tvLabel, lpLabel);
...
}
Any suggestions as to which view type I can return from InstantiateInput so this widget can support the greatest amount of view types (the motivation here is that I want to use a CheckBox in place of the EditText (FieldInput)?
Try TextView (android.widget.TextView) . Both Checkbox and EditText extends TextView which supports the gravity param to support aligning it's content
Im using a textSwither for hide/show a part of text like this way :
txtDescription.setInAnimation(in);
txtDescription.setOutAnimation(out);
txtDescription.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
TextView textView = new TextView(ShopContentDetailActivity.this);
textView.setTextColor(getResources().getColor(R.color.NightDark));
return textView;
}
});
And call this method for change text :
private void changeDescription() {
if (displayShort)
txtDescription.setText(Html.fromHtml(shortDescription));
else txtDescription.setText(fullDescription);
displayShort = !displayShort;
}
My problem is when user touch textSwitcher to display text and touch again to hide it ,the height of parent view (a linear layout ) of textSwitcher remain as when text is displayed and dont change its height.
How can i update the parent view when text change ? (its ok when touch for display full text)
I resolve my problem in this way : (I think TextSwutcher change visibility of TextViews to INVISIBLE not GONE then i use this trck)
private void changeDescription() {
if (displayShort) {
txtDescription.setCurrentText(null); // this line change invisible textView visible but without text and make it height 0 and then set the second textView text
txtDescription.setText(Html.fromHtml(shortDescription));
} else txtDescription.setText(shopContent.getDescription());
displayShort = !displayShort;
}
But there is a problem that is not so important for my case: when you hide a part of text its blink.
if i resolve it i will update my answer.
In my activity I have the following views
TextView player1;
TextView player2;
TextView player3;
TextView player4;
EditText player1name;
EditText player2name;
EditText player3name;
EditText player4name;
Each of the TextView's has the onclick listener applied to it. and so fires the OnClick function.
When we get to the onClick this is what i am currently doing:
#Override
public void onClick(View v) {
//the v variable is the clicked textview, in this case "player1"
//hide the textview and show the resultant edittext
v.setVisibility(View.GONE);
player1name.setVisibility(View.VISIBLE);
//set focus on edit text and when focus is lost hide it and set the textview text
player1name.requestFocus();
imm.showSoftInput(player1name, InputMethodManager.SHOW_FORCED);
player1name.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View y, boolean x) {
v.setVisibility(View.VISIBLE);
player1name.setVisibility(View.GONE);
imm.hideSoftInputFromWindow(player1name.getWindowToken(), 0);
String name = player1name.getText().toString();
if (name.equals("")) {
v.setText("Player Name1");
} else {
v.setText(name);
}
}
});
}
However with this solution I will need to duplicate this code and change the view names for player2 - player2name, player3 - player3name etc
i can obviously grab the clicked TextView via v, however what i cant seem to do is grab its corresponding EditText.
i had thought of doing this:
View test = v + "name";
//then i replace all references to player1name with the test variable
but it doesnt work it wants me to convert View test; into a string
any suggestions?
EDIT: made it easier to understand my question
View test = v + "name";
will give a compile error. Because "v" is not a string type. and also even if it was String, test is not. This line is pretty wrong.
There a few options to achieve what you want,
You can use hashmap
Declare a global field for hashmap
private final HashMap<Integer,EditText> map = new HashMap<Integer,EditText>();
and in onCreate method put your textview id as key, and put your edittext variables in value.
player1name = (EditText) findViewById(R.id.player1name);
map.put(R.id.textView1, player1name);
// for the rest
in onClick method
EditText e = map.get(v.getId());
Then replace them with "e"
e.requestFocus(); //example
Will you please state your problem clearly? Currently, your language is very ambiguous and I can not figure out, exactly what are you looking for. It will help us to know your problem and in turn solve it.
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 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