I have a custom progress bar and basically when I enter a value and then press a button, I would like to have that progress bar update. My progress bar is going to have only three values basically it can be 0 percent, 33 percent, 66 percent, or 100 percent. For some reason when I do go to the page with the progress bar and even enter the value into the TextView and then enter the button, it's just showing a loading circle for some reason that never ends. I've posted my all the code needed for the progress bar, so does anyone know why it's not showing the correct progress bar and why it's just showing that loading circle?
custom_progressBar.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Define the background properties like color etc -->
<item android:id="#android:id/background">
<shape>
<gradient
android:startColor="#000001"
android:centerColor="#0b131e"
android:centerY="1.0"
android:endColor="#0d1522"
android:angle="270"
/>
</shape>
</item>
<!-- Define the progress properties like start color, end color etc -->
<item android:id="#android:id/progress">
<clip>
<shape>
<gradient
android:startColor="#007A00"
android:centerColor="#007A00"
android:centerY="1.0"
android:endColor="#06101d"
android:angle="270"
/>
</shape>
</clip>
</item>
progressBarShirts.java
package ankitkaushal.app.healthysizing;
import android.graphics.drawable.Drawable;
import android.sax.TextElementListener;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class progressBarShirts extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_bar_shirts);
final DatabaseHelper db = new DatabaseHelper(this);
final TextView currentSizeDisplay = (TextView) findViewById(R.id.currentSizeShirts);
Button enterNewSizeButton = (Button) findViewById(R.id.newSizeEnterButtonShirts);
final EditText newSize = (EditText) findViewById(R.id.newSizeShirts);
final String currentSize = db.getCurrentShirtSize();
currentSizeDisplay.setText("Your current size: " + currentSize);
Drawable draw = getResources().getDrawable(R.drawable.custom_progressbar);
enterNewSizeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String size = newSize.getText().toString().toUpperCase();
currentSizeDisplay.setText("Your current size: " + size);
db.updateSizeShirts(size);
}
});
ProgressBar shirts = (ProgressBar) findViewById(R.id.progressBarShirts);
shirts.setProgressDrawable(draw);
String startSize = db.getStartSizeShirts();
String newCurrentSize = db.getCurrentShirtSize();
String limit = db.getLimit("setLimitShirts");
shirts.setMax(100);
if (startSize.equals("XL") && limit.equals("S")) {
if (newCurrentSize.equals("L")) {
// Fill progress bar by 1/3
shirts.setProgress(33);
}
if (newCurrentSize.equals("M")) {
// Fill progress bar by 2/3
shirts.setProgress(66);
}
if (newCurrentSize.equals("S")) {
// Fill progress bar 100 percent
shirts.setProgress(100);
// Give a toast saying you've reached your limit
Toast.makeText(this, "You've reached your limit! ", Toast.LENGTH_LONG).show();
// Set all values in database to null
db.removeLImit("setLimitShirts");
}
}
}
}
activity_progress_bar_shirts.xml
<?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:background="#29A9D2"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Track your progress"
android:id="#+id/TrackProgressShirts"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF"
android:textSize="30dp"
android:layout_marginTop="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Current Size: Large"
android:id="#+id/currentSizeShirts"
android:layout_gravity="center_horizontal"
android:textSize="30dp"
android:layout_marginTop="80dp"
android:textColor="#FFFFFF" />
<EditText
android:layout_width="350dp"
android:layout_height="wrap_content"
android:id="#+id/newSizeShirts"
android:layout_marginTop="20dp"
android:hint="Enter your new size"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="30dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter new size"
android:id="#+id/newSizeEnterButtonShirts"
android:layout_gravity="center_horizontal" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="300dp"
android:progressDrawable="#drawable/custom_progressbar"
android:layout_height="wrap_content"
android:id="#+id/progressBarShirts"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginTop="170dp"
android:max="100"
android:minWidth="300dp"
android:minHeight="100dp"
android:maxWidth="300dp"
android:maxHeight="100dp"
android:indeterminateOnly="true"
android:progress="0" />
</LinearLayout>
Related
I've been looking into an Android problem for some time. I have not found anything on the internet to advance this probelm...
I'm looking to make 3 buttons in a circle that would be one inside the other (See image)
The final objective is that I can insert an image in each of the buttons and that when I click on one, the latter rotates around the central point.
But for the moment I am already blocking the creation of the 3 buttons.
I tested this :
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadiusRatio="20"
android:shape="ring"
android:thicknessRatio="0"
android:useLevel="false">
<solid android:color="#android:color/holo_green_dark"/>
</shape>
But :
1. The click is always on the last button which make sence...
2. I don't know how i can add a image here
I have also try to draw the 3 circles in Paint and then use them as background Button with different height.
It works, but the shape of the button is still a rectangle. So it is not optimized.
Does anyone have a solution?
I know it is possible because i've seen that in different apps.
I just don't know how to realize that...
Thanks in advance
EDIT :
I would like something like that.
X "independant" rings where i can rotate each one separatly.
EDIT 2 : area click
The button appear as a circle --> OK
But the shape of the area click is still a square.
In my case I need precision, i can't afford to apply listener of button 1 when the user click of button 2.
Not sur if i am understandble... Please say if not.
EDIT - Reworked Code For Shape
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="100dp" />
<stroke
android:width="3dp"
android:color="#808080" />
<solid android:color="#color/colorPrimaryDark" />
</shape>
Set this as background of your button and add Manual Height & width to button. Your button will show up.
EDIT
Use Framelayout to overlay your buttons over each other. Change the sizes of button as per your liking. Animated the buttons (rotation) when a button gets clicked.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/button"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_gravity="center"
android:background="#drawable/oval_green"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:background="#drawable/oval_blue"
android:text="Button" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Buttons are under FrameLayout. Now attach code to them and test. both buttons are working fine. I hope this put you in the right path.
EDIT - With ImageView
Ensure to cut the Images in proper portions.
Your layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/ivLarge"
android:layout_width="250dp"
android:layout_height="250dp"
android:focusableInTouchMode="false"
android:scaleType="fitXY"
app:srcCompat="#drawable/large" />
<ImageView
android:id="#+id/ivMedium"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_gravity="center"
android:focusableInTouchMode="false"
android:scaleType="fitXY"
app:srcCompat="#drawable/medium" />
<ImageView
android:id="#+id/ivSmall"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:focusableInTouchMode="false"
android:scaleType="fitXY"
app:srcCompat="#drawable/small" />
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Your MainActivity.java
package com.auvitronics.testing;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
public static final String TAG = "TransparentButton";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView ivLarge = findViewById(R.id.ivLarge);
final ImageView ivMedium = findViewById(R.id.ivMedium);
ImageView ivSmall = findViewById(R.id.ivSmall);
ivLarge.setDrawingCacheEnabled(true);
ivMedium.setDrawingCacheEnabled(true);
ivSmall.setDrawingCacheEnabled(true);
ivLarge.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View view, MotionEvent event)
{
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
int pixel = bitmap.getPixel((int) event.getX(),(int) event.getY());
if (pixel == Color.TRANSPARENT)
{
System.out.println("Large Transparent Color");
return false;
}else {
view.setRotation(view.getRotation() + 90);
System.out.println("Large Colored Area");
return true;
}
}
});
ivMedium.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View view, MotionEvent event)
{
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
int pixel = bitmap.getPixel((int) event.getX(),(int) event.getY());
if (pixel == Color.TRANSPARENT)
{
System.out.println("Medium Transparent Color");
return false;
}else {
view.setRotation(view.getRotation() - 90);
System.out.println("Medium Colored Area");
return true;
}
}
});
ivSmall.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View view, MotionEvent event)
{
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
int pixel = bitmap.getPixel((int) event.getX(),(int) event.getY());
if (pixel == Color.TRANSPARENT)
{
System.out.println("Small Transparent Color");
return false;
}else {
view.setRotation(view.getRotation() + 45);
System.out.println("Small Colored Area");
return true;
}
}
});
}
}
And the Result is
My Client wants a diamond shaped progress that looks like this:
My first attempt was to use a library, but I can't find one that exists
My next attempt was to learn how to use the ProgressBar view that comes with android, and set my own drawable using this answer (the closest thing to an answer on stack overflow), but the answer only works on ring shapes, not rectangles.
What is the best way to create a diamond-shaped progress view? (By any means: custom view, progressDrawable) and how do I do that?
After some more tests, I came up with a hacky answer. It's just 4 progress bars aligned to the edge of a Relative layout, and a CardView on top of them. Rotate the whole thing, and wrap it in a class and bam, you got yourself a diamond progress bar. (Use math to calculate the progress of each bar)
It can be a little weird on the corners (where the progress bars overlap) but overall it works well enough
Usage:
ViewGroup background;
int count = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Something to add the progress bar to
background = (ViewGroup) findViewById(R.id.relative);
//initializing the progress bar
final DiamondProgress diamondProgress = new DiamondProgress(this);
diamondProgress.setMax(1000);
//adding the progress bar
background.addView(diamondProgress.getView());
/* Sample Code for animating the progress bar*/
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
diamondProgress.setProgress(count++);
}
});
}
}, 0, 25);
}
Code:
XML: layout/diamond_view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="wrap_content"
android:rotation="45"
android:padding="43dp"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="200dp"
android:layout_height="200dp">
<RelativeLayout
android:layout_width="wrap_content"
android:rotation="90"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="8dp"
android:layout_alignParentBottom="true"
android:rotation="180">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginLeft="3dp"
android:id="#+id/dp_progress4"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="#drawable/progress_drawable"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="8dp"
android:layout_alignParentBottom="true"
android:rotation="180">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginLeft="3dp"
android:progress="50"
android:id="#+id/dp_progress3"
android:progressDrawable="#drawable/progress_drawable"
style="?android:attr/progressBarStyleHorizontal"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:rotation="90"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginLeft="3dp"
android:progress="100"
android:id="#+id/dp_progress2"
android:progressDrawable="#drawable/progress_drawable"
style="?android:attr/progressBarStyleHorizontal" />
</RelativeLayout>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="8dp"
android:layout_marginLeft="3dp"
android:progress="100"
android:progressDrawable="#drawable/progress_drawable"
style="?android:attr/progressBarStyleHorizontal"
android:id="#+id/dp_progress1"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_margin="4dp"
android:id="#+id/dp_card"
android:elevation="10dp"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:rotation="-45"
android:id="#+id/dp_addView"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Sample Inside Content"
android:id="#+id/dp_text"
android:gravity="center"
android:textSize="24sp"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</RelativeLayout>
XML: drawable/progress_drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- background -->
<item android:id="#android:id/background">
<shape>
<corners android:radius="3dp"/>
<solid android:color="#f2f2f2" />
</shape>
</item>
<!-- for the actual progress bar -->
<item android:id="#android:id/progress">
<clip android:gravity="left">
<shape>
<corners android:radius="3dp"/>
<solid android:color="#color/colorAccent" />
</shape>
</clip>
</item>
</layer-list>
Java Class
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
/**
* Created by Pythogen on 9/27/2017.
*/
public class DiamondProgress {
Context context;
View view;
RelativeLayout addView;
int progress = 0;
int max = 100;
ProgressBar p1;
ProgressBar p2;
ProgressBar p3;
ProgressBar p4;
public DiamondProgress(Context context) {
this.context = context;
view = LayoutInflater.from(context).inflate(R.layout.diamond_view, null);
addView = ((RelativeLayout) view.findViewById(R.id.dp_addView));
p1 = (ProgressBar) view.findViewById(R.id.dp_progress1);
p2 = (ProgressBar) view.findViewById(R.id.dp_progress2);
p3 = (ProgressBar) view.findViewById(R.id.dp_progress3);
p4 = (ProgressBar) view.findViewById(R.id.dp_progress4);
}
public View getView() {
return view;
}
public RelativeLayout getHostOfInsideContent() {
return addView;
}
public void setProgress(int progress) {
this.progress = progress;
updateProgressBar();
}
public void setMax(int max) {
this.max = max;
p1.setMax(max);
p2.setMax(max);
p3.setMax(max);
p4.setMax(max);
}
public void updateProgressBar() {
double prog = ((double)progress)/max;
if (prog<.25) {
p1.setProgress(this.progress*4);
p2.setProgress(0);
p3.setProgress(0);
p4.setProgress(0);
} else {
p1.setProgress(max);
if (prog<.5) {
p2.setProgress((this.progress*4)-max);
p3.setProgress(0);
p4.setProgress(0);
} else {
p2.setProgress(max);
if (prog<.75) {
p3.setProgress((this.progress*4)-max*2);
p4.setProgress(0);
} else {
p3.setProgress(max);
p4.setProgress((this.progress*4)-max*3);
}
}
}
}
}
Oh, and if you plan on using this, be sure to add the CardView dependancy to your build.grade compile 'com.android.support:cardview-v7:25.1.1'
This question already has answers here:
Outlined Edit Text from Material Design
(7 answers)
Closed 4 years ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I am trying to create custom TextInputLayout. How can I create below custom TextInputLayout?
You should use Material Design style for Outline Box. Just simple use:
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
in TextInputLayout. See Text Field for Android in Material Design Guide
Here is an workaround:
1. Design your layout structure as below:
activity_test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:background="#android:color/white">
<!-- Username -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_marginTop="10dp"
android:background="#drawable/bg_rounded_input_field" />
<TextView
android:id="#+id/text_dummy_hint_username"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Username"
android:textSize="16sp"
android:background="#android:color/white"
android:visibility="invisible"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="Username"
android:textColorHint="#android:color/black"
app:hintTextAppearance="#style/HintTextStyle">
<EditText
android:id="#+id/edit_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text|textCapWords"
android:maxLines="1"
android:backgroundTint="#android:color/transparent"/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
<!-- Password -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<View
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_marginTop="10dp"
android:background="#drawable/bg_rounded_input_field" />
<TextView
android:id="#+id/text_dummy_hint_password"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Password"
android:textSize="16sp"
android:background="#android:color/white"
android:visibility="invisible"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="Password"
android:textColorHint="#android:color/black"
app:hintTextAppearance="#style/HintTextStyle">
<EditText
android:id="#+id/edit_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:maxLines="1"
android:backgroundTint="#android:color/transparent"/>
</android.support.design.widget.TextInputLayout>
</RelativeLayout>
</LinearLayout>
2. Use below drawable bg_rounded_input_field.xml for rounded corners.
res/drawable/bg_rounded_input_field.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:color="#android:color/black"
android:width="2dp">
</stroke>
<corners
android:radius="8dp">
</corners>
</shape>
3. Use below HintTextStyle to TextInputLayout by adding attribute app:hintTextAppearance="#style/HintTextStyle".
res/values/styles.xml
<style name="HintTextStyle" parent="TextAppearance.Design.Hint">
<item name="android:textSize">16sp</item>
</style>
4. Finally, in your Activity just show/hide TextView text_dummy_hint_username and text_dummy_hint_password
during focus change.
FYI, I have used Handler with delay 100 millis to
show the dummy hints TextView to sync with TextInputLayout hint text
animation.
TestActivity.java
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class TestActivity extends AppCompatActivity {
TextView textDummyHintUsername;
TextView textDummyHintPassword;
EditText editUsername;
EditText editPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
textDummyHintUsername = (TextView) findViewById(R.id.text_dummy_hint_username);
textDummyHintPassword = (TextView) findViewById(R.id.text_dummy_hint_password);
editUsername = (EditText) findViewById(R.id.edit_username);
editPassword = (EditText) findViewById(R.id.edit_password);
// Username
editUsername.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Show white background behind floating label
textDummyHintUsername.setVisibility(View.VISIBLE);
}
}, 100);
} else {
// Required to show/hide white background behind floating label during focus change
if (editUsername.getText().length() > 0)
textDummyHintUsername.setVisibility(View.VISIBLE);
else
textDummyHintUsername.setVisibility(View.INVISIBLE);
}
}
});
// Password
editPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// Show white background behind floating label
textDummyHintPassword.setVisibility(View.VISIBLE);
}
}, 100);
} else {
// Required to show/hide white background behind floating label during focus change
if (editPassword.getText().length() > 0)
textDummyHintPassword.setVisibility(View.VISIBLE);
else
textDummyHintPassword.setVisibility(View.INVISIBLE);
}
}
});
}
}
OUTPUT:
Hope this will help~
This is my XML file. All text in button is large.
I used android:textAllCaps="false", but no result.In what may be a problem?
All literals are declared in the strings.xml.
Nowhere isn't even talk of the big letters. I think problem in invested LinearLayout.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black_color"
android:gravity="center_vertical"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:id="#+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.3"
android:src="#drawable/ico_start" />
<EditText
android:id="#+id/feeld_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="#string/login"
android:phoneNumber="false" />
<EditText
android:id="#+id/feeld_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="#string/password"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:gravity="bottom"
android:padding="10dp">
<Button
android:id="#+id/button_register"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/register"
android:textSize="17dp" />
<Button
android:id="#+id/button_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:nestedScrollingEnabled="true"
android:text="#string/login"
android:textSize="17dp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<resources>
<string name="app_name">Lesson 6. Registration Form</string>
<string name="register">Register</string>
<string name="login">Login</string>
<string name="password">Password</string>
package com.egoriku.lesson6registrationform;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private final String login = "egorikftp";
private final String password = "androidN";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView mainImage = (ImageView) findViewById(R.id.imageView);
final EditText loginText = (EditText) findViewById(R.id.feeld_login);
final EditText passwordText = (EditText) findViewById(R.id.feeld_password);
Button buttonRegister = (Button) findViewById(R.id.button_register);
Button buttonLogin = (Button) findViewById(R.id.button_login);
buttonLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (loginText.getText().length() == 0) {
loginText.setError("Введите логин");
mainImage.setImageResource(R.drawable.ico_error);
}
if (passwordText.getText().length() == 0) {
passwordText.setError("Введите пароль");
mainImage.setImageResource(R.drawable.ico_error);
} else if (loginText.getText().toString().equals(login) && passwordText.getText().toString().equals(password)) {
Toast.makeText(getApplicationContext(), "Вы успешно вошли в систему!", Toast.LENGTH_LONG).show();
loginText.setText(null);
passwordText.setText(null);
mainImage.setImageResource(R.drawable.ico_ok);
} else {
Toast.makeText(getApplicationContext(), "Логин/Пароль введен неверно!", Toast.LENGTH_SHORT).show();
mainImage.setImageResource(R.drawable.ico_error);
loginText.setText(null);
passwordText.setText(null);
}
}
});
buttonRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (loginText.getText().length() == 0) {
loginText.setError("Введите логин");
mainImage.setImageResource(R.drawable.ico_error);
}
if (passwordText.getText().length() == 0) {
passwordText.setError("Введите пароль");
mainImage.setImageResource(R.drawable.ico_error);
} else {
//login = loginText.getText().toString();
//password = passwordText.getText().toString();
Toast.makeText(getApplicationContext(), "Вы успешно зарегистрированы! ", Toast.LENGTH_LONG).show();
loginText.setText(null);
passwordText.setText(null);
mainImage.setImageResource(R.drawable.ico_ok);
}
}
});
}
}
Screen of my program. Thanks.
The simplest method is to simply add this line inside your Button tag in xml
android:textAppearance="?android:attr/textAppearanceLarge"
Adding this line will show the text of your Button as you want whether capital or small depending upon the text in
android:text=" .... "
and change text size which fits perfectly
Button's widget in Android uses this style by default:
<item name="textAppearanceButton">#android:style/TextAppearance.Widget.Button</item>
Which enable by default the textAllCaps to true. As you can see in values/styles_base_text.xml of AppCompat theme, it could refer to the same style:
<style name="Base.TextAppearance.AppCompat.Button">
<item name="android:textSize">#dimen/abc_text_size_button_material</item>
<item name="textAllCaps">true</item>
<item name="android:textColor">?android:textColorPrimary</item>
</style>
Therefore, you need to override the current theme by your own. This question has been already resolved with this solution provided by #Galya. The steps are:
Create a new style for android:textAppearanceButton
Use the style's parent from the Base theme: #style/Base.TextAppearance.AppCompat.Button
Then, set caps to false with textAllCaps.
However, this will change all text caps buttons in the project. If you only need to handle these two buttons, I'd suggest you to create two TextViews and provide an onClickListener on them.
I created a cotuntdown with a start and stop button. The textview should be centered in the middle of the screen and around this textview is a circular progressbar displayed.
My Problem is, that the progress of the progressbar is anti clockwise and starts not at the top (12 o clock position of a watch) and that the background is not displayed.
The design of the progressbar should be a light grey background and a darkgrey progress.
Here is my main_activity:
<?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="fill_parent"
android:layout_height="fill_parent"
tools:context="letscho.timer.MainActivity">
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="300dip"
android:layout_height="300dip"
android:indeterminate="false"
android:progressDrawable="#drawable/circularprogressbar"
android:background="#drawable/circularprogressbar_background"
style="?android:attr/progressBarStyleHorizontal"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:max="60"
android:progress="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textViewTime"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="80sp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnStart"
android:layout_marginRight="12dp"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:textAlignment="center"
android:text="Start"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnStop"
android:layout_below="#+id/textViewTime"
android:layout_marginRight="12dp"
android:layout_marginLeft="12dp"
android:layout_marginBottom="12dp"
android:text="Stop"
/>
</LinearLayout>
</RelativeLayout>
And my progressbar drawables:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#android:id/progress">
<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thicknessRatio="19.0"
android:useLevel="true"
>
<gradient
android:startColor="#444444"
android:endColor="#444444"
android:type="sweep" />
</shape>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#android:id/progress">
<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thicknessRatio="19.0"
android:useLevel="true"
>
<gradient
android:startColor="#bbbbbb"
android:endColor="#bbbbbb"
android:centerColor="#bbbbbb"
android:type="sweep" />
</shape>
</item>
</layer-list>
And the java main:
package letscho.timer;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.os.Build;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.util.concurrent.TimeUnit;
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#SuppressLint("NewApi")
public class MainActivity extends AppCompatActivity {
int i=-1;
Button btnStart, btnStop;
TextView textViewTime;
ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = (Button) findViewById(R.id.btnStart);
btnStop = (Button) findViewById(R.id.btnStop);
textViewTime = (TextView) findViewById(R.id.textViewTime);
mProgressBar = (ProgressBar) findViewById(R.id.progressbar);
textViewTime.setText("60");
final CounterClass timer = new CounterClass(60000, 1000);
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
}
});
}
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
/**
* #param millisInFuture The number of millis in the future from the call
* to {#link #start()} until the countdown is done and {#link #onFinish()}
* is called.
* #param countDownInterval The interval along the way to receive
* {#link #onTick(long)} callbacks.
*/
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#SuppressLint("NewApi")
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String s = String.format("%02d", TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(s);
textViewTime.setText(s);
mProgressBar.setProgress((int)(millisUntilFinished / 1000));
}
#Override
public void onFinish() {
textViewTime.setText("00");
}
}
}
Thx for your help!