I am trying to use the following code
public int id=GeneralPreferenceFragment.getView().getParent().getId();
inside SettingAvtivity/MainActivity .
but it shows following error
Non-static method 'getView()' can not be referenced from a static Context
please help me resolving this issue
following is the method used
public void method1(View view)
{
Button bt1=(Button)findViewById(R.id.button);
bt1.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
// ViewGroup vg = (ViewGroup) mCurrentFragment.getView().getParent();
Toast.makeText(SettingsActivity.this,"Sound",Toast.LENGTH_SHORT).show();
DataSyncPreferenceFragment fragment;
GeneralPreferenceFragment fragment1;
GeneralPreferenceFragment frag1;
fragment= new DataSyncPreferenceFragment();
fragment1=new GeneralPreferenceFragment();
GeneralPreferenceFragment prefFragment = new GeneralPreferenceFragment();
int id = prefFragment.getView().getParent().getId();
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
ft.replace(R.id.layout_1id,fragment);
ft.remove(fragment1);
ft.add(R.id.layout_1id,fragment);
ft.addToBackStack(null);
ft.commit();
}
});
}
if i use an instance of General preferenceFragment then it shows
Cannot resolve getId();
code for header that links fragment-pref_header.xml
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<!-- These settings headers are only used on tablets. -->
<header
android:fragment="com.example.amit.check1.SettingsActivity$GeneralPreferenceFragment"
android:icon="@drawable/ic_info_black_24dp"
android:title="@string/pref_header_general"
android:id="@+id/id3"/>
<header
android:fragment="com.example.amit.check1.SettingsActivity$NotificationPreferenceFragment"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/pref_header_notifications" />
<header android:fragment="com.example.amit.check1.SettingsActivity$DataSyncPreferenceFragment"
android:icon="@drawable/ic_sync_black_24dp"
android:title="@string/pref_header_data_sync" />
</preference-headers>
SettingActivity.java
package com.example.amit.check1;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.support.annotation.IdRes;
import android.support.v7.app.ActionBar;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.telecom.Call;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import java.util.List;
public class SettingsActivity extends AppCompatPreferenceActivity {
//private int mcontainerid;
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
}
else if (preference instanceof RingtonePreference) {
if (TextUtils.isEmpty(stringValue)) {
preference.setSummary(R.string.pref_ringtone_silent);
} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));
if (ringtone == null) {
// Clear the summary if there was a lookup error.
preference.setSummary(null);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
}
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
private static boolean isXLargeTablet(Context context) {
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}
private static void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// int id=GeneralPreferenceFragment.getView().getParent().getId();
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onIsMultiPane() {
return isXLargeTablet(this);
}
@Override
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| GeneralPreferenceFragment.class.getName().equals(fragmentName)
|| DataSyncPreferenceFragment.class.getName().equals(fragmentName)
|| NotificationPreferenceFragment.class.getName().equals(fragmentName);
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
Preference preference = findPreference("launchFragment");
preference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
// launch fragment
// addPreferencesFromResource(R.xml.pref_notification);
// Toast.makeText(getActivity(),"hellp",Toast.LENGTH_SHORT).show();
return false;
}
});
setHasOptionsMenu(true);
bindPreferenceSummaryToValue(findPreference("example_text"));
bindPreferenceSummaryToValue(findPreference("example_list"));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class NotificationPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_notification);
setHasOptionsMenu(true);
bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class DataSyncPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_data_sync);
setHasOptionsMenu(true);
bindPreferenceSummaryToValue(findPreference("sync_frequency"));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
public void method1(View view)
{
Button bt1=(Button)findViewById(R.id.button);
bt1.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
// ViewGroup vg = (ViewGroup) mCurrentFragment.getView().getParent();
Toast.makeText(SettingsActivity.this,"Sound",Toast.LENGTH_SHORT).show();
DataSyncPreferenceFragment fragment;
GeneralPreferenceFragment fragment1;
GeneralPreferenceFragment frag1;
fragment= new DataSyncPreferenceFragment();
fragment1=new GeneralPreferenceFragment();
GeneralPreferenceFragment prefFragment = new GeneralPreferenceFragment();
// int id = prefFragment.getView().getId();
//String st=Integer.toString(id);
//Toast.makeText(SettingsActivity.this,st,Toast.LENGTH_SHORT).show();
// int id = frag1.getView().getParent().getId();
//ViewGroup vg = (ViewGroup) findViewById(id);
FragmentManager fm=getFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
//ft.add(1234,fragment,"f1");
//int id=fragment.getId();
//String s1=Integer.toString(id);
// Toast.makeText(SettingsActivity.this,s1,Toast.LENGTH_SHORT).show();
// ft.add(R.id.layout_1id,fragment);
//ft.add() will add fragment in previous fragment to
// the previous fragment
// ft.replace(R.id.layout_1id,fragment);
// ft.hide(fragment1);
//ft.hide() is used to hide an fragment;
// ft.detach(fragment1);
//detach method removes the fragment from UI.but it can be reused with the help
//of attach method.
//Toast.makeText(SettingsActivity.this,"removed frag1",Toast.LENGTH_SHORT).show();
//ft.show(fragment);
ft.remove(fragment1);
//remove method removes the fragment from UI;you can reuse fragment again,for reusing
//again you would have to use add method
ft.add(R.id.layout_1id,fragment);
ft.addToBackStack(null);
ft.commit();
}
});
}
}