I have made a list view containing two childs. Now i am trying to append a series of children to each of these two childs.
I am using following procedure.
super.onCreate(savedInstanceState);
setContentView(R.layout.report_main_layout);
CharSequence[] dummyChar = {"a","b"};
listView = (ListView) findViewById(R.id.UIMainAccountListView);
adapter = new CustomArrayAdapterOfMainList(this, dummyChar);
listView.setAdapter(adapter);
In the custom adapter i am using following code.
if (position == 0)
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, debitAccounts,sumDebit);
viewHolder.listView.setAdapter(adapter);
}
else
{
viewHolder.listView = (ListView) view.findViewById(R.id.UIReportTrialBalanceListView);
adapter = new CustomArrayAdapterForChildList(context, creditAccounts, sumCredit);
viewHolder.listView.setAdapter(adapter);
}
All i am getting is like this, I want to extend it so that all of the accounts show, List view should wrap content but it is not wrapping content
This will stack two listviews and force them to take up half of their parent each.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="2">
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<ListView
android:id="#+id/list2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>
You can remove the cache color by setting android:cacheColorHint="#android:color/transparent" for each listview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingBottom="20sp" >
<ListView
android:id="#+id/child1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="#+id/child2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/child1" />
</RelativeLayout>
This layout for first child1 listview above the second child2 listview
Thanks
Related
the following code does not work as desired.
Does anyone here has a hint?
Thank you in advance,
Sven
Already played around by putting this in onCreate, by making listView, strings, arrayAdapter instance fields and so one. Did not helped.
Making it final or not did not helped.
Changing the length and/or/xor the array's content did not helped.
Activity class:
...
ListView listView = this.findViewById(R.id.listView);
final int n = 10;
String[] strings = new String[n];
for (int i = 0; i < n; i++) {
strings[i] = "X" + i;
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings);
Log.d(this, "#1 " + arrayAdapter.getCount());
listView.setAdapter(arrayAdapter);
Log.d(this, "#2 " + listView.getChildCount());
...
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:keepScreenOn="true"
tools:context=".AndroidMainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.9">
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="1.0">
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".33"
android:background="#007f00"
android:onClick="onClickStart"
android:text="#string/log_start" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".34"
android:background="#00007f"
android:onClick="onClickMark"
android:text="#string/mark" />
<Button
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight=".33"
android:padding="0dp"
android:background="#7f0000"
android:onClick="onClickStop"
android:text="#string/log_stop" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Instead of showing a list with 10 items, nothing is shown. Debug shows "#1 10" for first and "#2 0" for second statement. I've copied this 1:1 from another well working project incl. the layout XML.
I try to make a ScrollView to act like a ListView. So every row will be a TextView added dynamically. but the program crashes. I have a ScrollView inside a LinearLayout. What i do wrong? Thanks.
Here is my code
scroll = (ScrollView)findViewById(R.id.scrollView1);
layout = (LinearLayout) findViewById(R.id.LinearLayout1);
layout.setBackgroundColor(Color.TRANSPARENT);
TextView[] tx = new TextView[10];
for (int i = 0; i < 10; i++) {
tx[i] = new TextView(HelloWorldActivity.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tx[i].setText("This is the textviewNo" + i);
layout.addView(tx[i]);
}
scroll.addView(layout);
XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/timeflag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="50sp"
android:gravity="center"/>
<Button
android:id="#+id/pause"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/timeflag"
android:layout_marginTop="37dp"
android:text="Start" />
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ScrollView>
</LinearLayout>
</RelativeLayout>
A scrollView Can only have one child. If you try to add more than 1 child to the scroll view then it will crash. You should have the LinearLayout inside of the scrollview and then dynamically add textViews to the linear layout.
You are trying to add LinearLayout1 as its own child's child (being your own grandfather, is a confusing notion).
Change you XML around like this:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/pause"
>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
You cannot add views to a scrollview. You can only add them to a viewgroup, like linear or relativelayout.
It is not exactly clear what you are trying to achieve, but what is wrong with using a listview for this? You could set a max height on the listview. Or you could try wdziemia's suggestion
I want to have two buttons top of a listview. If I scroll below the listview I want that the buttons stay at top. I searched through many solutions on stackoverflow however I could not find the exact one for my case.
My layout xml(mainlayout.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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/latest_news_button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tracked_news_button" />
</LinearLayout>
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
The graphical layout produced is just what I wanted:
Here is my code to use this layout, in case I might fail to bind the Java code to layout;
public class MainActivity extends ListActivity{
Button latestButton;
Button trackedButton;
ListView lv = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
latestButton = (Button)findViewById(R.id.button1);
latestButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Latest Button!", Toast.LENGTH_LONG).show();
}
});
trackedButton = (Button)findViewById(R.id.button2);
trackedButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Tracked Button!", Toast.LENGTH_LONG).show();
}
});
this.lv = getListView();
String[] values = new String[]{"asdfa","qwerqwer","banbn"};
lv.setAdapter(new ArrayAdapter<String>(BilTrackerMainActivity.this, android.R.layout.simple_expandable_list_item_1, values));
}
however when this activity is called from my other activity my app unfortunately closed. LogCat says:
Your content must have a ListView whose id attribute is 'android.R.id.list'
But I try adding lv = (ListView)findViewById(R.id.mylist); instead of this.lv = getListView();. I could not solve this issue.
I am aware of ListView#addHeaderView but I want to solve my particular problem with using this kind of layout.
Just change your listview id to android:id="#android:id/list"
Use a RelativeLayout. Place the second button to the right of the first button using layout_toRightOf then place the listview beneath button one using layout_below. Something like this (I haven't tested it, apologies for any typos)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/latest_news_button" />
<Button
android:id="#+id/button2"
android:layout_toRightOf="#id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tracked_news_button" />
<ListView
android:id="#+id/mylist"
android:layout_below="#id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Here is the xml you are looking for
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF394952">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
style="#android:style/ButtonBar">
<Button
android:id="#+id/btn_AddMore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/newItem"
android:textColor="#FF394952"/>
</LinearLayout>
<ListView
android:id="#+id/game_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_weight="1.0" />
</LinearLayout>
And when you design Activity for this dont use List activity directly use Activity and set adapter on the ListView.
ListView lv = (ListView) findViewById(R.id.game_list);
ArrayAdapter<String> listItem = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, ALBUM_NAME);
lv.setAdapter(listItem);
lv.setTextFilterEnabled(true);
Use below xml file for that
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/mLlayout1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/latest_news_button" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/tracked_news_button" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/mLlayout1" >
</ListView>
</RelativeLayout>
Create FrameLayout with [your ListView] and [LinearLayout with your buttons].
Do once something like this within onCreate()
LinearLayout myButtons = ...;
myButtons.bringToFront();
So, your ContentView will have a FrameLayout with two child, one of those is in foreground.
That's all. Hope it helped))
Hey, I want to change the TypeFace of one of the TextViews to an external font.. How can I do that?
Code:
public class English extends Activity {
<...>
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quran_english);
listview=(ListView) findViewById(R.id.QuranEnglishListView);
<..add into list..>
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.quran_english_row,
new String[] {"Arabic","English"},
new int[] {R.id.QuranEnglishTextViewArabic,R.id.QuranEnglishTextViewEnglish});
listview.setAdapter(adapter);
xml for each row (quran_english_row.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">
<LinearLayout android:gravity="right" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="fill_parent">
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewArabic" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewEnglish" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
xml for the layout (quran_english.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">
<ListView android:id="#+id/QuranEnglishListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
</LinearLayout>
This crashes because of NullPointer:
((TextView) findViewById(R.id.QuranEnglishTextViewArabic)).setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
So how can I reach the TextView that is in the row to change its TypeFace? Thanks
You can try to get the layout for each list item by
View rowView = adapter.getView(...);
Then get the TextView of the layout by
TextView txt = (TextView)rowView .findViewById(R.id.QuranEnglishTextViewEnglish);
txt.setTypeFace(...);
and set your TypeFaces. Haven't tested that yet, but i guess it could work.
But I recommend extending your own BaseAdapter. Then create another list-row class which provides the inflated layouts for your BaseAdapter. There you'll be able to set the typefaces easily for every TextView.
try this links it works http://techdroid.kbeanie.com/2011/04/using-custom-fonts-on-android.html see steps and class MyTextView and MyButton shown above layout
I have a ListView that uses SimpleAdapter, each row has 2 TextViews, and I would like to use an external font to one of the two TextView.. Here is the code of the class:
public class QuranEnglish extends Activity {
<...>
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.quran_english);
listview=(ListView) findViewById(R.id.QuranEnglishListView);
<..add into list..>
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.quran_english_row,
new String[] {"Arabic","English"},
new int[] {R.id.QuranEnglishTextViewArabic,R.id.QuranEnglishTextViewEnglish});
listview.setAdapter(adapter);
xml for each row (quran_english_row.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">
<LinearLayout android:gravity="right" android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="fill_parent">
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewArabic" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
<TextView android:textSize="21px" android:text="TextView" android:id="#+id/QuranEnglishTextViewEnglish" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
xml for the layout (quran_english.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">
<ListView android:id="#+id/QuranEnglishListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
</LinearLayout>
So how can I put the external typeface of the TextView that is in the row (quran_english_row.xml)? I tried using this line though it crashed my application:
((TextView) findViewById(R.id.QuranEnglishTextViewArabic)).setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
R.id.QuranEnglishTextViewArabic
is not an id of one of your views here. It is an id of a example view, which is used to fill the list. In order to change the font, you need to get one view of your list. This works if you have chosen one of your views:
TextView view = getSelectedView();
if (view != null) {
view.setTypeface(Typeface.createFromAsset(getAssets(),"dejavusans.ttf"));
}