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.
Related
This is what I want to have:
Populate a ListView with data I request from a server
Populate the ListView to a Navigation Drawer
I have populated the ListView with the data I requested and it's not empty. However I can't get it to populate the ListView to the Navigation Drawer.
ListView commentsList = (ListView) findViewById(R.id.right_drawer);
CommentsAdapter adapter = new CommentsAdapter(HomeCategoryActivity.this, R.layout.comment_single, comments, fname, lname);
commentsList.setAdapter(adapter);
The above code represents this:
And it's XML which is in main Layout:
<ListView
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#2B2B2B"
android:choiceMode="singleChoice" />
That adapter works and I populate the ListView successfully and not null. And then I assign a Navigation Drawer:
DrawerLayout rightDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_cat);
rightDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
XML of that:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:materialdesign="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout_cat"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="activity.classes.HomeCategoryActivity">
And finally I dont know how to attach the ListView to the Drawer..
What am I doing wrong? I think it does not require an adapter to assign the ListView to the Drawer?
You should place the ListView as the second child of the DrawerLayout The first child will be the content when the drawer is off-screen and the second child will be the content visible when the drawer is on-screen (aka the drawer itself).
Also, by doing this:
commentsList.setAdapter(adapter_right);
You are resetting the adapter (the source of the data) on the list view so you will not see the data you originally had set with your CommentsAdapter.
See the example xml file on the Google DrawerLayout Dev Page.
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 have an Activity which loads a custom ImageView ( https://github.com/MikeOrtiz/TouchImageView/blob/master/src/com/example/touch/TouchImageView.java). Inside onCreate, I use setImageBitmap to display a huge image (4Mb...).
The problem is that this activity takes 4 seconds to show, but there is no blocking method which I could place in a AsyncTask. What I mean is that all my Activity code is executed, but the activity comes only when the image is loaded, and I don't know when its finish.
What I want to do is showing a ProgressDialog while the Activity loads it, and displays it when it's done.
How can I resolve this ? Here is some code.
Hope you'll understand my problem, it's pretty hard to explain. Thanks !
carte.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<loki.test.lokitest.affichage.TouchImageView
android:id="#+id/image"
android:src="#drawable/loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<Button
android:id="#+id/bouton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/textBouton"/>
</RelativeLayout>
CarteActivity.java (The one who takes 4 seconds)
public class CarteActivity extends Activity implements OnClickListener{
private Group groupe;
private String android_id;
private ConnexionServeur serveur;
private TouchImageView vue;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d("Loki","onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.carte);
findViewById(R.id.bouton).setOnClickListener(this);
vue = ((TouchImageView)findViewById(R.id.image));
vue.setImageBitmap(Singleton.getInstance().getImage());
groupe = Singleton.getInstance().getGroupe();
android_id = Singleton.getInstance().getAndroid_id();
serveur = Singleton.getInstance().getServeur();
if(groupe != null)
groupe.afficherGroupe(vue);
}
}
EDIT :
The image is loaded with :
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.myImage);
But I don't think that this is this code which takes time, it is already done when i use setImageBitmap. I can't figure out what takes so much time, but I wonder if it's a metod I can access.
Make Layout xml top container (Top RelativeLayout) carry two elements
1 Progress Bar - With Visibility Set to Visible. Make it large and in the center
2 Relative Layout - Entire Content Body . Make it invisible first. It will be under progressbar and will be filling entire screen both height and width wise.
In oncreate setContentView and then starts a AysncTask that loads the imageview in doInBackground.In its postExecute make content body visible and progress bar invisible.
Postexecute will get called only after when your loading is complete
Hope this help and you can translate all this in code.
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.
How can I set the background color of an Activity to white programatically?
Add this single line in your activity, after setContentView() call
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
Get a handle to the root layout used, then set the background color on that. The root layout is whatever you called setContentView with.
setContentView(R.layout.main);
// Now get a handle to any View contained
// within the main layout you are using
View someView = findViewById(R.id.randomViewInMainLayout);
// Find the root view
View root = someView.getRootView();
// Set the color
root.setBackgroundColor(getResources().getColor(android.R.color.red));
I prefer coloring by theme
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
?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="#FFFFFF"
android:id="#+id/myScreen"
</LinearLayout>
In other words, "android:background" is the tag in the XML you want to change.
If you need to dynamically update the background value, see the following:
Exercise: Change background color, by SeekBar
In your onCreate() method:
getWindow().getDecorView().setBackgroundColor(getResources().getColor(R.color.main_activity_background_color));
Also you need to add to values folder a new XML file called color.xml and Assign there a new color property:
color.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="main_activity_background_color">#000000</color>
</resources>
Note that you can name the color.xml any name you want but you refer to it by code as R.color.yourId.
EDIT
Because getResources().getColor() is deprecated, use getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
instead.
You can use this to call predefined android colours:
element.setBackgroundColor(android.R.color.red);
If you want to use one of your own custom colours, you can add your custom colour to strings.xml and then use the below to call it.
element.setBackgroundColor(R.color.mycolour);
However if you want to set the colour in your layout.xml you can modify and add the below to any element that accepts it.
android:background="#FFFFFF"
Button btn;
View root;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
root =findViewById(R.id.activity_main).getRootView();
root.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
});
}
To get the root view defined in your xml file, without action bar, you can use this:
View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);
So, to change color to white:
root.setBackgroundResource(Color.WHITE);
View randview = new View(getBaseContext());
randview = (View)findViewById(R.id.container);
randview.setBackgroundColor(Color.BLUE);
worked for me. thank you.
final View rootView = findViewById(android.R.id.content);
rootView.setBackgroundResource(...);
for activity
findViewById(android.R.id.content).setBackgroundColor(color)
The best method right now is of course
getWindow().getDecorView().setBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.main_activity_background_color));
Please be aware though, if you have anything set as the background color in Designer, it will overwrite anything you try to set in your code.