I am using fonttype defined this way, and android studio's layout design view is showing the string/textview in the weathericon font.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryLight">
<TextView
android:id="#+id/tvFragThird"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:fontFamily="#font/weathericons"
android:text="#string/textview"
android:textSize="30sp" />
</RelativeLayout>
But,when I am running this piece of code:
public class ThirdFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_third, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragThird);
// Typeface typeface = ResourcesCompat.getFont(getContext(), R.font.weathericons);
// tv.setTypeface(typeface);
tv.setText("");//getArguments().getString("msg"));
return v;
}
I am just getting the text in normal font. I have tried both defining the fontfamily in layout and typeface in java, and none of them are working.
what I am missing?
As it's mentioned here https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml, custom font using fontFamily, needs API >= 26.
So change TextView tag to android.support.v7.widget.AppCompatTextView tag (if you use API < 26).
I had this problem and i came across above link and did above change and my problem is solved and i hope, it solves yours (if it has not been solved yet) and others.
Update:
As RobCo commented, if you can change to AppCompatActivity, you don't need above change. It's mentioned here : https://developer.android.com/reference/android/support/v7/widget/AppCompatTextView
Related
I'm new to Android, and I'm having a problem with checkboxes and textviews not refreshing at runtime. The TextView has to change color from light gray to a lighter gray, and the Checkbox has to change the custom drawable that it has from red to green. I have tried the detach and attach the fragment but it shuts down and maybe I don't know where to put the detach and attach in the fragment. I have tried invalidate() and requestLayout(), and it shuts down there too. I tried having them in onCreateView() and maybe I shouldn't put them there. I don't have an activity with my fragment. Here are the images that I need to change. What it has in the preview window is the green custom checkbox drawables but at runtime they are the red custom drawables which is what they were before I changed them to green, and the TextViews are a lighter gray in the preview window but they are a darker gray at Runtime which is what they were before.
public class CheckListFragment extends Fragment{
private RelativeLayout fragment_checklist;
private Fragment fragment = new CheckListFragment();
private Checkbox chkStart, chkDownload;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_checklist, container, false);
fragment_checklist = (RelativeLayout) rootView.findViewById(R.id.fragment_checklist_gather);
chkStart = (CheckBox) rootView.findViewById(R.id.chkStarted);
chkDownload = (CheckBox) rootView.findViewById(R.id.chkDownloaded);
//tried to explicitly make it green but doesn't work.
chkStart.setButtonDrawable(R.drawable.custom_green_checkbox);
chkDownload.setButtonDrawable(R.drawable.custom_green_checkbox);
fragment.getView().invalidate(); // tried these
fragment.getView().requestLayout();// tried these
//tried this. Maybe in the wrong spot.
FragmentTransaction ft = getFragmentManager.beginTransaction();
ft.detach(fragment);
ft.attach(fragment);
ft.commit();
return rootView;
}
}
<RelativeLayout xmlns:ads="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<CheckBox
android:id="#+id/chkStarted"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start"
android:layout_below="#+id/chkInProgess"
android:gravity="top"
android:padding="4dp"
android:checked="true"
android:enabled="false"
android:layout_marginTop="10dp"
android:button="#xml/custom_checkbox_green"/>
<CheckBox
android:id="#+id/chkDownloaded"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#+id/chkStarted"
android:text="#string/download"
android:gravity="top"
android:padding="4dp"
android:checked="true"
android:enabled="false"
android:button="#xml/custom_checkbox_green"/>
</RelativeLayout>
The textviews are not changing colors either at runtime, and I have tried to refresh the layouts, but they're not refreshing at runtime. They are what they need to be in the preview window, but they are not what they're supposed to be at runtime. Any help would be much appreciated. Thanks in advance!
I figured it out. I had read on some other stackoverflow pages that they set their textview to not visible and then visible. That didn't work for me, but I figured out that setting the checkboxes to unchecked and then checked again fixed it in the onCreateView.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_checklist, container, false);
chkStart = (CheckBox) rootView.findViewById(R.id.chkStarted);
chkDownload = (CheckBox) rootView.findViewById(R.id.chkDownloaded);
chkStart.setChecked(false);
chkStart.setChecked(true);
chkDownload.setChecked(false);
chkDownload.setChecked(true);
return rootView;
}
The problem was the checkboxes were already checked and that meant they were red and for it to appear green, it had to be unchecked then checked again. Also, I had other code that made it appear green. So, that was my solution.
I've been developing an app on Android 5.1.1, and everything works fine. But when I test it on a 4.0.4 device, none of the buttons display any text. Any idea why this would be?
Each button is the UI of a fragment. This is the layout, named just_a_button.xml:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/button_copper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:visibility="gone"/>
This is an example of how the fragments set up their views:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Button button = (Button) inflater.inflate(R.layout.just_a_button, container, false);
button.setText(R.string.member_info);
// set click listener omitted
return button;
}
The button's visibility is controlled by an event receiver. The button is shown as expected, and its click listener works. There's just no text on it.
Edit: The solution was to put the button inside a layout. I'm still curious if this requirement was ever documented, and why only the text seems to be affected when the button is the root view.
Instead of this
button.setText(R.string.member_info);
use
button.setText(getActivity().getResources().getString(R.string.member_info));
You are directly defining string resource, but setText() takes string as parameter.
Edit:
change you xml like below and then findviewbyid for your button
<?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" >
<Button
android:id="#+id/Mybutton"
style="#style/button_copper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:visibility="gone"/>
</LinearLayout>
In your fragment onCreateView() do as below :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.just_a_button, container, false);
Button button = (Button) view.findViewById(R.id.Mybutton);
button.setText(getActivity().getResources().getString(R.string.member_info));
// set click listener omitted
return view;
}
I've defined the following TextView:
<TextView
android:id="#+id/card12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="15dp"
android:padding="15dp"
android:background="#drawable/selector_card_background"
android:text="Go to Google"/>
And I'd like to open a webpage when clicking the TextView (which shows a text instead of the full url)
I've read a lot about this, but all the thing I've tried doesn't work for me. I've tried writing android:autoLink="web" and view.setMovementMethod(LinkMovementMethod.getInstance()); but this doesn't make the TextView clickable. I also tried via intent but for this I'd have to use Button instead of TextView.
Also say that I'm not using Activity, I'm using Fragment:
public class InformationFragment extends Fragment {
public InformationFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.information_layout, container, false);
return rootView;
}
}
Any ideas about how to do this? Thank you so much.
Try this:
TextView link = (TextView)findViewById(R.id.card12);
link.setText(Html.fromHtml("Go to Google"));
link.setMovementMethod(LinkMovementMethod.getInstance());
I have tried everything I can possibly think of, I have tried many solutions in Stack as well. None seem to resolve my issue. I am trying to pass a string that I passed from an earlier intent (parsed JSON data). Everytime I call set text I get a Null Pointer Exception, I have tried using a method to set the text as well as just setting it. Any assistance would be greatly appreciated.
This is the fragment I am trying to update.
public class HomeFragment extends Fragment{
TextView tv;
public HomeFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater lf = getActivity().getLayoutInflater();
View rootView = lf.inflate(R.layout.fragment_home, container, false);
tv = (TextView) rootView.findViewById(R.id.accountName);
tv.setText("test");
return rootView;
}
}
I just put a regular string in to test. Here is the XML.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/lblCompany"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp" />
</RelativeLayout>
Thanks In advance for absolutely any help.
This is the id set on your TextView in xml:
android:id="#+id/lblCompany"
This is the id you are searching for in onCreateView:
tv = (TextView) rootView.findViewById(R.id.accountName);
The ids need to match for you to update the text in that particular TextView. Try
tv = (TextView) rootView.findViewById(R.id.lblCompany);
instead.
How can I add a simple static header to my listview inside a listFragment? I want to create the header from an xml def and add it through inflation.
My onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View detailList = inflater.inflate(R.layout.detail_fragment, container, false);
View detailListHeader = inflater.inflate(R.layout.daily_sales_header, null, false);
container.addView(detailListHeader, 0);
return detailList;
}
This creates the header, but it is not above the listview, rather the listview appears underneath the header, ie the header is overlaying the listview.
Any hints on the correct implementation?
Putting hackbod's description into code for you since his answer created more questions before the answers came. Sometimes I just want the fish. I don't always need to know how the net is made...
To start with, create a layout that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myListViewWithHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:textStyle="bold"
android:textSize="20sp" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Then, in your onCreateView method you do this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.myListViewWithHeader, null);
return view;
}
The header can now be populated by doing this:
// get the header view
TextView headerView = (TextView) getView().findViewById(R.id.header);
headerView.setText("Header text goes here");
Notice that my header is a TextView, but it can be replaced with another view if you like. In that case you will need to do a getView().findViewById(R.id.xxxxx) for each view inside the header you want to work with
You should NEVER EVER be adding views directly to the container in onCreateView(). Please read the documentation: http://developer.android.com/reference/android/app/Fragment.html#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
Also see the various sample code in the Fragment documentation, as well as the API demos: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/index.html
There is nothing special about using a Fragment here. Just build a view hierarchy containing a ListView like you normally would in an Activity or elsewhere. You always need to return one View from onCreateView; this is the root of your hierarchy.
For example you could make the ListView and then use this to add a header to it: http://developer.android.com/reference/android/widget/ListView.html#addHeaderView(android.view.View)