Refresh a textview when click a radio button Android - android

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
}
});

Related

Change button text over other button android

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

Android realtime add button

I need a button that, when clicked, would add a TextView below it. Also, the TextView cannot be made invisible and appear on click in this case, it must add the TextView. Was looking for this and couldn't find anything that suits my needs.
Set a listener on the button and for each click, create a new TextView and add it to the layout.
final LinearLayout theLayout = (LinearLayout)findViewById(R.id.thelayout);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello");
tv.setTextColor(Color.BLACK);
theLayout.addView(tv);
}
});
Update: Added text color above

Unable to change TextView size on button click, why?

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*/));

How can we change the questions and options without changing text view and layout

I am developing an quiz based application. I wanted to know like, how can we change the questions and its options without changing the text view where the question will appear and the layout when the user clicks next button i want the same text view and layout only question and its options should change.I am new to the android so can anyone help me out ..
you can change the text of texview on click of next button like this
TextView textView=(TextView) findViewById(R.id.textview1);
Button next=(Button) findViewById(R.id.nextbutton);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
textView.setText("Your Text");
}
});

How to display a text in android after a button click in android?

After making an auto complete text view once the user click the OK button(which i made) i wanted the application to display the contents of selected text in auto complete as a button or as a text view..I find only Toast class for this..which will last only for a short time..Is there any other way of doing it??
OK, fjust write onClick method in OnClickListener as
btnOk.setOnClickListener(new View.OnClickListener(){
public void onClick(View view)
{
String text=mEdit.getText();
mText.setText(text);
}
});

Categories

Resources