1

I am very new to Android programming. Following is what I am doing: I have an EditTextBox. And when it is in focus I would want to display a custom number pad. To implement this, following are the code snippets: layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_twopane"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <FrameLayout android:id="@+id/fragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"/>

    <FrameLayout android:id="@+id/keyPadFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

In the Fragment class, defined a Callbacks interface which the parent activity has to implement

public interface Callbacks {
   void onAnswerEditTextSelected(Fragment fragment);
}

Added a listener to the EditText

mAnswerEditText.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
      if(!mNumberPadShown) {
         mCallbacks.onAnswerEditTextSelected(QuizFragment.this);
      }
   });

In the Parent activity, I have the following:

@Override
public void onAnswerEditTextSelected(Fragment fragment) {
    FragmentManager fm  = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    Fragment numFragment = NumberPadFragment.newInstance(fragment);
    ft.add(R.id.keyPadFragmentContainer, numFragment);
    ft.commit();
}

At runtime I get No view found for id 0x7f ...(classname:id/keyPadFragmentContainer) for fragment NumberPadFragment. Can you please tell me where I am going wrong.

Thank you

user2808089
  • 49
  • 1
  • 5
  • Some more information. I realize my mistake, but I am not sure how to address this. The FragmentActivity has a member variable of type ViewPager and I have setContentView(mViewPager) - instead of setContentView(R.id.activity_twopane). However, I do have mViewPager.setId(R.id.activity_twopane). Hence, findViewById(R.id.keyPadFragmentContainer) returns null. – user2808089 Sep 27 '13 at 18:56
  • The intent is that the page should show the EditText and other widgets on the top and the keypad in the bottom, when the EditText is in focus. – user2808089 Sep 27 '13 at 19:02
  • I figured out my mistake and the solution. As I am new to this forum, I will have to wait for 8 hours to provide a solution. The short answer is that in the layout file, I have a ViewPager and a FarmLayout (for the numberpad). With that change and by setting the content view in the ParentActivity to the twopanelayout, I was able to get it to work. – user2808089 Sep 27 '13 at 20:01
  • have you also considered showing your own customized keyboard instead? – android developer Sep 27 '13 at 22:37
  • For my app, I needed a way to enter only numbers (integers and decimals, signed/unsigned) - so I have my own customized numberpad with a set of numbers, +/-, . and AC (for all clear) – user2808089 Sep 27 '13 at 22:41
  • i see, i've created an answer that might be easier for you. – android developer Sep 27 '13 at 22:47

1 Answers1

0

if it's just numbers that you need, there is already a way to do it without any customization . it's called inputType.

it can be done either in XML or programmatically.

example :

<EditText android:inputType="number" android:digits="0123456789." 
 .../> 

for more buttons, you can create a custom keyboard, as shown here:

i'm pretty sure the API demos have a sample for it too.

Community
  • 1
  • 1
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Thank you android developer for your feedback. The number pad has a custom look and feel to it - similar to one of the links (the second link that you referred me to). – user2808089 Sep 30 '13 at 18:57