I made a new class and I wanted to make an OnClickListener for a button but I notice that the onCreate wasn't being called. Any help is appreciated.
public class BedtimeBottomSheet extends Activity {
private static final String TAG = "BedtimeBottomSheet";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate: ");
}
}
Consider that you should use setContentView(binding.getRoot()); in your onCreate method.
Related
I'm trying to rewrite my code from android:onClick9" to setOnClickListener`
since that crashes, bu I'm getting an error. Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button9=findViewById(R.id.button9);
button9.setOnClickListener(
new Button.OnClickListener() {
public void onClick9(View view) {
button9.setText("OK");
}
}
);
}
**ERROR**
error: <anonymous com.example.tablelayout6.MainActivity$1> is not
abstract and does not override abstract method onClick(View)
in OnClickListener new Button.OnClickListener() {
EDIT
EDIT 2 I have tried button9 but still the label of button9 doesn't change and new Button.OnClickListener() is shadowed.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button9=findViewById(R.id.button9);
button9.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View view) {
TextView status=findViewById(R.id.button9);
button9.setText("OK");
}
}
);
}
When you pass an anonymous class to method, you have to implements all of its abstract/interface inherited method.
In your code, you wrote new method called onClick9, and didn't override the abstract method onClick as needed, so it gives new an error.
You should rename your method or override on click separately.
Is there a way I can use two classes or more for one Activity in android studio?
I used this Test code but this App crashes:
Note : This is for learning purposes so that it can be used to split up huge classes into sub classes
//Main Activity Class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Test ob=new Test ();
ob.test();
}
}
// Test Class
public class Test extends MainActivity {
public void test()
{
TextView t=findViewById(R.id.h);
t.setText("Miaooo");
}
}
What you can do is to pass the activity with as the parameter of constructor and use that reference to call "findViewbyId()"
public class Test{
public void test(Activity activity)
{
TextView t=activity.findViewById(R.id.h);
t.setText("Miaooo");
}
while in your main activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Test ob=new Test (this);
ob.test();
}
If you want to your "Test" class to be extended by an activity than you should use activity creation wizard to setup all the stuff in manifest and .xml. It would be quite long process to do it manually.
When you write class Test extends MainActivity it means that Test should be activity too and you have to write setContentView(R.layout.your_layout); to set the view layer for your activity. Also, Test should be registered as Activity in your manifest file. Anyway if you want to change the text of the TextView why you want to create a new activity?
place your code in the activity main.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test();
}
public void test()
{
TextView t=findViewById(R.id.h);
t.setText("Miaooo");
}
It's quite long to change the code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
to:
public class MainActivity extends AppCompatActivity {
ActivityMainBinding mainBinding;
#Override
protected void onCreate(Bundle savedInstanceState) {
mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
super.onCreate(savedInstanceState);
setContentView(mainBinding.getRoot());
}
}
every time I create a new project or an activity. Is it possible to automate this process?
You can not automate the process since we have to provide the layoutId ourself. Activity is not gonna bind to a layout automatically. What you can do is Create a BaseActivity and inherit it from all your Activites. Below is a template with binding .
public abstract class BaseActivity<B extends ViewDataBinding> extends AppCompatActivity {
protected abstract int getContentViewId();
protected B binding;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, getContentViewId())
}
}
class MainActivity extends BaseActivity<ActivityMainBinding> {
#Override
public int getContentViewId() {
return R.layout.activity_main;
}
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// now you can directly access binding here
}
}
This is just for Binding you can Also add some reusable method in BaseActivity and use them in Any Activity without writing them again. and super.onCreate(savedInstanceState) should be first line for call .
No, I think it's not possible but you can make Live Template to get it frequently.
Or Simple that you can make a copy of that folder as a template. Whenever you need to create a new project it will be easy to copy/paste and just change the name of the project.
Thank you.
I want to increment the test variable whenever onCreate() is called but my app is crashing. Please help me find out the fix and please tell me why my app is crashing.
public int test = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView num = (TextView) findViewById(R.id.textNum);
new MainActivity().test++;
num.setText(new MainActivity().test);
}
You should never create an instance of Activity by your self
You can directly access your test in your MainActivity no need to use MainActivity().test to access your test variable
if you want to set an integer value to your textview then you need to convert your integer into a string value like String.valueOf(test) or ""+test
Try this way
public int test=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView num=(TextView) findViewById(R.id.textNum);
test++;
num.setText(String.valueOf(test));
}
if you want to increment your test variable whenever onCreate() called then you can use SharedPreferences
SAMPLE CODE
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
public class Main2Activity extends AppCompatActivity {
String prefName = "MY_PREFS_NAME";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
SharedPreferences prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = getSharedPreferences(prefName, MODE_PRIVATE).edit();
int test = prefs.getInt("counter", 0);
test++;
editor.putInt("counter", test);
editor.apply();
}
}
Okay so let me get you to understand a bit about the activity lifecycle:
onCreate()
Is called every time the activity is created, meaning there is really no use for you to increment the test in this method, because it will always be 1.
onStart()
Will be called every time the activity is shown, meaning when the activity is created or when you go back to the activity, I think this is the method where you increment test.
example
public int test = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView num = (TextView) findViewById(R.id.textNum);
}
#Override
protected void onStart() {
super.onStart();
test++;
num.setText(String.valueOf(test));
}
My MainActivity class has an if that checks is something is true it starts a new Activity which sets the text of a TextView to what I want.
MainActivity:
public static int caseA = 1;
if(caseA) {
Intent i = new Intent(Timer.this, WorkTimerNotification.class);
startActivity(i);
}
then it goes to the new Activity and it should check the value of caseA.
WorkTimerNotification:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
TextView timerMetric = (TextView)findViewById(R.id.tester_texter);
if(MainActivity.caseA) {
timerMetric.setText("min");
}
Problem: It does not change the text. Also, lets say I have many other if statements in mainactivity like:
if(caseB) {}
if(caseC) {}
//and so on..
How would I perform the checks? More importantly, how do I get this caseA check to work though.
You need to call findViewById() in onCreate():
private TextView timerMetric;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work_timer_notification);
timerMetric = (TextView)findViewById(R.id.tester_texter);
if(caseA) {
timerMetric.setText("min");
}
}
Right now you are calling it before calling setContentView(), before the view hierarchy is generated. For this reason, your code fails.