I have a custom layout java file that I create and add views to like this:
PredicateLayout layout = new PredicateLayout(this);
for (int i = 0; i < someNumberThatChangesEveryTime; i++)
{
TextView t = new TextView(this);
t.setText("Hello");
t.setBackgroundColor(Color.RED);
t.setSingleLine(true);
layout.addView(t, new PredicateLayout.LayoutParams(2, 0));
}
setContentView(layout);
What I would like to do is define my TextView in an xml, so I can add it like so:
TextView t = (TextView) findViewById(R.id.textview);
And of couse also add other kinds of views. I have no idea how to write this xml file, or how to connect it to this activity. The layout I'm using is this one: Line-breaking widget layout for Android
Find the id of the root layout in xml and add other ui elements programatically to the root layout.
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
andorid:id="#+id/ll
tools:context=".MainActivity" >
<TextView
android:id="#+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Blank text" />
</RelativeLayout>
Your MainActivity
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout ll = (RelativeLayout) findViewById(R.id.ll);
TextView tv= (TextView) findViewById(R.id.tv);
// add other ui elements to the root layout ie Relative layout
}
}
Related
So I have researched everywhere but no else seems to have this problem probably because they know how to program, but anyways I'm trying to add a TextView to a Relative Layout in my Android app, but no matter what I do I cannot get the text and the buttons I already have in the layout.
Here is my code for the activity:
public class EnterApp extends Activity {
int marblesInJar=0;
int targetMarblesInJar=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent= getIntent();
targetMarblesInJar= intent.getIntExtra("MESSAGE_marbles",0);
RelativeLayout marble= new RelativeLayout(this);
TextView textView = new TextView(getApplicationContext());
textView.setTextSize(20);
textView.setText("Your marble jar will be full when it has "+ targetMarblesInJar + " marbles. You have " + marblesInJar + " marbles.");
textView.setTextColor(Color.BLACK);
marble.addView(textView);
this.setContentView(R.layout.activity_enter_app);
// Show the Up button in the action bar.
setupActionBar();
}
here is my code for the xml relative layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".EnterApp" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button2"
android:layout_alignBottom="#+id/button2"
android:layout_marginRight="16dp"
android:layout_toLeftOf="#+id/button2"
android:onClick="addMarble"
android:text="#string/addMarble" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="82dp"
android:layout_marginRight="22dp"
android:onClick="lossMarble"
android:text="#string/loseMarble" />
</RelativeLayout>
You already have the RelativeLayout so you don't need to do
RelativeLayout marble= new RelativeLayout(this);
this creates a new RelativeLayout. Just create your TextView and add it to this layout after you inflate it with setContentView()
public class EnterApp extends Activity {
int marblesInJar=0;
int targetMarblesInJar=0;
RelativeLayout marble;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setContentView(R.layout.activity_enter_app);
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText("Your marble jar will be full when it has "+ targetMarblesInJar + " marbles. You have " + marblesInJar + " marbles.");
textView.setTextColor(Color.BLACK);
marble = (RelativeLayout) findViewById(R.id.relativelayout); // id you gave your root layout
marble.addView(textView);
In your onCreate() method you have the setContentView() method which sets the activity's view to the layout file defined in it. Right Now the parent view of your activity is the relative layout with id "relativeLayout". You need to add your textview to that relativeLayout for it to show up. The code for that is as follows:
RelativeLayout parent = (RelativeLayout) findViewById(R.id.relativeLayout);
parent.addView(textView);
It should show up now.
This is my Project XML Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flowLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- enter code here -->
</LinearLayout>
I need to access it in the JAVA file which should contain 3 sentences in 3 text views ...
public class MainActivity extends Activity {
private View layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();
//enter code here
}
private void findViewById() {
View layout = findViewById(R.id.flowLayout);
//enter code here
}
}
To add a TextView to your layout do this:
LinearLayout layout = (LinearLayout)findViewById(R.id.flowLayout); //depending on which layout is it.
Then, Create a TextView, set it text and add it to the layout:
TextView tvText = new TextView(this);
tvText.setText("your desired sentence");
layout.addView(tvText);
and do this for all 3 of your TextViews, for setting the height and width use LayoutParams (example):
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 2, 0);
As you can see this is the way you set Mergins as well, in the end set those params to the textView:
tvText.setLayoutParams(layoutParams);
I just started android and I tried below questions to get this answer before posting it here:
Android - Adding layout at runtime to main layout
Add button to a layout programmatically
Dynamically adding a child to LinearLayout with getting each child's position
And I am still not able to add a button to linear layout :(
Below is the code for activity, please let me know where I am wrong:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.activity_main, null);
Button btn = new Button(this);
btn.setId(123);
btn.setText("Welcome to WI FI World");
layout.addView(btn);
}
And xml looks like below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
Try assigning an id to your layout then add the button to the layout.
Im pretty sure those 2 layouts are not the same, so you are infact adding a button to a layout that is never displayed.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.lnr_main);
Button btn = new Button(this);
btn.setId(123);
btn.setText("Welcome to WI FI World");
layout.addView(btn);
}
With Layout assigned an id
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lnr_main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
Take the Id for LinearLayout in XML and in jave code use that id of LinearLayout from XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linear" >
</LinearLayout>
In onCreate():
LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
//Select widgets
linear.addView()
Give an id to your linearLayout.
Then in your code
LinearLayout layout = (LinearLayout)findViewById(R.id.given_id);
Keep the rest the same, should work.
I have an android xml layout, main.xml. I would like to add controls to this layout at runtime (I would like to add a series of additional linear layouts that contain buttons controls). Can I do that and if yes, how?
Thanks
I see the error u r doing here
LinearLayout mainLayout = (LinearLayout) findViewById(R.layout.main);
You r taking the layout as Linearlayout object, you should take the LinearLayout id
Try this
LinearLayout lnr = (LinearLayout) findViewById(R.id.LinearLayout01);
Button b1 = new Button(this);
b1.setText("Btn");
lnr.addView(b1);
You can add controls programmatically if you want in your code, or even another XML with a View and an Inflater.
Here you can read the basics: http://developer.android.com/guide/topics/ui/declaring-layout.html
Ok, I have got it to work.
The steps are the following:
First inflate the xml layout, ie,
View view = View.inflate(this, R.layout.main, null);
Then instantiate the container object from the xml layout into a ViewGroup class, ie,
ViewGroup container = (ViewGroup) view.findViewById(R.id.myContainer);
Then create a linearLayout object, create and add onto that any controls needed, add the linearLayout to the container object and use setContentView on the view object, ie,
container.addView(buttonsLayout);
this.setContentView(view);
You can do this quite easy by setting an id on the layout on which you want to add views to. Say your main.xml look like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/label"
android:layout_width="fill_parent"/>
<LinearLayout android:id="#+id/container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
Lets assume that you want to add your additional views to the LinearLayout with id id/container. In your onCreate method you could retrieve that object for later use:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContainer = (ViewGroup)view.findViewById(R.id.container);
}
Now you are all set to add other views to your container ViewGroup:
LinearLayout theButtons = getButtons()
mContainer.addView(theButtons);
In the getButtons method you need to create your LinearLayout containing the buttons you need. Either you do this programmatically or by inflating a view defined in an XML file. See LayoutInflater.inflate.
just try this:
LinearLayout mainLinearLayout = (LinearLayout) findViewById(R.layout.llmain);
now create button dynamically like this
Button btn1 = new Button(this);
btn1.setText=("Button 1");
mainLinearLayout .addView(btn1);
now if you want to add onether linearlayout then add it below button then
LinearLayout llinner = new LinearLayout(this);
Button btn2 = new Button(this);
btn2.setText=("Button 2");
mainLinearLayout .addView(btn2);
llinner.addView(btn2 );
mainLinearLayout .addView(llinner);
Try this :
LinearLayout ll =(LinearLayout)findViewById(R.id.linlay);
Button b = new Button(this);
b.setText("Hello");
l.addView(b);
This might help you
Here is what I did to display a set of runtime buttons on table layout.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = View.inflate(this, R.layout.activity_main, null);
TableRow myrow = (TableRow) view.findViewById(R.id.myrow);
Button btn1 = new Button(this);
btn1.setText("Button 1");
myrow .addView(btn1);
Button btn2 = new Button(this);
btn2.setText("Button 2");
myrow .addView(btn2);
this.setContentView(view);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:id="#+id/myview"
android:layout_width="404dp"
android:layout_height="691dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableLayout
android:id="#+id/mylayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow android:id="#+id/myrow"></TableRow>
</TableLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
AVD Pixel4API30
Output screenshot
I'm new to android platform , I'd like to settext using textviews , I tried to write set text into two textviews but its just draws one textview why?
I can't draw two textviews
TextView tv1;
TextView tv2;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
tv1 = new TextView(this);
tv2 = new TextView(this);
tv1.setText("Hello");
tv2.setText("How are you?");
}
On Android, the user interface normally should be created using XML-files, instead of Java code. You should read up on the tutorials on android.com, especially:
http://developer.android.com/guide/topics/ui/declaring-layout.html
An example:
In your res/layout/main.xml, you define the text TextView's:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="#+id/TextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 1"/>
<TextView android:id="#+id/TextView2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="TextView 2"/>
</LinearLayout>
Then if you use setContentView in the activity to display this, the app will show to TextView's:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
}
If you want to programmatically set the text in the Activity, just use findViewById():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
((TextView)this.findViewById(R.id.TextView1)).setText("Setting text of TextView1");
}
I definitely second TuomasR's suggestion to use XML layouts. However, if you're wanting to add new TextViews dynamically (i.e. you don't know how many you will need until runtime), you need to do a couple of other steps to what you are doing:
First, define your LinearLayout in main.xml (it's just easier that way than LayoutParams, IMO):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/my_linear_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
Now, you can go to your code, and try the following:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//This inflates your XML file to the view to be displayed.
//Nothing exists on-screen at this point
setContentView(R.layout.main);
//This finds the LinearLayout in main.xml that you gave an ID to
LinearLayout layout = (LinearLayout)findViewById(R.id.my_linear_layout);
TextView t1 = new TextView(this);
TextView t2 = new TextView(this);
t1.setText("Hello.");
t2.setText("How are you?");
//Here, you have to add these TextViews to the LinearLayout
layout.addView(t1);
layout.addView(t2);
//Both TextViews should display at this point
}
Again, if you know ahead of time how many views that you need, USE XML.