LayoutInflater doesn't works in PreferenceFragment - android

I'm building a PrefenceFragment but I've a problem to inflate a custom layout within the fragment.
Pratically, I created a custom layout to inflate but doesn't works (the layout was not inflated). I'm trying some solutions but I don't understand where I wrong...
This is my sample code:
public class MyPreferenceFragment extends PreferenceFragment
{
private ListView listjob;
#Override
public void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
View viewlist = inflater.inflate(R.layout.pref_listview_job, null);
TextView txtProva = (TextView)viewlist.findViewById(R.id.txtProva);
txtProva.setText("This is a text");
}
}
This is my custom layout pref_listview_job.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtProva"/>
</LinearLayout>
Thanks!

If you extend PreferencesFragment, you need to consider doing two things:
Create an XML file describing your settings screen. This is not a standard layout file.
You need to use addPreferencesFromResource(R.xml.preferences) method to inflate this preferences XML file.
Here is a link to documentation with the XML file and method I mentioned:
http://developer.android.com/reference/android/preference/PreferenceFragment.html
If you need a custom preferences page, then you have two options. Either you use PreferencesFragment with customer preferences, or you extend a non-preference Fragment (e.g. ListFragment) and implement all preferences as standard views.

Related

including the content from 2 activities in 1 activity

I have 2 activities (lets call their layouts activity_one and activity_two). Both with seperate XML templates and seperate Activities.
in the onCreate(Bundle savedInstanceState) of one of them I am calling setContentView(R.layout.activity_one);
One of the subviews in this layout is a LinearLayout. I want to put the content of R.layout.activity_two within this LinearLayout and if possible use the code from its activity class (the onClickListeners etc).
The reason I want to do this is that the Tablet version of the app I am building should show data from both views.
Is this possible? How would I do this?
Thanks :).
You should use fragments for that, with fragments you will be able to have two separate layout files, two separate fragments - which will be quite similar to your current activities (its quite easy to convert activity to fragment), and also you will need additional activity that will include in itself those two fragments.
You can still have your current activities showing their layouts, just create FragmentActivity for each such activity, and show apropriate fragment.
Other solution might be with using layout include tag, that allows to include one layout into another.
btw. fragments were introduced to make it easier to build tablet versions (big screen) of application, while still being able to show UI version for phones (smaller screen).
// try this way here i just gave simple demo
**activity_one.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<Button
android:id="#+id/btnActivityOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity One"/>
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/activity_two"/>
</LinearLayout>
**activity_two.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnActivityTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity Two"/>
</LinearLayout>
**MyActivity.java**
public class MyActivity extends Activity {
private Button btnActivityOne;
private Button btnActivityTwo;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
btnActivityOne = (Button) findViewById(R.id.btnActivityOne);
btnActivityTwo = (Button) findViewById(R.id.btnActivityTwo);
btnActivityOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MyActivity.this,"Activity One Button",Toast.LENGTH_SHORT).show();
}
});
btnActivityTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MyActivity.this,"Activity Two Button",Toast.LENGTH_SHORT).show();
}
});
}
}
Simple work using actvities itself
For your first question
content of R.layout.activity_two within your LinearLayout
Use the <include ../> tag for that
Eg: <include layout="#layout/activity_two" /> within your LinearLayout
For your Second Question
if possible use the code from its activity class (the onClickListeners etc).
Make a class Extends Activity and add all the listeners you need in common and name the class as BaseActivity and extend BaseAcitivity instead of Activity for your 2 Activities.

How to display Android UI via XML Layout?

Currently I understand I can create a XML Layout and pass that to setContentView(...), or I can pass a Customized view to setContentView(...).
But what if I want to combine the elements of both? Is it possible to use a layout first, then to programmatically add to the UI via java code?
For example: How could I create a view that uses an Asset background picture with an added loading widget on top?
ADDED INQUIRY: Right now and I think of a View and a Layout as two things for setContentView to display. But can a View hold a layout within it to be displayed?
Yes its possible to set an XML layout with setContentView(), and programmatically add more views/widget to that layout. Here's a short example.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/background_image">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Some text"/>
<LinearLayout
android:id="#+id/custom_content_root"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- This is where we will add views programmatically -->
</LinearLayout>
</LinearLayout>
TestActivity.java
public class TestActivity extends Activity {
private LinearLayout mRoot;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the layout
setContentView(R.layout.main);
// Get the Linearlayout we want to add new content to..
mRoot = (LinearLayout) findViewById(R.id.custom_content_root);
// Create a TextView for example
TextView moreText = new TextView(this);
// Set the layout parameters of the new textview.
moreText.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
moreText.setText("More text :)");
// Add the new textview to our existing layout
mRoot.addView(moreText);
}
}
The result is an activity with background_image.png as background, and two textviews with text ;)
You can add any type of view (TextView, EditText, ImageView, Buttons etc) this way.
Yes, it is possible to add widgets after using setContentView(). It's also possible to inflate XML layouts yourself using LayoutInflater.
You could add the loading widget to a layout that was defined inside your XML by getting it using findViewById and then using a method from ViewGroup.

Android Can we add a footer view to a content view

I have a 'ParentActivity'
public class ParentActivity extends Activity
and all the activities of my application extends this ParentActivity
public class MainActivity extends ParentActivity
I have roughly 20 activities all extending ParentActivity and all activities have different layouts from each other(like some have LinearLayout ,some RelativeLayout,some ScrollView)
My app is almost complete,but my client now requires to have an adsense ad display at the end of each activity.
One general solution is to include following layout to all of my activity layouts
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dip"
android:orientation="vertical"
android:background="#color/header_bgcolor"
android:gravity="bottom"
android:layout_gravity="bottom">
<com.google.ads.GoogleAdView
android:id ="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
But what I want to do is on ParentActivity I will define a function which automatically adds the following view to end of the content view as follows
ViewGroup view =(ViewGroup)this.findViewById(android.R.id.content);
View adSenseView = View.inflate(this, R.layout.ad_sense_layout,view);
GoogleAdView adView =(GoogleAdView)adSenseView.findViewById(R.id.adView);
AdSenseSpec adSenseSpec = new AdSenseSpec("CLIENT_ID")
.setCompanyName("COMPANY NAME")
.setAppName("APP NAME")
.setChannel("Channel Id")
.setAdType(AdType.TEXT);
adView.showAds(adSenseSpec);
In this way I am able to add the adsense view at the bottom but it overlaps the lower end of the content view.So is there any way ,I can dynamically add a footerview to a content view of an activity.
I am still working on solution,and if I find any I will surely post.
Thanks in advance
Also, I wonder if some of the setters you are using have an xml equivilent, removing some of the need for the code?
Put your last code snippet in ParentActivity::onCreate and define a_child_activity::onCreate as { /* my create stuff */; super.onCreate(); }
Just a beginner so probably rubbish though.

Set a SeekBarPreference in the preferencemenu and not using dialog

I'm creating preference menu and trying to show the seekbar in the preference menu and not by showing a dialogbox.
Do I have to make it through a new xml layout file and new Preference class?
It's seems so much work in contrast to make a Dialog SeekBarPreference.
Any comments, tips, code snips or lenks would be appreciated.
What do you want exactly? Is something like this:
Edit:
The PreferenceActivity extends from the ListActivity, so the preference is essentially a listview. You can achieve this feature this way:
Create a PreferenceActivity and in the onCreate() method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addPreferencesFromResource(R.xml.pref);
}
In the "main" Layout, make sure there is a ListView, so that the your preference.xml
file will be linked to that ListView. Declare it this way:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Note the id for the ListView must be:#android:id/list

How to implement master and child page like Activity in Android?

I want to place a common banner and menu on each Activity with footer too.
Can anyone guide me how can I implement master and child page like asp.net in Android???
Any help would be appreciated.
You could have each of your Activities extend a common base class which has a onCreateOptionsMenu method which inflates the menu from the same XML each time. Though as you can't have multiple inheritance, this may be tricky when you want to have plain activities and list activities, for example.
Another way would be to have a Util class where you have a method like setupMenu(Menu) which each of your Activities can call if you're doing some more complex menu setup.
In terms of the XML UI layout for each of your Activities, you can include a common banner by using the <include/> tag.
The solution was pretty easy.
You need to extends "Activity" Class,in onCreate function SetContentView to your base xml layout and also need to override setContentView in base Activity Class
For Example:
1.Create "base_layout.xml" with the below code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#000000"
android:padding="15dp" >
<LinearLayout android:orientation="horizontal" android:background="#000000"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:minHeight="50dp" android:paddingLeft="10dp">
<ImageView android:layout_width="wrap_content" android:id="#+id/ImageView01"
android:adjustViewBounds="true" android:layout_height="wrap_content"
android:scaleType="fitCenter" android:maxHeight="50dp" />
</LinearLayout>
<LinearLayout android:id="#+id/linBase"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</LinearLayout>
</LinearLayout>
2.Create "BaseActivity.java"
public class BaseActivity extends Activity {
ImageView image;
LinearLayout linBase;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.base_layout);
linBase = (LinearLayout)findViewById(R.id.linBase);
}
#Override
public void setContentView(int id) {
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(id, linBase);
}
}
and
public class SomeActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.some_layout);
//rest of code
}
}
The only thing I noticed so far was that when requesting a progress bar (requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS)) this needs to be done before calling super.onCreate. I think this is because nothing can be drawn yet before calling this function.
This worked great for me and hopefully you will find this useful in your own coding.
I've had the same problem and solved it using ActivityGroup.
I suppose that menu items will move user to another activity, so with the same menu in every activity closing application with BACK button can be almost impossible (after some time user will have to go back through all activities he had ever seen).
I haven't found any good tutorials in english so have written mine some time ago (it's somewhat too short and in polish only, but Google Tranlslated version should be understandable) check this
You can also check how the TabHost works
ViewStub is the solution
activity_masterpage.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ViewStub android:id="#+id/stub_content"
android:inflatedId="#+id/subTree"
android:layout_width="match_parent"
android:layout_height="match_parent" />
stub = (ViewStub) findViewById(R.id.stub_content);
stub.setLayoutResource(R.layout.content_layout);
stub.inflate();

Categories

Resources