how to create button and image view in class that extend view? - android

I have a class that extend activity and that get its view in this way:
setContentView((View) viewClass);
viewClass is class that extends from view.
I need to implement some button and image view in this class. but how?

You need to extends your custom view from ViewGroup or kind of layout for example LinearLayout, this will help you to adding another views to your layout programmatically from activity like :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout view = new LinearLayout(this);
setContentView(view);
TextView tv = new TextView(this);
tv.setText("asas");
view.addView(tv);
}
}

You can do something like below
let your main activity be as below
public class MainActivity extends Activity{
MyView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Context ctx = getApplicationContext();
view=new MyView(ctx);
setContentView(view);
}
}
and let your view class extend any container layout.
public class MyView extends RelativeLayout{
public MyView(Context context) {
super(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(context);
tv.setText("hello");
tv.setId(1);
Button b = new Button(context);
params1.addRule(RelativeLayout.BELOW, tv.getId());
b.setText("Hi");
b.setId(2);
this.addView(tv);
this.addView(b, params1);
}
}

Related

How to design text view in android without layout use only java class code

I am designing an android application where I have not need to create any layout xml file, but create over the Java code itself. I try with this way:-
public class MainActivity extends Activity{
TextView tb;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setBackgroundResource(R.drawable.ch);
}
}
Use this code, this will work for you
public class Amit extends Activity{
TextView tb;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().getDecorView().setBackgroundResource(R.drawable.ch);
tb = new TextView(Amit.this);
tb.setText("hallo hallo");
tb.setId(5);
tb.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
setContentView(tb);
}
Use TextView like this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("Here is your textview");
ll.addView(tv);
}
You may try this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
TextView tv1 = new TextView(this);
tv1.setText("HELLO");
ll.addView(tv1);
setContentView(ll);
}
Try this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creating a new RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText("Test");
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
// Setting the parameters on the TextView
tv.setLayoutParams(lp);
// Adding the TextView to the RelativeLayout as a child
relativeLayout.addView(tv);
// Setting the RelativeLayout as our content view
setContentView(relativeLayout, rlp);
}
}

How to add relative layout to a View

I have a custom Relative Layout and added a button to it
RelativeLayout rel_layout = new RelativeLayout(mcontext);
RelativeLayout.LayoutParams rel_param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
rel.setLayoutParams(rel_param);
Button b = new Button(mcontext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
b.setLayoutParams(params);
b.setText("Test");
rel_layout.addView(b);
Now, I want this Relative layout to be added to a View. My View Class looks like this is
public class CustomView extends View {
Context mcontext;
public CustomView(Context context) {
super(context);
this.mcontext = context;
}
}
and in the Main Activity I am calling this view in my setConentView()
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CustomView(this));
}
}
So now on the Screen I should have the relative layout with the button in it. I should not use any XMl's.
I need help in how to add the dynamic relative created to be added to my CustomView class
Hope I have explained my problem clearly.
// try this
custom_view.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relCustomView"
android:background="#android:color/darker_gray"
android:padding="5dp">
</RelativeLayout>
public class CustomView extends View{
private Context context;
public CustomView(Context context) {
super(context);
this.context=context;
}
public View getCustomView(){
View v = LayoutInflater.from(context).inflate(R.layout.custom_view,null,false);
RelativeLayout relCustomView = (RelativeLayout) v.findViewById(R.id.relCustomView);
Button b = new Button(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
b.setLayoutParams(params);
b.setText("Test");
relCustomView.addView(b);
return v;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new CustomView(this).getCustomView());
}
You are not supposed to do that. RelativeLayout is a ViewGroup. ViewGroup is a View that can hold one or more Views as children. You should implement your own ViewGroup instead of View.
You can find a tutorial for implementing custom ViewGroup here:
https://developer.android.com/reference/android/view/ViewGroup.html
Also, I'd like to suggest that you might actually want to place RelativeLayout directly on your Activity and place some customized widgets on top of it along with the button you mentioned.
Your custom view has to extend a ViewGroup, not a View.

Adding Views to XML layout in Java

I already have an XML layout with some buttons in it, and now i want to add a textview to the same layout, but in my Java class. I don't get any errors until the "addView" line. I would also appreciate it if someone could tell me a better way to add onto a pre-existing XML layout in Java.
public class MyActivity extends Activity{
TextView textview;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout=(RelativeLayout)findViewById(R.id.mylayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParam(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textview=new TextView(this);
textview.setId(16);
textview.setText("Help");
layout.addView(textview, params);
setContentView(layout);
}
Your code should be like this, since you already have xml layout, you should first setContent to xml layout and then add new view to the Relativelayout.
public class MyActivity extends Activity{
TextView textview;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.file_name);
layout=(RelativeLayout)findViewById(R.id.mylayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParam(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textview=new TextView(this);
textview.setId(16);
textview.setText("Help");
layout.addView(textview, params);
}

Add Button on custom View in Android

I have the following class
public class GameActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View gameView = new GameView(this);
setContentView(gameView);
}
}
and now I'd like to add a Button to my class GameView.
public GameView(Context context) {
super(context);
// Code ...
}
I need this button during my game, so it should be alywas in front of all the other canvas' I'm drawing. How can I do that?
Do you want to create a new button?
Button b = new Button(context);
b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
gameView.addView(b);
Use ViewGroup as GameView parent insted of simple View
ViewGroup gameView = new GameView(this);
public class GameView extends ViewGroup { //...
View gameView = new GameView(this);
replace the above line by below line::
View gameView = new GameView(this).createView();
and now in the Game view createview with button and etc.
public class GameView extends View {
private Activity _activity;
public GameView (Activity _activity) {
super(_activity);
// TODO Auto-generated constructor stub
this._activity = _activity;
}
public View createView(){
LinearLayout l = new LinearLayout(_activity);
l.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(_activity);
btn.setId(1);
btn.setText("btn"+(i+1));
l.addView(btn);
return l;
}
}
try this working code:::

Adding View to a Custom ViewGroup

I am trying to add Views to a custom ViewGroup. The ViewGroup gets drawn, but no Views that were added to it are visible. LineView's (which extends a View) onDraw() method does not get called. What am I doing wrong?
public class TestActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ShapeView shapeView = new ShapeView(this);
shapeView.setBackgroundColor(Color.RED);
drawContainer = (RelativeLayout)findViewById(R.id.draw_container);
drawContainer.addView(shapeView);
}
}
public class ShapeView extends ViewGroup {
private LineView mLineView;
public ShapeView (Context context) {
super(context);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(200, 200);
this.setLayoutParams(p);
mLineView = new LineView(context);
this.addView(mLineView);
}
}
I maybe wrong but should you not be adding the shapeView to your layout "main"? What is drawContainer? I can't see it being instantiated and not in the docs. I assume main contains a relative/linear layout.
Access it by:
RelativeLayout rel = (RelativeLayout) findViewById(R.id.container);
(where the id is changed to match your id)
then:
rel.addView(shapeView);

Categories

Resources