I have a custom Dialog that shows a few buttons. The idea is to have a scrollable Dialog where one can choose a number or letter from 0 to 9, or 0 to F or 0 to Z …
So my first problem is how do I add these buttons through code and not xml since there is a variable number of buttons each time. Even the simplest code crashes on me so I’m probably not doing anything right.
Also the code I have with a few xml buttons chrashes when I click on the buttons saying it can’t find the onClick function. As you can see I have android:onClick=”onClickDialogbutton” in my button xml and the function does exist in my java code but still it crashes.
Hope someone can take look at the code and help me with adding buttons programatically and get the onClick to work.
Here is my code:
DialogTestActivity.java:
package com.test.dialog;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
public class DialogTestActivity extends Activity {
Dialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickStartDialog( View view ) {
dialog = new Dialog( this );
dialog.requestWindowFeature( Window.FEATURE_NO_TITLE );
dialog.setContentView( R.layout.dialog );
dialog.setCancelable( true );
dialog.show();
// I here wish to add buttons through code and not xml.
// This gives an error as it is now.
Button button = new Button( this );
( ( LinearLayout )findViewById( R.id.Buttons ) ).addView( button );
}
public void onClickDialogButton( View view ) {
dialog.dismiss();
}
}
Main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="onClickStartDialog"
android:text="Start Dialog" />
</LinearLayout>
Dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="54dip"
android:layout_height="150dip"
android:gravity="center" >
<ScrollView
android:layout_width="48dip"
android:layout_height="144dip" >
<LinearLayout
android:id="@+id/Buttons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/ButtonId0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickDialogbutton"
android:text="0"
android:textSize="32dip" />
<Button
android:id="@+id/ButtonId1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickDialogbutton"
android:text="1"
android:textSize="32dip" />
<Button
android:id="@+id/ButtonId2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="onClickDialogbutton"
android:text="2"
android:textSize="32dip" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
You probably need to call
findViewByIdon thedialog. Simply callingfindViewByIdinside of yourActivitywill try to find a view or child view currently being displayed in your activity with the specified id. In this case that is probably returningnull, hence the crash. Try it again withdialog.findViewById...