How to add PreferenceActivity to fragment activity - android-studio

I have a class Fragment1 1) and I want to extend also PreferenceActivity to add 2) so as to include in my fragment 1) a menu preference.any ideas on how to implement it
1.
public class Fragment1 extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
if(container == null){
return null;
}
return inflater.inflate(R.layout.fragment_main1, container, false);
}
}
2)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pred_general);

Related

How to edit a TextView in a fragment from MainActivity

I have a textview in a fragment that I want to edit in MainActivity.java (The parent activity). I've tried many methods such as creating a function in the fragment code:
public class HomeFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
return root;
}
public void status(String txt){
TextView textView = getView().findViewById(R.id.status_message);
textView.setText(txt);
}
}
and calling it in MainActivity like so:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HomeFragment fragment = new HomeFragment();
fragment.status("Test");
}
}
But this results in the app crashing. How can I fix this? I am using the Bottom Navigation Activity Template.
Best thing I would advice is to create your fragment instance in your main activity like this:
public class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HomeFragment fragment =
HomeFragment.createInstance("Test");
}
}
And then in your fragment:
public class HomeFragment extends Fragment {
private string text;
public View onCreateView(#NonNull LayoutInflater
inflater, ViewGroup container, Bundle
savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home,
container, false);
TextView textView =
root.findViewById(R.id.status_message);
return root;
}
public static HomeFragment createInstance(String text)
{
HomeFragment f =new HomeFragment ();
Bundle bundle=new Bundle();
bundle.putString("key", text);
f.set arguments(bundle);
return f;
}
#Override
public void onCreate(#Nullable Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getArguments != null) {
text= getArguments.getString("key");
status(text);
}
}
public void status(String txt){
textView.setText(txt);
}
}
Hopes this helps
Fixed this by accessing MainActivity from the fragment rather than accessing Fragment from MainActivity. Instead of changing the textview from MainActivity, send the string you want to put in the textview through a method and change the textview in the fragment.
MainActivity.java
public class MainActivity extends AppCompatActivity{
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public String sendData(){
return text;
}
}
HomeFragment.java
public class HomeFragment extends Fragment {
TextView tv;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
tv = root.findViewById(R.id.textview);
MainActivity ma = (MainActivity)getActivity();
tv.setText(ma.sendData());
return root;
}
}

Passing context for a radio group in a fragment instead of activity?

I'm trying to use a RadioGroup widget inside a fragment instead of an activity. I'm getting an error on the following line of code:
RadioButton button = new RadioButton(this);
I'm not sure what to pass instead of 'this'
I believe the problem is either in the snippet above or here:
RadioGroup group = (RadioGroup) getView().findViewById(R.id.radioGroup);
Full code:
public class fragment1 extends Fragment {
private Button btnNextFrag;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
createRadioButtons();
return view;
}
private void createRadioButtons() {
RadioGroup group = (RadioGroup) getView().findViewById(R.id.radioGroup);
String[] questions1 = getResources().getStringArray(R.array.question_1_answers);
for (int i=0;i<questions1.length;i++){
String question = questions1[i];
RadioButton button = new RadioButton(this);
button.setText(question);
group.addView(button);
}
}
}
The constructor requires context so use getActivity() instead of this. You can also use getContext().
public class fragment1 extends Fragment {
private Button btnNextFrag;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
createRadioButton(view);
return view;
}
private void createRadioButton(View view) {
RadioGroup group = (RadioGroup)view.findViewById(R.id.radioGroup);
String[] questions1 = getResources().getStringArray(R.array.question_1_answers);
for (int i=0;i<questions1.length;i++){
String question = questions1[i];
RadioButton button = new RadioButton(getActivity());
button.setText(question);
group.addView(button);
}
}
}

Android Studio - same menu for all activities (Navigation Drawer)

I'm new to Android Studio (and also pretty new to javascrips), so this might be a stupid question.
I do not want to maintain the same code more than once if possible, which is why I am trying to create a navigation-menu, to be used in all my activities.
So far I have created a "BaseActivity" that contains all the functions for my menu. This one works fine.
Now my problem:
In my other activities I can not get it to show up (not without issues anyway). It seems to me like it overwrites the layout.
My base ("BaseActivity"):
public class BaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.string.Open, R.string.Close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
NavigationView NavigationView=(NavigationView)findViewById(R.id.navigation_view);
NavigationView.setNavigationItemSelectedListener(this);
}
#Override
public void setContentView(int layoutResID)
{
super.setContentView(R.layout.activity_base);
ViewGroup content = (ViewGroup) findViewById(R.id.navigation_view);
getLayoutInflater().inflate(layoutResID, content, true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if(id==R.id.nav_Home) {
startActivity(new Intent(this, MainActivity.class));
}
else
if(id==R.id.nav_Activity1) {
startActivity(new Intent(this, Activity1.class));
}
return super.onOptionsItemSelected(item);
}
}
If I change the app to start through this activity the menu works, but the layout seems to be overwritten (it's empty).
When I try to run it normally (MainActivity), I get some issues.
MainActivity:
public class MainActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }}
The menu-icon is there, but I can not click on it.
If I remove the code in my MainActivity it works as before (with the layout problem).
I have tried something like this:
public class MainActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ViewGroup content = (ViewGroup) findViewById(R.id.navigation_view);
getLayoutInflater().inflate(R.layout.activity_main, content, true);
}}
Not working well. The layout from MainActivity seems to be implemented in the menu. It looks very strange.
Any ideas how to solve my problem?

How to pass data from activity to fragment?

I'm creating a basic app and i stucked at this point where I'm unable to pass data from an activity to fragment.
EmailPage.java
public class EmailPage extends AppCompatActivity{
ImageButton btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emailpage);
btn = (ImageButton) findViewById(R.id.emailButton);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(EmailPage.this, YourName.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
}
});
}
}
Email Page
NavHeaderMainMenu1.java
public class NavHeaderMainMenu1 extends Fragment {
public NavHeaderMainMenu1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.nav_header_main_menu1, container , false);
}
}
NavHeaderMainMenu1

ActionBar.Tablistener deprecated

I'm trying to build a swipe tabs view by using fragment activity and actionbar.tablistener. I have referenced other people tutorial and do it, however, it keep on deprecated and do not successful run it. Then I changed to AppComBatActivity and it do not works, show viewgroup error which I don't know why. THen I changed to ActionBarActivity, it worked finally, but no matter how I edit my tab activity, in java class and xml too, it does not show anything.
I have actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS) deprecated as well.
I have attached my code as following...
fragmentmenu.class
public class FragmentMenu extends FragmentActivity implements ActionBar.TabListener {
ActionBar actionBar;
ViewPager viewPager;
FragmentPageAdapter ft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_menu);
viewPager = (ViewPager)findViewById(R.id.pager);
ft = new FragmentPageAdapter(getSupportFragmentManager());
actionBar = getActionBar();
viewPager.setAdapter(ft);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("Menu").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Report").setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("Setting").setTabListener(this));
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}
}
</code>
FragmentPagerAdapter
<code>
public class FragmentPageAdapter extends FragmentPagerAdapter {
public FragmentPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new MyActivity();
case 1:
return new Report();
case 2:
return new Setting();
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
</code>
One of the fragment
<code>
public class Report extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.report, container, false);
}
}
}}
Please help!
ActionBar.Tablistener is deprecated. You need to use the TabLayout instead.
Check this tutorial for more infos :
http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

Resources