How to use the listView.setEmptyView()? - android

I want to add a progress bar in a listview when the list loads and if there are no results after the loading is finished I want to add a textVeiw saying the list is empty using the setEmptyView function.
I tried using this code to add a progress bar in the list but now luck:
#Override
protected void onPreExecute() {
ProgressBar progress = new ProgressBar(parent);
progress.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
((ListView) view).setEmptyView(progress);
super.onPreExecute();
}
how do I add a progress bar in the list pragmatically without using xml layout to inflate it from?
thaks

Try this way,I am not tried but got the reference from this Android – ListView => setEmptyView()
listView.setEmptyView(findViewById(R.id.progressBar));
and let me working or not.
Good Luck.

Related

How to add Text to new Progrss bar in android programmatically

Hi I am trying to show a new progress bar on my android app since the progress dialog is deprecated i want to use new progress bar. Right now my code does bring up progress bar but i want to add a text to it.
See my code below
RelativeLayout layout= view.findViewById(R.id.upload_layout);
pDialog =new ProgressBar(container.getContext(),null,android.R.attr.progressBarStyleSmall);
pDialog.setIndeterminate(false);
pDialog.setBackgroundColor(Color.argb(240,255,255,255));
pDialog.setVisibility(View.GONE);
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(pDialog,params);
Edit:
Sorry I guess the question wasn't clear please see the image. I have no errors.
Progress bar on the screen to which i want add text like "Uploading..."
Any help would be appreciated. Thanks in advance
I guess you want to add text "Uploading..." below the pDialog,you can add code in your code's end
TextView up = new TextView(pDialog.getContext());
up.setText("Uploading...");
RelativeLayout.LayoutParams paramUp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
paramUp.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(up,paramUp);
but I suggest you build progress bar in xml.

One layout as background of other layout

I want to set My Loading layout in the front of mainview layout and make it transparent.
I made it like this:
So, the question is: How to make mainView disabled (not clickable) While loadingView is visible.....?
ProgressDialog ProgressDialog = new ProgressDialog(this);
ProgressDialog.setMessage("");
ProgressDialog.setCancelable(true);
ProgressDialog.show();
Try to set the mainview's corresponding layout not to be clickable, by using the setClickable() method.
Set it back to true when loading ends.
It would be useful if you post some code.
Also, take a look to this Android Layout make all children's not clickable
it's about the layout's children, but i believe it is also helpful
If your loadingView is covering the mainView, you can either make it (loadingView) clickable or add a onClickListener to it.
Now the clicks won't be taken by the mainView and its children.
In terms of code, you can either write:
loadingView.setClickable(true);
Or set click listener like:
loadingView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do something here if you want
}
});

List Dialog similar to Android L Notifications (Transparent ListView Divider?)

I have a dialog containing a dynamic ListView. I'm trying to style it similar to the Android L notifications center but I can't seem to do so.
My dialog window background is already transparent and the ListView content is set up but I can't seem to add transparent dividers to it. When I change my listview's divider color to transparent, it completely disappears removing the gap in between. After manually setting a divider height, it returns but it's not transparent. Instead, it's some sort of dark blue-ish transparent color.
My code for this is fairly simple:
// Create ListView
ListView mList = new ListView(context);
mList.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// Create dialog base
final Dialog mDialog = new Dialog(context, R.style.TransparentDialog);
mDialog.setContentView(mList);
mDialog.setCanceledOnTouchOutside(true);
mDialog.setCancelable(true);
// Add listview content...
// ...
// Apply dividers and content
mList.setDivider(new ColorDrawable(R.color.transparent));
mList.setDividerHeight(72);
mList.setAdapter(mAdapter);
.
How would I go on to styling it like it? My only issue with this is the lack of transparent ListView divider.
This is what I'm trying to achieve:
I ended up achieving this effect using card images with transparent margins embedded in them.

How to get a spinner into an alertbox/dialog?

I am trying to put a spinner into an alert box and would much appreciate it, if someone would point me in the direction of a tutorial or show some code on how this can be done.
Create an xml layout with a spinner
in your code:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.layoutname);
you can access the spinner like this
Spinner spin = (Spinner)dialog.findViewById(R.id.spinnerid);
If you're using an alert dialog, you can add a custom layout containing your Spinner to your existing dialog.
To see an example of this, look for the "DIALOG_TEXT_ENTRY" case in this example: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/AlertDialogSamples.html
You can do it like this:
// ProgressBar properties
RelativeLayout.LayoutParams progressParams = new RelativeLayout.LayoutParams(Patterns.PROGRESS_BAR_WIDTH, Patterns.PROGRESS_BAR_WIDTH);
progressParams.addRule(RelativeLayout.CENTER_VERTICAL);
progressParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
mProgress = new ProgressBar(context);
mProgress.setIndeterminate(true);
rootLayout.addView(mProgress,progressParams);
mProgress.setVisibility(View.VISIBLE);
Where rootLayout is your Activity's layout where you want to put the spinning "box". The LayoutParams that I used is just to place the box in the layout's center. When your box is no longer necessary, you can dismiss it like this:
mProgress.setVisibility(View.GONE);
layoutBg.removeView(mProgress);

Android - change custom title view at run time

I am using a custom title view in my application for each activity. In one of the activities, based on button clicks I need to change the custom title view. Now this works fine every time when I make a call to setFeatureInt.
But if I try to update any items in the custom title (say change the text of a button or a text view on the title), the update does not take place.
Debugging through the code shows that the text view and button instances are not null and I can also see the custom title bar. But the text on the text view or the button is not updated. Has anyone else faced this problem?
How do I resolve it?
Thanks.
EDIT
Here's what I tried. Does not get updated even on calling postInvalidate.
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.text_title);
TextView databar = (TextView) findViewById(R.id.title_text);
databar.setText("Some Text");
databar.postInvalidate();
Button leftButton = (Button) findViewById(R.id.left_btn);
leftButton.setOnClickListener(mLeftListener);
leftButton.setText("Left Btn");
leftButton.postInvalidate();
Button rightBtn = (Button) findViewById(R.id.right_btn);
rightBtn.setOnClickListener(mRightListener);
rightBtn.postInvalidate();
The problem is that the only Window implementation (PhoneWindow) uses a LayoutInflater in its setFeatureInt method and instantiates the new layout with inflate and attachToRoot=true. Consequently, when you call setFeatureInt, the new layouts are not replaced but attached to the internal title container and thus drawn on top of each other.
You can workaround this by using the following helper method instead of setFeatureInt. The helper simply removes all views from the internal title container before the new custom title feature is set:
private void setCustomTitleFeatureInt(int value) {
try {
// retrieve value for com.android.internal.R.id.title_container(=0x1020149)
int titleContainerId = (Integer) Class.forName(
"com.android.internal.R$id").getField("title_container").get(null);
// remove all views from titleContainer
((ViewGroup) getWindow().findViewById(titleContainerId)).removeAllViews();
// add new custom title view
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, value);
} catch(Exception ex) {
// whatever you want to do here..
}
}
I'm not sure whether the current setFeatureInt behaviour is intended, but it is certainly not documented one way or the other which is why I'll take this to the android devs ;)
EDIT
As pointed out in the comments, the aforementioned workaround is not ideal. Instead of relying on the com.android.internal.R.id.title_container constant you could simply hide the old custom title whenever you set a new one.
Let's assume you have two custom title layouts:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/custom_title_1" ...
and
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/custom_title_2" ...
and you want to replace custom_title_1 with custom_title_2, you could hide former and use setFeatureInt to add the latter:
findViewById(R.id.custom_title_1).setVisibility(View.GONE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_2);
The correct way to do this is as follows:
requestWindowFeature( Window.FEATURE_CUSTOM_TITLE );
setContentView( R.layout.my_layout );
getWindow().setFeatureInt( Window.FEATURE_CUSTOM_TITLE, R.layout.my_custom_title );
super.onCreate( savedInstanceState );
Please note that the order of these statements is very important.
If you call super.onCreate() before any of the other statements you will get a blank title bar, which the hack of finding the title bar id and removing all the Views from it will fix but is not recommended.
Are you calling invalidate or postInvalidate to redraw the view after updating the text? If it's a custom View, can you put a breakpoint in the draw code to make sure it's getting called?
If you're on the UI thread, you can call 'invalidate' if you're not, you must call 'postInvalidate' or the view won't redraw itself.
Just my 2c worth:
When working in a MapActivity, requesting a custom title resulted in no title at all being shown.
Luckily, all I wanted to do was to set the title text differently, and I soon realized that just calling setTitle() inside of onCreate() worked for me (I called it after I called setContentView())
Sorry, but I don't have time right now to debug this any more and figure out why what I was doing didn't work, and why changing it made it work. As I said, just thought this might help someone out down the road.

Categories

Resources