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);
}
Related
I have an ImageView which I want it to enlarge once the app is launched
So, firstly I did this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.screenshot198);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(params);
layout.addView(imageView);
imageView.setPivotX(1.0f);
imageView.setScaleX(1.5f);
imageView.setScaleY(1.5f);
}
It works great!
Then I try to set it at bottom by adjusting the code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.screenshot198);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
imageView.setLayoutParams(params);
layout.addView(imageView);
imageView.setPivotX(1.0f);
imageView.setPivotY(1.0f);
imageView.setScaleX(1.5f);
imageView.setScaleY(1.5f);
}
Hey! What's going on with my tree!?
The only difference between these two paragraphs of code is that, I've added [params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);] in order to put image at bottom, and added [setPivotY(1.0f);] so the image should scale upward from bottom.
But it seems the [setPivotY(1.0f);] doesn't work properly. Anything I'm missing? Thanks!
Comment by #rupps surprisingly helped me. Changing to setPivotY(view.getHeight()) fixed my case.
When using setPivotY(view.getBottom()) I was getting an unexpected offset/margin when using setScaleY().
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.
I have recently coded my button to be aligned to the right of the screen and it seems to not want to work.
I have also checked to see if my parent layout is set to cover the whole width of the screen and it is.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//Vertical Layout - For the layout of the newSheetLayout.
newSheetLayoutV = new LinearLayout(this);
newSheetLayoutV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in Vertical layout. Used for the next, back and save buttons.
newSheetButtonLayoutH = new LinearLayout(this);
newSheetButtonLayoutH.setOrientation(LinearLayout.HORIZONTAL);
//Vertical Layout - For the layout of the newSheetLayout.
dataShowV = new LinearLayout(this);
dataShowV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in the dataShow V. Used for the data entry objects.
dataShowH = new LinearLayout(this);
dataShowH.setOrientation(LinearLayout.HORIZONTAL);
//Next button.
nextButton = new Button(this);
nextButton.setText("NEXT >");
nextButton.setOnClickListener(nextListener);
//Back button.
backButton = new Button(this);
backButton.setText("< BACK");
backButton.setOnClickListener(backListener);
backButton.setEnabled(false);
//Save button.
saveButton = new Button(this);
saveButton.setText("Save");
saveButton.setOnClickListener(saveListener);
saveButton.setEnabled(false);
//Addition of the buttons to the button view in order that needs to be seen.
newSheetButtonLayoutH.addView(backButton);
newSheetButtonLayoutH.addView(nextButton);
newSheetButtonLayoutH.addView(saveButton);
//Layout Param's for the buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newSheetButtonLayoutH.setLayoutParams(params);
//Set save to anchor right.
LinearLayout.LayoutParams saveButtonParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
saveButtonParam.gravity = Gravity.RIGHT;
saveButton.setLayoutParams(saveButtonParam);
//Add all the layout Views to the main layout view.
newSheetLayoutV.addView(dataShowV);
newSheetLayoutV.addView(newSheetButtonLayoutH);
newSheetLayoutV.setGravity(Gravity.BOTTOM);
setContentView(newSheetLayoutV);
}
If it is something simple and I'm very blind to the solution, I will punish myself accordingly.
Thank you for your help.
try
saveButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.RIGHT);
if you are using xml and your need is not to do it in dynamic way then do
android:layout_gravity="right"
Edit:
i think this is what you want
LinearLayout newSheetLayoutV,dataShowV,dataShowH,newSheetButtonLayoutH,newlayout;
Button nextButton,backButton,saveButton;
//RelativeLayout ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
newSheetLayoutV = new LinearLayout(this);
newSheetLayoutV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in Vertical layout. Used for the next, back and save buttons.
newSheetButtonLayoutH = new LinearLayout(this);
newSheetButtonLayoutH.setOrientation(LinearLayout.HORIZONTAL);
newlayout = new LinearLayout(this);
newlayout.setOrientation(LinearLayout.VERTICAL);
newlayout.setGravity(Gravity.RIGHT);
newlayout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
//newSheetButtonLayoutH = new RelativeLayout(this);
//Vertical Layout - For the layout of the newSheetLayout.
dataShowV = new LinearLayout(this);
dataShowV.setOrientation(LinearLayout.VERTICAL);
//Horizontal layout - nested in the dataShow V. Used for the data entry objects.
dataShowH = new LinearLayout(this);
dataShowH.setOrientation(LinearLayout.HORIZONTAL);
//Next button.
nextButton = new Button(this);
nextButton.setText("NEXT >");
//nextButton.setOnClickListener(nextListener);
//Back button.
backButton = new Button(this);
backButton.setText("< BACK");
//backButton.setOnClickListener(backListener);
backButton.setEnabled(false);
//Save button.
saveButton = new Button(this);
saveButton.setText("Save");
//saveButton.setOnClickListener(saveListener);
saveButton.setEnabled(false);
LinearLayout.LayoutParams saveButtonParam = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//saveButtonParam.gravity = Gravity.RIGHT;
saveButton.setLayoutParams(saveButtonParam);
//Addition of the buttons to the button view in order that needs to be seen.
newSheetButtonLayoutH.addView(backButton);
newSheetButtonLayoutH.addView(nextButton);
newlayout.addView(saveButton);
newSheetButtonLayoutH.addView(newlayout);
//Layout Param's for the buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
newSheetButtonLayoutH.setLayoutParams(params);
//Set save to anchor right.
//Add all the layout Views to the main layout view.
newSheetLayoutV.addView(dataShowV);
newSheetLayoutV.addView(newSheetButtonLayoutH);
newSheetLayoutV.setGravity(Gravity.BOTTOM);
setContentView(newSheetLayoutV);
}
Try giving RelativeLayout params
RelativeLayout.LayoutParams rel_lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rel_lp.addRule(RelativeLayout.ALIGN_RIGHT);
rel_lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
btn.setLayoutParams(rel_lp);
Try setting the layout_gravity of LinearLayout instead of your Button.
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);
}
}
I am making an Android App and i need one of my Layouts to have Zoom control and have it be Scrollable Vertical, but i don't know how to successfully do this?
use this jar in your application
private ZoomView zoomView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zoomable);
View v = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.zoomableview, null, false);
v.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
zoomView = new ZoomView(this);
zoomView.addView(v);
main_container = (LinearLayout) findViewById(R.id.main_container);
main_container.addView(zoomView); }