I'm currently trying to retrieve the size of my button but it return wrong size. The text of my button is variable.
I retrieve the button width like that :
button.getWidth()
But it says 420px even if I set a larger text.
How can I get the correct size?
I have already tried to use addOnLayoutChangeListener, it worked but in te onlayoutchange function I could not update the UI.
override onWindowFocusChanged
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int w=button.getWidth();
Log.d(TAG,w);
}
Related
In the settings in my phone can I change the font size of apps globally .
Settings> Display> Font size
Can I make the font size (small, normal, large , very large ) also read with Java and, if applicable . set only for my app ?
I tried to change the font size as follows.
Button buttonbig = (Button) dialog.findViewById(R.id.btn_big);
buttonbig.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
float fScale = getResources().getConfiguration().fontScale;
String fontSize = String.valueOf(fScale);
conf.fontScale =fScale + 0.15f;
getResources().getConfiguration().setTo(conf);
TextView textView = (TextView) dialog.findViewById(R.id.lbl_size_act);
textView.setText(fontSize);
}
});
dialog.show();
My Text View shows me the new size . However, the text remains the same size .Have anyone an idea what's wrong ?
First of all, I strongly advise against overriding the language, font size, or other settings, because as you pointed out, this is something you should do globally.
If you want to set it for your whole app, the right place to do so would be in the Application.
public class App extends Application {
#Override
public void onCreate() {
super.onCreate();
Configuration configuration = getResources().getConfiguration();
configuration.fontScale *= 2;
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
}
}
If you want to change it during runtime, you would also set it in application, but you would have to finish and start your activity again to take effect.
If you want to change only this textView's font size, how about trying
textView.setTextSize(float);
http://developer.android.com/reference/android/widget/TextView.html#setTextSize(float)
I am using the typeface in my text view and it is all looking good. Now I want to set the seekbar which will help the user to adjust the text size under the text view.
I have just used the seekbar and played with it. this is how I am using my Seekbar to make the text larger
/// SeekBar Progress
sk.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int p=0;
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
if(p<30)
{
p=30;
sk.setProgress(p);
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {
// TODO Auto-generated method stub
p=progress;
myTextView.setTextSize(p);
}
});
this is working and changing the size of text in the text view, But now there comes the problem :
Problem :
Problem is , the size is increasing and decreasing also when we take the seek bar to the left from its mid point. where as I want to set the minimum and maximum value. So to set the minimum and maximum I need to get the size of text inside the text view . and I have not found any method to get the textsize like for example myTextview.getTextSize() , there is no such method like it.
Let me tell you I am setting the size of the text view from the dimens file in the different values folder for supporting all the device and to make my text look good on tablets and small devices.
So now All I need to get is My text size inside the Text view . Please help me in getting the text size. thank you.
There is TextView.getTextSize(). You can also go TextView.getPaint().getTextSize().
try this
text_one.setText("your text");
text_one.measure(0, 0); //must call measure!
text_one.getMeasuredHeight(); //get height
text_one.getMeasuredWidth();
Do check that your myTextView is indeed TextView and not just View when you initialize it. e.g.
TextView myTextView = (TextView) findViewById(yourTextViewId);
I think the simplest answer is
myTextView.getTextSize();
Actually I was casting my value wrong. and Now it is solved. after doing this
float size = myTextView.getTextSize();
When the EditText line is blank (the default), I'd like the ImageView icon color to be white.
When the icon is pressed, I'd like the color to be black and then revert back to white because the press clears the EditText line, so it would then again be empty.
With the ImageView, I first set src equal to a white drawable. Any thoughts on how to switch the color state based on the length test of the EditText line?
I'm not too exactly sure what the question is asking for, I got confused when you brought in ImageView and hover because I don't believe phones support the onHoverListener as well.. Do you mean changing the EditText accent color when you click on it to type something? If so it will be under the colors.xml-->colorAccent
Also if you mean to change an images background color
public void onClick(View v) {
imageView.setBackgroundColor(Color.parseColor(EditText.getText().toString()));
}
Hopefully I provided you with some help to continue your project.
To update icon if textview empty, check Android: How can I validate EditText input? with addTextChangedListener() then
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.i("Text", charSequence + "");
if(charSequence.length()==0)
ib.setImageResource(R.drawable.button_unpressed);
else
ib.setImageResource(R.drawable.button_pressed);
}
and to change imagebutton background follow its state How to set image button backgroundimage for different state? with "create an xml in your drawable"
If you want to keep color of icon remain black after pressed, just override onClick() by
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ib.setImageResource(R.drawable.button_pressed);
}
});
I have a problem... have been thinking about it for a while now and been looking on line and still haven't come up with a clear explanation...
I have a number of textviews and have set onClickListeners to each of them.. and when the user clicks on one of them I want them to have the ability to change the text to another set of string array options which I have created progammatically. When the user selects an option the text should change to the option they choose. (I.e. TextView was A now it is B. hope this makes sense.. anyway... )
The current solution was to set a OnClickListener to every TextView and when someone pressed it an individual dialog showed. But I found that if I do this the code would be so long it would take an eternity to code so am hoping someone has a more elegant way of coding such a long process =(
So I guess my question would be... 1) is there a way I can find out which text view was pressed and then change the text of that TextView being pressed within a single method? to save me having to code 1000 alert dialogs...
http://i.stack.imgur.com/LRJGz.png
I would advise you to use a grid view.
You can see which textview was pressed like this:
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
//get id
switch (v.getId()) {
case R.id.textView1: ...
}
});
One of the ways to do what you want is to use the text view setTag() and getTag() methods.
On init of a text view use the setTag() to set some value to identify the view.
In the on click event use the getTag() on the view argument to know which view was clicked.
I would suggest holding the textviews in an array, like so:
TextView[] textViewArray = new TextView[textViewCount];
Then using a for loop assign each one a tag of integer - it's position
textViewArray.setTag(i)
And add an onClickListener to each one, again using a for loop:
textviewArray[i].setOnClickListener(etc...)
Then when one is clicked, you can use get the position of view that was clicked. This will require a custom method inside of your:
textviewArray.setOnClickListener(new customOnClickListener())
Where your customOnClickListner is like this:
private class customOnClickListener implements CompoundButton.{
public void OnClick(View view){
int position = (Integer) view.getTag()
///Do more code here - your processing
}
}
Hope that makes sense :)
For your for loops, you could use for(i = 0, i
Use set id for all text, where set the id positive integer(distinct), and then have one on view click listener(set it all) where u catch all text view clicks(downcast view with textview) and in side it put a switch case where you handle clicks on which text view is clicked.
You have to set "onClickListner" on all of of your textview.
For Saving some length of code i would suggest you create a function of your dialogbox, and give some int parameter to it, which would be directly called by the clickListener of textview,
Like ,
int i=0;
......
textView1 = (TextView)findViewById(R.id.yourtextview1);
textView2 = (TextView)findViewById(R.id.yourtextview2);
......
......
// and so on, for your all textviews
#Override
public void onClick(View view) {
if (view.equals(textView1)) {
i = 1;
CustomDialog(i);
}
//Similarly for all your textViews..
..........
Make A function CustomDialog Like
public void CustomDialog(int i){
if(i==1){
//Do something
}
}
with this code, my program just force close(error)
***public View x = findViewById(R.string.nfoname);***
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
//edittext
***final EditText infoname=(EditText)findViewById(R.id.infoname);***
//clear,confirm
Button clear = (Button)findViewById(R.id.buttonclear);
Button confirm = (Button)findViewById(R.id.buttonconfirm);
//clear button
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
infoname.setText("");
}
});
//confirm button
confirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
***x=(View) infoname.getText();***
}
});
}
the one with the * are the source of error
program function:
if the user clicks confirm, his name will be set to R.string.nfoname
which will then be used in another layout through TextView x = setText(R.string.nfoname);
I am not sure that you can save text to the R.string. This is a generated class that the compiler creates for you. It gets packaged with your apk. Think of the resources as a means of translation and to present text to the screen.
I think what you would want to do is save the user input as a SharedPreference or in a database.
See:SharedPreferences on the android docs for an example usage.
At least in the case of your variable infoname scoping is most likely causing your application to throw an error. infoname is a local variable to the function onCreate(), not an instance variable for your class, so it can't be accessed by your onClick() methods because they are part of an anonymous class.
Another thing I'd question is why you marked infoname as final? It goes out of scope when onCreate() exits so if it gets changed, you can see who changed it since it only exists while the method is executing.
You cannot set values to R.string.xxx because all these values will be constants much like a read only stuff. If you want to use the value of edit text to another layout use class variables or intent.putextra()
Coming to ur source code i see this
public View x = findViewById(R.string.nfoname);
How can a view be found by R.String? This should be R.id.
final EditText infoname=(EditText)findViewById(R.id.infoname);
Why this editText has to be final?
***x=(View) infoname.getText();***
You just use infoname.getText().toString() you will get the string value of the Edittext's current text.
Dude you can do stuff simply.
public View x = findViewById(R.string.nfoname);
This can't work as not only are you trying to find a View using a R.string resource id, you are doing it before setContenView(...) is called in your onCreate(...) method. Even if you used a valid View resource id such as R.id.infoname then x will be null because the content view hasn't been inflated yet.
final EditText infoname=(EditText)findViewById(R.id.infoname);
Apart from the pointless use of final this should'nt cause problems as long as R.id.infoname is actually the resource id of an EditText.
x=(View) infoname.getText();
Not only will x be null but calling getText() on an EditText returns an Editable which is not a View nor is it possible to cast it to View. Even if you used getText().toString() which is the correct way to get the text from an EditText it still wouldn't be possible to cast a String to a View.
Also, as for this...
TextView x = setText(R.string.nfoname);
It would have to be...
TextView x = (TextView) findViewById(<some id>);
x.setText(getString(R.string.nfoname));