How to display a number inside a progress spinner - java

I have a progress spinner displayed in the menu. I'd like to display a number inside the spinner, how would I go about doing this? Below are some of the code snippets for what I am doing to make the progress spinner work:
public void onCreateOptionsMenu(...) {
...
if (uploadCount > 0) {
MenuItem updatingMenuItem = menu.findItem(R.id.photo_capture_action_updating);
updatingMenuItem.setActionView(R.layout.action_progressbar);
updatingMenuItem.expandActionView();
} else {
menu.removeItem(R.id.photo_capture_action_updating);
}
}
And below is the action_progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ProgressBar>
Thank You,

You can add a textview along with this progressbar,with gravity center and wrap it inside a frame layout.
xml-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/progressBarLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ProgressBar>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="14" />
</FrameLayout>
Hope this solves your problem.

Related

Nested Scroll View Not Scroll to Bottom of Activity

I am having an issue with nested scroll view when reach bottom.
Inside nestedScrollView I have a button, in bottom of page I have a input field to enter phone number. When press button, if phone number filed not filled, I want to scroll to Bottom of Activity to highlight input field.
But in my activity it scrolling only half, not scrolling till end.
I tried all beloved java codes to fix this issue, but all works same (Not reaching bottom, stops on middle).
Java Codes
CoordinatorLayout mainCoordinate = findViewById(R.id.mainCoordinate);
NestedScrollView nestedScrollView = findViewById(R.id.nestedScroll);
1.
//Scroll to bottom of view
nestedScrollView.post(new Runnable() {
#Override
public void run() {
nestedScrollView.smoothScrollTo(0, mainCoordinate.getBottom());
}
});
2.
//Scroll to bottom of view
nestedScrollView.post(new Runnable() {
#Override
public void run() {
nestedScrollView.fullScroll(View.FOCUS_DOWN);
}
});
3.
nestedScrollView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
final int scrollViewHeight = nestedScrollView.getHeight();
if (scrollViewHeight > 0) {
nestedScrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
final View lastView = nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1);
final int lastViewBottom = lastView.getBottom() + nestedScrollView.getPaddingBottom();
final int deltaScrollY = lastViewBottom - scrollViewHeight - nestedScrollView.getScrollY();
/* If you want to see the scroll animation, call this. */
nestedScrollView.smoothScrollBy(0, deltaScrollY);
/* If you don't want, call this. */
nestedScrollView.scrollBy(0, deltaScrollY);
}
}
});
XML Codes
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:id="#+id/mainCoordinate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:ignore="RtlHardcoded">
<android.support.design.widget.AppBarLayout
android:id="#+id/flexibleAppbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:visibility="invisible">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/flexible.example.collapsing"
android:layout_width="match_parent"
android:layout_height="258dp"
app:contentScrim="?colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/imageMain"
android:layout_width="match_parent"
android:layout_height="258dp"
android:foreground="#drawable/ripple_effect_circle_card"
android:src="#drawable/back_button_white" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
app:behavior_overlapTop="48dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:fitsSystemWindows="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/price.card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchorGravity="bottom"
card_view:cardBackgroundColor="#color/card_back"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="8dp">
.........................
.........................
.........................
//My Other All Views
.........................
.........................
.........................
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile.card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchorGravity="bottom"
card_view:cardBackgroundColor="#color/card_back"
card_view:cardCornerRadius="4dp"
card_view:contentPadding="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="start"
android:padding="5dp"
android:text="Phone No." />
<EditText
android:id="#+id/etPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:gravity="end"
android:hint="Enter your phone"
android:inputType="phone"
android:text=""/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<Button
android:id="#+id/btnBookNow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"/>
</android.support.design.widget.CoordinatorLayout>
I attached a video that explains my exact problem link
Please, Does anyone having a better way or other suggestion to fix this issue?
Thanks in Advance
Pls try Android layout inspector to check height of your NestedScroll.
Try to add
android:layout_gravity="bottom|center" to Button.
In NestedScrollView set layout heigh to android:layout_height="wrap_content"

Swiping the pages with ViewPage

I'm trying to fill the page by blocks of linearlayout by data. Also I'm using ViewPager for changing pages. Every page should have its own data blocks, but now I'm getting linearlayouts that doesn't swipes out (blocks stand still when I'm swiping pages) but there is another linearlayout right above the other ones that doesn't constist data but sliding when I swipe pages.
main.xml:
<?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="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<android.support.v4.view.ViewPager
android:id="#+id/page"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<LinearLayout
android:orientation="vertical"
android:id="#+id/page_blocks"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>
table_row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/program_frame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip">
<TextView
android:id="#+id/StudyName"
/>
<TextView
android:id="#+id/Auditorium"
/>
<TextView
android:id="#+id/StudyKindName"
/>
<TextView
android:id="#+id/LectureTitle"
/>
<ImageView
android:id="#+id/imageView2"
/>
</RelativeLayout>
onCreateView of ViewPage:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.table_row, null);
framesContainer = (LinearLayout) getActivity().findViewById(R.id.page_blocks);
for (int i = 0; i < 16; i += 4) {
Frame frame = new Frame(getActivity());
try {
frame.setStudyName(s.get().get(0).Days().get(i));
frame.setStudyKindName(s.get().get(0).Days().get(i + 1));
frame.setAuditorium(s.get().get(0).Days().get(i + 2));
frame.setLectureTitle(s.get().get(0).Days().get(i + 3));
framesContainer.addView(frame);
} catch (Exception e) {
e.printStackTrace();
}
}
return view;
}
Also pic:
Is there a way to hide the empty block and make all blocks slide after the page when swiping?
Your problem is that you are using RelativeLayouts, and since you are not aligning its children elements(ViewPager and LinearLayout), they are overlapping:
<?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="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<android.support.v4.view.ViewPager
android:id="#+id/page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<LinearLayout
android:orientation="vertical"
android:id="#+id/page_blocks"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/page" >
</LinearLayout>
This line tells the viewpager that its position is going to be at the top of the RelativeLayout:
android:layout_alignParentTop="true"
And this one tells the LinearLayout its position is below the viewpager:
android:layout_below="#id/page"
There are some other properties that can be set --> RelativeLayout
To be honest, I'm not sure what you are trying to do. But maybe you just need to get rid of the RelativeLayout and use LinearLayouts instead.

adding view to ViewFlipper causes java.lang.StackOverflowError

I want to have a dialog with buttons
whenever i click on a specific button, I want the dialog to "flip" and show another layout. A click on another button will return to the original dialog's view.
I try to use ViewFlipper as follows:
xmls:
send_feedback_dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/feedback" />
</LinearLayout>
...
social_actions_dialog.xml
<?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:id="#+id/main_activity_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" >
<ViewFlipper
android:id="#+id/viewFlipper"
android:layout_width="300dp"
android:layout_height="407dp"/>
<RelativeLayout
android:id="#+id/main_activity_card_face"
android:layout_width="300dp"
android:layout_height="407dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#android:color/white"
android:clickable="true"
android:onClick="onCardClick"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
...
code:
final ViewFlipper viewFlipper = (ViewFlipper) mDialog
.findViewById(R.id.viewFlipper);
View feedbackview = View.inflate(mContext,
R.layout.send_feedback_dialog, viewFlipper);
// ((ViewGroup)
// feedbackview.getParent()).removeView(feedbackview);
// viewFlipper.addView(feedbackview);
// View socialActions = View.inflate(mContext,
// R.layout.social_actions_dialog, viewFlipper);
// ((ViewGroup) socialActions.getParent())
// .removeView(socialActions);
// viewFlipper.addView(socialActions);
private void flipDialog(ViewFlipper viewFlipper,
boolean isSocialActionsShown, AlphaAnimation alphaIn,
AlphaAnimation alphaOut) {
if (isSocialActionsShown) {
isSocialActionsShown = false;
viewFlipper.setInAnimation(alphaIn);
viewFlipper.setOutAnimation(alphaOut);
// Show the next Screen
viewFlipper.showNext();
} else {
isSocialActionsShown = true;
viewFlipper.setInAnimation(alphaIn);
viewFlipper.setOutAnimation(alphaOut);
viewFlipper.showPrevious();
}
}
I used to get java.lang.StackOverflowError and then i have commented some rows and now the flip is executed but nothing changes.
how can i make it properly work?
what is the right way to organize the dialog layout in different files?
i have tried to create the dialog in a different file but it requires many things and maybe it's better to use it as an inner class

Google Map V2 on Top half of the android screen and list view on the bottom half of the android screen

I am working on a Android project in which I need to show Google Map v2 on top half of the Android Screen and in the bottom half of the same android screen, I need to show a ListView.
Below is the code, I have so far which will be launched as soon as I launch my Application. I have setup properly the AndroidManifest.xml file for Google Map V2 key.
public class SampleActivity extends Activity {
#TargetApi(11)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().hide();
}
findViewById(R.id.sample_button).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
int width = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 40, getResources()
.getDisplayMetrics());
SlideoutActivity.prepare(SampleActivity.this,
R.id.inner_content, width);
startActivity(new Intent(SampleActivity.this,
MenuActivity.class));
overridePendingTransition(0, 0);
}
});
}
}
And below is the sample.xml file which I need to modify so that I can add stuff which will have Google Map v2 in top half of the android screen and in the bottom half, I will have ListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inner_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_android" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="45dip"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:background="#bb000000">
<Button style="#android:style/Widget.Button.Small"
android:id="#+id/sample_button"
android:layout_width="35dip"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:text=">" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/sample_button"
android:layout_centerVertical="true"
android:textSize="19sp"
android:textColor="#ffffff"
android:text="Facebook-like slide-out nav"/>
</RelativeLayout>
</RelativeLayout>
Can anyone help me with this? I am done with all my settings related to setting up the Google Map API v2 key. I just need to plugin the code to start showing the google map v2 on the TOP half of the android screen and list view on the bottom half of the android screen.
Any help will be appreciated on this. Thanks
Update:-
Will this updated XML file will work to have Google Map v2 on top half of the android screen and ListView on bottom half of the android screen?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/inner_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg_android" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="45dip"
android:background="#bb000000"
android:paddingLeft="2dip"
android:paddingRight="2dip" >
<Button
android:id="#+id/sample_button"
style="#android:style/Widget.Button.Small"
android:layout_width="35dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:text=">" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/sample_button"
android:text="Proximity Application"
android:textColor="#ffffff"
android:textSize="19sp" />
</LinearLayout>
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.8"
android:orientation="horizontal" >
<ListView
android:id="#+id/mylist"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
</LinearLayout>
This layout should present you a map in the top half of the screen an a ListView in the bottom half:
<?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"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/list">
</ListView>
</LinearLayout>
</LinearLayout>

Dynamically adding TextView to ScrollView on Android

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

Categories

Resources