How to apply custom font to my ListView items? - android

This my main java code:
public class MainActivity extends ActionBarActivity {
String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
ListView listView = (ListView) findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
}
}
My activity_main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffff"
tools:context=".ListActivity" >
<ListView
android:id="#+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
my activity_listview.xml file:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"
android:textColor="#0060FF">
</TextView>

1.First make Custom ArrayAdapter
2.In custom ArrayAdaper override getView()
3.inflate TextView Layout in it.
4. Assign Your Custom Font which is in Your asset Folder
Typeface font1=Typeface.createFromAsset(getAssets(), "/fonts/UNDERCOV.ttf");
button_1=(Button)findViewById(R.id.button1);
button_1.setTypeface(font1);

Related

WearableListView is always null when I changed my wear layout to WatchViewStub from BoxInsetLayout

I have an app that was using BoxInsetLayout but the round layout wasn't working out so I have to change it to WatchViewStub.
I changed to code so that it uses either a round layout or a square layout and that works find. There was a WearableListView inside the original layout file which was just one file. Now it is 3 files, the main one, and the round or square one. Now the listview is always null even though there are no compile time errors.
My onCreate in the main activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_select);
WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
#Override
public void onLayoutInflated(WatchViewStub watchViewStub) {
mRectangleBackground = (RelativeLayout) findViewById(R.id.rectLayout);
mRoundBackground = (RelativeLayout) findViewById(R.id.roundLayout);
}
});
WearableListView listView = (WearableListView) findViewById(R.id.list); // this is always null
listView.setHasFixedSize(true);
listView.setAdapter(new SportAdapter(this));
listView.setClickListener(this);
}
My view select xml file (the WatchViewStub)
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="#layout/view_select_square"
app:roundLayout="#layout/view_select_round"
android:keepScreenOn="true"
android:id="#+id/stub"
android:background="#color/green_forest">
</android.support.wearable.view.WatchViewStub>
The square layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:padding="10dp"
app:layout_box="all"
android:id="#+id/rectLayout">
<TextView
android:id="#+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:fontFamily="sans-serif-thin"
android:paddingBottom="5dp"
android:text="#string/text_select_sport"
android:textColor="#color/white"
android:textSize="26sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text_title"
android:background="#color/white"
android:padding="4dp">
<android.support.wearable.view.WearableListView
android:layout_centerInParent="true"
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:dividerHeight="0dp"/>
</RelativeLayout>
</RelativeLayout>
Whenever I call listView anything like setAdapter, setHasFixedSize, I get a null reference error. Why is my listView always null?
Here is the xml file when it was BoxInsetLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout
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:keepScreenOn="true"
android:id="#+id/parent"
android:background="#color/green_forest">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
app:layout_box="all">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text_title"
android:text="#string/text_select_sport"
android:textSize="26sp"
android:textColor="#color/white"
android:fontFamily="sans-serif-thin"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:paddingBottom="5dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:padding="4dp"
android:layout_below="#+id/text_title">
<android.support.wearable.view.WearableListView
android:layout_centerInParent="true"
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:dividerHeight="0dp"/>
</RelativeLayout>
</RelativeLayout>
</android.support.wearable.view.BoxInsetLayout>
and the main activity before any changes
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_select);
WearableListView listView = (WearableListView) findViewById(R.id.list);
listView.setHasFixedSize(true);
listView.setAdapter(new SportAdapter(this));
listView.setClickListener(this);
}
So I fixed it by adding another method loadAdapter like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_select);
WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
#Override
public void onLayoutInflated(WatchViewStub watchViewStub) {
mRectangleBackground = (RelativeLayout) findViewById(R.id.rectLayout);
mRoundBackground = (RelativeLayout) findViewById(R.id.roundLayout);
listView = (WearableListView) findViewById(R.id.list);
loadAdapter();
}
});
}
private void loadAdapter() {
listView.setHasFixedSize(true);
listView.setAdapter(new SportAdapter(this));
listView.setClickListener(this);
}
as seen in this example here https://gist.github.com/PomepuyN/c30760eaee1e58fdd8fa
ugh...

How to change color of text inside ListView?

How can i change color of ListView Text(in drawer layout) in my code?
In my code listview is in various other layout.
I have tried various other code available in stackoverflow but none seemed to work for me
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/lltoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/lltoolbar"
android:text="#string/hello_world" />
</RelativeLayout>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_marginTop="55dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/drawerList"
android:entries="#array/abcd"
android:background="#FFFFFF"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left" />
</android.support.v4.widget.DrawerLayout>
private Toolbar toolbar;
TextView textView;
ImageView iv;
DrawerLayout drawerLayout;
private ListView listView;
private String[] names;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_bar);
textView = (TextView) findViewById(R.id.toolbar_title);
iv = (ImageView) findViewById(R.id.ivClick);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
listView = (ListView) findViewById(R.id.drawerList);
names = getResources().getStringArray(R.array.abcd);
listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, names));
listView.setOnItemClickListener(this);
}
you should create an xml layout for each row of your listview then create a class extend BaseAdapter and inside of getview inflate xml layout then set color for textview
Follow these steps:
Create an xml layout which will be shown in each row of your listview. (currently you are using the default android.R.layout.simple_list-item_1 layout) something like this:my_listrow_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sample text"
android:text_color="#006699"
android:id="#+id/list_item"/>
</LinearLayout>
2. in your activity code, change your list adapter code to this:
listView.setAdapter(new ArrayAdapter<>(this, R.layout.my_listrow_layout,
R.id.list_item, names));
my_listrow_layout is your custom made layout for the listview, R.id.list_item is the id of your textview in the listview layout. To change the color of the text add android:text_color="your desired color" value in the layout you just created. ( I have set it to #006699 which a dark blue color).

Why does listView addHeaderView() display nothing?

Why listView can't addHeaderView to itself?
I have added the params into the header rather than WRAP_CONTENT,MATCH_PARETN.
MainActivity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.lv);
LinearLayout head = (LinearLayout) getLayoutInflater().inflate(R.layout.layout, null);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(
200,200
);
head.setLayoutParams(params);
list.addHeaderView(head);
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:id="#+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
layout.xml
<?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="match_parent">
<ImageView
android:id="#+id/iv"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
What should i do?
Try to imprort LayoutParams from RelativeLayout class instead of AbsListView. I think it may be problem of Class casting.

ListFragment Textview unnecessary lines

ListFragment generates the following view :
I am trying to eliminate the white lines and nothing has helped so far. Any suggestions?
Layout code:
<TextView xmlns:android= " http: //schemas.android.com/apk/res/android"
android:id="#+id/Genere_list"
android:textAppearance="?android:attr/listViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:textColor="#android:color/white" />
The code used to create the view:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.test_album_view, Generes.GENERE_TITLES));
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setDivider(null);
setListAdapter(new ArrayAdapter<String>(getActivity(), R.layout.test_album_view, Generes.GENERE_TITLES));
}
You need to create an ListView.xml like the following:
<?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" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#f19000"
android:dividerHeight="1dip" >
</ListView>
</LinearLayout>
What you want to change are these lines:
android:divider="#f19000"
android:dividerHeight="1dip"
You can also use
android:divider="#android:color/transparent"
to make your lines transparent.
As said, you have to use this:
android:divider="#null"
android:dividerHeight="0dp"
In ListView:
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp">
</ListView>

Android: tabactivity - content of all tabs gets overlapped at first

I was testing TabActivity with a list in each tab.
While running the app, the contents of the tabs gets overlapped like this.
After i click on the tabs the overlapping gets cleared.
Here is my code :
testtabs.xml layout :
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<ListView
android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</FrameLayout>
</LinearLayout>
</TabHost>
And Test Activity
public class TabbedActivity extends TabActivity {
private static final String LIST1_TAB_TAG = "List1";
private static final String LIST2_TAB_TAG = "List2";
private ListView listView1;
private ListView listView2;
private TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testtabs);
tabHost = getTabHost();
// setup list view 1
listView1 = (ListView) findViewById(R.id.list1);
// create some dummy strings to add to the list
List<String> list1Strings = new ArrayList<String>();
list1Strings.add("List 11");
list1Strings.add("List 12");
list1Strings.add("List 13");
list1Strings.add("List 14");
listView1.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, list1Strings));
// setup list view 2
listView2 = (ListView) findViewById(R.id.list2);
List<String> list2Strings = new ArrayList<String>();
list2Strings.add("Test2 List 21");
list2Strings.add("Testing 22");
list2Strings.add("More test 23");
list2Strings.add("Test Again 24");
listView2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list2Strings));
// add views to tab host
tabHost.addTab(tabHost.newTabSpec(LIST1_TAB_TAG).setIndicator(LIST1_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView1;
}
}));
tabHost.addTab(tabHost.newTabSpec(LIST2_TAB_TAG).setIndicator(LIST2_TAB_TAG).setContent(new TabContentFactory() {
public View createTabContent(String arg0) {
return listView2;
}
}));
tabHost.setCurrentTab(0);
}
}
You can remove both ListView from xml layout and just create them in java code.
e.g. listView1 = new ListView(this);
Everything will be Ok.
Another solution I found is to add the following tag to both the listviews in the layout XML file:
android:visibility="invisible"
You are taking two ListView's in a FrameLayout that is the reason for your over-lapping.
If you want that you should have one ListView below the other keep the ListView's inside the LinearLayout like this,
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
<ListView
android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>

Categories

Resources