I have a preference fragment with a preference screen hierarchy. I would like to add a custom layout to define an "about the author" section, adding imageview to an element and a link (intent to browser). I searched but I didn't find any resources about this topic.
In your preference xml file (res/xml/prefs.xml), add a Preference with a custom layout:
<Preference
android:layout="#layout/custom_preference"/>
Example of layout/custom_preference.xml with an ImageView and a TextView with a link that will be opened in the browser:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_book" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="http://www.mylink.com"
android:autoLink="all"/>
</LinearLayout>
It's the last preference in the screenshot:
This is just a follow up to the answer by Carmen (30 October 2016):
If you want to dynamically change the properties on your custom layout, after the screen has loaded, you cannot use something like this:
PreferenceScreen customPref = (PreferenceScreen) findPreference("customPreference");
View prefRow = customPref.getView(null, null);
ImageView imageView = (ImageView)prefRow.findViewById(R.id.image_view);
imageView.setImageResource(R.drawable.whatever);
Although it won't crash, it will not actually do anything. It's odd and confusing.
You have to use a custom class that extends android.preference.Preference and override onBindView() to change properties on the custom layout. The technique is explained here: How to manage custom preference layout [Note: For PreferenceFragmentCompat, you have to extend androidx.preference.Preference and override onBindViewHolder()]
Another way is to use getView() but ensure that you use View view = super.getView(convertView, parent);: Android Set Custom Preference Layout
Related
This may seem like a duplicate but I can't find an answer anywhere.
It seems everyone has "TextView" available to edit in their activity xml file, whereas I only have the following:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/liv1"
android:layout_below="#+id/lt1"
android:layout_marginTop="29dp"
android:layout_alignRight="#+id/lt1"
android:layout_alignEnd="#+id/lt1"
android:layout_alignLeft="#+id/lt1"
android:layout_alignStart="#+id/lt1"
/>
Using android:gravity = centerdoes nothing when I put it in between the "ListView" brackets.
So what is it I should do?
You should create a custom ListView adapter that fits your needs, and also create an inflater's xml layout and align the text there whatever you want.
More info here:
Custom Adapter for List View
http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/
You should set gravity in your item, that you using in your adapter. And there modify your textView like this way:
<TextView
android:id="#+id/textView"
android:layout_width="match_parent" <= set this
android:layout_height="wrap_content"
android:text="#string/invalid"
android:gravity="center" <=this your gravity
/>
Any body please tell me how we can decrease or increase size of text in Tab button . Is it possible to do so?? I want use some large text in App.
Please help me.....
Thanks in advance
When we add tabs to our TabHost, we use TabHost.newTabSpec() to create a TabSpec object. Using the default tab look, we would call setIndicator(String, Drawable) on the TabSpec (passing the text and image we want displayed on the tab), and then call setContent(intent) on the TabSpec, passing an intent for an Activity we want to be used as that tab’s content. However, in this case, we want to use a custom layout for the tabs, and we want to simply use the Views defined in our tab_activity_layout.xml for the content, instead of individual Activities.
For custom tabs, first, we must define the custom layout of our tabs as a layout resource file. Then we will programatically inflate a View object using that layout, set whatever attributes we want, and pass it into TabSpec.setIndicator(View). Here is a very simple example layout, and some of the code to use it: Please check the link
Custom View for Tab
If you use the code yourEditText.setTextSize(16);
or if you use the .xml android:textSize="16sp"
You just could make your custom tab buttons and it is quite easy and straightforward:
Create a normal like xml layout for the tab. For example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/rlayout"
android:gravity="center_vertical|center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon4"
android:src="#drawable/somedrawable"
android:layout_height="42dp"
android:layout_width="42dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="News"
android:textSize="12dp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:paddingBottom="1dp"
android:layout_below ="#+id/icon4"/>
</LinearLayout>
Then in your tab activity class infalte this layout, like this:
LayoutInflater inflater0 = LayoutInflater.from(this);
View view0 = inflater0.inflate(R.layout.tabbuttonlayout, null);
Afterwards just use it normally in the tab host:
th = getTabHost();
th.addTab(th.newTabSpec("tag0").setIndicator(view0).setContent(intent0));
I hope this helps to better customize your tab.
I'm a novice on the Android platform when it cames to development. However I'm going further from basic Views and I'd like to create something like the following buttons:
This is what I want to achieve. I first tought that a Button with a custom background would have sufficed. However I don't know any way to make that small darker line with the text inside. All of the image reacts like a button and gets highlighted when you touch it.
Can you help me?
If you look at the source code for Apollo you can see ArtistsFragment is not made up of Buttons but rather an inflated RelativeLayout created by a subclass of the SimpleCursorAdapter class.
Since any view can have an OnClickListener you can make any create a layout to look however you want and still have it act like a button:
// Or load it as an item from an existing layout.
View myView = this.getLayoutInflater().inflate(R.layout.anything);
myView.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// Do stuff.
}
});
Every segment with an image could be a Layout with the background set to the appropriate image. Then, you just put the button inside of the layout.
You have to use Framelayout or RelativeLayout. For example:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/your_drawabele" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="your_text" />
</FrameLayout>
I use this example for creating custom tab. I don't know how to add image in tab view.
I try adding ImageView element in tabs_bg.xml but image is not displayed, only text
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgTab"
android:background="#drawable/img"/>
Can someone help me with this?
Thanks
You must specify the android:src attribute for the ImageView:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgTab"
android:src="#drawable/img"/>
With no content (src), its size will be 0x0 (due to wrap_content).
Specifying the indicator (header) of a tab view is made from java code, usually chained:
myTabHost.newTabSpec(tag).setIndicator(myTab).setContent(intent);
where myTabHost is your TabHost instance, and myTab is a View instance that will be used as the header of this tab.
You can create your own tab: define its layout in xml and add all the views (image, text...) you need on it.
For reference (complete sample) see the update part of this answer.
The layout/tab.xml file contains the layout of the tab headers (including an icon too).
I have a preference activity which gets it's layout from the following XML. This makes a button appear at the bottom of the preferences.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.stealthcopter.nexus.nexusgl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView android:id="#android:id/list"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button android:text="This is a button on top of all preferences."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
However when open a nested PreferenceScreen inside it reverts to the showing just a listview, is there a simple way to keep the layout the same between the preference screens?
I tried PreferenceScreen.setLayoutResource(R.id.layout.main); but that just changes the view displayed before it is opened
Update
I fixed (Kludged) this by changing the nested PreferenceScreens into normal preferences and overriding their onclick methods to clear the preferences from the listview and reload the preferences from an appropriate XML file.
I believe that this is another symptom of the bug described here
A work around is to reset the layout each time a preference screen is opened.
What are you trying to achieve with the custom preference screen? The is no preference to take into account here? I also I'm pretty sure that preference layout have to be in a 'xml' folder not the 'layout'