Create generic/base form class regardless if activity or fragment - java

I'm trying to create a generic/base form regardless if it is an activity or fragment. To make it simple, a Form can submit so:
class BaseFormActivity extends AppCompatActivity {
public abstract void submitForm();
#Override
public void setContentView(int layoutResID) {
ConstraintLayout activityBaseForm = (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base_form, null);
FrameLayout frameBaseForm = activityBaseForm.findViewById(R.id.frame_base_form);
getLayoutInflater().inflate(layoutResID, frameBaseForm, true);
findViewById(R.id.btn_submit).setOnClickListener(v -> submitForm()) // for the sake of simplicity, there's a button that will trigger submitForm() method
super.setContentView(activityBaseForm);
}
}
Here, I just include some default layout for a form, and a button for submit that triggers the abstract method submitForm(). But, this is only for android activities. How can I make this also available for fragments without writing a BaseFormFragment? I don't want to repeat default behaviors from activity to the fragment and vice versa.

Consider it as a sample Presenter class which handle your button click and get all form fields and send to server
public class MyPresenter {
private MyPresenterIView iViewInstance;
public MyPresenter(MyPresenterIView iView){
this.iViewInstance=iView;
}
public void onSubmitClick(){
//write your logic here
String fieldOneText=iViewInstance.getFieldOneText();
sendToServer(fieldOneText);
}
private void sendToServer(String stringInfo){
//send info to server
}
}
MyPresenterIView Interface
public interface MyPresenterIView{
String getFieldOneText();
}
And use Presenter in your Activity or Fragment
//implement MyPresenterIView to your Activity or Fragment
public class MyActivity extent SomeActivity implements MyPresenterIView{
private MyPresenter myPresenter;
//in onCreate or onCreateView(if its a fragment) initialize myPresenter
protected void onCreate(..){
myPresenter=new MyPresenter(this);//this will enforce Activity/Fragment to implement IView
}
#Override //comes from MyPresenterIView
public String getFieldOneText(){
return ((EditText)findViewById(R.id.edttext_field_one)).getText().toString().trim();
}
}

Related

Android Activity Reusability

I've created a reusable xml layout that looks like this
I would like to use same component on different activities , what i want to do is to create BaseActivityClass that extends Activity.
public class BaseActivityClass extends Activity {
int layout_id = R.layout.SomeLayout;
final int menu_button_id = R.id.menuButton;
final int save_button_id = R.id.saveButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout_id);
Button btn = findViewById(menu_button_id);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//this functionality will be same on every child class
}
});
}
}
I would like to extend that class as
public class SomeActivityClass extends BaseActivityClass {
int layout_id = R.layout.SomeOtherLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
There is no constructor class for Activity.On intent call only class name is referenced.
And subclass cannot change super class variables(hiding).
I dont want to copy paste same code that is in BaseActivityClass into other classes.
A wrapper class might solve the problem but that seems too sloppy imo.
How can i solve this design issue?
I am free to comment on any idea
Sounds like you want the base class to control the main container layout and allow the derived class to provide a "content" layout. Is that correct? If so, you can use this pattern:
Step 1 - Add a ViewStub to your base layout. Some pseudocode to give you the idea:
<ConstraintLayout>
<!-- Common Stuff -->
<Button id="menu">
<Button id="save">
<!-- "Content" area to be filled by derived classes -->
<ViewStub id="viewStub" />
</ConstraintLayout>
Step 2 - Update your base layout to provide a way to inflate content into the "content area". Some Pseudocode:
public abstract class BaseActivityClass extends Activity {
int layout_id = R.layout.SomeLayout;
final int menu_button_id = R.id.menuButton;
final int save_button_id = R.id.saveButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout_id);
Button btn = findViewById(menu_button_id);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//this functionality will be same on every child class
}
});
// Get the ViewStub, set the derived class's content layout on it and inflate it,
// thereby displaying the derived class's view layout within the base class's
// "content area"
ViewStub vs = findViewById(viewStub);
vs.setLayout(getContentLayoutId());
vs.inflate();
}
// Define abstract method that all derived classes must implement to provide
// the id of the layout to show in the "content area"
#LayoutRes
public abstract int getContentLayoutId();
}
Step 3 - Update your derived class to provide the layout you want to display. Some pseudocode:
public class SomeActivityClass extends BaseActivityClass {
int layout_id = R.layout.SomeOtherLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
#LayoutRes
public int getContentLayoutId() { return layout_id; }
}
Now when SomeActivityClass is created, it calls the super class onCreate which inflates your base layout and then asks the derived class for it's main content layout that it shoves into the "content area".
I've used this pattern on my projects and it works quite well.
Another option is to simply pass the layout ID via the super class constructor. If the base Activity is abstract it will never be instantiated and doesn't have to adhere to the zero-arg constructor rule. Only your derived class does. So you can do something like this:
public abstract class BaseActivityClass extends Activity {
private final int mContentLayoutId;
protected BaseActivityClass(int contentLayoutId) {
mContentLayoutId = contentLayoutId;
}
protected void onCreate(Bundle state) {
// Same code to load ViewStub, but use mContentLayoutId instead
}
}
public SomeOtherActivity extends BaseActivity {
public SomeOtherActivity() {
super(R.layout.SomeOtherLayout); // Call super with derived layout
}
}
Hope that helps!

How do I communicate between a class and a fragment which uses it?

I'm using Android Studio. I haven't been able to find an answer online, so even a link to a solution would be helpful.
I have an Activity, which includes a number of Fragments. One of these Fragments is called BookGridFragment, which uses a class called BookGrid.
BookGridFragment looks like this (I've left out irrelevant bits):
public class BookGridFragment extends Fragment {
BookGrid myBookGrid;
public BookGridFragment() {}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
// Inflate layout
View rootView = inflater.inflate(
R.layout.fragment_book_grid, container, false);
myBookGrid = rootView.findViewById(book_grid);
return rootView;
}
public void setBook(Book thisBook) {
myBookGrid.setBook(thisBook);
}
}
The BookGrid class is:
public class BookGrid extends View {
private Book mBook;
public BookGrid(Context thisContext, AttributeSet attrs) {
super(thisContext, attrs);
}
public void setBook(Book newBook) {
mBook = newBook;
}
protected void onDraw(Canvas canvas) {
if (mBook == null) return;
canvas.save();
draw_book_details();
// draw_book_details() is a function which just takes
// the book info and displays it in a grid
canvas.restore();
}
public boolean onTouchEvent(MotionEvent event) {
// This function responds to the user tapping a piece of
// book info within the grid
// THIS IS WHERE I'M HAVING PROBLEMS
}
}
So, that all works fine. The issue is, that I need the BookGridFragment to know when the user touches the BookGrid and to pass that information to another Fragment (via the Activity). So, I assume that when the onTouchEvent is reached, that should somehow notify the BookGridFragment that the BookGrid was touched, but I can't figure out how to do that.
Everything I've found online is about passing information between Fragments, but that approach doesn't work here as the BookGrid class doesn't "know" that it's within a BookGridFragment.
You could use the same idea that is used to communicate a Fragment and an Activity. Create an interface:
public interface OnBookGridTouched{
void onTouchGrid();
}
Add a variable to your BookGrid:
private OnBookGridTouched mCallback;
Add a setter to this variable:
public void setCallback(OnBookGridTouched callback){
mCallback = callback;
}
Then make your fragment implement the interface:
public class BookGridFragment extends Fragment implements OnBookGridTouched {
You'll be forced to implement the method onTouchGrid
In your fragment onCreateView pass the fragment to your custom view:
myBookGrid.setCallback(this);
Finally, in your custom view you can call the callback to reference the fragment:
public boolean onTouchEvent(MotionEvent event) {
// This function responds to the user tapping a piece of
// book info within the grid
// THIS IS WHERE I'M HAVING PROBLEMS
mCallback.onTouchGrid();
}
A solution could be to set the onTouch/onClick listener in the fragment instead of in the BookGrid itself. From there you can use the fragment method getActivity() to call an activity method, parsing on the correct data to the correct fragment.
I think this situation is very similar to a Fragment containing a Button.
The Button has a method which accepts something implementing a certain interface (for the Button: View.OnClickListener). The Fragment calls that method (for the Button: setOnClickListener()) to pass in the desired Object implementing all the required methods, either an anonymous class or maybe a field or the Fragment itself. There are pros and cons for all three approaches, it depends on your situation which one is best.
They have in common that BookGrid should have an interface as well as a method so other classes can set the current Object implementing that interface.
I am not quite sure about the exact scenario that you are having there. However, if the problem is the communication between the fragment and an activity which hosts the fragment, then you might think of the following implementation.
Let me point out some of your concerns first.
Everything I've found online is about passing information between
Fragments, but that approach doesn't work here as the BookGrid class
doesn't "know" that it's within a BookGridFragment.
BookGrid class will know the context of its existence when you will pass the Context towards it while calling a function of it. So I would like to suggest passing the context of the Activity or Fragment when you are calling a function from your BookGrid class.
public class BookGrid extends View {
private Book mBook;
private Context context;
public BookGrid(Context thisContext, AttributeSet attrs) {
super(thisContext, attrs);
this.context = thisContext;
}
public void setBook(Book newBook) {
mBook = newBook;
}
protected void onDraw(Canvas canvas) {
if (mBook == null) return;
canvas.save();
draw_book_details();
// draw_book_details() is a function which just takes
// the book info and displays it in a grid
canvas.restore();
}
public boolean onTouchEvent(MotionEvent event) {
// Call the function of your host activity
((YourActivity)(thisContext)).onBookGridTouched();
}
}
Now write a public method in your activity class which hosts the fragment named onBookGridTouched.
public void onBookGridTouched() {
// Communicate with other fragments here
}
However, a noble approach of solving this problem in a more generic way is to use an interface and then implement the interface wherever necessary like #LeviAlbuquerque suggested.
I am just putting another workaround which is a bit static.
Assuming that you have ONE Activity responsible of all fragments:
1.Create an interface in your BookGrid:
public interface ActionHappened {
void onActionHappened();
}
2.Create an instance of your interface within your BookGrid class and trigger the method onActionHappened where you want it to be triggered. For instance, if you would like it to happen in your onDraw(), then do the following:
ActionHappened actionHappened;
protected void onDraw(Canvas canvas) {
if (mBook == null) return;
canvas.save();
draw_book_details();
// draw_book_details() is a function which just takes
// the book info and displays it in a grid
canvas.restore();
actionHappened.onActionHappened();
}
3.Implement your interface within your activity
public class ActivityA extends AppCompatActivity implements BookGrid.ActionHappened {}
4.Within your implemented method, trigger the method:
#Override
public void onActionHappened() {
Fragment fragmentA = getSupportFragmentManager().findFragmentByTag(R.id.fragmentA);
Fragment fragmentB = getSupportFragmentManager().findFragmentByTag(R.id.fragmentB);
//Trigger that method from your activity to fragmentA or fragmentB
fragmentA.doWork();
fragmentB.doWork();
}
Wether you would like to pass data to fragmentA or fragmentB, doWork() method will do that for you. Make you create such a method in the corresponding fragment.

How to call a Main activity method in another non activity class

Below is My Main activity with ColorChange method.I want to call this Colorchange method in ImageColor Class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ColorChange() { // <----- Want to call this method in below class
ImageView blue = (ImageView) findViewById(R.id.imageView);
blue.setColorFilter(0xff000000);
}
}
And this is my class where i want to call the ColorChange method of Mainactivity.
public class ImageColor {
public void Imager() {
// Want to call my ColorChange method here
MainActivity obj = new MainActivity();
obj.ColorChange(); //<-------- Using mainactiviy object crashes my app.
}
}
I have already tried using Mainactivity as object it crashes my app.I also cannot declare my ColorChange method static because it uses findViewbyid.Please let me know if there is any way to call Color change method in this Image Color Class.
Try this way. It will help you.
public class ImageColor {
public void Imager(Activity activity) {
// Want to call my ColorChange method here
if(activity instance of MainActivity)
((MainActivity)activity).ColorChange(); //<-------- Using mainactiviy object crashes my app.
}
}
use interface to communicate with activity from non activity class. create colorChange() in interface and get the instance of interface in non activity class and call that method
class MainActivity {
interface mInterface = new interface() {
public void colorChange(){
}
}
}
pass mInterface to non activity class and call colorChange of interface when you want ..
You have to pass activity as a parameter in ImageColor class
Then call your ColorChange() method by refference of Activity.
Like This-
public class ImageColor {
Activity activity;
public ImageColor(Activity activity)
{
this.activity = activity;
}
public void Imager()
{
if(activity instance of MainActivity)
((MainActivity)activity).ColorChange();
}
}
Activity classes are created by Android. So the above method is not correct.
You have 2 ways to access the method in activity.
1 . using a static method
public static void ColorChange() {
ImageView blue = (ImageView) findViewById(R.id.imageView);
blue.setColorFilter(0xff000000);
}
}
Using a callback mechanism
public interface ImageLoadedcallback{
void onColorChanged(int color);
}
And update
public class ImageColor {
public void Imager(ImageLoadedcallback callback) {
callback.onColorChanged(color)
}
}
And In activity
public void ColorChange() {
new ImageLoader().Imager(new ImageLoadedcallback{
#Override
public void onImageLoaded(Color color){
ImageView blue = (ImageView) findViewById(R.id.imageView);
blue.setColorFilter(0xff000000);
});
}
To make it clear, make an Activity as a static variable can lead to Activity leak, so we must avoid doing that.
I suppose if the Activity where you create ImageColor object is MainActivity, you can pass MainActivity directly to achieve what you want.
public class ImageColor {
public void Imager(MainActivity activity) {
activity.ColorChange();
}
}
If you called it from other class(not from MainActivity), you can always passing MainActivity to that other class object to be used for ImageColor object.
PS: Check about java naming convention too, it will help you to write a better code

Android: call a function only on child activity from parent activity

My ParentActivity.java is like this
public class ParentActivity extends Activity{
public void childOnlyMethod(){
Log.d(TAG,"child only method triggered in parent activity");
}
public void startChildActivtityButton(){
startActivity(new Intent(this, ChildActivity.class));
}
public void childOnlyMethodButton(){
childOnlyMethod();
}
}
And my ChildActivity.java is like this
public class ChildActivity extends ParentActivity{
#Override
public void childOnlyMethod(){
Log.d(TAG,"child only method triggered in child activity");
}
}
The problem is when I press childOnlyMethodButton, childOnlyMethod() in both parent and child activity gets invoked I want it to be invoked on child only how can I achieve that?
#Override annotation does nothing, it is only used to tell the compiler and IDE that this method overrides its super class. Non-static methods are associated to objects, not classes. Overriding means completely replace the method in its super class. So if you invoke childOnlyMethod on ChildActivity, only child version will be invoked.
I'm guessing you were actually clicking on the parent activity instance. I don't really get the point why you want to invoke a child method on parent reference. If you can post the real code, I can give you more precise answer.
However, you can try the following code. This example will only invoke child version childOnlyMethod on ChildActivity instance. But it will still invoke parent's childOnlyMethod if you click the button on ParentActivity.
public class ParentActivity extends Activity implements View.OnClickListener {
private static final String TAG = "ParentActivity";
private Button mStartChileButton;
private Button mButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStartChileButton = (Button) findViewById(R.id.startChileButton);
mButton = (Button) findViewById(R.id.button);
mStartChileButton.setOnClickListener(this);
mButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.R.id.startChileButton:
startActivity(new Intent(this, ChildActivity.class));
break;
case R.id.R.id.button:
default:
childOnlyMethod();
break;
}
}
public void childOnlyMethod() {
Log.d(TAG, "Called from ParentActivity");
}
}
public class ChildActivity extends ParentActivity {
private static final String TAG = "ChildActivity";
#Override
public void childOnlyMethod() {
Log.d(TAG, "Called from ChildActivity");
}
}
If you are extending a parent class, all the methods in the parent class are passed down somewhat to the child class, if you don't want the method in the parent class to be implemented, first start by removing that #Override annotation in the child class above the method. Then use this snipped below, all it does is make the Method in the parent class either private or protected so that it can't be accessed in the child class.
public class ParentActivity extends Activity {
private void childOnlyMethod(){
Log.d(TAG,"child only method triggered in parent activity");
}
//OR
protected void childOnlyMethod(){
Log.d(TAG,"child only method triggered in parent activity");
}
}

How to invoke info-getting from an Android fragment to the MainActivity from within the MainActivity?

So I'm trying to wrap my head around Android Fragments. If I put the following code in my MainActivity:
public void getMessage(Object obj) {
Log.wtf("My object: ", obj.toString());
}
and the following code in my fragment:
((NewNotificationRule)getActivity()).getMessage("Yah wohooo!");
I get the "Yah wohooo!" into my MainActivity. The thing is that this pushes that string from my fragment to my Activity, where I want it to work the other way around. The fragment just defines a couple EditTexts, so upon hitting the submit-button defined in the xml called by the MainActivity, I want the MainActivity to pull the information defined in the EditTexts within the fragment so that it can submit it into the DB. So to conclude: I want to pull something (R.id.myEditText to be precise) from within my MainActivity instead of pushing it from within the fragment.
Is there any way that I can pull the contents of an EditText from a fragment into an Activity? All tips are welcome, since I'm totally lost here..
One fast option (not sure if this is safe or recommended tho) is creating a class in your project with attributes needed to store info and instancing an object of this class in the MainActivity. Then, reference it from the fragment and fill in it the data you need to save (e.g. within an attribute EditText1Data or something) whenever the text is changed or introduced into the fragment's EditText. Then just store into the DB the data contained in the object you filled with the Fragment info. Place some default values to the atttributes in the constructor of this called class to avoid null stuff problems. This can help you easily transfer Data in both directions Activity<-->Fragments , even tho this mightmean you must be very careful since you can get null pointer exceptions.
//This is Your DataClass used to transfer Data between Activity and Fragment.
public class DataClass {
public String EditText1Value;
public String EditText2Value;
public DataManager()
{
EditText1Value="Default Text";
EditText2Value="Default Text";
}
}
//This is the MainActivityClass
public class MainActivity extends Activity{
//instance of the DataClass to be passed to fragments with the method getDataClass
public DataClass dataClass = new DataClass();
//Main Activity code goes here...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
}
//This method returns a reference to the DataClass Object
public DataClass getDataClass()
{
//Return this class instance object of the DataClass class
return (dataClass);
}
//Now this is the method to push data to DB, called whenever an activity button is pressed.
private boolean WriteToDB ()
{
//Suppose this receives a String
WritetoDB(dataClass.EditText1Value);
}
}
//And this is the Fragment that sends data through the DataClass Object
public class ExampleFragment extends Fragment {
//Used to reference MainActivityObject and store info
DataClass dataClass;
//Used to Reference Activity's EditTexts
private EditText editText1;
//TextWatcher used to detect the EditText Field Changes
private TextWatcher EditText1_Txtwtr;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View v = inflater.inflate(R.layout.whatever_layout, container, false);
editText1= (EditText)v.findViewById(R.id.idEditText1);
setHasOptionsMenu(true);
return v;
}
#Override
public void onResume ()
{
super.onResume();
//code...
//Get MainActivity's DataClass object reference.
dataClass= ((MainActivity)getActivity()).getDataClass();
//store info whenever you need to, not necessarily on each keystroke, and store it in the object, not in the DB
dataClass.EditText1Value = editText1.getText().toString();
// Also, to capture whenever a edittext changes, you can use a textwatcher.
EditText1_Txtwtr= new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3)
{}
#Override
public void afterTextChanged(Editable editable)
{}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3)
{
dataClass.EditText1Value = editText1.getText().toString();
}
}
//Asign TextWatcher to your Edit Text.
editText1.addTextChangedListener(EditText1_Txtwtr);
}
}

Categories

Resources