Find Id of View in Child Activity - android

I have an issue , need your help , my question is : I have a multiple activity , so i have created one abstract class which hold the view . Now my question i am able to pass the view to the Base Activity but how to finds view id in Child Activity Class.
I Tried like this :
public abstract class BaseActivity extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResourceId());
}
public abstract int getLayoutResourceId();
}
My Activity Class:
public class ServiceActivity extends BaseActivity {
Button startSerice_btn, stopService_btn;
MyService myService;
#Override
public int getLayoutResourceId() {
return R.layout.service_xml;
}
}

You can access your views after the super.onCreate() call in the ServiceActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Access them here
}

I think you no need to set content view in BaseActivity like below :
public abstract class MyAppBaseActivity extends AppCompatActivity implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
And you just extends Activity like below :
// for MyCustomActivity1
public class MyCustomActivity1 extends MyAppBaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View view) {
}
}
// for MyCustomActivity2
public class MyCustomActivity2 extends MyAppBaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onClick(View view) {
}
}

Related

Application static property is null

I instantiate a static property at the beginning of Activity's onCreate(), but some of my users get NullpointerException when I try to get it in MainActivity.
In my opinion, you code should be
public class TestActivity extends AppCompatActivity {
public static String mValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mValue = "my_value";
}
}
The value TestActivity.mValue should be null util the onCreate executes.You can init the mValue like this
public class TestActivity extends AppCompatActivity {
public static String mValue;
static {
mValue = "my_value";
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
}

Put common listeners inside MainActivity class

Im interested if i can to set some common listeners inside main activity class? For my project i use FirebaseAuth, so i would like to init it in MainActivity onCreate(), setup needed listeners in onStart() and onStop(), and then inherit that class in every other activity class.
Some code to please you :]
MainActivity class [parent]:
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
protected FirebaseAuthentication firebaseAuthentication;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
firebaseAuthentication = new FirebaseAuthentication(FirebaseAuth.getInstance(), FirebaseDatabase.getInstance());
}
#Override
protected void onStart() {
super.onStart();
firebaseAuthentication.addAuthStateListener();
}
#Override
public void onStop() {
super.onStop();
firebaseAuthentication.removeAuthStateListener();
}
}
AuthActivity class [child]:
public class AuthActivity extends MainActivity implements FirebaseAuthentication.OnUserAuthListener {
#BindView(R.id.viewPager) LockableViewPager viewPager;
private String userUID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_market);
ButterKnife.bind(this);
firebaseAuthentication.setOnUserAuthListener(this);
firebaseAuthentication.isSingedIn(); // check if user is singed in
}
#Override
// response for firebaseAuthentication.isSingedIn() above
public void onAuthSuccess(String userUID) {
this.userUID = userUID;
}
#Override
// response for firebaseAuthentication.isSingedIn() above
public void onAuthFailure(String message) {
snackbar(message);
Intent intent = new Intent(this, AuthActivity.class);
startActivity(intent);
finish(); // TODO mb should to delete it
}
}
Can this implementations bring me errors (maybe NullPointerExeption or what unexpectedly in future)?
Would be great if you provide me some sources to read/watch.
Thank you.
Perfect example of abstraction, but not really a question.
You will not get any nullpointers or other errors by implementing it like this.

Generic object in Android Activity

public class MyClassActivity<T> extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// todo..
}
It is possible?
How startActivity with T object?

Restarting activity inside onClick method

I have an app which reads .txt file and displays contents in table layout.
here is my MainActivity.java file:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TableLayout(this,0));
}
}
Inside TableLayout class when adding first row (headers), i also add on click listeners.
Here is the code that gets executed when click happens:
public class MyOnClickListener extends MainActivity implements OnClickListener {
int rowNumber;
public MyOnClickListener(int rowNumber) {
this.rowNumber = rowNumber;
}
#Override
public void onClick(View v) {
setContentView(new TableLayout(context,rowNumber));
}
};
context is saved from when activity is first started, but i get nullpointexception error with this as an argument.
What i would like to do when header is clicked is to recreate table with header number argument.
So my question is what should i do to restart table creation within onClick method?
Edit: this is how context is saved
Context context;
public TableLayout(Context context, int rowNr) {
super(context);
this.context = context;
I would probably do something like this
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TableLayout(this, getRowNumber()));
}
protected int getRowNumber(){
return 0;
}
}
and then you do something like
public class MyOnClickListener extends MainActivity implements OnClickListener {
// here you initialize rowNumber
static int rowNumber = 0;
#Override
protected void getRowNumber(){
return rowNumber;
}
#Override
public void onClick(View v) {
// here you set your rowNumber
rowNumber = some_value;
recreate();
}
};
P.S: I haven't compiled this but you can get the idea

extends another activity class and reuse listener codes to views such as buttons etc

I have created an activity called ButtonActivity that has a lot of buttons and listeners. I want to create another activity TwoButtonsActivity to extend ButtonActivity so that the listeners I created can be resused.
TwoButtonsActivity is similar to ButtonActivity but with small changes.
Is this possible?
When I execute the code, I find that the extended activity do not respond to button click.
Here is the base activity:
public class ButtonActivity extends Activity {
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
button.setText("Got Pressed:" + ++count);
}
});
}
}
Below is the extends Activity:
public class TwoButtonsActivity extends ButtonActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
you can have a activity with listeners same below
public class ButtonActivity extends Activity {
int count = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
}
public void mylistener1(View v)
{
switch(v.getid()){
case R.id.button:{
//do somthings
}break;
}}
public class TwoButtonsActivity extends ButtonActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
and in layout of TwoButtonsActivity(main) you set in tag of your button android:onclick="mylistener1"
and you should set to any button or view that you want it use this listener

Categories

Resources