Such must be fair, that when selecting a button to change the text size? I tried to add in a onCreate if but not work correctly, is cheek only one time to start the application. The function not remains active for next time. Thank you
button
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/inbox_string"
android:id="@+id/button"
android:textColor="@drawable/text_button_culoare"
android:drawableLeft="@drawable/ic_view_list_white_24dp"
android:background="@android:color/transparent"
style="?android:attr/borderlessButtonStyle"
android:layout_centerHorizontal="true"
android:gravity="left|center_vertical"
android:layout_gravity="center_horizontal"
android:focusable="true"
android:enabled="true"
android:clickable="true"
android:contextClickable="true"
android:elegantTextHeight="true"
android:layout_marginTop="16dp" />
Activity:
public class Work_screen extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_screen);
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.button) {
}
}
};
findViewById(R.id.button).setOnClickListener(clickListener);
final Button button = (Button) findViewById(R.id.button);
if (button.isSelected()) {
Context context = getApplicationContext();
CharSequence text = "selected";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
button.setTextSize(22);
} else {
Context context = getApplicationContext();
CharSequence text = "not";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
}
You should place some code in your onClick method, because this method is called when you click on that button. The reason why your code is just called the first time is that the ONCREATE method is just called once at the creation of your application.
Just place your if else statement in your onClick method and it should work. And you should create your button variable as a class variable to access it in your listener even if it's not part of your onCreate method.