Android: Add EditText to View error - android

bascially I want to have two views. One view is for the canvas, used for drawing rectangles and another view or adding of new editText boxes whenever a rectangle is drawn. When I run my program, an error occured "java.lang.NullPointerException". Is it possible to add the edittext boxes onto the canvas view so that I keep only one view?
My code are as follows:
public class MainActivity extends Activity {
public static DrawRect DR;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DR = new DrawRect(this);
rectbutton = (Button) findViewById(R.id.rectbutton);
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lp.addRule(RelativeLayout.BELOW, R.id.rectbutton);
DR.setLayoutParams(lp);
mainLayout.addView(DR);
rectbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
addRect(); // in my DrawRect class i have this method to draw rect on canvas
addEditText();
}// onclick
});
}
private void addEditText(){
RelativeLayout editTextLayout = new RelativeLayout(this);
EditText editText = new EditText(this);
editTextLayout.addView(editText);
mainLayout.addView(editTextLayout);
}
}
Please Advice. Thank you.

Which line its giving error. if its giving error in
mainLayout.addView(editTextLayout);
means mainLayout is null.you can add EditText in canvas.
the problem is in
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);
check the id in layout xml.

Related

Scrollview scrolls to origin when child is focused

I have a View (RelativeLayout), managed by a ScrollView.
The view holds an EditText. Whenever the EditText gets focused, either by click or a call of requestFocus() on it, the ScrollView jumps back to its top position. How is it possible to make the ScrollView stay at its actual position?
The code is as follows:
public class MyView extends RelativeLayout{
public MyView(Context context){
super(context);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int)(4 * 600));
lp.height = (int)(3000);
lp.width = ViewGroup.LayoutParams.MATCH_PARENT;
setLayoutParams(lp);
setBackgroundColor(0xddeeeeee);
EditText editText = new EditText(getContext());
editText.setLayoutParams(new ViewGroup.LayoutParams(150, 150));
editText.setX(200);
editText.setY(400);
editText.setBackgroundColor(0xff0000aa);
addView(editText);
}
and the activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout listView = (LinearLayout) findViewById(R.id.project_list);
ScrollView scrollView = new ScrollView(getApplicationContext());
MyView view = new MyView(getApplicationContext());
scrollView.addView(view);
listView.addView(scrollView);
}
Actually if you want your entire layout pan up than you should use :
SOFT_INPUT_ADJUST_PAN
meaning:
getActivity().getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
android:focusableInTouchMode="true"
android:focusable="true"
add these line to your Relative Layout which is inside Scroll View

Android Dyanmic buttons layout

When creating dynamic buttons I would like them to stack one under the other vertically. I am not sure how to create this effect.
for(int i = 0; i <notificationArrayList.size(); i++)
{
if(i == 0)
{lp.addRule(RelativeLayout.BELOW, R.id.searchButton);}
else
{} //maybe tell the code here to stack under the lastID?
Notification oNote = notificationArrayList.get(i);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText(oNote.NotificationText);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
rl.setLayoutParams(lp);
rl.addView(btn, lp);
}
Maybe in the else statement have it get the last id and add RelativeLayout that way?
The easiest way would be to put all the buttons in a LinearLayout and just add the LinearLayout beneath the search button. This produces easier code, but slightly worse drawing performance. Pseudocode would be like:
LinearLayout ll = new LinearLayout(context);
for(i=0; i<numButtons; i++) {
ll.addView(new Button(context));
}
RelativeLayout.LayoutParam lp = new RelativeLayout.LayoutParam();
lp.addRule(RelativeLayout.BELOW, R.id.searchButton);
relativeLayout.addView(ll,lp);
This example should give you an idea:
public class MyActivity extends Activity {
private RelativeLayout rel;
private EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
rel = (RelativeLayout) findViewById(R.id.main_rel);
editText = (EditText) findViewById(R.id.pref_edit_text);
Button button = new Button(this);
button.setText("Delete");
// create the layout params that will be used to define how your
// button will be displayed
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your button below your object (here a editText)
params.addRule(RelativeLayout.BELOW, editText.getId());
// set the layoutParams on the button
button.setLayoutParams(params);
// add button to your RelativeLayout
rel.addView(button);
}
}

Android custom view and visible button

I made my custom view and I want to add view.button, so I made this solution:
Public void onCreate(Bundle savedInstanceState)
{
Button up;
up = new Button(getApplicationContext());
up.setText("ahoj");
up.setHeight(100);
up.setWidth(100);
up.setTop(200);
up.setLeft(100);
LinearLayout layout = new LinearLayout(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(layout);
myview view = new myview(this);
layout.addView(view);
layout.addView(up);
I only see my view but no button. My view only draw some PNG file. Does anyone know where is the problem? Thanks much.
The most likely reason is that your custom view is added with layout params MATCH_PARENT. It takes the whole of the layout and the button is not visible. Try instead adding your custom view with WRAP_CONTENT params:
MyView view = new myview(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LAYOUTParams.WRAP_CONTENT)
layout.addView(view, lp);
You have the code right but in the wrong order. Try this:
Public void onCreate(Bundle savedInstanceState)
{
Button up;
LinearLayout layout = new LinearLayout(getApplicationContext());
up = new Button(getApplicationContext());
up.setText("ahoj");
up.setHeight(100);
up.setWidth(100);
up.setTop(200);
up.setLeft(100);
myview view = new myview(this);
layout.addView(view);
layout.addView(up);
setContentView(layout);
super.onCreate(savedInstanceState);
}

OnDraw() method of custom view only called once on button click

I'm trying to call onDraw method on Button click ,button the view is updated only once also onDraw method is called . But no change in the position of line.
I have this custom view
public LineSeekbar(Context context) {
super(context);
this.setWillNotDraw(false);
setNewX(130);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("GRAPH","draw");
paint = new Paint();
paint.setARGB(225, 215, 10, 20);
paint.setStrokeWidth(2);
paint.setStyle(Style.FILL);
canvas.drawLine(130,900,getNewX(),100, paint);
setNewX(getNewX()+15);
}
Calling this from activity class
final Bitmap mBackgroundImage = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
cv =new Canvas(mBackgroundImage);
LineView = new LineSeekbar(LineActivity.this);
LineView.setLayoutParams(new LayoutParams(500,500));
LineView.onMeasure(500,500);
LineView.invalidate();
LineView.draw(cv);
// LineView = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mImageView.setBackgroundColor(android.R.color.white);
mImageView.setImageBitmap( mBackgroundImage );
LinearLayout ll =(LinearLayout) findViewById(R.id.linearLayout1);
ll.addView(mImageView);
Button inc = (Button) findViewById(R.id.increase);
inc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LineView.invalidate();
LineView.draw(cv);
}
});
You should call an instance with small letter because it is not a class: lineView insted of LineView.
Since you are calling it from another thread (UI) you should say
LineView.postInvalidate();
When you create LineView instance for the first time, onDraw will be executed on its own and show the first line.
It is not clear what is LineView.draw(cv); You can draw the background image in onDraw just before drawing the line. You can use onSizeChange method in the custom view to find its real dimensions and resize your bitmap...
In onDraw insert a line
Log.e("LineView", Integer.toString(getNewX()));
and then in DDMS - LogCat watch the output when you press the button.
Try this:
You didn't made an object of the LineView class like you should. You should use small letters for instances.
final Bitmap mBackgroundImage = Bitmap.createBitmap(500,500, Bitmap.Config.RGB_565);
cv =new Canvas(mBackgroundImage);
LineView lineView = new LineSeekbar(LineActivity.this);
lineView.setLayoutParams(new LayoutParams(500,500));
lineView.onMeasure(500,500);
lineView.invalidate();
lineView.draw(cv);
// LineView = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mImageView.setBackgroundColor(android.R.color.white);
mImageView.setImageBitmap( mBackgroundImage );
LinearLayout ll =(LinearLayout) findViewById(R.id.linearLayout1);
ll.addView(mImageView);
Button inc = (Button) findViewById(R.id.increase);
inc.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
lineView.invalidate();
lineView.draw(cv);
}
});
Solved it ,I placed custom view in xml layout ,its working fine.

Panning video in runtime

I want to make the view move around the screen. Is that possible?
in other words, I want panning to be possible and I think that has something to do with the view.
How do you do Panning a video preview?
If you want to move your view all over the screen, its possible. Presuming this is indeed your requirement, here's what you could do. Make the view a child of Relative Layout. Everytime you want to move the view, get the RelativeLayout.LayoutParams of the child view, change relevent margins and set this as the child view's LayoutParam.
If you are doing this to a SurfaceView (needed to play the video), you get surfaceChanged callback everytime you change the margin.
Here's a sample code of the tweak I did for API Demos' CameraPreview activity which does the same. The SurfaceView is moved from left to right. Hope this helps.
Regards,
Anirudh.
public class CameraPreview extends Activity {
protected static final String TAG = "CameraPreview";
private Preview mPreview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Create our Preview view and set it as the content of our activity.
mPreview = new Preview(this);
mPreview.setId(100);
RelativeLayout mainLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams mainLp = new RelativeLayout.LayoutParams(640, 480);
mainLp.leftMargin = 20;
mainLayout.addView(mPreview, mainLp);
Button btn = new Button(this);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
RelativeLayout.LayoutParams nLp = (LayoutParams) mPreview.getLayoutParams();
nLp.leftMargin += 10;
Log.v(TAG,"nLp.leftMargin: " + nLp.leftMargin);
mPreview.setLayoutParams(nLp);
}
});
btn.setText("Click me!");
RelativeLayout.LayoutParams btnLp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
btnLp.addRule(RelativeLayout.BELOW, mPreview.getId());
mainLayout.addView(btn ,btnLp);
setContentView(mainLayout);
}
}

Categories

Resources