0

I'm new here and also in android developing I'm making a news app which should change the url (from the news will be fetch) when the radio button is selected from the settings activity .

Settings activity have two radio button for English and for Hindi .

Here is LaunchActivity.java ( The method in which i want the string) In fetchBloodGroup() method i want to pass the string in place of APIConfig.Hindi

public void fetchBloodGroup() {
        if (mActivity.customMethods.isNetworkAvailable(mActivity)) {
            //mActivity.showHideProgressDialog(R.string.show_progress_dialog);
            StringRequest stringRequest = new StringRequest(Request.Method.GET,APIConfig.Hindi, new com.android.volley.Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Log.d("lkghldg", response);


                    try {
                        JSONObject mainJsonObject = new JSONObject(response);

                        JSONArray jsonArrayArticles = mainJsonObject.getJSONArray("items");


                        for (int i = 0; i < jsonArrayArticles.length(); i++) {

                            NewsModel newsModel = new NewsModel();
                            JSONObject jsonObject = jsonArrayArticles.getJSONObject(i);







                            newsModel.setAuthor(jsonObject.getString("author"));
                            newsModel.setTime(jsonObject.getString("pubDate"));


                            newsModel.setTitle(jsonObject.getString("title"));
                            newsModel.setDescription(jsonObject.getString("description"));
                            newsModel.setImage(jsonObject.getString("thumbnail"));
                            newsModel.setUrl(jsonObject.getString("link"));


                            newsList.add(newsModel);


                        }


                        adapter = new VerticlePagerAdapter(mActivity, newsList);
                        verticleViewPager.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }


                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {

            };

            stringRequest.setShouldCache(false);
            stringRequest.setRetryPolicy(new DefaultRetryPolicy(3000, 2, 2f));
            RequestQueue requestQueue = Volley.newRequestQueue(mActivity);
            requestQueue.add(stringRequest);


        } else {
            //   Snackbar.make(mActivity.coordinatorLayout, R.string.no_internet_connection, Snackbar.LENGTH_LONG).show();

            Toast.makeText(getActivity(), "No internet connection", Toast.LENGTH_LONG).show();

        }


    }

Here is the Settings.java Activity from i wanted the desired string to pass when radio button is selected

package com.santoshkumar.newsinshorts.Activities;
import android.os.Bundle;
import android.widget.RadioGroup;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import com.santoshkumar.newsinshorts.R;

public class Settings extends AppCompatActivity {

    private RadioGroup radiolanguage;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_activity);
        getSupportFragmentManager()
                .beginTransaction()
                .commit();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        radiolanguage = (RadioGroup) findViewById(R.id.radiolanguage);
        radiolanguage.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                switch(checkedId)
                {
                    case R.id.radio_eng:
                        break;
                    case R.id.radio_hindi:
                        break;
                    default:
                }
            }
        });


    }



}

In each case there will be separate string

Please help me Any type of help will be appreciated Thanks in advance

TECHones
  • 9
  • 2

2 Answers2

0

I don't think creating the settings like you did is a good choice. See the developer settings guide for a better solution. You'll see that the selected settings can also be stored in SharedPreferences and that you can get them from there pretty easy wherever you have a context.

Also I don't know if it's just code censoring or not but you probably also don't need an empty fragment transaction at the start.

CodeRed
  • 481
  • 4
  • 17
0

So to pass data between Activities you use Intent and add keys to the Bundle when passing from A to B. When passing from B to A you use startActivityForResult.

Here is an answer to a question like this one that explains all in depth - https://stackoverflow.com/a/45979367/2249224

PS - for a Settings page I would recommend holding the values in SharedPreferences or a Database and have ActivityA in onResume query the Database for the value and do what's needed.

Rafael Skubisz
  • 460
  • 3
  • 9