I'm trying to stop the progress of the seek bar unless the user started below progress(10), this ensures the user actually has to slide it, thus avoiding accidental activation. I've tried this so far and it looks like it'd work, however the slide bar can still be moved from anywhere (progress(0) up to progress(100).
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
private Boolean isInRange = true;
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
if (seekBar.getProgress() > 95) {
((MainActivity)getActivity()).currentOrder = null;
((MainActivity)getActivity()).showMain();
} else {
seekBar.setProgress(0);
isInRange = false;
}
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
if (seekBar.getProgress() < 10) {
isInRange = true;
} else {
seekBar.setProgress(0);
isInRange = false;
}
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(!isInRange) {
seekBar.setProgress(0);
}
}
});// Set on seek bar change listener
just in case you need it, here's the XML code of the seekbars container:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorFocus"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:id="#+id/seperator_bottom"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/darker_gray"
android:orientation="horizontal" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_delivery_disabled"
android:padding="16dp"
android:layout_margin="16dp">
<SeekBar
android:id="#+id/seek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:max="100"
android:progressDrawable="#android:color/transparent"
android:thumb="#mipmap/ic_slide" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/delivered"
android:textAppearance="#android:style/TextAppearance.DeviceDefault.Large"
android:gravity="center"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:id="#+id/textView" />
</RelativeLayout>
</LinearLayout>
The setProgress is absolute value, not relative to where your progress currently is. What you actually are doing is essentially resetting the progress to 0 any time when the touch is not within the first 10 percent of the SeekBar, not within 10 percent of where the progress is. That, of course, is assuming that you can not provided max attribute, in which case the max value is set at 100.
So, you need to adjust your code to read current value, then compare it to current and see if it is within 10 percent. I would also recommend to keep track of starting position and end position rather then only having a simple boolean value to track it. Then you have to evaluate the value in relation to current position.
Related
Im using android studio , i have xml which have horizontalscrollview
Then linearlayout then multiple imageviews ..
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
What i need is to make this horizantalscrollview to keep automatically infinite scrolling the images till the end then repeat the scrolling again and again.
what i mean by auto scrolling that the horizontalscrollview keep scrolling without using hand
Did you try to use wrap_content in layout_width?
Now, u adapt your width layout to the width of the screen, i think that if u put the value wrap_content it will solve ur problem (u have layout_height=wrap_content, but the height is "asociated" with a Vertical scroll, not with horizontal scroll):
android:layout_width="wrap_content"
I hope this help u.
ok Thanks for your support .. i found the solution
Timer timer1 = new Timer("horizontalScrollViewTimer");
timer1.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (scrollingLeft) {
if (scroll.getScrollX() == 0) {
scroll.smoothScrollBy(5, 0);
scrollingLeft = false;
} else {
scroll.smoothScrollBy(-5, 0);
}
} else {
if (scroll.canScrollHorizontally(View.FOCUS_RIGHT)) {
scroll.smoothScrollBy(5, 0);
} else {
scroll.smoothScrollBy(-5, 0);
scrollingLeft = true;
}
}
}
});
}
}, 1000, 50);
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/customedittext"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="#+id/answer1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:fontFamily="#font/timeandspace"
android:scrollHorizontally="false"
android:scrollbars="vertical"
android:hint="1."
android:background="#color/transparent"
android:inputType="text"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#color/colormain"
android:textSize="25sp" /></LinearLayout>
How can I wrap the text vertically or set a maximum width of my text without using android:inputType="textmultiline"?
reason if why i didnt want that input type because i am using the Enter in keyboard to submit the text and not to add line. Hope someone can help me :)
And, how can I prevent automatically showing keyboard and focus to edittext at the start of activity?
Thanks a lot <3
This will not add a line:
reason if why i didnt want that input type because i am using the
Enter in keyboard to submit the text and not to add line
answer1.setOnEditorActionListener(new OnEditorActionListener()
{
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
if (event.getAction() == KeyEvent.KEYCODE_ENTER)
{
// Do whatever you wanna do on "Enter"
return true;
}
return false;
}
});
And, how can I prevent automatically showing keyboard and focus to
edittext at the start of activity?
Remove any EditText.requestFocus() call in your activity.
I currently have code that should make the LinearLayout toggle between being VISIBLE and GONE, but it only toggles if it is visible, and does not toggle if the Layout is Gone. Could someone explain what i have done wrong?
XML:
<ImageButton
android:id="#+id/info_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/myimage"
android:onClick="toggleInfo"/>
<LinearLayout
android:id = "#+id/text_box"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:gravity="center">
<TextView
android:id="#+id/info_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/test"
android:textSize="18sp"
android:textStyle="normal"
android:background="#color/white"
/>
</LinearLayout>
JAVA:
public void toggleInfo(View view) {
LinearLayout infoText = findViewById(R.id.text_box);
if (infoText.getVisibility() == LinearLayout.GONE) {
infoText.setVisibility(LinearLayout.VISIBLE);
}
if (infoText.getVisibility() == LinearLayout.VISIBLE) {
infoText.setVisibility(LinearLayout.GONE);
}
}
By default your layout is visible. And when you are calling the method, it checks if the view is visible. The second if condition satisfies with it. So first time the LinearLayout.GONE is getting called.
But when you are clicking second time (now your view is not visible), the first condition satisfies and LinearLayout.VISIBLE is getting called. Now your view is visible. So the second condition also satisfies, and the LinearLayout.GONE is getting called again.
Just put and else and it will work.
public void toggleInfo(View view) {
LinearLayout infoText = findViewById(R.id.text_box);
if (infoText.getVisibility() == LinearLayout.GONE) {
infoText.setVisibility(LinearLayout.VISIBLE);
}else if (infoText.getVisibility() == LinearLayout.VISIBLE) {
infoText.setVisibility(LinearLayout.GONE);
}
}
Just do this:
public void toggleInfo(View view) {
LinearLayout infoText = findViewById(R.id.text_box);
infoText.setVisibility(infoText.getVisibility() == LinearLayout.GONE ? LinearLayout.VISIBLE : LinearLayout.GONE);
}
Rupam absolutely right. Another way to fix is - to add return to your first condition
also avoid using LinearLayout.GONE
better use View.GONE instead(if something will change - for example you will decide to use RelativeLayout instead of current LinearLayout... well... you got me I think)
if (infoText.getVisibility() == LinearLayout.GONE) {
infoText.setVisibility(LinearLayout.VISIBLE);
return;
}
if (infoText.getVisibility() == LinearLayout.VISIBLE) {
infoText.setVisibility(LinearLayout.GONE);
}
I was wondering if there is a way to group EditText in a way that you can only traverse among a group of them.
The problem is that I rely on reaching the end of the first group and use EditorInfo.IME_ACTION_DONE in order to trigger a certain action.
The inner group of EditTexts is inside a LinearLayout.
Put the text boxes in a layout(linear or relative) only those EditText which you want to make a group. layout will act as group. Add this on your every Edittext xml--
android:maxLines="1"
if it does not works the you can handle focus.
Focus Handling
Focus movement is based on an algorithm which finds the nearest neighbor in a given direction. In rare cases, the default algorithm may not match the intended behavior of the developer.
Change default behaviour of directional navigation by using following XML attributes:
android:nextFocusDown="#+id/.."
android:nextFocusLeft="#+id/.."
android:nextFocusRight="#+id/.."
android:nextFocusUp="#+id/.."
Besides directional navigation you can use tab navigation. For this you need to use
android:nextFocusForward="#+id/.."
To get a particular view to take focus, call
view.requestFocus()
To listen to certain changing focus events use a View.OnFocusChangeListener
Keyboard button
You can use android:imeOptions for handling that extra button on your keyboard.
Additional features you can enable in an IME associated with an editor to improve the integration with your application. The constants here correspond to those defined by imeOptions.
The constants of imeOptions includes a variety of actions and flags, see the link above for their values.
Value example
ActionNext :
the action key performs a "next" operation, taking the user to the next field that will accept text.
ActionDone :
the action key performs a "done" operation, typically meaning there is nothing more to input and the IME will be closed.
Code example:
<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=".MainActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="16dp"
android:imeOptions="actionNext"
android:singleLine="true"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="24dp"
android:imeOptions="actionDone"
android:singleLine="true"
android:ems="10" />
</RelativeLayout>
If you want to listen to imeoptions events use a TextView.OnEditorActionListener.
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
performSearch();
return true;
}
return false;
}
});
Edit:
For dynamic edittext you can try..
EditText etCurrent;
Set an OnTouchlistener on each EditText:
valueET.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (etCurrent != null) {
if (etCurrent.getId() != v.getId()) {
if (!check(etCurrent.getId())) {
etCurrent.setSelection(0,
etCurrent.getText().toString().length());
return true;
} else {
etCurrent = (EditText) v;
}
}
} else {
etCurrent = (EditText) v;
}
}
return false;
}
});
When I click "present" button the "percentage" does not change while the entered values changes. After the first click, the value of the "percentage" changes. The same problem occur when I press the "missed" button. After the first click, when I pressed "present" button increment function works and for "missed" button decrement function works.
I want that when "button b" is pressed it calculates the "percentage". when a user press "present" it increases the values of both "e1 (attended) and e2 (total)" from the previous entered values and then calculates the "percentage". For "missed" button it increases the value of e2 only and then calculates the percentage.
EditText e1,e2;
Button b,save;
TextView t;
String attended,total,mysum;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText) findViewById(R.id.editTextN1);
e2=(EditText) findViewById(R.id.editTextN2);
b=(Button) findViewById(R.id.buttoncalcsum);
save= (Button) findViewById(R.id.save);
t=(TextView) findViewById(R.id.textViewresult);
SharedPreferences pre=getSharedPreferences("data",Context.MODE_PRIVATE);
e1.setText(pre.getString("attended", ""));
e2.setText(pre.getString("total", ""));
t.setText(pre.getString("mysum", ""));
b.setOnClickListener(new OnClick());
}
public class OnClick implements OnClickListener
{
#Override
public void onClick(View v)
{
double n1 = Integer.parseInt(e1.getText().toString());
e1.setBackgroundColor(Color.CYAN);
double n2 = Integer.parseInt(e2.getText().toString());
e2.setBackgroundColor(Color.BLUE);
double mysum;
mysum=(n1/n2)*100;
if(mysum>65)
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.GREEN);}
else
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.RED);}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void execute(View v) {
SharedPreferences prefre=getSharedPreferences("data",Context.MODE_PRIVATE);
Editor editor=prefre.edit();
editor.putString("attended", e1.getText().toString());
editor.putString("total", e2.getText().toString());
editor.putString("mysum", t.getText().toString());
editor.commit();
finish(); }
public void increment(View v) {
int t1 = Integer.parseInt(e1.getText().toString());
e1.setText(String.valueOf(t1+1));
int t2 = Integer.parseInt(e2.getText().toString());
e2.setText(String.valueOf(t2+1));
double mysum;
mysum=((double)t1/t2)*100;
if(mysum>65)
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.GREEN);}
else
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.RED);}
}
public void decrement(View v) {
int p1 = Integer.parseInt(e1.getText().toString());
int p2 = Integer.parseInt(e2.getText().toString());
e2.setText(String.valueOf(p2+1));
double mysum;
mysum=((double)p1/p2)*100;
if(mysum>65)
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.GREEN);}
else
{ t.setText(String.format("%.2f", mysum));
t.setBackgroundColor(Color.RED);}
}
and here is the activity_main.xml file
<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"
android:background="#drawable/bg"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.counter.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="90dp"
android:text="ATTENDED" />
<EditText
android:id="#+id/editTextN1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="42dp"
android:layout_toRightOf="#+id/textView1"
android:ems="10"
android:inputType="number"
android:hint="ENTER VALUE HERE"
android:gravity="center"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editTextN1"
android:layout_marginTop="39dp"
android:layout_toLeftOf="#+id/editTextN1"
android:text="TOTAL" />
<EditText
android:id="#+id/editTextN2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignLeft="#+id/editTextN1"
android:layout_alignRight="#+id/editTextN1"
android:ems="10"
android:inputType="number"
android:hint="ENTER VALUE HERE"
android:gravity="center" />
<Button
android:id="#+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/buttoncalcsum"
android:layout_alignParentBottom="true"
android:onClick="execute"
android:text="SAVE" />
<Button
android:id="#+id/btnatnd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/save"
android:layout_alignRight="#+id/editTextN2"
android:onClick="increment"
android:text="PRESENT" />
<Button
android:id="#+id/btnmiss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/save"
android:layout_alignParentLeft="true"
android:onClick="decrement"
android:text="MISSED" />
<Button
android:id="#+id/buttoncalcsum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/btnmiss"
android:text="CALCULATE" />
<TextView
android:id="#+id/textViewresult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/buttoncalcsum"
android:layout_marginTop="46dp"
android:layout_toLeftOf="#+id/btnatnd"
android:text="PERCENTAGE"
android:textAppearance="?android:attr/textAppearanceLarge" />
In increment()/decrement() function you update the values of Attended/Total TextViews, but you are then calculating the sum from old values.
Try to first update the values, e.g.:
public void increment(View v) {
...
int t1 = Integer.parseInt(e1.getText().toString());
e1.setText(String.valueOf(t1+1));
int t2 = Integer.parseInt(e2.getText().toString());
e2.setText(String.valueOf(t2+1));
t1++; t2++; // updating values here
....
mysum=((double)t1/t2)*100;
....
}
As far as I am understanding, you have not imported Buttons btnatnd and btnmiss. Also in the functions of increment() and decrement(), you are setting the text of TextView such that t1, t2 are increasing and p2 is decreasing. However the values of t1, t2 and p2 remain unchanged.
You should also use t1+=1;, t2+=1; and p2-=1;
Hope this helps !
AS Far as I understood , I think you want to calculate percentage , and sum and other functions and you are not getting the updated answer as you are using the Shared preferences.
So in this case I would suggest you two following methods:
1: When You calculate the percentage or any other thing (I am supposing that you are well in calculations and calculating it with any mistake) . You should make two different methods One for saving calculations in the shared preferences and the other to retrieve the last result. So when you calculate first call the save method and then call the retrieve method to retrieve values in text fields or where ever you want
2:This is more efficient way. You can make the receiver for any change in the values of of preferences and it will notify as when they are changed , so its up to you where you use those updates.