Create and Add TextView Progmmatically in android - android

I am trying to add TextView progmmatically into may RelativeLayout. it does not working.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rl = (RelativeLayout) findViewById(R.layout.activity_main);
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello World");
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rl.addView(tv);
setContentView(R.layout.activity_main);
}

The call to setContentView(R.layout.activity_main) will reinflate your layout, overriding the addition you made before it. Move it up, so it's the first thing after the super call. Something like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.layout.activity_main);
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello World");
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rl.addView(tv);
}

You should call setContentView(R.layout.activity_main) right after super.onCreate.
I've did this:
I went to main_activity.xml and added an attribute android:id="#+id/activity_main" to RelativeLayout.
Edited code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//RelativeLayout rl = (RelativeLayout) findViewById(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.activity_main);
TextView tv = new TextView(MainActivity.this);
tv.setText("Hello World");
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
rl.addView(tv);
}
But still, the textView will overlap other generated items on RelativeView. Maybe you should consider using LinearLayout with vertical orientation instead.

Related

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

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);
}
}

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 adapt the width of a LinearLayout in a RelativeLayout programmatically?

I have a main RelativeLayout object and I want add a LinearLayout object into this,but also by setting the LayoutParams of LinearLayout,the elements inside it,align to left,while I would like that the last element is attached at the right end of the window.
This my first test code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
setContentView(mainLayout);
LinearLayout Bottom = new LinearLayout(this);
Button scale = new Button(this);
scale.setText("Scale");
Button attach = new Button(this);
attach.setText("Attacch");
TextView text = new TextView(this);
text.setText("Something");
Bottom.addView(scale);
Bottom.addView(text);
Bottom.addView(attach);
mainLayout.addView(Bottom);
}
}
and this is the result:
if I edit the code in this way:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar= getActionBar();
RelativeLayout mainLayout = new RelativeLayout(this);
mainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
setContentView(mainLayout);
LinearLayout Bottom = new LinearLayout(this);
Button scale = new Button(this);
scale.setText("Scale");
Button attach = new Button(this);
attach.setText("Attacch");
attach.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView text = new TextView(this);
text.setText("Something");
text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
Bottom.addView(scale);
Bottom.addView(text);
Bottom.addView(attach);
mainLayout.addView(Bottom);
}}
I get this:
as you can see the attacch button disappears...
In short,I want center text and align attach button to the right
// try this way
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
RelativeLayout mainLayout = new RelativeLayout(this);
LinearLayout Bottom = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER;
Button scale = new Button(this);
LinearLayout.LayoutParams scaleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
scaleParams.gravity=Gravity.CENTER;
scale.setText("Scale");
Button attach = new Button(this);
LinearLayout.LayoutParams attachParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
attachParams.gravity=Gravity.CENTER;
attach.setText("Attacch");
TextView text = new TextView(this);
LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
textParams.gravity=Gravity.CENTER;
text.setText("Something");
Bottom.addView(scale,scaleParams);
Bottom.addView(text,textParams);
Bottom.addView(attach,attachParams);
mainLayout.addView(Bottom,layoutParams);
setContentView(mainLayout,new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT));
}

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);
}

Dynamically updating TabWidget icons?

Is it possible to update the TabWidget icons/indicators? If so, how, when you are creating TabContent through intents?
Edits below with answer thanks to #Bart
Generic code:
MainActivity:
public class TestActivity extends TabActivity {
public int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//TabHost mTabHost = (TabHost) findViewById(android.R.id.tabhost);
TabHost mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab1").setContent(
new Intent(this, OneActivity.class)).setIndicator("One"));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setContent(
new Intent(this, TwoActivity.class)).setIndicator("Two"));
changeTitle(0, 20);
}
public void changeTitle(int cnt, int i){
View v = getTabWidget().getChildAt(cnt);
TextView tv = (TextView) v.findViewById(android.R.id.title);
tv.setText("One ("+i+")");
}
}
OneActivity (ChildActivity):
public class OneActivity extends Activity {
TextView tv;
Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.one);
tv = (TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int i = Integer.parseInt((String) tv.getText());
i++;
tv.setText(""+i);
TestActivity parent = OneActivity.this.getParent();
parent.changeTitle(0,i);
}
});
}
}
I'd like to show the number in the Tab's title
It is possible but it is a bit of tricky:
ViewGroup vg = (ViewGroup) getTabHost().getTabWidget().getChildAt(0);
TextView tv = (TextView) vg.getChildAt(1);
You should pay attention to indices while calling getChildAt(). The best way is always to debug and check.
Since 1.6 TabWidget has a method getChildTabViewAt thanks to which you could skip the first line and write:
ViewGroup vg = (ViewGroup) getTabHost().getTabWidget().getChildTabViewAt(0);
I attach also source code of Android SDK to show how TabWidget icons are built:
View tabIndicator = inflater.inflate(R.layout.tab_indicator,
mTabWidget, // tab widget is the parent
false); // no inflate params
final TextView tv = (TextView) tabIndicator.findViewById(R.id.title);
tv.setText(mLabel);
final ImageView iconView = (ImageView) tabIndicator.findViewById(R.id.icon);
iconView.setImageDrawable(mIcon);
As you can see, it contains a TextView and ImageView.
Try get the imageView like this
ImageView v = (ImageView)tabActivity.getTabHost().getTabWidget().getChildTabViewAt(i).findViewById(android.R.id.icon);
v.setImageDrawable(getResources().getDrawable(R.drawable.newIcon));
Given a arrays for titles and icons
for (int i = 0; i < numTabs; i++ ) {
TabSpec spec = host.newTabSpec(String.valueOf(i)); //sets tab index
Drawable icon = res.getDrawable(icons[i]); //sets the icon
// set tab text and icon
spec.setIndicator(titles[i], icon); //sets the title
host.addTab(spec);
}

Categories

Resources