pass string from startActivityForResult to onActivityResult in RecyclerView - android-studio

I want to upload image and pass string(UID) from startActivityForResult to onActivityResult, and get the string(UID) on onActivityResult.
The startActivityForResult in RecyclerView and onActivityResult in MainActivity.
I can upload image but can't get the string(UID), the string(UID) is null on onActivityResult.
The Null Message
I would appreciate if someone can help me with my questions.
Here is startActivityForResult in RecyclerView:
class ViewHolder extends RecyclerView.ViewHolder {
private TextView textView;
private Button UploadImageButton;
ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
UploadImageButton = itemView.findViewById(R.id.UploadImageButton);
UploadImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
intent.putExtra("UID", textView.getText().toString());
((Activity) context).startActivityForResult(intent, 6);
}
});
}
}
Here is onActivityResult in MainActivity:
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (resultCode == RESULT_OK) {
if (resultData != null) {
String UID = resultData.getStringExtra("UID");
Log.d("UID", UID);
}
}
}
The layout placement

Note
Your startActivityForResult() will only give result of the selected image in the form of Uri. Which means, whatever extras you sent via the intent is useless.
So to solve your issue, one thing you can do is first get the result of the startActivityForResult(), i.e. image Uri, then combine that with the "UID" and then pass these to he MainActivity.

Related

setLayout not inflating correct xml layout?

been alright so far. I try to use docs or find my issues.
However my mainActivity has 4 buttons. These buttons depending on which view is clicked passes the R.id.button. I pass this value into an intent which Activity2 uses bundle to get id integer. I then setContentView using this.
However im having issues getting correct view to inflate now, it seems like its loading an old layout. My aim is to inflate 3 separate layout on Activity2 depending which button was clicked.
I know my other other button does inflate another activity and get proper xml layout.
Why are my other 3 buttons not inflating on Activity2 properly?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button pizza;
Button hamburger;
Button icecream;
Button history;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pizza = findViewById(R.id.buttonPizza);
hamburger = findViewById(R.id.buttonBurger);
icecream = findViewById(R.id.buttonIceCream);
history = findViewById(R.id.buttonHistroy);
pizza.setOnClickListener(this);
hamburger.setOnClickListener(this);
icecream.setOnClickListener(this);
history.setOnClickListener(this);
}
#Override
public void onClick(View v){
int idView = v.getId();
switch(v.getId()) {
case R.id.buttonBurger : startOrderActivity(R.layout.burger_layout);
makeToast(idView);
break;
case R.id.buttonPizza : startOrderActivity(R.layout.pizza_layout);
makeToast(idView);
break;
case R.id.buttonIceCream : startOrderActivity(R.layout.icecream_layout);
makeToast(idView);
break;
case R.id.buttonHistroy : startOrderActivity(R.layout.history_layout);
makeToast(idView);
break;
}
}
private void startOrderActivity(int id){
if(id != R.layout.history_layout) {
Intent intentOrder = new Intent(this, MakeOrderActivity.class);
intentOrder.putExtra("layout", id);
startActivity(intentOrder);
}
else {
Intent intentOrder = new Intent(this, OrderHistoryActivity.class);
intentOrder.putExtra("layout", id);
startActivity(intentOrder);
}
}
public void makeToast(int id){
String txt = String.valueOf(id);
Toast newToast = Toast.makeText(getApplicationContext(),txt,Toast.LENGTH_LONG);
newToast.show();
}
}
public class MakeOrderActivity extends AppCompatActivity {
Button placeOrderButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getIntent().getExtras();
int layoutNum = data.getInt("layout");
setContentView(layoutNum);
placeOrderButton = findViewById(R.id.placeOrder);
}
#Override
protected void onStart() {
super.onStart();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.order, menu);
return true;
}
public void onCheckboxClicked(View view){
}
}
Answer is to create a View which is created by getInflator.
https://stackoverflow.com/a/17070408/8443090

Pass EditText value to ListView from two different activities

appreciate if you can help me here.
I am creating a To Do List, where the ListView is in the MainToDoList Activity with a "Add a new Task" button that takes the user to the second activity.
In the second activity, there is a Edit Text field where user can input the title of the task. Two buttons are "Save" and "Cancel"
My question is, how to pass the edit text value after pressing the save button to display it into the listview in the first activity.
First Activity:
public class Todolistactivity extends AppCompatActivity {
private Button btn;
private ListView list;
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.todolistactivity);
btn = findViewById(R.id.addTask);
list = findViewById(R.id.task_list);
}
private void addTask() {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(Todolistactivity.this, EditToDo.class));
}
});
}
Second Activity:
public class EditToDo extends Todolistactivity {
private static final String TAG = "EditToDo";
private Button save;
private Button cancel;
private EditText title;
#Override
protected void onCreate (Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.activity_taskedit);
save = findViewById(R.id.saveTask);
cancel = findViewById(R.id.cancelTask);
title = findViewById(R.id.taskTitle);
saveButton();
cancelButton();
}
private void saveButton(){
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent backToDo = new Intent
(getBaseContext(),Todolistactivity.class);
String titleEntered = title.getText().toString();
backToDo.putExtra("task", titleEntered);
startActivity(backToDo);
}
});
}
Use startActivityForResult
in your Todolistactivity class
private void addTask() {
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivityForResult(new Intent(Todolistactivity.this, EditToDo.class), 100); // 100 is request code.
}
});
}
//...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
if(resultCode == Activity.RESULT_OK){
String result = data.getStringExtra("task");
}
else if (resultCode == Activity.RESULT_CANCELED) {
//...
}
}
}
In Your EditToDo class
private void saveButton(){
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String titleEntered = title.getText().toString();
Intent intent = new Intent();
intent.putExtra("task", titleEntered);
setResult(Activity.RESULT_OK, intent);
finish();
}
});
}

how to Update ListView after deletion

Here I'm using Custom Adapter by implementing listAdapter to Support buttons in the list items. When i click the Delete button it deletes currect row. But i can't able to refresh this listView.
File Structure is MainActivity -> FragmentClass-> this Custom class for the listAdpater.
public class Category_ListView_Custom_Adapter extends BaseAdapter implements ListAdapter{
private Realm realm;
private ArrayList<String> list = new ArrayList<>();
private Context context;
public Category_ListView_Custom_Adapter(ArrayList<String> list, Context context){
this.list = list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int i) {
return list.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
View view1 = view;
realm = Realm.getDefaultInstance();
if(view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.category_listview_row, null);
}
TextView listItemText = (TextView) view.findViewById(R.id.item);
listItemText.setText(list.get(i));
ImageView edit = (ImageView) view.findViewById(R.id.image);
ImageView delete = (ImageView) view.findViewById(R.id.delete);
edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// list.remove(i);
/// Toast.makeText(view.getContext(), "hey "+list.get(i).toString(), Toast.LENGTH_SHORT).show();
// notifyDataSetChanged();
Intent intent = new Intent(context, AddCategory.class);
intent.putExtra("category","expense");
intent.putExtra("subcategory", list.get(i));
context.startActivity(intent);
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RealmResults<Category> categorylist = realm.where(Category.class).equalTo("Id",i+1).and().equalTo("Subcategory",list.get(i)).findAll();
for (Category category : categorylist) {
realm.beginTransaction();
categorylist.deleteFirstFromRealm();
realm.commitTransaction();
try {
context.notifyAll();
}catch (Exception e){
Toast.makeText(context, e.toString(), Toast.LENGTH_SHORT).show();
}
// Expense.call_resume
}
}
});
return view;
}
}
Please Help me. Thank you.
You are .removing from a wrong view. Try to make root List<> static and do Class.list.get(i).remove or something like that.
Also, try to hide a view inside your get view.

Unable to find AppLovin SDK key. Please add meta-data android:name="applovin.sdk.key" android:value="YOUR_SDK_KEY_HERE" into AndroidManifest.xml

Hello everyone today I was busy with camera actions just applied rutin camera action on my application and apploving sdk key error actualy application is working but I need to know why I have this this warming on my android monitor
1. Here is my class where I start action and putExtra into intend
public class CameraActionFragment extends Fragment
{
private static final int REQUEST_CODE_IMG = 2;// for image
private static final int REQUEST_CODE_VIDEO = 1;// for video
public static final int RESULT_OK = -1;
Button btn_frag_camera_image;
Button btn_frag_camera;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_camera_action_3, container, false);
//return inflater.inflate(R.layout.fragment_camera_action_3, container, false);
btn_frag_camera_image = (Button) view.findViewById(R.id.btn_frag_camera_image);
btn_frag_camera = (Button) view.findViewById(R.id.btn_frag_camera);
// Image Action
btn_frag_camera_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Hello From Image", Toast.LENGTH_SHORT).show();
Intent intenImatToSec = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intenImatToSec, REQUEST_CODE_IMG);
}
});
// Camera Action
btn_frag_camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intenImatToSec = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intenImatToSec, REQUEST_CODE_VIDEO);
//intenImatToSec.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
//intenImatToSec.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 10);
//Toast.makeText(getActivity(), "Hello From Camera", Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_IMG) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
Intent intentBitMap = new Intent(getActivity(), DisplayImage.class);
// aldıgımız imagi burda yonlendirdiğimiz sınıfa iletiyoruz
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
intentBitMap.putExtra("byteArray", _bs.toByteArray());
startActivity(intentBitMap);
} else if (requestCode == REQUEST_CODE_VIDEO) {
Uri videoUrl = data.getData();
Intent intenToDisplayVideo = new Intent(getActivity(), DisplayVideo.class);
intenToDisplayVideo.putExtra("videoUri", videoUrl.toString());
startActivity(intenToDisplayVideo);
}
}
} }
1. And I play my video here
public class DisplayVideo extends Activity
{
private static final int REQUEST_CODE_VIDEO = 100;
VideoView videoView;
Button btn_cancel;
Button btn_send_cahallenge;
Button btn_image_play;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_video_activity);
videoView = (VideoView) findViewById(R.id.videoview_display_video_actvity);
btn_cancel = (Button) findViewById(R.id.btn_display_image_cancel);
btn_send_cahallenge = (Button) findViewById(R.id.btn_display_image_send_cahallenge);
btn_image_play = (Button) findViewById(R.id.btn_display_image_play);
// CameraActionFragmentten gelen uri
Bundle extras = getIntent().getExtras();
Uri myUri = Uri.parse(extras.getString("videoUri"));
videoView.setVideoURI(myUri);
btn_image_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
videoView.start();
}
});
btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences shp = getSharedPreferences("pref", MODE_PRIVATE);
SharedPreferences.Editor editor = shp.edit();
editor.putInt("frag_no", 2);
editor.commit();
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_VIDEO && resultCode == RESULT_OK) {
Uri videoUrl = data.getData();
videoView.setVideoURI(videoUrl);
//videoView.setMediaController(new MediaController(getApplicationContext()));
//videoView.requestFocus();
}
}
}
3. Cant find some usefull guide about AppLovin SDK key. Lately I
defined facebook sdk key and everything was good and after camera
actions ı have this problem
Error is
E/AppLovinSdk: Unable to find AppLovin SDK key. Please add meta-data android:name="applovin.sdk.key" android:value="YOUR_SDK_KEY_HERE" into AndroidManifest.xml.
07-29 16:24:26.390 12911-12911/? E/AppLovinSdk: Called with an invalid SDK key from: java.lang.Throwable:
at com.applovin.impl.sdk.AppLovinSdkImpl.a(Unknown Source)
at com.applovin.sdk.AppLovinSdk.b(Unknown Source)
at com.applovin.sdk.AppLovinSdk.c(Unknown Source)
at com.applovin.sdk.AppLovinSdk.b(Unknown Source)
at com.qihoo.security.SecurityApplication.onCreate(360Security:263)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1014)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4806)
at android.app.ActivityThread.access$1600(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1452)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:234)
at android.app.ActivityThread.main(ActivityThread.java:5526)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
you have an invalid SDK key you need to setup AppLovin correctly
please try to add:
<meta-data android:name="applovin.sdk.key"
android:value="YOUR_SDK_KEY_HERE"/>
to your AndroidManifest file inside application tag.
Please check below link to find AppLovin SDK Key
Click Here To open AppLovin's Key Window

How to implement onActivityResult within RecyclerView Fragment

I am trying to have a button within a Tab which is part of an activity for uploading images, when clicking the button inside one of the 2 Tabs presented in the activity ( Tab 1 pics, Tab 2 Vid ) the user could choose an image from the device.
My problem is implemneting the onClick Listener and onActivityResult which is not being used when placed after onClick, plus the #Override is being underlined as an error.
I guess i am not implementing the onClick the right way and not sure how to activate onActivityResult the proper way, i would appreciate any guidance;
MainActivty;
public class upload extends AppCompatActivity {
public String UserID;
private static final int REQUEST_SIGNUP = 0;
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload);
Firebase.setAndroidContext(this);
// Adding Toolbar to Main screen
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Setting ViewPager for each Tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
// Set Tabs inside Toolbar
TabLayout tabs = (TabLayout) findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
Adapter adapter = new Adapter(getSupportFragmentManager());
adapter.addFragment(new UploadPictures(), "Pictures");
adapter.addFragment(new UploadVideos(), "Videos");
viewPager.setAdapter(adapter);
}
static class Adapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public Adapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
And here is the Fragment Code;
public class UploadPictures extends android.support.v4.app.Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final RecyclerView recyclerView = (RecyclerView) inflater.inflate(
R.layout.recycler_view, container, false);
ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView picture;
public EditText tagEditText;
public Button tagCurLoc, choosePic, uploadContent;
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.pictue_upload, parent, false));
picture = (ImageView) itemView.findViewById(R.id.imgToUpload);
tagEditText = (EditText) itemView.findViewById(R.id.tagEditText);
tagCurLoc = (Button) itemView.findViewById(R.id.tagCurLoc);
choosePic = (Button) itemView.findViewById(R.id.choosePic);
choosePic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {
onActivityResult(requestCode, resultCode, data);
Toast.makeText(getActivity().getApplicationContext(), "Canceled ", Toast.LENGTH_SHORT).show();
} else {
if (requestCode == RESULT_LOAD_IMAGE) {
Uri selectedImgPath = data.getData();
picture.setImageURI(selectedImgPath);
RealFilePath = Uri.parse(String.valueOf(selectedImgPath));
Toast.makeText(getActivity().getApplicationContext(), " " + RealFilePath, Toast.LENGTH_SHORT).show();
}
}
}
});
uploadContent = (Button) itemView.findViewById(R.id.uploadContent);
}
}
/**
* Adapter to display recycler view.
*/
public class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
// Set numbers of List in RecyclerView.
private Context mContext;
public ContentAdapter(Context context) {
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return LENGTH;
}
}
Not sure if the onClick listener for the button should be implemented in the Adapter or the View holder, and from there the issue of onActivityResult cant be used ?
Weel, solved after some more reading and few try outs.
The onClick listener should be implemented at the ViewHolder class followed by
#Override
onActivityResult.
within the onClickListener onActivityResult invoked by;
startActivityForResult(intent, RESULT_LOAD_IMAGE);
and within onActivityResult;
onActivityResult(requestCode, resultCode, data);
Considering all of this taking place in an activity extending Fragment.

Resources