I'm using a PreferenceFragment and want the version of my app to appear below the List that holds all the settings. I know for a normal ListView I could just do addFooterView(theTextView), but the PreferenceFragment doesn't provide access to doing this (no access to the ListView that is populated from preferences xml). Anyone have a slick way of doing this?
George's answer (Option 1) was on the right track, but I found it better to take it one step further than simply adding a preference. I created a custom preference by using the layout in the accepted answer here, and then was able to totally customize what I wanted to show by centering text, and even making the preference only appear under certain circumstances (for example, if the user has the most recent version, don't show the preference, etc.).
I used the Android docs on Settings to create a class that extends Preference to do the dynamic work needed.
You can add an empty preference item in the end of xml, then give it an custom layout.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- other preferences -->
<Preference app:layout="#layout/place_holder" />
</PreferenceScreen>
In your PreferencesFragment, just new a custom PreferenceCategory, then add it to PreferenceScreen. you can do your things in the onCreateView part of the custom PreferenceCategory.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
initPrefs();
PreferenceCategory preferenceCategory = new PreferenceCategory(getActivity()) {
#Override
protected View onCreateView(ViewGroup parent) {
super.onCreateView(parent);
View view =
getActivity().getLayoutInflater().inflate(R.layout.layout_infos, null);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "thanks", Toast.LENGTH_SHORT).show();
}
});
return view;
}
};
getPreferenceScreen().addPreference(preferenceCategory);
}
Related
I would really like to set the layout depending on which combination of two checkboxes are selected. Since there are four possible states, I have four layouts to display items underneath the checkboxes, if selected. I have made this work using four classes, but there must be a more efficient way to do this.
Basically, I would like to have drop-down EditTexts for user input displayed under the checkboxes only if they are selected. If the setContentView statements are replaced with the commented ones, I can cycle through any combination of checkboxes, but as the code is, only one layout change is able to be made and I don't understand why. Please help with any suggestions.
**I realize the CompoundButton object is unused here.
public class First extends Activity implements OnCheckedChangeListener{
CheckBox emailBox,smsBox;
#Override public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
emailBox=(CheckBox)findViewById(R.id.checkBox_1);
smsBox=(CheckBox)findViewById(R.id.checkBox_2);
emailBox.setOnCheckedChangeListener(this);
smsBox.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton compound,boolean isChecked){
if(!emailBox.isChecked()&&!smsBox.isChecked()){
setContentView(R.layout.activity_1);
// Toast.makeText(First.this,"None Checked",Toast.LENGTH_SHORT).show();
}
if(emailBox.isChecked()&&!smsBox.isChecked()){
setContentView(R.layout.activity_2);
// Toast.makeText(First.this,"Email Checked",Toast.LENGTH_SHORT).show();
}
if(smsBox.isChecked()&&!emailBox.isChecked()){
setContentView(R.layout.activity_3);
// Toast.makeText(First.this,"Sms Checked",Toast.LENGTH_SHORT).show();
}
if(emailBox.isChecked()&&smsBox.isChecked()){
setContentView(R.layout.activity_4);
// Toast.makeText(First.this,"Both Checked",Toast.LENGTH_SHORT).show();
}
}
}
More efficient way will be use fragments instead of changing layouts :)ORGroup Controls in layouts and set thier visiblity to View.GONE then set visiblility for appropriate group to View.Visible
To display a drop-down info box (like a text view) you can place e.g a lable which is empty(or rather you hide it) under these two check boxes and check for changes in checkboxes and then change the lable I mentioned earlier in runtime using java part of the code to display what u want. I hope I've got your point and this will help you.
I was able to solve this issue very simply by wrapping the relevant portions of the layout in a vertical LinearLayout and toggling between visibility='gone' and visibility='visible' as indicated in the code below; so I eventually thought I should come back here to share (if anyone has a simpler, more efficient method, perhaps let me know?)
public class MainActivity extends Activity implements OnCheckedChangeListener{
CheckBox emailBox,smsBox;
LinearLayout portion_1,portion_2,portion_3;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
portion_1=(LinearLayout)findViewById(R.id.hider_1);
portion_2=(LinearLayout)findViewById(R.id.hider_2);
portion_3=(LinearLayout)findViewById(R.id.hider_3);
emailBox=(CheckBox)findViewById(R.id.checkBox_1);
smsBox=(CheckBox)findViewById(R.id.checkBox_2);
emailBox.setOnCheckedChangeListener(this);
smsBox.setOnCheckedChangeListener(this);
}
public void onCheckedChanged(CompoundButton compound,boolean isChecked){
if(!emailBox.isChecked()&&!smsBox.isChecked()){
portion_1.setVisibility(View.GONE);
portion_2.setVisibility(View.GONE);
portion_3.setVisibility(View.GONE);
emailBox.setChecked(false);
smsBox.setChecked(false);
}
if(emailBox.isChecked()&&!smsBox.isChecked()){
portion_1.setVisibility(View.VISIBLE);
portion_2.setVisibility(View.GONE);
portion_3.setVisibility(View.VISIBLE);
emailBox.setChecked(true);
smsBox.setChecked(false);
}
if(smsBox.isChecked()&&!emailBox.isChecked()){
portion_1.setVisibility(View.GONE);
portion_2.setVisibility(View.VISIBLE);
portion_3.setVisibility(View.VISIBLE);
emailBox.setChecked(false);
smsBox.setChecked(true);
}
if(emailBox.isChecked()&&smsBox.isChecked()){
portion_1.setVisibility(View.VISIBLE);
portion_2.setVisibility(View.VISIBLE);
portion_3.setVisibility(View.VISIBLE);
emailBox.setChecked(true);
smsBox.setChecked(true);
}
}
}
In my preferences UI, I need to hide a preference based on certain conditions.
public class MyFragment extends PreferenceFragment {
public void onCreate(Bundle state) {
addPreferencesFromResource(...);
ListPreference myList = (ListPreference) findPreference("myid");
...
if (condition) {
// hide myList
}
}
}
I cannot seem to find any method either on ListPreference or on PreferenceFragment to hide it from being shown in the UI. Would appreciate if you can point me in the right direction.
After much debugging, turns out it was quite simple. Here is what you need to do:
First, obtain the PreferenceCategory the item belongs to. Next, just call removePreference on it.
PreferenceCategory myCategory = (PreferenceCategory) findPreference("myPrefCategory");
myCategory.removePreference(myList);
For those wondering how to remove item that has no parent category - you should name your root like this:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="root_preferences">
<PreferenceCategory
android:title="Developer tools"
android:key="dev_tools_category">
Then for example:
if (!BuildConfig.DEBUG) {
PreferenceScreen rootPreferences = (PreferenceScreen) findPreference("root_preferences");
PreferenceCategory devCategory = (PreferenceCategory) findPreference("dev_tools_category");
rootPreferences.removePreference(devCategory);
}
In example above "dev_tools_category" could be any preference widget or like I showed - a category.
You can just use the inherited methods from the Preference superclass.
myList.setShouldDisableView(true);
myList.setEnabled(false);
The first will make the backing view of the preference disappear when the preference is disabled, the second will actually disable the preference.
I am new to android and would like to know as to why thiswont work.If i have a set of images in my res folder and i want to display them based on users choice such that the Mainactivity is like below:-
public class Activity3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.drawable.img1);//here i put the image name
}
}
Suppose i use this code snippet
public class Activity3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(R.id.text1=='1')
setContentView(R.drawable.img1);
else
setContentView(R.drawable.img2);
}
}
This doesnt work.But i would like to know why,how is the android stuff really working.This does seem to me logically right.
You can't set drawable to setContentView, but you can do this instead:
setContentView(R.layout.MyLayout);
ImageView view = (ImageView)findViewById(R.id.myviewid);
view.setImageResource(R.drawable.img1);
In each click add view.setImageResource(R.drawable.img1); to change the imageView resources.
if you see more carefully the documentation, can see the definition for setContentView method:
public void setContentView (int layoutResID)
if you note, the method receive a layoutResId, the key word is layout, remember that android used the logic (view - controller), an activity is the controller and a layout is the view. You can have visual elements in your view, like an image (in android can be contained in an ImageView).
For example, you can define an xml layout to the next way:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/selected_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</ImageView>
this xml only contain an ImageView, in this ImageView with the id selected_image you will show the choice image.
In your activity you need put the next code (imagine the xml layout call image_layout.xml):
public class Activity3 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_layout);
//here you need find the ImageView in this layout
((ImageView) findViewById(R.id.selected_image).setImageResource(R.drawable.img1);
}
}
I know this is a very basic question, however as a newbie i cant get to work around it.
So, I want to have multiple activities to use same the xml layout(consist for example of 1 imagebutton, and multiple textviews with different IDs). Now, for every activity, I want them to view the same layout but override the views with data unique to every activity. What is the best way to do this? And also, the imagebutton should open different URLs in a video player(youtube links).
And can somebody tell me what is the most practical way to learn android programming?
UPDATE
This is my current code:
public class TemakiActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contentviewer);
}
}
For example I have a textview with ID "descriptionviewer", and a button with ID "videolink", now, how do you code those in?
You can share the same layout file and the set the attributes for views in the onCreate(..) method of each activity.
If you want a different URL to open for each image button you could set it at runtime as follows
public void onCreate(Bundle b) {
Button button =(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//different action for each activity
}
});
}
Yes you can! I had multiple activities inflate the same layout but they save different shared preferences.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.same_layout);
TextView urlDesc = (TextView)findViewById(R.id.descriptionviewer);
urlDesc.setText("url_1"); //now in other activities-- urlDesc.setText("url_2");
ImageButton aButton = (ImageButton)findViewById(R.id.videolink);
aButton.setOnClickListener(aButtonListener);
}
private OnClickListener aButtonListener = new OnClickListener() {
public void onClick(View v) {
// go open url_1 here. In other activities, open url_x, url_y, url_z
finish();
}
};
Same code just swapping the text you want to set for the TextView and url to open in OnClickListener(). No more to change.
I would like to get View instance that is used to display specific Preference in my
PreferenceActivity, so i can modify its properties, for example:
public class SettingsActivity extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference);
Preference pref = findPreference("key");
pref.getView().setVisibility(View.GONE);
//not necessarily setVisibility, i hope you get my point
}
}
I only found this method:
getView (View convertView, ViewGroup parent). But it seems confusing to me, that if i want to get View of my preference, i need to provide view and viewGroup as parameters??
Could someone explain how to use this method, or point me to another method to get View from my Preference instance.
PS: if possible, i would rather NOT extend Preference class, but i dont mind it if necessary
To get the view of a desired preference you can use this:
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
Preference preference=findPreference(“preferenceKey”);
View preferenceView=getListView().getChildAt(preference.getOrder());
//Do your stuff
}
Note: You can not do this in the onCreate method because it will throw a NullPointerException.
PreferenceActivity inherits the ListActivity class. ListActivity has a method called getListView() which returns the ListView that displays your preferences.
EDIT: Here is the code in my comment formatted:
getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
// ... put listener code here
});
I'm not sure on how to get the view for a preference, but if you want to remove the view from the screen (set visibility to View.gone) you can use the following:
getPreferenceScreen().removePreference(thePreference)