I am facing a strange problem in my material design app . Some thumbnails are opening and loading details activity as expected , but some are not opening instead there is crash happening . in this video u can see the problem I am facing .
I am attaching the link to my project ZIP file link with this ,My Project
this is the main activity ....
public class MainActivity extends AppCompatActivity implements ReaderAdapter.ReaderOnClickItemHandler {
public final static String READER_DATA = "reader";
public final static String POSITION = "position";
private final static String TAG = MainActivity.class.getSimpleName();
private static final String SAVED_ARRAYLIST = "saved_array_list";
private static final String SAVED_LAYOUT_MANAGER = "layout-manager-state";
private ApiInterface mApiInterface;
private List<Reader> mNetworkDataList;
#BindView(R.id.main_recycler_view)
RecyclerView mRecyclerView;
#BindView(R.id.main_linear_layout)
LinearLayout mErrorLinearLayout;
#BindView(R.id.main_progress_bar)
ProgressBar mProgressBar;
#BindView(R.id.toolbar_main)
Toolbar toolbar;
#BindView(R.id.main_reload_button)
Button mButton;
private ReaderAdapter mReaderAdapter;
private Parcelable onSavedInstanceState = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
if (null != toolbar) {
setSupportActionBar(toolbar);
toolbar.setTitle(getResources().getString(R.string.app_name));
}
mApiInterface = ApiClient.getApiClient().create(ApiInterface.class);
mReaderAdapter = new ReaderAdapter(this, this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
mRecyclerView.setAdapter(mReaderAdapter);
mRecyclerView.setLayoutManager(linearLayoutManager);
// getting the data from api using retrofit interface ApiInterface
if (savedInstanceState != null) {
onSavedInstanceState = savedInstanceState.getParcelable(SAVED_LAYOUT_MANAGER);
mNetworkDataList = savedInstanceState.getParcelableArrayList(SAVED_ARRAYLIST);
}
if (null == mNetworkDataList) {
loadData();
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadData();
}
});
}else {
loadAdapter();
}
}
public void loadData() {
final Call<List<Reader>> listCall = mApiInterface.getAllReaderData();
// now binding the data in the pojo class
listCall.enqueue(new Callback<List<Reader>>() {
//if data is successfully binded from json to the pojo class onResponse is called
#Override
public void onResponse(Call<List<Reader>> call,
Response<List<Reader>> response) {
Log.d(TAG, "Response : " + response.code());
mNetworkDataList = response.body();
loadAdapter();
}
//if data binding is not successful onFailed called
#Override
public void onFailure(Call<List<Reader>> call, Throwable t) {
//cancelling the GET data request
listCall.cancel();
showError();
}
});
}
private void loadAdapter() {
if (null != mNetworkDataList) {
showReaderList();
mReaderAdapter.ifDataChanged(mNetworkDataList);
if (onSavedInstanceState != null) {
mRecyclerView.getLayoutManager().onRestoreInstanceState(onSavedInstanceState);
}
}
}
/**
* this method is for showing the error textview and making all other views gone
*/
private void showError() {
mRecyclerView.setVisibility(View.GONE);
mProgressBar.setVisibility(View.GONE);
mErrorLinearLayout.setVisibility(View.VISIBLE);
}
/**
* this method is for showing the recyclerview and making all other views gone
*/
private void showReaderList() {
mRecyclerView.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
mErrorLinearLayout.setVisibility(View.GONE);
}
private int numberOfColumns() {
DisplayMetrics displayMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
// You can change this divider to adjust the size of the poster
int widthDivider = 400;
int width = displayMetrics.widthPixels;
int nColumns = width / widthDivider;
if (nColumns < 2) return 2;
return nColumns;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(SAVED_LAYOUT_MANAGER, mRecyclerView.getLayoutManager()
.onSaveInstanceState());
if (mNetworkDataList != null)
outState.putParcelableArrayList(SAVED_ARRAYLIST, new ArrayList<Parcelable>(mNetworkDataList));
}
#Override
public void onClickItem(int position, Reader reader, ImageView mImage, TextView mTitle) {
// Check if we're running on Android 5.0 or higher
Intent readerIntent = new Intent(this, ReaderDetailsActivity.class);
Bundle mBundle = new Bundle();
mBundle.putParcelable(READER_DATA, reader);
mBundle.putInt(POSITION, position);
readerIntent.putExtras(mBundle);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// Apply activity transition
ActivityOptionsCompat activityOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
this,
// Now we provide a list of Pair items which contain the view we can transitioning
// from, and the name of the view it is transitioning to, in the launched activity
new Pair<View, String>(mImage,
ReaderDetailsActivity.VIEW_NAME_HEADER_IMAGE),
new Pair<View, String>(mTitle,
ReaderDetailsActivity.VIEW_NAME_HEADER_TITLE));
ActivityCompat.startActivity(this, readerIntent, activityOptions.toBundle());
} else {
// Swap without transition
startActivity(readerIntent);
}
}
}
this is details activity ......
public class ReaderDetailsActivity extends AppCompatActivity {
private static final String TAG = ReaderDetailsActivity.class.getSimpleName();
private static final String SAVED_ARRAYLIST = "saved_array_list";
private static final String SAVED_LAYOUT_MANAGER = "layout-manager-state";
private final static String ARTICLE_SCROLL_POSITION = "article_scroll_position";
// View name of the header image. Used for activity scene transitions
public static final String VIEW_NAME_HEADER_IMAGE = "detail:header:image";
// View name of the header title. Used for activity scene transitions
public static final String VIEW_NAME_HEADER_TITLE = "detail:header:title";
private int position;
private Reader reader;
private int[] scrollPosition = null;
#BindView(R.id.scrollView_details)
ScrollView mScrollView;
#BindView(R.id.details_fragment_title)
TextView mTitle;
#BindView(R.id.imageView_details)
ImageView mImageView;
#BindView(R.id.textView_author_details)
TextView mAuthor;
#BindView(R.id.textView_published_date)
TextView mPublishDate;
#BindView(R.id.textView_description)
TextView mDescription;
#BindView(R.id.floatingActionButton_Up)
FloatingActionButton mFloatingActionButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader_details);
ButterKnife.bind(this);
Bundle bundle = getIntent().getExtras();
position=0;
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mScrollView.scrollTo(0,0);
}
});
ViewCompat.setTransitionName(mImageView, VIEW_NAME_HEADER_IMAGE);
ViewCompat.setTransitionName(mTitle, VIEW_NAME_HEADER_TITLE);
if (null != bundle) {
position = bundle.getInt(MainActivity.POSITION);
reader = bundle.getParcelable(MainActivity.READER_DATA);
if(null != reader) {
mTitle.setText(reader.getTitle());
mPublishDate.setText(reader.getPublishedDate());
mAuthor.setText(reader.getAuthor());
GlideApp.with(this)
.load(reader.getPhoto())
.into(mImageView);
mDescription.setText(reader.getBody());
}
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
scrollPosition = savedInstanceState.getIntArray(ARTICLE_SCROLL_POSITION);
if (scrollPosition != null) {
mScrollView.postDelayed(new Runnable() {
public void run() {
mScrollView.scrollTo(scrollPosition[0], scrollPosition[0]);
}
}, 0);
}
}
}
Json link I am parsing for this project .
Here is a screen recording of my project where u can see the problem I am facing , recording
this is a console log when I am trying to debug ....
when it is working fine the console log is 08/09 20:31:31: Launching app
No apk changes detected since last installation, skipping installation of /home/soumyajit/AndroidStudioProjects/MaterialReader/app/build/outputs/apk/debug/app-debug.apk
$ adb shell am force-stop lordsomen.android.com.materialreader
$ adb shell am start -n "lordsomen.android.com.materialreader/lordsomen.android.com.materialreader.activities.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Connecting to lordsomen.android.com.materialreader
Connected to the target VM, address: 'localhost:8601', transport: 'socket'
and when it is crashing the console log is
08/09 20:31:31: Launching app
No apk changes detected since last installation, skipping installation of /home/soumyajit/AndroidStudioProjects/MaterialReader/app/build/outputs/apk/debug/app-debug.apk
$ adb shell am force-stop lordsomen.android.com.materialreader
$ adb shell am start -n "lordsomen.android.com.materialreader/lordsomen.android.com.materialreader.activities.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -D
Connecting to lordsomen.android.com.materialreader
Connected to the target VM, address: 'localhost:8601', transport: 'socket'
Disconnected from the target VM, address: 'localhost:8601', transport: 'socket'
thanks in advance ..
Maximum Parcelable size should not be exceed 1mb. In you app it is 2.1 Mb. Without passing app date to the next activity you can try to pass item id and load data in next activity. Otherwise you can cache the list data and you can load the data from the local database in the details activity. If you cannot see crash log in android studio it because it set as "show only selected activity". In this case app get close and then this type of logs doesnot show in the android studio. switch that to No Filter and you can see the all logs.
Related
I want to load the clicked image in the next activity in fullscreen view. I don't know what I am doing wrong. The app is running completely fine but the image is not loading on the next screen. am new to android development and I thought I should start learning by doing a project. I am not understanding what is the problem in my coding.
public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.ImageViewHolder> {
private Context mCtx;
private List<Category> imageslist;
public CategoriesAdapter(Context mCtx, List<Category> imageslist) {
this.mCtx = mCtx;
this.imageslist = imageslist;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_images,parent,false);
return new ImageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ImageViewHolder holder, final int position) {
final Category images=imageslist.get(position);
Glide.with(mCtx).load(images.url).into(holder.imageView);
holder.imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(mCtx, LoadWall.class);
intent.putExtra("url", (Parcelable) imageslist.get(position));
mCtx.startActivity(intent);
}
});
}
#Override
public int getItemCount() {
return imageslist.size();
}
class ImageViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.image_view);
}
}
}
This is the activity where I want to load the image. But its not loading. Please Help
public class LoadWall extends AppCompatActivity {
ImageView imageView;
int myImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_load_wall);
imageView=findViewById(R.id.load_image);
get();
}
private void get(){
if(getIntent().hasExtra("url")){
String image=getIntent().getStringExtra("url");
set(image);
}
else{
Toast.makeText(this,"No Data",Toast.LENGTH_SHORT).show();
}
}
private void set(String image){
Glide.with(this).load(image).into(imageView);
}
}
Here you only need to pass URL as a String. Parcelable is not required.
intent.putExtra("url", imageslist.get(position).getUrl());
Option 1)
In your next activity you are looking for String.
String image=getIntent().getStringExtra("url");
in first activity in onClick() change your code to this.
Intent intent=new Intent(mCtx, LoadWall.class);
intent.putExtra("url", imageslist.get(position));
intent.startActivity(intent);
As i see imageslist.get(position) will return Category so you will need to add your url parameter. Something like this imageslist.get(position).getUrl();
Option 2)
If you want to get Category object then in next activity change to this
Category category = getIntent().getParcelableExtra("url");
Your imagesList contains data of type Category and you send an entry from this list to the next activity. However, the second activity expects a String to be received. It will check to see if it has an extra (it has - but not what you need), then try to retrieve that value as a String which will result in a null String. Having null, Glide will not load anything.
In other words, you made a small mistake on what data you use. When you load the data, you do it correctly by using the url field
final Category images=imageslist.get(position);
Glide.with(mCtx).load(**images.url**).into(holder.imageView);
But when you are passing it to the next activity, you send the entire object
intent.putExtra("url", **(Parcelable) imageslist.get(position)**);
Using the url field as you did in the first case will make your app work properly.
Start your Activity
Intent intent=new Intent(mCtx, LoadWall.class);
intent.putExtra("url", images.url);
mCtx.startActivity(intent);
I try to put advanced ads inside the dialog box when you close the application, but when you open a dialog box does not load the ad for the first time. ... I am worried that I load the ad inside the application
without appearing and at closing I put it in the dialog box for fear that the agent considers it a google violation to download the ad without its appearance
Constant code from android developer
public class MainActivity extends AppCompatActivity {
private static final String ADMOB_AD_UNIT_ID = "ca-app-pub-3940256099942544/2247696110";
private static final String ADMOB_APP_ID = "ca-app-pub-3940256099942544~3347511713";
AdLoader.Builder builder;
UnifiedNativeAdView adView;
private Button refresh;
private CheckBox startVideoAdsMuted;
private TextView videoStatus;
private UnifiedNativeAd nativeAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, ADMOB_APP_ID);
refresh = findViewById(R.id.btn_refresh);
startVideoAdsMuted = findViewById(R.id.cb_start_muted);
videoStatus = findViewById(R.id.tv_video_status);
refresh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View unusedView) {
refreshAd();
}
});
refreshAd();
}
/**
* Populates a {#link UnifiedNativeAdView} object with data from a given
* {#link UnifiedNativeAd}.
*
* #param nativeAd the object containing the ad's assets
* #param adView the view to be populated
*/
private void populateUnifiedNativeAdView(UnifiedNativeAd nativeAd, UnifiedNativeAdView adView) {
// Set the media view. Media content will be automatically populated in the media view once
// adView.setNativeAd() is called.
MediaView mediaView = adView.findViewById(R.id.ad_media);
adView.setMediaView(mediaView);
// Set other ad assets.
adView.setHeadlineView(adView.findViewById(R.id.ad_headline));
adView.setBodyView(adView.findViewById(R.id.ad_body));
adView.setCallToActionView(adView.findViewById(R.id.ad_call_to_action));
adView.setIconView(adView.findViewById(R.id.ad_app_icon));
adView.setPriceView(adView.findViewById(R.id.ad_price));
adView.setStarRatingView(adView.findViewById(R.id.ad_stars));
adView.setStoreView(adView.findViewById(R.id.ad_store));
adView.setAdvertiserView(adView.findViewById(R.id.ad_advertiser));
// The headline is guaranteed to be in every UnifiedNativeAd.
((TextView) adView.getHeadlineView()).setText(nativeAd.getHeadline());
// These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
// check before trying to display them.
if (nativeAd.getBody() == null) {
adView.getBodyView().setVisibility(View.INVISIBLE);
} else {
adView.getBodyView().setVisibility(View.VISIBLE);
((TextView) adView.getBodyView()).setText(nativeAd.getBody());
}
if (nativeAd.getCallToAction() == null) {
adView.getCallToActionView().setVisibility(View.INVISIBLE);
} else {
adView.getCallToActionView().setVisibility(View.VISIBLE);
((Button) adView.getCallToActionView()).setText(nativeAd.getCallToAction());
}
if (nativeAd.getIcon() == null) {
adView.getIconView().setVisibility(View.GONE);
} else {
((ImageView) adView.getIconView()).setImageDrawable(
nativeAd.getIcon().getDrawable());
adView.getIconView().setVisibility(View.VISIBLE);
}
if (nativeAd.getPrice() == null) {
adView.getPriceView().setVisibility(View.INVISIBLE);
} else {
adView.getPriceView().setVisibility(View.VISIBLE);
((TextView) adView.getPriceView()).setText(nativeAd.getPrice());
}
if (nativeAd.getStore() == null) {
adView.getStoreView().setVisibility(View.INVISIBLE);
} else {
adView.getStoreView().setVisibility(View.VISIBLE);
((TextView) adView.getStoreView()).setText(nativeAd.getStore());
}
if (nativeAd.getStarRating() == null) {
adView.getStarRatingView().setVisibility(View.INVISIBLE);
} else {
((RatingBar) adView.getStarRatingView())
.setRating(nativeAd.getStarRating().floatValue());
adView.getStarRatingView().setVisibility(View.VISIBLE);
}
if (nativeAd.getAdvertiser() == null) {
adView.getAdvertiserView().setVisibility(View.INVISIBLE);
} else {
((TextView) adView.getAdvertiserView()).setText(nativeAd.getAdvertiser());
adView.getAdvertiserView().setVisibility(View.VISIBLE);
}
// This method tells the Google Mobile Ads SDK that you have finished populating your
// native ad view with this native ad. The SDK will populate the adView's MediaView
// with the media content from this native ad.
adView.setNativeAd(nativeAd);
// Get the video controller for the ad. One will always be provided, even if the ad doesn't
// have a video asset.
VideoController vc = nativeAd.getVideoController();
// Updates the UI to say whether or not this ad has a video asset.
if (vc.hasVideoContent()) {
videoStatus.setText(String.format(Locale.getDefault(),
"Video status: Ad contains a %.2f:1 video asset.",
vc.getAspectRatio()));
// Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The
// VideoController will call methods on this object when events occur in the video
// lifecycle.
vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
#Override
public void onVideoEnd() {
// Publishers should allow native ads to complete video playback before
// refreshing or replacing them with another ad in the same UI location.
refresh.setEnabled(true);
videoStatus.setText("Video status: Video playback has ended.");
super.onVideoEnd();
}
});
} else {
videoStatus.setText("Video status: Ad does not contain a video asset.");
refresh.setEnabled(true);
}
}
/**
* Creates a request for a new native ad based on the boolean parameters and calls the
* corresponding "populate" method when one is successfully returned.
*
*/
private void refreshAd() {
refresh.setEnabled(false);
builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
// OnUnifiedNativeAdLoadedListener implementation.
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
if (nativeAd != null) {
nativeAd.destroy();
}
nativeAd = unifiedNativeAd;
FrameLayout frameLayout =
findViewById(R.id.fl_adplaceholder);
adView = (UnifiedNativeAdView) getLayoutInflater()
.inflate(R.layout.ad_unified, null);
populateUnifiedNativeAdView(unifiedNativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
VideoOptions videoOptions = new VideoOptions.Builder()
.setStartMuted(startVideoAdsMuted.isChecked())
.build();
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.setVideoOptions(videoOptions)
.build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
refresh.setEnabled(true);
Toast.makeText(MainActivity.this, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
videoStatus.setText("");
}
Now I'm trying to put the code refresh method insaid dialog box instead of refresh method
public void showdilog(){
builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);
builder.forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
// OnUnifiedNativeAdLoadedListener implementation.
#Override
public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
// You must call destroy on old ads when you are done with them,
// otherwise you will have a memory leak.
if (nativeAd != null) {
nativeAd.destroy();
}
nativeAd = unifiedNativeAd;
FrameLayout frameLayout =
findViewById(R.id.fl_adplaceholder);
adView = (UnifiedNativeAdView) getLayoutInflater()
.inflate(R.layout.ad_unified, null);
populateUnifiedNativeAdView(unifiedNativeAd, adView);
frameLayout.removeAllViews();
frameLayout.addView(adView);
}
});
VideoOptions videoOptions = new VideoOptions.Builder()
.setStartMuted(startVideoAdsMuted.isChecked())
.build();
NativeAdOptions adOptions = new NativeAdOptions.Builder()
.setVideoOptions(videoOptions)
.build();
builder.withNativeAdOptions(adOptions);
AdLoader adLoader = builder.withAdListener(new AdListener() {
#Override
public void onAdFailedToLoad(int errorCode) {
refresh.setEnabled(true);
Toast.makeText(MainActivity.this, "Failed to load native ad: "
+ errorCode, Toast.LENGTH_SHORT).show();
}
}).build();
adLoader.loadAd(new AdRequest.Builder().build());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
bulider.setView(adView);
builder.setMessage(R.string.onfirm_exit)
.setCancelable(false)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
What you're doing is that you're building and showing your dialog box at the same time. So, at the time you show your dialog box you're also loading your native ad.
You should BUILD your dialog box in advance, and don't call alert.show() in your build function.
So, call your buildDialog() in MainActivity in advance.
In your onBackPressed function call alert.show().
Hope it helps.
I am developing an application for Android Marshmallow (API LEvel 23).
I have an Activity which contains a ListView.
I populate that ListView by using a BaseView Adapter.
I want to start an Activity whenever I push a button inside my ListView.
However, this Activity needs to access the camera which in the latest Android version means that I should ask for the camera permission during runtime.
The camera using Activity will be an ZXing Activity.
Here is the Activity code:
public class ProviderListActivity extends AppCompatActivity {
public boolean can_access_camera = false;
public final int got_camera = PackageManager.PERMISSION_DENIED;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_provider_list);
ListView listView = (ListView) findViewById(R.id.ProviderListView);
CardsDBHelper dbHelper = new CardsDBHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
listView.setAdapter(new ProviderListAdapter(this, db));
}
public void start_scan(){
if (have_camera_permission()){
start_scan_activity();
} else {
Log.d("ProviderListActivity", "I CANNOT ACCESS THE CAMERA");
}
}
public void start_scan_activity(){
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
}
public boolean have_camera_permission(){
//Check for permissions
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if (permissionCheck == PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, got_camera);
} else {
can_access_camera = true;
}
return can_access_camera;
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case got_camera: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
can_access_camera = true;
} else {
can_access_camera = false;
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
The getView() method in my Adapter is:
public View getView(int position, View convertView, final ViewGroup parent) {
Button grid_button;
if (convertView == null) {
// if it's not recycled, initialize some attributes
grid_button = new Button(mContext);
grid_button.setText("Hello Text");
Drawable d = mContext.getDrawable(R.drawable.circle);
grid_button.setCompoundDrawablesRelativeWithIntrinsicBounds(null,d,null,null);
grid_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//What do I need to put inside here?
}
});
} else {
grid_button = (Button) convertView;
}
return grid_button;
}
You may have noticed that the Adapter gets an extra db argument.
This is a SQLite database object which is which is used inside the Adapter, but this is not relevant.
I am not sure how to call my scan Activity by using the onClick override.
I tried passing the parent Activity as an argument in the Adapter and call the scan method, but I get the following exception:
java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode`
Does anyone have a solution for my problem?
You are getting java.lang.IllegalArgumentException: Can only use lower 8 bits for requestCode Error because you are using -1(PackageManager.PERMISSION_DENIED) as requestCode for requestPermissions method.
To make it work use any requestCode between 1 to 255.
public final int got_camera = 24;
When I run my app in my mobile device, I get this under the logcat :
error opening trace file: No such file or directory (2) . Any one know what met be the cause? I tried to re-build the project and clean it under:
Project > Clean...
But I still get the same error. The app says unfortunately the you cant record on this device, of which is a message I have created under my String.xml files if the phone does not support recordings.
Any one with help please do so. I will be happy to get one.
Here is the code:
public class MainActivity extends Activity {
public static final String FILE_DIRECTORY = "iRecorded_Calls";
public ListView listView;
public ScrollView mScrollView;
public TextView mTextView;
public static final String LISTEN_ENABLED = "ListenEnabled";
private static final int CATEGORY_DETAIL = 1;
private static final int NO_MEMORY_CARD = 2;
private static final int TERMS = 3;
public RadioButton radEnable;
public RadioButton radDisable;
public static final int MEDIA_MOUNTED = 0;
public static final int MEDIA_MOUNTED_READ_ONLY = 1;
public static final int NO_MEDIA = 2;
private static Resources res;
private Context context;
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(this, HomeActivity.class));
res = getResources();
listView = (ListView) findViewById(R.id.mylist);
mScrollView = (ScrollView) findViewById(R.id.ScrollView02);
mTextView = (TextView) findViewById(R.id.txtNoRecords);
SharedPreferences settings = this.getSharedPreferences(LISTEN_ENABLED,
0);
boolean silent = settings.getBoolean("silentMode", false);
if (!silent)
showDialog(CATEGORY_DETAIL);
context = this.getBaseContext();
}
//Explorer the file directory
#SuppressWarnings("deprecation")
#Override
protected void onResume() {
if (updateExternalStorageState() == MEDIA_MOUNTED) {
String filepath = Environment.getExternalStorageDirectory()
.getPath();
final File file = new File(filepath, FILE_DIRECTORY);
if (!file.exists()) {
file.mkdirs();
}
final List<Model> listDir = ListDir2(file);
filepath = getFilesDir().getAbsolutePath();
final File file2 = new File(filepath, FILE_DIRECTORY);
if (!file2.exists()) {
file2.mkdirs();
}
final List<Model> listDir2 = ListDir2(file2);
listDir.addAll(listDir2);
if (listDir.isEmpty()) {
mScrollView.setVisibility(TextView.VISIBLE);
listView.setVisibility(ScrollView.GONE);
} else {
mScrollView.setVisibility(TextView.GONE);
listView.setVisibility(ScrollView.VISIBLE);
}
final CallsAdapter adapter = new CallsAdapter(this, listDir);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
adapter.showPromotionPieceDialog(listDir.get(position)
.getCallName(), position);
}
});
I managed to figured out what was the problem on my code and fixed it and now I can be able to recorder the incoming and outgoing calls. The problem was under my RecordSercive class on the recorder part
> //________Test with the Default outputFormat and default
> Encoder________
>
> /* recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
> recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
> recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);*/
>
> //_______End for the Test with the Default outputFortmat and default Encoder________
That is where I got the Message that says unfortunately the you cant record on this device
So I changed my code to this:
> recorder = new MediaRecorder();
> recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK + MediaRecorder.AudioSource.VOICE_UPLINK);//test using the addition sign
> for both DOWNLINK + UPLINK
> recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
> recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
> myFileName = getFilename();
> recorder.setOutputFile(myFileName);
Which worked fine for me, but the sound quality is is a problem, of which I will just have to increase the volume using the
> volume = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
but under the LogCat the on Eclipse when I debug my app, I still get the error opening trace file: No such file or directory (2). Of which I think it might be caused by other reasons of which I'm currently investigating like (Chris Stratton) mentioned.
In the latest version of Android they have clearly restricted working with a network of the main stream.
This had been done for UI not to break.
Possibly you raised the API version in the manifest.
While I would like to make everything programatically whenever I can, and leave XML blank, that is not possible where I work (designers will not work with raw program code...)
So, to that extend, almost every one of my activity that I work with or built contains a large block of findViewById at the start somewhere.
This is especially painful if I had to create it from scratch and I am now allowed to programatically create an array of buttons and add it to the layout... Thankfully they will let me do that if it gets to 10+ elements and they are all the same type.
Is there a way to avoid this? Or a way to make it automatically cast it into what I am assigning it to? So I don't need to type Button mButton = (Button) findview...
If there is some way to automagically generate java instances of buttons and what nots from the XML given the layout that would be great (esp if the names were nice and autocompleteable).
Try to use Android annotations
AndroidAnnotations is an Open Source framework that speeds up Android development. It takes care of the plumbing, and lets you concentrate on what's really important. By simplifying your code, it facilitates its maintenance.
Here is a simple example of how your code can dramatically shrink, and become much easier to understand:
Before Android Annotations
public class BookmarksToClipboardActivity extends Activity {
BookmarkAdapter adapter;
ListView bookmarkList;
EditText search;
BookmarkApplication application;
Animation fadeIn;
ClipboardManager clipboardManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN);
setContentView(R.layout.bookmarks);
bookmarkList = (ListView) findViewById(R.id.bookmarkList);
search = (EditText) findViewById(R.id.search);
application = (BookmarkApplication) getApplication();
fadeIn = AnimationUtils.loadAnimation(this, anim.fade_in);
clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
View updateBookmarksButton1 = findViewById(R.id.updateBookmarksButton1);
updateBookmarksButton1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
updateBookmarksClicked();
}
});
View updateBookmarksButton2 = findViewById(R.id.updateBookmarksButton2);
updateBookmarksButton2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
updateBookmarksClicked();
}
});
bookmarkList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView p, View v, int pos, long id) {
Bookmark selectedBookmark = (Bookmark) p.getAdapter().getItem(pos);
bookmarkListItemClicked(selectedBookmark);
}
});
initBookmarkList();
}
void initBookmarkList() {
adapter = new BookmarkAdapter(this);
bookmarkList.setAdapter(adapter);
}
void updateBookmarksClicked() {
UpdateBookmarksTask task = new UpdateBookmarksTask();
task.execute(search.getText().toString(), application.getUserId());
}
private static final String BOOKMARK_URL = //
"http://www.bookmarks.com/bookmarks/{userId}?search={search}";
class UpdateBookmarksTask extends AsyncTask {
#Override
protected Bookmarks doInBackground(String... params) {
String searchString = params[0];
String userId = params[1];
RestTemplate client = new RestTemplate();
HashMap args = new HashMap();
args.put("search", searchString);
args.put("userId", userId);
HttpHeaders httpHeaders = new HttpHeaders();
HttpEntity request = new HttpEntity(httpHeaders);
ResponseEntity response = client.exchange( //
BOOKMARK_URL, HttpMethod.GET, request, Bookmarks.class, args);
Bookmarks bookmarks = response.getBody();
return bookmarks;
}
#Override
protected void onPostExecute(Bookmarks result) {
adapter.updateBookmarks(result);
bookmarkList.startAnimation(fadeIn);
}
}
void bookmarkListItemClicked(Bookmark selectedBookmark) {
clipboardManager.setText(selectedBookmark.getUrl());
}
}
After:
#NoTitle
#Fullscreen
#EActivity(R.layout.bookmarks)
public class BookmarksToClipboardActivity extends Activity {
BookmarkAdapter adapter;
#ViewById
ListView bookmarkList;
#ViewById
EditText search;
#App
BookmarkApplication application;
#RestService
BookmarkClient restClient;
#AnimationRes
Animation fadeIn;
#SystemService
ClipboardManager clipboardManager;
#AfterViews
void initBookmarkList() {
adapter = new BookmarkAdapter(this);
bookmarkList.setAdapter(adapter);
}
#Click({R.id.updateBookmarksButton1, R.id.updateBookmarksButton2})
void updateBookmarksClicked() {
searchAsync(search.getText().toString(), application.getUserId());
}
#Background
void searchAsync(String searchString, String userId) {
Bookmarks bookmarks = restClient.getBookmarks(searchString, userId);
updateBookmarks(bookmarks);
}
#UiThread
void updateBookmarks(Bookmarks bookmarks) {
adapter.updateBookmarks(bookmarks);
bookmarkList.startAnimation(fadeIn);
}
#ItemClick
void bookmarkListItemClicked(Bookmark selectedBookmark) {
clipboardManager.setText(selectedBookmark.getUrl());
}
}