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.
Related
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.
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");
}
I am developing a very basic Android App that adds a number in TextView whenever I hit the Button. The digit showing in TextView is also preserved when the orientation of the Mobile changes using the onSaveInstanceState() and onRestoreInstanceState() functions.
The problem is when orientation changes the value is preserved but when the Button is pressed once again after the changing of the orientation it again starts the counting from 0 rather then starting it from the preserved value.
My code:
public class MainActivity extends AppCompatActivity {
TextView showValue;
int counter=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showValue = (TextView) findViewById(R.id.CounterValue);
}
public void countIN(View view)
{
counter++;
showValue.setText(Integer.toString(counter));
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("my_text", showValue.getText().toString());
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
showValue.setText(savedInstanceState.getString("my_text"));
}
}
Thank You for your response.
add
counter=Integer.parseInt(savedInstanceState.getString("my_text"));
inside onRestoreInstanceState method
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
showValue.setText(savedInstanceState.getString("my_text"));
counter=Integer.parseInt(savedInstanceState.getString("my_text"));
}
You can save and get data using several methods
First If your data is small, then you can use onSavedInstanceState and onRestoreInstanceState .. for details follow this answer or this one
Second if you have too much data to store, then use ViewModel instead; this is a tutorial that you can start from.
I am new to programing and I am trying to clean up my main class.
There is just too much going on there.
I was wondering if it is possible to set up a way that 2 classes were to control 1 xml file.
For now I started doing this, but it didnt work:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....SOME CODE....
getParsha();
}
}
public void getParsha() {
new Parsha();
}
In new Parsha I had this code:
public class Parsha extends AppCompatActivity {
Parsha() {
setContentView(R.layout.activity_main);
....SOME CODE.....
}
}
I also tried doing this in onCreate() and it didnt work.
I am not sure why, is this even allowed to do in Android.
Please Note: I didnt get any error, it simply just didnt process the code, no Log or anything.
Thank you.
When you call the Activiy shoud use Intent
Like this
intent = new Intent(MainActivity.this, Parsha.class);
startActivity(intent);
finish();//if use need to stop use first activity.
And then, In your second activity namely Parsha, why you are set content view in inside constructor
You should give this in onCreate block only
setContentView(R.layout.activity_main);
Your two activity should be in following structure
public class MainActivity extends AppCompatActivity{
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
....SOME CODE....
getParsha();
}
public void getParsha() {
intent = new Intent(MainActivity.this, Parsha.class);
startActivity(intent);
}
}
Second Activity
public class Parsha extends AppCompatActivity{
#Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....SOME CODE....
}
}
Rather than using xml file in both java files why dont you just make the common variables static and keep the code in Parsha.java, use the class wherever inside onCreate(). The following example might give you a clear idea.
MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView list;
ArrayAdapter<String> adap;
static ArrayList<String> arr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
arr = new ArrayList<String>();
new Parsha();
arr.add("Hello");
arr.add("There");
adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arr);
list.setAdapter(adap);
}
}
Parsha.java
public class Parsha extends AppCompatActivity{
Parsha(){
arr.add("This");
arr.add("Works");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
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.