doubts action bar, toolbar, etc - android

Well, I'm trying to make an app that needs to have some sort of ActionBar, which only have to schedule and design once, and then serve me for all my APP activity.
I'm not sure how this works, need a little guidance please.
To give you an idea of what I want, I would like something like this:
A top, that is the general of the APP. And then below as well as a submenu with icons, with different options.
What exactly would have to use it? I've been tinkering with ActionBar but that it me for the top .. But to the other as you would?
Both want them to be static and are always in every activity of my APP, without having to copy and paste the code of each una..Que I guess and this somehow you are ready to embed it anywhere, programming only once.

This question doesn't really have much to do with Android. This is about how you can re-use code multiple places.
You could use inheritance for this purpose.
Say you have 3 different activities, but you want them to have a set of common features - in your case the ActionBar.
In that case you could create an abstract class that implements the ActionBar and make all your activities inherit from this abstract class.
The hierarchy could look something like this:
public abstract class BaseActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.oncreate(savedInstanceState);
// Setup your common ActionBar here.
}
}
Now for this abstract class to do its work, you'll have to make all your activities inherit from this, like so:
public class MyActivityA extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.oncreate(savedInstanceState); // It's important to call through to super to have it setup the ActionBar for the current activity.
// Next call setContentView(R.layout.my_activity_layout);
// And what else you need to do.
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_a_menu, menu);
return true;
}
}
And for the next Activity you do the same:
public class MyActivityB extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.oncreate(savedInstanceState);
// Next call setContentView(R.layout.my_activity_layout);
// And what else you need to do.
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_b_menu, menu);
return true;
}
}
The onCreateOptionsMenu is implemented in all your activities, as to create different menu items and the same should go for your onOptionsItemSelected.
Now this is a very basic example of how to share basic features for multiple classes and it should be something you'd have to be knowledgable about before you start working with Android, as the above code is common Java.
Also keep in mind, that it's a very broad question you're asking.
I actually just wanted to put this in a comment, but decided it would become too large to fit into the comments.

Related

Change text content depending on button clicked within the same class

My problem is exactly like this link but it's not in android.
I have one button on a layout and two buttons on an another one. On my application, ClickScreen activity can be triggered by either FirstCase activity or SecondCase activity.
I tried to make a conditional statement on my ClickScreen for which activity is triggered but couldn't handle it. I don't want to create two more classes to do this since it's not an efficient technique.
private void goTo2ndPage() {
Intent i3 = new Intent(this, ClickScreen.class);
startActivity(i3);
}
public class ClickScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.click_screen);
}
}
As we discussed in comments. It looks like what you really want is to add extra data in your intent so that Started Activity can get it and act accordingly.
Check out this post !

Cannot get a switch listener working with setOnCheckedChange(onCheckedChange)

I'm having a problem having a switch button listener to work correctly. It is in my main activity as is:
public class MainActivity extends WearableActivity {
private Switch mySwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Enables Always-on
setAmbientEnabled();
// React to settings change
mySwitch= findViewById(R.id.mySwitch);
mySwitch.setOnCheckedChangeListener(this); // <--------
}
public void onCheckedChange(CompoundButton s, boolean isChecked) {
...
}
}
At the arrow line, I tried with this (like a sample on the internet). It is marked in Android Studio as an error. AS suggests me to cast to a CommpoundButton.onCheckedChange (or similar). When I start the app, it crashs saying MainActivity cannot be casted to that. I cannot do also setOnCheckedChangeListener(onCheckedChange);
What I am doing wrong?
You need to implement OnCheckedChangeListener if you want to use this as a option for setOnCheckedChangeListener().
public class MainActivity extends WearableActivity implements CompoundButton.OnCheckedChangeListener{
This is because setOnCheckedChangeListener() will only accept an instance of OnCheckedChangeListener, so you can't simply use this since the Activity that this is pointing to isn't an instance of OnCheckedChangeListener.
However, since OnCheckedChangeListener is an interface, this is easily rectified by implementing OnCheckedChangeListener. Thanks to how inheritance works in Java, Activity automatically becomes an instance of OnCheckedChangeListener once the interface has been implemented.
It looks like this was the option you were going for since you're also overriding the onCheckedChange() method inside your Activity.

What is the best way to organize android development code?

I know that every situation will be different, but I just wanted see if there was a general recommendation.
Currently, I have my activities (screens) dynamically creating custom button objects and custom edit text objects. Each of these objects have listeners to see if their state has changed. These object classes have all the logic for the screen. The activity's only job is to assign objects to the widgets I created in XML.
Part of me thinks it should be opposite, where the activity contains all the logic for all the widgets on the screen and simply waits for the objects to notify it when the listeners go off.
Which way is more "standard" ?
I use the following way. I have a common EventHandler sub class in every activity or fragment and I add a single instance belonging to activity to each UI item. EventHandler implements OnClickListener, OnChanged.., and so on.
I would also recommend looking at this library, if you are familiar with DI concept:
https://github.com/roboguice/roboguice/
Here is an example of code of mine:
package com.x.y;
public class DashboardActivity extends FragmentActivity {
private EventHandler eventHandler = new EventHandler();
#SomeAnnotationForInit(R.id.some_id)
private Button feedButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard_activity);
initGui();
}
private void initGui() {
feedButton.setOnClickListener(eventHandler);
}
private class EventHandler implements View.OnClickListener {
#Override
public void onClick(View view) {
if(view.equals(...)) {
//TODO:
}
}
}
}

How do I get orientation working for one specific fragment of my app?

Before marking this question as duplicate, I would like to specify that I have a strange requirement I am running through. My app is supported by both phones and tablets, BUT orientation is supported ONLY by tablets and not by phones. I have restricted it to work only for tablets. But now, i have a strange issue, where I need to enable orientation in just one fragment in the phone. In my case say xfragment within x module. How do I go about that without removing the restriction on phones for not supporting orientations. Is there any other way around it?
Currently in my fragment utils I am doing this:
public static void setActivityOrientation(Activity activity){
boolean tabletSize = MyUtils.isTablet(activity);
if (tabletSize) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
} else {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
and calling it oncreate at base activity:
FragmentUtils.setActivityOrientation(this);
How do I make it work only for that particular fragment? Any ideas? thanks!
If xfragment occupies whole screen, you can use the same code above in proper life cycle of xfragment as
public class XFragment extends android.support.v4.app.Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!MyUtils.isTablet(getActivity())){
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
}
}
#Override
public void onPause() {
super.onPause();
if(!MyUtils.isTablet(getActivity())){
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
}
Case 2:
If XFragment is not in full screen and you have multiple fragment, Then only way is to call detach and attach XFragment manually.

How to call (and use) onOptionsItemSelected from MainActivity in other classes

I'm making my very first Android application but I ran into a problem.
I have over 8 different classes which all use the same actionbar.
Now in place of calling the method in every different class (and having a lot of double code) I would like to call the method of the main class in my other classes.
This is a part of my code for the onOptionsItemSelected in main.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.actionbar_new_income:
Intent newIncome = new Intent(this, NewIncome.class);
this.startActivity(newIncome);
return true;
}
}
Now I was wondering how I could call the method in another class (newIncome.java)
I have this so far, but it keeps saying I need to add arguments. And I ofcourse need to be able to detect which menuitem is clicked..
MainActivity main = new MainActivity();
main.onOptionsItemSelected();
Any help please?
Thanks!
You should not do this. If you have common code then put it in a class (not an activity) that is accessible by any activity that needs it.
You will still have some duplication but this is normal.
A good way of reducing activity launch code is to add a static method to each activity that you can call which launches the activity it is in.
E.g in your NewIncome Activity you could have
Public static void Launch(Context c) {
Intent newIncome = new Intent(c, NewIncome.class);
C.startActivity(newIncome);
}
You could then launch this activity from any other activity just by calling
NewIncome.Launch(this);
If required you can add parameters to the method and then add Extras to the Activity using these parameters.
You can do it like the following example if your menu entries are totally independent of the activity in which they are contained:
In each activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return CommonClass.HandleMenu(this, item.getItemId());
}
In a common class
public class CommonClass {
public boolean HandleMenu (Context c, int MenuEntry) {
switch (MenuEntry) {
case R.id.actionbar_new_income:
NewIncome.Launch(c);
etc....
...
}
}
If your 8 classes are activities you may define a base activity with the onOptionsItemSelected which is the one where you put the elements in the actionbar you want. Then make the other activities derive from it.

Categories

Resources