I'm completely new to Java and Android..
While using actionbarsherlock, I'm having a problem that I can't solve.
My app has around 8 Activities in total, and each of them has the SAME action bar,
using ABS.
So I was wondering if there is a way to keep the whole ABS part in 1 class,
and then call it in other activities when needed. Or else, I would have to write the same code in each activity to reach the same action bar, which really doesn't look correct.
I remember before using ABS, I had to use a separate row in XML then inflate it in other activities when needed. But this whole ABS project seems too vast for a newbie like me and I'm
really getting confused.. can any one help me clarify? Thanks in advance.
Use a base class which sets up the action bar.
public abstract class BaseActivity extends SherlockFragmentActivity {
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar ab = getSupportActionBar();
ab.whatever....
}
}
And then in your activities...
public class MySweetActivity extends BaseActivity {
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.whatever);
}
}
Related
im learning Android programming with Android Studio. A few days ago we made a Spinner and filled it with a list of different countries, who were stored as an String-Array in the Strings.xml file.
To implement the ArrayAdapter we used the createFromRessource() method:
adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_list_item_1);
Today i learned how to fill a ListView with items. But now, the teacher implemented the String array first, and then the Arrayadapter with new ArrayAdapter ....
public class ConstraintLayout extends AppCompatActivity {
ListView list;
ArrayAdapter <String> adapter;
String countries[];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_constraint_layout);
list = findViewById(R.id.list);
countries = getResources().getStringArray(R.array.countries);
adapter = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, countries);
list.setAdapter(adapter);
I dont understand two things: The first example would work with the ListView too, and its less code than the second one. Why would someone use the second one for making the ArrayAdapter?
Why do i need Generics here? The ArrayAdapter can uses Strings anyway or not?
Tried to llok for an answer on Google.
The code is here:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button push_me,secondButton;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
push_me = findViewById(R.id.push_me);
secondButton = findViewById(R.id.secondButton);
push_me.setOnClickListener(this);
secondButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.push_me:
textView.setText(R.string.Someone_pushed);
break;
case R.id.secondButton:
textView.setText(R.string.hello_android);
break;
}
}
if I take out the break (the two breaks down in the program) the program isn't working properly but I can not understand why.
From the "case" label that applies onwards, your code is executed until the switch block is ended (with a }), or until it is left with break (or, depending on context, return, throw, continue, possibly some other things I'm forgetting right now). That's just how it works in Java (and many other C-influenced programming languages).
This behavior is somewhat similar to that of labels and goto instructions, which predate "modern" structured programming. As those have fallen completely out of use unless you have to write Assembly by hand, this behavior is not intuitive for most programmers today, but it lives on as one of those weird traditions, similar to the 80-character line limit which goes all the way back to punchcards.
I use MapBox in android studio and show a point in map. Now I want to have a function to get a LatLng variable as an input and show that point on the map.(I want to have a function outside of onMapReady, which, with the call of the function, send points to the function as input and within the function, points appear on the map.). Please guide me
private MapView mapView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, YOUR_MAPBOX_ACCESS_TOKEN);
setContentView(R.layout.activity_main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(#NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
#Override
public void onStyleLoaded(#NonNull Style style) {
// Map is set up and the style has loaded. Now you can add data or make other map adjustments
}
});
}
});
So, Simple. Write below code in onMapReady method and Use this mapboxMap variable to add the marker point on Map.
LatLng latLng = new LatLng(20.5992, 72.9342);
mapboxMap.addMarker(new MarkerOptions().position(latLng).setTitle("set title of marker point"))
public static class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
private final ItemListActivity mParentActivity;
private final List<DummyContent.DummyItem> mValues;
private final boolean mTwoPane;
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
DummyContent.DummyItem item = (DummyContent.DummyItem) view.getTag();
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(WebpageDetailFragment.ARG_ITEM_ID, item.id);
WebpageDetailFragment fragment = new WebpageDetailFragment();
fragment.setArguments(arguments);
mParentActivity.getSupportFragmentManager().beginTransaction()
.replace(R.id.webpage_detail_container, fragment)
.commit();
This happens when you change the default item names during the wizard of creating the activity.
just rename the ItemListActivity to the name of your Activity . ( the name of the file that has that code error).
use refactor rename to change it everywhere it's mentioned.
If the ItemListActivity (or whatever you ended up calling it) extends from Activity and not AppCompatActivity then try changing the line
mParentActivity.getSupportFragmentManager().beginTransaction()
to
mParentActivity.getFragmentManager().beginTransaction()
To illustrate what I mean with this, it is similar to WhatsApp, where various options are displayed in the toolbar when a chat is selected.
I have a similar layout, so a MainActivity with Fragments containing RecyclerViews. Now when an item in a RecyclerView is selected I would like to get a similar behaviour as in WhatsApp. The RecyclerViews have an Adapter that implements an OnClickListener.
However, from this Adapter I do not seem to have access to Views from the MainActivity. I tried the following (inside the OnClick method in the Adapter), but it did not work since the view could not be found.
View view = getActivity().findViewById(R.id.toolbar_main_activity);
if( view instanceof Toolbar) {
Toolbar toolbar = (Toolbar) view;
toolbar.setTitle("TestTitle");
}
Does anyone know how to get the intended behavior or have a reference to a tutorial?
UPDATE: for who is also stuck with this and this is still quite confusing, here is how I solved it in my own words
My Fragment contains the Interface by adding the following code to it;
OnItemsSelected mCallBack;
public interface OnItemsSelected {
void onToolbarOptions(String title);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
mCallback = (OnItemsSelected) getActivity();
}
Also I passed 'mCallback' to the adapter like this;
MyAdapter adapter = new MyAdapter(myList, mCallback);
The RecyclerView adapter implements OnClickListener. In the OnClick method I called; 'mCallBack.onToolbarOptions("someTitle");'. And finally I made my MainActivity implement the method; 'implements myFragment.onItemsSelected' and I added the following code to it also;
#Override
public void onToolbarOptions(String title) {
toolbar.setTitle(title);
}
With this, only the title is changed, but from this it is quite easy to make other changes to the toolbar, such as changing the menu items.
Inside your Fragment you make an Interface and a global variable like this:
OnItemsSelected mCallBack;
public interface OnItemsSelected {
public void onToolbarOptions();
}
Then when in your RecyclerView items are selected or clicked you call:
mCallBack.onToolbarOptions();
In your Activity implement the Interface like this plus the method onToolbarOptions():
public static class YourActivityName extends AppCompatActivity
implements YourFragmentName.OnItemsSelected {
public void onToolbarOptions(){
// CHANGE YOUR TOOLBAR HERE
}
//.....OTHER STUFFS IN YOUR ACTIVITY
}