Reputation: 11669
Summary
Adding android:layout_alignParentBottom="true"
to the TextView element located in the activity_main.xml positions the textview at the bottom of an Android App.
It is also possible to change the TextView position in Android using Java, e.g.:
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 48);
textView.setText("hello");
changes the font size of hello
.
Attempts
In order to position hello
at the bottom of the application, the list of possible methods was inspected and textView.setBottom(TRIM_MEMORY_BACKGROUND);
was chosen. Note: other constants than TRIM_MEMORY_BACKGROUND
could be specified, but in order to check the output of setBottom a random constant was chosen.
However, it is impossible to run the application as setBottom results in the following error:
Once @SuppressLint("NewApi")
has been added it is possible to run the Application, but hello
is not placed at the bottom.
According another Q&A the following code
RelativeLayout.LayoutParams params1 = new
RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textView.setLayoutParams(params1);
should be able to change the position of hello
, but this does not work either.
Code
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
// @SuppressLint("NewApi")
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(this);
TextView textView2 = new TextView(this);
// textView.;
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textView.setLayoutParams(params1);
// textView.setBottom(TRIM_MEMORY_BACKGROUND);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 48);
textView.setText("hello");
textView2.setText("world");
ll.addView(textView);
ll.addView(textView2);
setContentView(ll);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Views: 7502
Reputation: 3000
An Android View
is positioned within a ViewGroup
by modifying its layout parameters.
Each subclass of ViewGroup
has a corresponding subclass of layout parameters which must be used to achieve fine control using the features of that view group.
In this case, if you want to lay out your view using the features of RelativeLayout
, you need to use RelativeLayout
as your view group, and RelativeLayout.LayoutParams
as your layout parameters on each child view.
The original code is using layout parameters for a relative layout, but your view group is a LinearLayout
. This can't work as expected, but it won't trigger a compile time error or a class-cast exception because the setLayoutParams
call takes as argument the parent class of all layout parameter classes.
I ran test code modified as follows (simply by using a view group consistent with the layout manager) and it resolved the issue:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout ll = new RelativeLayout(this);
TextView textView = new TextView(this);
TextView textView2 = new TextView(this);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
textView.setLayoutParams(params1);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 48);
textView.setText("hello");
textView2.setText("world");
ll.addView(textView);
ll.addView(textView2);
setContentView(ll);
}
Upvotes: 2
Reputation: 115
RelativeLayout ll = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
ll.setLayoutParams(params);
TextView textView = new TextView(this);
// textView.;
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
textView.setLayoutParams(params1);
// textView.setBottom(TRIM_MEMORY_BACKGROUND);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 48);
textView.setText("hello");
ll.addView(textView);
setContentView(ll);
Upvotes: 2