In my TopRated.java file, I have an image view which changes when the seekbar is used. It also changes when the next and previous buttons are used. however, i want both buttons and the seeker to be in sync, that is, when the next button is pressed, i want it to also move the seeker up by one, and when the previous button is pressed, i want it to also move the seeker down by one.
please help
This is my TopRated.java file
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
public class TopRated extends Activity {
SeekBar imgseekbar;
ImageView iconimageview;
Button prevbutton;
Button nextbutton;
int progress=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toprated);
iconimageview=(ImageView)findViewById(R.id.imageView);
imgseekbar=(SeekBar)findViewById(R.id.seekBar);
prevbutton=(Button)findViewById(R.id.button8);
nextbutton=(Button)findViewById(R.id.button9);
iconimageview.setImageResource(R.drawable.diablo);
imgseekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
updateImageResource(progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progress--;
updateImageResource(progress);
}
});
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progress++;
updateImageResource(progress);
}
});
}
private void updateImageResource(int progress) {
if (progress==2) {
iconimageview.setImageResource(R.drawable.eye);
}else if (progress==3) {
iconimageview.setImageResource(R.drawable.facebook);
}else if (progress==4) {
iconimageview.setImageResource(R.drawable.google);
}else if (progress==5) {
iconimageview.setImageResource(R.drawable.house);
}else if (progress==6) {
iconimageview.setImageResource(R.drawable.mail);
}else if (progress==7) {
iconimageview.setImageResource(R.drawable.pen);
}else if (progress==8) {
iconimageview.setImageResource(R.drawable.photos);
}else if (progress==9) {
iconimageview.setImageResource(R.drawable.skype);
}else if (progress==10)
iconimageview.setImageResource(R.drawable.twitter);
}
}
This is my toprated.xml file
<?xml version="1.0" encoding="utf-8"?>
<SeekBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/seekBar"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:max="10"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_below="#+id/seekBar"
android:layout_centerHorizontal="true"
android:minHeight="50dp"
android:minWidth="50dp"
android:background="#drawable/icon_animation"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev"
android:id="#+id/button8"
android:layout_alignBottom="#+id/imageView"
android:layout_toStartOf="#+id/imageView"
android:layout_marginRight="5dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="#+id/button9"
android:layout_alignBottom="#+id/imageView"
android:layout_toEndOf="#+id/button8"
android:layout_marginLeft="50dp" />
I also have another xml which holds all my images, icon_animation.xml
please help!
any help would be appreciated.
use below line to update seek bar
seekbar.setProgress(int)
Related
I am making an application for Android and I have made buttons to play and pause. My play button and pause one are set up, so when I click the play or pause button and first it will run, but in button two it doesn't work.The code is given below.
MainActivity.java
package com.example.cdp.keroncong;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.media.MediaPlayer;
import android.view.View;
public class MainActivity extends Activity {
MediaPlayer mplayer;
public void playAudio(View view) {
mplayer.start();
}
public void pauseAudio(View view) {
mplayer.stop();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mplayer = MediaPlayer.create(this, R.raw.Button1);
//mplayer = MediaPlayer.create(this, R.raw.Button1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Thank's before
Try this Code, it will help you
public class MainActivity extends AppCompatActivity {
MediaPlayer Song;
int pause;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); }
public void play(View view){
if(Song == null){
Song = MediaPlayer.create(this , R.raw.music);
Song.start();
Toast.makeText(MainActivity.this, "Song Play", Toast.LENGTH_SHORT).show(); }
else if(!Song.isPlaying()){
Song.seekTo(pause);
Song.start();
Toast.makeText(MainActivity.this, "Song Play", Toast.LENGTH_SHORT).show(); }
}
public void pause(View view){
if(Song!= null){
Song.pause();
pause = Song.getCurrentPosition();
Toast.makeText(MainActivity.this, "Song Pause", Toast.LENGTH_SHORT).show();
}
}
public void stop(View view){
Song.stop();
Song = null;
Toast.makeText(MainActivity.this, "Song Stop", Toast.LENGTH_SHORT).show();
}
}
xml.file
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Play"
android:layout_gravity="center_horizontal"
android:background="#drawable/play"
android:onClick="play" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pause"
android:layout_gravity="center_horizontal"
android:background="#drawable/pause"
android:onClick="pause" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/stop"
android:layout_gravity="center_horizontal"
android:background="#drawable/stop"
android:onClick="stop" />
Let me know if it will help you
This my activity_main.xml
**activity_main.xml**
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
android:id="#+id/relativeLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:id="#+id/orange"
android:layout_below="#+id/textView"
android:layout_alignParentEnd="true"
android:layout_marginEnd="215dp"
android:onClick="playAudio" />
<Button
android:id="#+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/playButton"
android:layout_below="#+id/playButton"
android:onClick="pauseAudio"
android:text="Pause"
tools:layout_editor_absoluteX="88dp"
tools:layout_editor_absoluteY="58dp" />
<Button
android:id="#+id/banana"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/orange"
android:layout_below="#+id/orange"
android:onClick="playAudio"
android:text="Play"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="58dp" />
<Button
android:id="#+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/orange"
android:onClick="pauseAudio"
android:text="Pause"
tools:layout_editor_absoluteX="88dp"
tools:layout_editor_absoluteY="0dp" />
</android.support.constraint.ConstraintLayout>
Try this, place it in your OnCreate method right after this mplayer = MediaPlayer.create(this, R.raw.Button1);
// Play song
Button playButton = (Button) findViewById(R.id.playButton);
playButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mPlayer.start();
}
});
// Pause song
Button pauseButton = (Button) findViewById(R.id.pauseButton);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
mPlayer.pause();
}
});
i modified your XML to look like this although I can't fully correct it until I see the whole thing
<Button
android:id="#+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/orange"
android:layout_below="#+id/orange"
android:text="Play"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="58dp" />
<Button
android:id="#+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/orange"
android:text="Pause"
tools:layout_editor_absoluteX="88dp"
tools:layout_editor_absoluteY="0dp" />
</android.support.constraint.ConstraintLayout>
I'm currently working on an application and I have and unwanted behavior on my custom EditText Dialog. Here's the XML code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/editText_dialog"
android:layout_width="fill_parent"
android:inputType="textCapSentences"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</android.support.design.widget.TextInputLayout>
<RelativeLayout
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/close_dialog"
android:id="#+id/cancel_dialog"
android:textSize="20sp"
android:layout_margin="20dp"
android:layout_gravity="right"
android:layout_toLeftOf="#+id/valid_dialog" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/validate_dialog"
android:id="#+id/valid_dialog"
android:textSize="20sp"
android:layout_margin="20dp"
android:layout_gravity="right"
android:textColor="#android:color/holo_blue_light"
android:layout_alignParentRight="true" />
</RelativeLayout>S7
</LinearLayout>
And here is the class for the TextEditDialog
package com.fitme.staffbooker;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.NumberPicker;
public class EditTextDialog extends Dialog {
private EditText editText;
public boolean dismissed = false;
public EditTextDialog(Context context, String title) {
super(context, android.R.style.Theme_Holo_Light_Dialog);
setTitle(title);
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_text_dialog);
editText = (EditText) findViewById(R.id.editText_dialog);
findViewById(R.id.valid_dialog).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dismissed = true;
dismiss();
}
});
findViewById(R.id.cancel_dialog).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
}
public void setHint(String str) {
editText.setHint(str);
}
public void setText(String str) {
editText.setText(str);
}
public void setInputType(int type) {
editText.setInputType(type);
}
public String getValue() {
return editText.getText().toString();
}
}
Here's the output i'm having on my Galaxy S7 Edge :
But when I'm running my app on a Galaxy SIII Emulator it seems broken :
It seems that this only happens on 4.3 and below. I have no idea what is happening.
I'm new to android app development. I was building a basic calculator app in android. I have no compile time errors in the code but when i'm trying to run this app it shows "Unfortunately calculator has stopped working". I have copied XML Layout and .java file one after the other.
`
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/input1"/>
<EditText
android:id="#+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/input1"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/input2"/>
<EditText
android:id="#+id/ed3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:hint="#string/input2"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/result"/>
<EditText
android:id="#+id/ed2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/result"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp" >
<Button
android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/add"/>
<Button
android:id="#+id/btn2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/sub"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/btn3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/mul"/>
<Button
android:id="#+id/btn4"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/div"/>
</LinearLayout>
</LinearLayout>
package com.example.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity
{
public Integer int3;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText input1=(EditText) findViewById(R.id.ed1);
final EditText input2=(EditText) findViewById(R.id.ed2);
final Integer int1=Integer.parseInt(input1.getText().toString());
final Integer int2=Integer.parseInt(input2.getText().toString());
Button add=(Button) findViewById(R.id.btn1);
add.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int3=int1+int2;
}
});
Button sub=(Button) findViewById(R.id.btn2);
sub.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int3=int1-int2;
}
});
Button mul=(Button) findViewById(R.id.btn3);
mul.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int3=int1*int2;
}
});
Button div=(Button) findViewById(R.id.btn4);
div.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int3=int1/int2;
}
});
final EditText result=(EditText) findViewById(R.id.ed2);
result.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
result.setText(Integer.toString(int3));
}
});
}
}'
Do not Add this on onCreate()
final Integer int1=Integer.parseInt(input1.getText().toString());
final Integer int2=Integer.parseInt(input2.getText().toString()); )
DELETE IT !!
Otherwise, You have to add this convert sentences when you create onClickListener !
you should add these line in every onClick methods like this.
add.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Integer int1=Integer.parseInt(input1.getText().toString());
Integer int2=Integer.parseInt(input2.getText().toString());
int3=int1+int2;
}
});
Button sub=(Button) findViewById(R.id.btn2);
sub.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Integer int1=Integer.parseInt(input1.getText().toString());
Integer int2=Integer.parseInt(input2.getText().toString());
int3=int1-int2;
}
});
There will be no error.
Additionally you should consider how about clicking without edittext value, it can cause just the error you got !Actually, you have to void assigning null(space) value to integer. Your error is putting space to integer. Edittext have space value and you are changing it to integer. Your Error !!
I have a button defined in my XML layout, when it is clicked it calls a function that is supposed to translate the ImageView man 100 pixals to the right, it moves the image to the right but when i click the button a second time it resets the image to its starting position and repeats the same animation across the same path. (i have android:fillAfter="true" in my xml) I tried adding the following line inside the onAnimationEnd -
#Override
public void onAnimationEnd(Animation arg0) {
man.layout(xCurrentPos + 100, yCurrentPos, xCurrentPos+ (man.getWidth()) + 100, yCurrentPos + man.getHeight());
}
however when the animation finishes the ImageView glitches a bit to the left of where the ImageView stopped its animation (it hops backwards a bit) i tried playing with xCurrentPos + 100 parameters example changing it to xCurrentPos + 70 to get it to stop glitching but nothing worked, also i tried putting
man.layout(man.getLeft(), man.getTop(), man.getRight(), man.getBottom());
in the onAnimationEnd however it just reset the image to its orginal postion
Here is my code,
package com.junglejackapps.theman;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
Button bup, bdown, bleft, bright;
int xCurrentPos;
int yCurrentPos;
ImageView man;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bup = (Button) findViewById(R.layout.activity_main);
bdown = (Button) findViewById(R.layout.activity_main);
bleft = (Button) findViewById(R.layout.activity_main);
bright = (Button) findViewById(R.layout.activity_main);
man = (ImageView) findViewById(R.id.man);
}
#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 onClick_function_up (View v) {
}
public void onClick_function_right (View v) {
xCurrentPos = man.getLeft();
yCurrentPos = man.getTop();
Animation anim= new TranslateAnimation(xCurrentPos, xCurrentPos+100, yCurrentPos, yCurrentPos);
anim.setDuration(2500);
anim.setFillAfter(true);
anim.setFillEnabled(true);
anim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) {}
#Override
public void onAnimationRepeat(Animation arg0) {}
#Override
public void onAnimationEnd(Animation arg0) {
man.layout(xCurrentPos + 100, yCurrentPos, xCurrentPos+ (man.getWidth()) + 100, yCurrentPos + man.getHeight());
}
});
man.startAnimation(anim);
}
public void onClick_function_down (View v) {
}
public void onClick_function_left (View v) {
}
}
and my 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:orientation="vertical"
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=".MainActivity"
android:fillAfter="true">
<ImageView
android:id="#+id/man"
android:layout_width="70dp"
android:layout_height="120dp"
android:layout_marginLeft="22dp"
android:contentDescription="#string/arrow"
android:src="#drawable/man"
/>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignLeft="#+id/button2"
android:layout_marginBottom="20dp"
android:text=""
android:onClick="onClick_function_up" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="19dp"
android:text=""
android:onClick="onClick_function_up_down"/>
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button2"
android:layout_marginRight="33dp"
android:text=""
android:onClick="onClick_function_right"
/>
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_marginLeft="15dp"
android:text=""
android:onClick="onClick_function_left" />
</RelativeLayout>
Any help or suggestions would be appreciated, thanks.
Try using deltas in the animation, and set the margins at the end.
Here's an example:
TranslateAnimation anim = new TranslateAnimation(0, 0, amountToMoveRight, amountToMoveDown);
anim.setDuration(2500);
anim.setAnimationListener(new TranslateAnimation.AnimationListener()
{
#Override
public void onAnimationStart(Animation animation) { }
#Override
public void onAnimationRepeat(Animation animation) { }
#Override
public void onAnimationEnd(Animation animation)
{
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams();
params.topMargin += amountToMoveDown;
params.leftMargin += amountToMoveRight;
view.setLayoutParams(params);
}
});
view.startAnimation(anim);
Make sure fillAfter is set to false in this scenario.
Hope this helps :)
Hi I need a soft keyboard with only numeric values 0 to 9 and Enter key. Shouldn't show anything other than these like . , ( ) etc...
I tried several options as suggested here but nothings seems to work for me.
setRawInputType(Configuration.KEYBOARD_QWERTY)
setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED)
setRawInputType(InputType.TYPE_CLASS_NUMBER)
setRawInputType(InputType.TYPE_CLASS_PHONE)
I always have the extra characters show up on the keyboard like:
setRawInputType(Configuration.KEYBOARD_12KEY) shows a keyboard like this:
Would appreciate any help. Thanks in advance.
NOTE:
android:minSdkVersion="14": ICS4.0
android:targetSdkVersion="17": JB 4.2
All you can do for standard keyboards is suggest input types. The keyboard can still display or not display whatever keys it wants. If you must have certain keys and only those, you need to create a custom soft keyboard. If it's only for your app, and especially if it's only for one activity, I wouldn't actually implement a standard keyboard, but just use views/buttons that do the appropriate actions.
I've faced the same problem , and i found tat there is no android keyboard like this available
and that the only way is to implement your own.
so i would like to share with you my implement and hopefully save you some valuable time:
i've created this xml , you can modify the colors,fonts and the size of the keyboard accourding to your needs:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<LinearLayout
android:id="#+id/one_to_three"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/one_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="1"
android:textSize="25sp" />
<Button
android:id="#+id/two_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="2"
android:textSize="25sp" />
<Button
android:id="#+id/three_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="3"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/four_to_six"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#+id/one_to_three"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/four_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="4"
android:textSize="25sp" />
<Button
android:id="#+id/five_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="5"
android:textSize="25sp" />
<Button
android:id="#+id/six_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="6"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/seven_to_nine"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#+id/four_to_six"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/seven_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="7"
android:textSize="25sp" />
<Button
android:id="#+id/eight_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="8"
android:textSize="25sp" />
<Button
android:id="#+id/nine_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="9"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/zero"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#+id/seven_to_nine"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/zero_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:text="0"
android:textSize="25sp" />
<Button
android:id="#+id/back_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Back"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/done"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="#+id/zero"
android:orientation="horizontal" >
<Button
android:id="#+id/done_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Done"
android:textSize="30sp" />
</LinearLayout>
</RelativeLayout>
i've created this fragment:
package com.galrom.keyboard; //replace it with your package
import com.example.calculator.R;//import your own R class
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.Button;
public class KeyBoardFragment extends Fragment {
private Button one_btn;
private Button two_btn;
private Button three_btn;
private Button four_btn;
private Button five_btn;
private Button six_btn;
private Button seven_btn;
private Button eight_btn;
private Button nine_btn;
private Button zero_btn;
private Button back_btn;
private Button done_btn;
private StringBuilder sb;
private onKeyBoardEvent keyboardEventListener;
private int maxLength=10;
private int currentLength;
public static KeyBoardFragment newInstance(String EditTextValue)
{
KeyBoardFragment fragment=new KeyBoardFragment();
Bundle bundle=new Bundle();
bundle.putString("et_value", EditTextValue);
fragment.setArguments(bundle);
return fragment;
}
#Override
public void onAttach(Activity activity) {
try{
keyboardEventListener=(onKeyBoardEvent)activity;
}
catch(ClassCastException e)
{
Log.e("ClassCastException in KeyBoardFragment row 50",activity.toString()+" must implement onKeyboardEvent");
e.printStackTrace();
}
super.onAttach(activity);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
sb=new StringBuilder(getArguments().getString("et_value"));
currentLength=sb.length();
View rootView=inflater.inflate(R.layout.numeric_keyboard_layout, container, false);
one_btn=(Button)rootView.findViewById(R.id.one_btn);
one_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
add("1");
}
});
two_btn=(Button)rootView.findViewById(R.id.two_btn);
two_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
add("2");
}
});
three_btn=(Button)rootView.findViewById(R.id.three_btn);
three_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
add("3");
}
});
four_btn=(Button)rootView.findViewById(R.id.four_btn);
four_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
add("4");
}
});
five_btn=(Button)rootView.findViewById(R.id.five_btn);
five_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
add("5");
}
});
six_btn=(Button)rootView.findViewById(R.id.six_btn);
six_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
add("6");
}
});
seven_btn=(Button)rootView.findViewById(R.id.seven_btn);
seven_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
add("7");
}
});
eight_btn=(Button)rootView.findViewById(R.id.eight_btn);
eight_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
add("8");
}
});
nine_btn=(Button)rootView.findViewById(R.id.nine_btn);
nine_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
add("9");
}
});
zero_btn=(Button)rootView.findViewById(R.id.zero_btn);
zero_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sb.length()>0)
add("0");
}
});
back_btn=(Button)rootView.findViewById(R.id.back_btn);
back_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(sb.length()>0)
{
currentLength--;
sb.deleteCharAt((sb.length())-1);
keyboardEventListener.backButtonPressed(sb.toString());
}
}
});
back_btn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
currentLength=0;
sb=new StringBuilder();
keyboardEventListener.backLongPressed();
return false;
}
});
done_btn=(Button)rootView.findViewById(R.id.done_btn);
done_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
keyboardEventListener.doneButtonPressed(sb.toString());
}
});
return rootView;
}
public interface onKeyBoardEvent
{
public void numberIsPressed(String total);
public void doneButtonPressed(String total);
public void backLongPressed();
public void backButtonPressed(String total);
}
public int getMaxLength() {
return maxLength;
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public void add(String num)
{
currentLength++;
if(currentLength<=maxLength)
{
sb.append(num);
keyboardEventListener.numberIsPressed(sb.toString());
}
else
currentLength--;
}
}
3.the effect of a poping keyboard under the EditText when it is pressed is achived by
creating an empty RelativeLayout that function as an container to the keyboard:
<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: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=".MainActivity" >
<com.galrom.keyboard.EditTextNoKeyBoard
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Key_board_container"
android:layout_centerHorizontal="true"
android:clickable="true"
android:ems="10" />
<RelativeLayout
android:id="#+id/Key_board_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="38dp"
android:background="#ffffff" >
</RelativeLayout>
when the user press on the EditText the we add the fragment to the container and when he presses done we hide it. the keyboard fragment comunicate with the Activity with the onKeyBoardEvent interace.
NOTE:the hosting activity must implement this interface or else a ClassCastException will be thown.
VERY IMPORTANT: i didn't handled the orientation change, if you change to ladscape while the keyboard is open it will crash, so either disable landscape mode or handle the orientation change to avoid a nullPointerException on the key_board_fragment.
this is the Activity that implemets the keyBoard:
package com.galrom.keyboard;
import com.example.calculator.R;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends FragmentActivity implements KeyBoardFragment.onKeyBoardEvent{
private EditText et;
private KeyBoardFragment keyboard_fragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.editText1);
et.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(keyboard_fragment==null)
{
keyboard_fragment=KeyBoardFragment.newInstance(et.getText().toString());
getSupportFragmentManager().beginTransaction().add(R.id.Key_board_container, keyboard_fragment).commit();
}
else
{
if(keyboard_fragment.isVisible())
getSupportFragmentManager().beginTransaction().hide(keyboard_fragment).commit();
else
{
keyboard_fragment=KeyBoardFragment.newInstance(et.getText().toString());
getSupportFragmentManager().beginTransaction().add(R.id.Key_board_container, keyboard_fragment).commit();
}
}
});
}
#Override
public void numberIsPressed(String total) {
// TODO Auto-generated method stub
et.setText(total);
}
#Override
public void doneButtonPressed(String total) {
// TODO Auto-generated method stub
et.setText(total);
if(keyboard_fragment.isVisible())
getSupportFragmentManager().beginTransaction().hide(keyboard_fragment).commit();
}
#Override
public void backLongPressed() {
// TODO Auto-generated method stub
et.setText("");
}
#Override
public void backButtonPressed(String total) {
// TODO Auto-generated method stub
et.setText(total);
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(keyboard_fragment!=null)
{
if(keyboard_fragment.isVisible())
getSupportFragmentManager().beginTransaction().remove(keyboard_fragment).commit();
else
super.onBackPressed();
}
else
super.onBackPressed();
}
}
and the last thing:
to disable the poping of the standart keyboard of android i've created an CustomEditText that simply returns false at: onCheckIsTextEditor() , this is the CustomEditText class:
package com.galrom.keyboard;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;
public class EditTextNoKeyBoard extends EditText {
public EditTextNoKeyBoard(Context context) {
super(context);
}
public EditTextNoKeyBoard(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public EditTextNoKeyBoard(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onCheckIsTextEditor() {
// TODO Auto-generated method stub
return false;
}
}
Hope it helped you out...
if you have suggestions for improvement i will be happy to hear.
Gal.
In addition to it set inputType="phone" on the EditText. That will open numeric pad keyboard once you start typing however it will include all extra characters related to the numbers. You would need to implement your own keyboard to keep only the numeric values.
This solution uses numberPassword by overriding the default transformation method for the EditText to show characters instead of dots.
<EditText
android:id="#+id/userid"
android:inputType="numberPassword"
android:maxLength="6"
/>
Add to OnCreate.
// Numeric 6 character user id
EditText input = findViewById(R.id.userid);
// Process input and show characters instead of dots
input.setTransformationMethod(SingleLineTransformationMethod.getInstance());
By default based on your device, the keyboard shows the special characters too in number keyboard . specifying the Keyboard type for the Text field you can achieve the expected result,such as
InputFieldName.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
i.e.
If you need only number included with special characters,then you can use
InputType.TYPE_CLASS_NUMBER
or
if you need to exclude those special characters too then use InputType.TYPE_NUMBER_VARIATION_PASSWORD
i have had the same problem that you have, and just came with a solution, perhaps its not elegant, nor its easy, but it does work splendid...
First of all, the only InputType that works with that keyboard (at least until 4.3) is "numberPassword", but this "hides" your input as dots. so i used that input with this transformation method:
private class ShowNumbersTransformationMethod implements TransformationMethod {
public CharSequence getTransformation(final CharSequence charSequence, final View view) {
return new PassCharSequence(charSequence);
}
#Override
public void onFocusChanged(final View view, final CharSequence charSequence, final boolean b, final int i,
final Rect rect) {
//nothing to do here
}
private class PassCharSequence implements CharSequence {
private final CharSequence charSequence;
public PassCharSequence(final CharSequence charSequence) {
this.charSequence = charSequence;
}
#Override
public char charAt(final int index) {
return charSequence.charAt(index);
}
#Override
public int length() {
return charSequence.length();
}
#Override
public CharSequence subSequence(final int start, final int end) {
return new PassCharSequence(charSequence.subSequence(start, end));
}
}
}
and then set it to your edittext:
edittext.setTransformationMethod(new ShowNumbersTransformationMethod());
Now, as said before, this is not the happiest solution, but i assure you it works like a charm. It would be 10 times easier to create your own custom keyboard, but, i didnt have that option, since my client wanted the standard keyboard, god knows why...
Hope it helped!
The keyboard itself chooses what keys to layout. The best you can do is specify InputType.TYPE_CLASS_NUMBER, but the keyboard will still display whatever it thinks is appropriate to a numeric text field.