using 2 Java classes to display information on 1 XML file - java

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>

Related

Why is onCreate() method not being called?

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.

I'm trying to create a Spinner in Android Studio

I'm new to Android Studio, any help would be greatly appreciated.
This is not the mainActivity but another page called StudentReg1 and branch refers to the spinner id as well as as string array in strings.xml
public class StudentReg1 extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.student_reg1);}
Spinner mySpinner = (Spinner) findViewById(R.id.branch);
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(StudentReg1.this,android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.branch));
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(myAdapter);
}
setDropDownViewResource and setAdapter appear in red, I don't seem to understand the issue.
I think you need to use:
ArrayAdapter<String> myAdapter = ArrayAdapter.createFromResource(StudentReg1.this,R.array.branch,android.R.layout.simple_spinner_item);
hey buddy use this easy library to make spinner
implementation 'com.jaredrummler:material-spinner:1.3.1'
<com.jaredrummler.materialspinner.MaterialSpinner
android:id="#+id/newAnnouncementSpinner1"
android:layout_marginHorizontal="25dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/select_type"/>
you can use the above code for your xml code and below for java code
private MaterialSpinner newAnnouncementSpinner1
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newAnnouncementSpinner1=view.findViewById(R.id.newAnnouncementSpinner1);
List<String> categories= new ArrayList<>();
categories.add("string one");
categories.add("string two");
categories.add("string three");
categories.add("string four");
categories.add("string five");
categories.add("string six");
newAnnouncementSpinner1.setItems(categories);
}

Is it possible to include viewBinding in every project automatically?

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.

TextView doesn't show the required text

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.

How to run method from child intent

My android application has two intents.
First one: main window,
second one contains the game view (SurfaceView).
When user push "return" button, main windows appears before the game view indent suspends (surfaceDestroyed).
However I need to run a method in game view before the main windows appears. I've stuck with this question.
Here code:
MainWindow class:
#Override
public void onClick(final View v) {
if (v.getId() == R.id.StartBtn) {
final Intent myIntent = new Intent(getApplicationContext(),
StartActivity.class);
this.startActivity(myIntent);
}
}
StartActivity class
public class StartActivity extends Activity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
}
GameView class
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
#Override
public void surfaceDestroyed(final SurfaceHolder arg0) {
//Do some operations before destroing
}
}
Layout activity_start
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".StartActivity" >
<com.somepackage.appname.GameView
android:id="#+id/game_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
So I need to start surfaceDestroyed in GameView before onResume on MainWindow intent
i am not sure if that is really what yiu are looking for, but i will give it a try:
You need to have an instance inside the other Activity before you can call any method there. You can achieve it by using this:
(OtherActivity) getActivity.myMethod();
Use Activity.this instead of getApplicationContext(). That May be the problem. If not please provide the code.
simply use
Intent intent = new Intent(MainAWindow.this,StartActivity.class);
startActivity(intent);
and where is ur OnResume method?... explain your question properly what u want to achieve exactly..?

Categories