Scrollview scrolls to origin when child is focused - android

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

Related

Android: Add EditText to View error

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.

Having trouble positioning android dynamically loaded buttons

I'm creating buttons dynamically in my class, I try to position them using 'offsetLeftAndRight()' or '.leftMargin' and '.topMargin' as follows,
public class instruction extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.instruct);
final Button btn = new Button(this);
RelativeLayout.LayoutParams paramsd2 =
new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsd2.leftMargin = 500;
paramsd2.topMargin = 500;
paramsd2.height = 60;
paramsd2.width = 200;
btn.offsetLeftAndRight(300);
btn.setLayoutParams(paramsd2);
addContentView(btn, paramsd2);
}
But the button always stays in the top left corner, how can I position it, what am I doing wrong?
AddContentView() is not the proper way to add a view in an already set layout.
make your main layout a RelativeLayout (check this in the instruct.xml layout file)
use its id to retreive a reference on it in your onCreate() method using
myRelativeLayout = (RelativeLayout) this.findViewById(R.id.itsId)
then add your button to this layout :
myRelativeLayout.addView(myButton);
the layout params of your button seems fine for positioning so it should work.
Set margin on button rather then layout
MarginLayoutParams marginParams = new MarginLayoutParams(backToMainScreenImageView.getLayoutParams());
marginParams.setMargins(0, 0, (int) UIUtil.getRadialButtonMainMargin(this), 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
backToMainScreenImageView.setLayoutParams(layoutParams);
Try something like this :
paramsd2.setMargin(500, 500, 0, 0);
btn.setLayoutParams(paramsd2);

application starts in black screen - no view visible

I'm trying to setup a simple view onto the root of phoneGap but i get black screen.
This is the code of a class to inflate and load from xml:
public TopBar(Context context){
myActivity = (Activity)context;
}
protected View createTopBarView() {
RelativeLayout mainLayout = new RelativeLayout(myActivity);
mainLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LayoutInflater inflater = myActivity.getLayoutInflater();
View layout = inflater.inflate(R.layout.top_bar, null);
topBarView = (LinearLayout) layout.findViewById(R.id.linearLayoutTopBar);
/**
* Setting Buttons....
*/
//Adding the View
mainLayout.addView(topBarView);
return mainLayout;
}
The method returns a view, and in a class that extneds droidGap i do:
private void createLayout(){
mainRelative = new RelativeLayout(this);
mainRelative.setBackgroundResource(R.drawable.background);
View topBarView = new TopBar(this).createTopBarView();
mainRelative.setVisibility(View.VISIBLE);
mainRelative.addView(topBarView);
root.addView(mainRelative);
}
This function is called from onCreate, But i get a black screen when running.
Can you try to add
mainLayout.addView(layout);
instead of
mainLayout.addView(topBarView);
I might to help
EDIT:
Have you done this: setContentView(yourLayout, LayoutParams); in onCreate?

Activity.addContentView(View) == ViewGroup.addContentView(View)?

I have a question regarding Android Activitys:
An Activity has the Method addContentView(View) while a ViewGroup has a (similar?) addView(View) Method.
Unfortunately its undocumented where the View from addContentView is placed. Is it like a LinearLayout just adding the View to the bottom, or is it more like a FrameLayout, which adds its Views "onTop" ? Does it depend on the ViewGroup set by setContentView?
If I dive into the sources I see that addContentView will call Window's abstract Method addContentView. Unfortunately I cannot see which class is implementing this Method. So whats the behaviour of Activitys addContentView exactly?
The base layout of every activity is a FrameLayout. This means the layout you usually set via setContentView() is a child of this layout. addContentView() adds just another child, therefore it behaves like a FrameLayout (which means it adds new UI elements above existing ones).
You can check this by using a tool called hierachyviewer from your ANDROID_SDK\tools folder. Here are two screenshots:
This is the layout before calling addContentView(), my activity consists of the default FrameLayout, holding a LinearLayout with a Button (my layout here). This is reflected in the bottom row here, the other elements above are the title/statusbar.
After adding a TextView via addContentView() it looks like this. You can see that the base FrameLayout got a new child.
public void addContentView(View view,
LayoutParams params) {
mActivity.addContentView(view, params);
}
//
public static void SetActivityRoot(Activity c) {
View v = ((ViewGroup)c.findViewById(android.R.id.content)).getChildAt(0);
ScrollView sv = new ScrollView(c);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
sv.setLayoutParams(lp);
((ViewGroup) v.getParent()).removeAllViews();
sv.addView((View) v);
c.addContentView(sv, lp);
}
//
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout =
(LinearLayout)findViewById(R.id.mainlayout);
//newButton added to the existing layout
Button newButton = new Button(this);
newButton.setText("Hello");
mainLayout.addView(newButton);
//anotherLayout and anotherButton added
//using addContentView()
LinearLayout anotherLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearLayoutParams =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button anotherButton = new Button(this);
anotherButton.setText("I'm another button");
anotherLayout.addView(anotherButton);
addContentView(anotherLayout, linearLayoutParams);
}
}

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