Skip to content

Instantly share code, notes, and snippets.

@Fbalashov
Last active July 4, 2016 20:11
Show Gist options
  • Save Fbalashov/a0bdc9df65128120ba1a0644b9dee78e to your computer and use it in GitHub Desktop.
Save Fbalashov/a0bdc9df65128120ba1a0644b9dee78e to your computer and use it in GitHub Desktop.
Code Styling
package com.fbalashov.spikingproject;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
/**
* Note this is not a fully functional class. It is intended to model how to design different elements of Java
* to achieve good styling.
*/
public abstract class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = AppCompatActivity.class.getSimpleName();
public static final String IMAGE_URL_EXTRA = "AppCompatActivity.ImageUrl";
private SearchView mSearchView;
private String imageUrl;
/**
* Returns the currently visible fragment
*/
abstract protected MainActivityFragment getCurrentFragment();
/**
* Starts the main activity, normally we would not have this method for an abstract activity class
*/
public static void startMainActivity(Context context, String imageUrl) {
Intent startIntent = new Intent(context, MainActivity.class);
startIntent.putExtra(IMAGE_URL_EXTRA, imageUrl);
context.startActivity(startIntent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageUrl = getIntent().getExtras().getString(IMAGE_URL_EXTRA);
View content = findViewById(android.R.id.content);
View text = findViewById(R.id.question_text_view);
content.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Toast.makeText(getApplicationContext(), R.string.edit_warning_message, Toast.LENGTH_SHORT).show();
return false;
}
});
text.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Log.d(LOG_TAG, "Ignoring click on content");
return true;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.search_menu, menu);
this.configureSearchView(menu);
return super.onCreateOptionsMenu(menu);
}
/**
* Configures the UI of the search view.
* @param menu the menu with the SearchView
*/
private void configureSearchView(Menu menu) {
MenuItem search = menu.findItem(R.id.search);
this.mSearchView = (SearchView)
MenuItemCompat.getActionView(search);
this.mSearchView.setSubmitButtonEnabled(false);
this.mSearchView.setIconifiedByDefault(true);
getCurrentFragment().setSearchViewListeners(mSearchView);
}
/**
* Children of the MainActivity should allow the activity to refresh their data and get their search views.
*/
interface MainActivityFragment {
/**
* forces fragment to refresh their data
*/
void reloadData();
/**
* Allows the child fragment to set listeners on the given search view.
*/
void setSearchViewListeners(SearchView searchView);
}
/**
* An inner class for loading an image from a url
*/
private class FetchImage extends AsyncTask {
private String imageUrl;
public FetchImage(String imageUrl) {
this.imageUrl = imageUrl;
}
@Override
protected Object doInBackground(Object[] objects) {
// fetch image from url
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
// update image
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment