I made a list view by the android.com's tutorial, and its in a tab widget, but the problem is that its spreading all over the tab's area and I want to create 2 lists in one tab, one beside the other... how can I do that?
here is a link to the tutorial:
http://developer.android.com/resources/tutorials/views/hello-listview.html
EDIT: ok that what I have in the onCreate method, there are 2 static final String arrays in this class called CONTACTS and FAVORITES, I copied MH's layout to an xml file called list_who and changed the IDs to lv_cons and lv_vav, and another layout called tv (TextView) that only have a text view because that what the adapter requires
super.onCreate(savedInstanceState);
ListView lv_cons = (ListView) findViewById(R.id.lv_cons);
ListView lv_fav = (ListView) findViewById(R.id.lv_fav);
lv_cons.setAdapter(new ArrayAdapter<String>(this, R.layout.tv, CONTACTS));
lv_fav.setAdapter(new ArrayAdapter<String>(this, R.layout.tv, FAVORITES));
setContentView(findViewById(R.layout.list_who));
If you want two display two lists side-by-side, there are a few things you'll need to change.
First the (minimal) layout for the tab's content:
<?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" >
<ListView
android:id="#+id/left_listview"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ListView
android:id="#+id/right_listview"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
If you followed the Hello ListView sample code you linked, you'll have to change your ListActivity back to Activity. The former basically offers a number of convenience methods for working with a single list, but you can do exactly the same with a regular activity, which you'll need to work with multiple lists.
You can inflate your ListViews like any other view by using their ids (there is no more getListView() convenience method, since we're not extending ListActivity anymore)`:
ListView leftList = (ListView) findViewById(R.id.left_listview);
ListView rightList = (ListView) findViewById(R.id.right_listview);
From that point on you can add all stuff to populate/manipulate the lists' contents.
//Edit: If you insist on creating the layout programmatically, simple convert above xml code to it's Java equivalent:
// Create ViewGroup as container for both lists
LayoutParams rootParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LinearLayout root = new LinearLayout(this);
root.setLayoutParams(rootParams);
// Create LayoutParams for the lists - both identical in this example
LinearLayout.LayoutParams listParams = new LinearLayout.LayoutParams(0, LayoutParams.FILL_PARENT);
listParams.weight = 1;
ListView leftList = new ListView(this);
ListView rightList = new ListView(this);
leftList.setLayoutParams(listParams);
rightList.setLayoutParams(listParams);
// Add lists to container
root.addView(leftList);
root.addView(rightList);
// Set container as content view
setContentView(contentView);
If you're looking to handle two separate ListViews in separate tabs (i.e. a ListView in each tab), then follow this tutorial: Android TabActivity with two list views.
You simply need to make one composite layout and extract your tabHost from the xml programmatically and then inflate the tabs on the fly. I further suggest using this method if you want two lists to communicate with each other via an Intent. Both tabs would be hosted in one activity. If I misunderstood your question and you want to display two separate, adjacent lists within one activity..then disregard my answer.
I think this is what you're looking for. Good luck.
Related
I am using < include /> to show a sublayout (layoutA) inside a Main layout.
however, what I want to do is to change the sublayout from layoutA to layoutB dynamically in Java. Which means in <include../> the line layout="#layout/layoutA" should change to layout="#layout/layoutB".I am not sure how to achieve this. Not sure if there is an option like view.setLayout() for .
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
<other data/>
<include
android:id="#+id/id1"
android:layout_width="match_parent"
android:layout_height="match_parent"
layout="#layout/layoutA"
/>
</LinearLayout>
Note: I have about 7-8 sublayouts so I do not just want to create multiple and hide them. Which could be an option.
you can replace include with fragment and keep replace fragment if subviews have different and complex logic if not or simple stuff you can use include and remove all views to remove old one and add a new view
ViewGroup parentLayout = findViewById(R.id.id1);
parentLayout.removeAllViews();
LayoutInflater inflater = LayoutInflater.from(this);
View newSublayout = inflater.inflate(R.layout.layoutB, parentLayout, false);
parentLayout.addView(newSublayout);
I have added a spinner in an application which gives the package information in only a word or couple of word. it seems like regular dropdown menu.
I tried to make changes in simple_spinner_dropdown_item.xml file but it does not help and also it is having a radio button which i dont require.
I am done with handling functionality and only needed to change design of that dropdown menu of spinner.
for that i am using checkedTextView like:
service_provider_spinner.setPopupBackgroundResource(R.drawable.ser_menu);
now i don't want that layout rather i like to add a background for each item and also want to set text padding and margin and all that. How can i do this in java file specificly and also in XML.
You can customize the spinner_layout and spinner_dropdown_layout by simply creating an xml layout for both containing only TextView like this as shown.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
And you can apply this xml to your adapter as shown below:
ArrayAdapter<String> adapter= new ArrayAdapter<String>(
getActivity(), R.layout.your_spinnerLayout, yourArray);
adapter.setDropDownViewResource(R.layout.your_spinner_dropdown_layout);
spinner.setAdapter(adapter);
I am a newbie, so, any help is appreciated. I read a lot of posts here at StackOverflow and also I searched for my doubt in Google, but it's hard to find a good answer.
Here is what I am trying to do:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageButton
android:background="#layout/roundcorners"
android:id="#+id/hug"
android:scaleType="centerInside"
android:cropToPadding="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="10dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/hug">
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:textColor="#000000"
android:textSize="15dip"
android:textStyle="bold"
android:paddingBottom="5dip"
android:clickable="true"
android:text="Hug">
</TextView>
</FrameLayout>
Above you guys can see the XML version of what I need.
The point is... I will have many of these FrameLayouts at run time. Every information to fill out the buttons will come from a database.
So, I need to create a Java Class where I can use a loop through all the registers from my database and instantiate a new FrameLayout Class (this class must have an ImageButton and a TextView as you can see from above XML) and just pass parameters, like this:
for (int i = 0; i < mArray.length; i++) {
button = new MyNewImageButton(name, src, text);
}
The above is just to simplify. What I mean is that I will pass parameters from my database when creating an Instance of this class that I am planning to create. Of course, every single button created will be added to the layout.
So... my question is: I know how to do this using XML, but I am REALLY having a hard time to create a class to do this.
Any thoughts? Any help is appreciated.
P.S.: Sorry if I made any mistake in my English, ok? I am a Brazilian. Someday my English will be flawless! Sorry if this question was already answered.
sorry to answer my own question to make another question. I tried to use the comments but there's a limitation in the number of characters, so, I am really sorry.
Hey guys and #blessenm. Well... I tried to use inflater and I came up with the following code:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// *******************************************
setContentView(R.layout.main);
//this is my main screen
//it's a linearlayout vertical orientation
ViewGroup parent = (ViewGroup) findViewById(R.id.tela_principal);
//these two new LinearLayouts will be one above the other,
//just like two lines
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
//inside of each linearlayout I set the orientation to horizontal
//so, everytime a picture is inflated from xml, it will fill in one
//linearlayout
l1.setOrientation(LinearLayout.HORIZONTAL);
l2.setOrientation(LinearLayout.HORIZONTAL);
//setting linearlayout parameters, so they fill the whole screen
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//the first two inflated xml imagebuttons I add to LinearView1
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
//the next two inflated xml imagebuttons I add to LinearView2
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
//after the above, we should have a grid 2X2
//after linearlayouts are filled, I add them to the main screen
parent.addView(l1, new LinearLayout.LayoutParams(0, 0, 1));
parent.addView(l2, new LinearLayout.LayoutParams(0, 0, 1));
However this is not working. In the errorlog I get the following message:
"Unhandled event loop exception".
Any ideas about what I am doing wrong?
Thanks!
If you are just trying to create a view from the xml and add it to the layout. Just use the LayoutInflater.
Inside the activity use something like
FrameLayout frame = (FrameLayout)getLayoutInfalter.inflate(
R.id.YOUR_VIEW_XML,null);
layout.addView(frame);
If you are trying to create a class extend the frame layout or the the view. Create a constructor which takes your parameters and assign's the required values.
EDIT:
To Acess Elements Inside
If you have set id's to those element, you can access them by
TextView text = (TextView)frame.findViewById(R.id.yourtextview);
Or you can use the child index like
TextView text = (TextView)frame.getChildAt(0);
It sounds like you are looking for a way to create a view class that will be an ImageButton and a TextView wrapped with a FrameLayout.
In this case, you could look into creating your own View class. Probably a View class that extends FrameLayout. See this dev article for more information about how to create a custom view. http://developer.android.com/guide/topics/ui/custom-components.html
Specifically the "Compound Controls" section: http://developer.android.com/guide/topics/ui/custom-components.html#compound
I want to know whether it is possible to use an xml layout file to define the content of a view dynamically from within the code. When we start an activity we pass it the xml layout to use with the method call setContentView(R.layout.main); but is it possible to use an xml layout file to define a dynamically created ViewGroup such as LinearLayout?
I have a layout xml which shows a score table for a game. Each score that is displayed on this screen needs to be dynamically added via code. I know that it is possible within the code to create a ViewGroup for this score and populate it with all the things I need to make a single score, and then do this every time for each score, and then add them all to the existing UI structure, already defined in the xml layout. What I would like to know is if it is possible to use another xml file to do this?
For example, a layout xml file:
<LinearLayout android:id="#+id/top">
<LinearLayout android:id="#+id/column_heading"/>
</LinearLayout>
In another xml layout file is something like:
<LinearLayout android:id="#+id/row">
<TextView/>
<TextView/>
<TextView/>
<TextView/>
</LinearLayout>
Within the code I would like to do something like the following:
LinearLayout top = (LinearLayout)findViewById(R.id.top);
for (int i = 0; i < num_of_rows; i++) {
LinearLayout row = new LinearLayout(this);
row.setContentView(R.layout.row); //where R.layout.row is the second layout above
// ... dynamically change values as needed
top.addView(row);
}
However .setContentView(...) is not a valid method of LinearLayout. Is there another way to do this? I know I could do it all by code, but that's rather messy and this way would seem to be very tidy and rational..
You should use LayoutInflater for this. Here is a short example
LinearLayout top = (LinearLayout)findViewById(R.id.top);
for (int i = 0; i < num_of_rows; i++) {
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout row = (LinearLayout)inflater.inflate(R.layout.row, null);
// ... dynamically change values as needed
top.addView(row);
}
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.row, null);
You can use LayoutInflater's inflate method to inflate an arbitrary layout from resources. If you provide a root view parameter to this method, the inflated layout will be contained within it. This way you can inflate the XML view into your row.
I have four tabs that hold four listviews, I want to set a background for each list view but whenever I try to add the background it puts the image in each cell of the listview instead of behind the list.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/pre"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="21sp">
</TextView>
I've realised that this is beacuse I've tried to add the background in a textview so it adds the image in each cell in the listview, so I have tried to add a linearlayout, listview and a imageview and put the background there but it force closes. I think this is becuse the tabhost uses main.xml to draw the main page and it conflicts, so I even tried to add the listview there still it nforce closes, it will only work if I have a textview only, howe can I add a background to each listview, below is the listview code;
public class prem extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Pre"};
ListView lv = getListView();
lv.setCacheColorHint(00000000);
lv.setAdapter(new ArrayAdapter<String>(this,
R.layout.list_item, names));
}
Okay, your XML layout files are going to be used in the setContentView() method. You haven't posted the code for your activity which includes the TabHost, but I'll assume you're using the default setContentView(R.layout.main);. If you're NOT using setContentView() (in the case of your ListActivity), adding a ListView to an XML file isn't going to change anything, because it's never being used.
You are correct in that you're having the issue because you're setting the background of the TextView. Since you're using a ListActivity, you'll need to set the ListView's background using code. ListView is a subclass of View, so you can use the methods from the View class to set your background resource for the ListView.
For example:
ListView listView = getListView();
//set background to color
listView.setBackgroundColor(#FF888888);
//set background to Drawable
listView.setBackgroundDrawable(myDrawable);
//set background to Resource
listView.setBackgroundResouce(R.id.my_res_id);
As per JonniBravo, adding....
If you have an XML layout file that you are using to set the view by using setContentView() in your activity, you can have a ListView defined inside that.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lists_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/paper_frame" />
</RelativeLayout>
The activity can "find it" using
lv = getListView();
As shown in the sample above, you can set the background for the ListView with the "background" attribute. As usual, this can be a valid drawable (e.g. a color, or an image). In my example it happens to be a 9.patch image that is stretched by Android to enclose the list, providing a frame.
Good luck.