Hello guys right now I'm working on an app in which there are many quotes, in a TextView and I want to change the size of TextView on button click, like if Small button is clicked then the TextView should become small, if Large is clicked it should be large, etc. But each time I press the Button app force closes! What's the problem? Here's OnClickListener for the Button:
small = (Button) findViewById(R.id.small);
small.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.quotes);
tv.setTextSize(20);
}
});
P.S: The TextView and the Button are in different Activities.
There is no way to change the TextView size by Button from one Activity in other Activity. You will get NullPointerException because the findViewById method will not find view and will return null. You should use SharedPreferences. You should save text size value when you click Button and read it in the second Activity.
In your setting Activity:
small.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(YourActivity.this.getBaseContext());
sharedPreferences.edit()
.putFloat("FONT_SIZE", 22)
.apply();
}
});
In your activity where you have TextView
TextView tv = (TextView)findViewById(R.id.quotes);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(YourActivity.this.getBaseContext());
tv.setTextSize(sharedPreferences.getFloat("FONT_SIZE", 13/*DEFAULT VALUE*/));
Related
I create an application in android studio and I need advice, I got one button, and I need to change the text on the second button clicks through to the first. I have a code that changes only TextView but not the text on the button.
NewText = (TextView)findViewById(R.id.textView1);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
final TextView finalNewText1 = NewText;
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Set Text on button click via this function.
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
Same concept as you did for textView
Button SecondButton,ChangeText; // declaring the buttons
SecondButton = (Button)findViewById(R.id.button2);
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//This changes the text on the second button
SecondButton.setText("New Text Here");
}
});
SecondButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do anything
}
});
Button ChangeText;
ChangeText = (Button)findViewById(R.id.ch_txt_ger);
ChangeText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//part to change the button text
Button tmp_button = (Button)findViewById(R.id.ch_txt_ger);
tmp_button.setText("Frohe Weihnachten");
//part to change the textview text
TextView NewText
NewText = (TextView)findViewById(R.id.textView1);
finalNewText1.setText(" (Frohe Weihnachten) ");
}
});
After Clicking outlooking
Here you go: You can define a temporary button variable and make the change on it if setting the same button on its own clicking is causing problems.
And if the text will not change according to user, and if you know it like On/OFF, Red/Green you can also code it with a selector file which would make the java code look more clean.
A tiny advise: Defining the TextViews and Buttons that will get affected should all be written in the same function and close to the place where they are being changed for you to keep track of where you coded them.
I would add one thing, in case if you want to save the new button name when you close and reopen your app, you could use Shared Preferences: https://developer.android.com/training/basics/data-storage/shared-preferences.html
How can I make a TextView selectable? By this I do not mean that I want to make the text within the TextView to be selected. What I want is that when the user taps on the TextView, it enters the selected state, and when the user taps another TextView in the layout, it exits the selected state and enters the default state.
You can assign an onClickListener to any view, and do whatever you want when it is clicked.
So bottom line:
findViewById(R.id.yourViewName).setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do whatever you need to do here
}
});
You can create an instance of TextView which will be used to know the last clicked TextView:
private TextView lastClicked;
Then in the onClickListener method of your TextViews, you have to change your lastClicked TextView to the current one.
myTextView.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
lastClicked = (TextView) v;
}
});
This way, you can retrieve the selected TextView from the lastClicked variable.
That already exists, it's not called selected, it's called focused. You can check if a TextView is focused by doing:
myTextView.isFocused()
And in an activity you can get the current focused view by doing
getCurrentFocus()
I need to refresh a texview when i click a radio button. My textview contains the free memory info and i want that when i click the radio button i use to clear the RAM cache the textview refreshes and so i can see in that moment the free RAM without exit the application and enter it again.I found this example
TextView tv = (TextView) view.findViewById(R.id.lastRefreshed);
tv.setText("Blah: " + time);
view.invalidate(); // for refreshment
Is it possible? Thanks
You can register click event for radio-button.
RadioButton rb = (RadioButton)findViewById(R.id.radioButton1);
rb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// refresh the textview here
}
});
I'm kind of new when it comes to Android Application development and I'm developing an app at the moment. I'm trying to get my TextView change every time the user clicks the Button(NEXT) and when another Button (PREVIOUS) gets clicked on I want it to change back to the original TextView. So basically I'd like to set up a certain amount of TextViews and be able to browse through them with the two Buttons I mentioned.
So far I only know how to make the the TextView change one time on a Button(NEXT) click. I'm using this piece of code for that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton Next = (ImageButton) findViewById(R.id.Next);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView Text1= (TextView) findViewById(R.id.Text1);
Text1.setText("New Text");
}
});
NOTE: The Button "PREVIOUS" isn't included yet because I didn't know what to do with it yet.
I'm getting the feeling this code is only used when you want the TextView to change one time and you need a whole different method to make it change multiple times.
I hope I provided you with enough information and you are willing to help me out here.
Thanks in advance!
public class MyActivity extends Activity implements View.OnClickListener {
int stringIdList[] = {R.string.text1, R.string.text2, R.string.text3, R.string.text4}
int stringListCounter = 0;
TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton next = (ImageButton) findViewById(R.id.Next);
ImageButton previous = (ImageButton) findViewById(R.id.Previous);
text1 = (TextView) findViewById(R.id.Text1);
Next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.Next && stringListCounter < stringIdList.length - 1) {
stringListCounter++;
} else if (id == R.id.Previous && counter > 0) {
stringListCounter--;
}
Text1.setText(stringIdList[stringListCounter]);
}
What this does is assigns your Activity to an OnClickListener to handle the click events. If Next was pressed and the counter is within the range of the array list, it will increase the counter. The same for previous. At the end of the click, it will set the text to whatever the ID is. This assumes your strings are in a strings.xml file which is recommended in the Android spec and is static.
I think you can store history as List and have all states of textview in each moment.
Only thing that you have to do is to take previous value from this history stack after pressing previous button.
Hi im new to android and I have a program that has a global variable define and it works, so I can set it and get it in every activity, BUT it dosnt like to be changed in an on click listener. I made it so on the screen there is an edittext and when someone presses a button I want the edittext text to be put into the global variable. here is my code:
Button SiteButton = (Button) findViewById(R.id.SiteButton);
SiteButton.setOnClickListener(new View.OnClickListener() {
TextView textviewS = (TextView) findViewById(R.id.SiteIdT);
EditText edittextS = (EditText) findViewById(R.id.SiteIdE);
TextView textviewB = (TextView) findViewById(R.id.BusIdT);
EditText edittextB = (EditText) findViewById(R.id.BusIdE);
public void onClick(View v) {
textviewS.setText(edittextS.getText());
((Global) this.getApplication()).setgSiteId(textviewS.getText().toString());
textviewB.setText(edittextB.getText());
((Global) this.getApplication()).setgVehicleId(textviewB.getText().toString());
}
});
but the getApplication() part is showing an error. can anyone help?
You should refer to your activity this, since View.OnClickListener doesn't have such a method:
// Bad code! read below
((Global) MyActivityClassName.this.getApplication()).setgSiteId(textviewS.getText().toString());
textviewB.setText(edittextB.getText());
((Global) MyActivityClassName.this.getApplication()).setgVehicleId(textviewB.getText().toString());
By the way, how do you cast the return from getApplication() to Global? You will get a class cast exception there.