TextView improperly displaying in Layout in Android 6 and below - android

I am dynamically creating views in onCreate() and then use setMargins() and setLayoutParams() to position them on the layout.
This works as expected in Android 7 but the parameters are ignored in Android 6 and below.
I assume it has something to do with views not being created in onCreate() or something like that but dont know why it would work in Android 7, or how to get around it.
Below is example code of the problem and some screenshots for visualization.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewGroup viewGroup = (ViewGroup) findViewById(android.R.id.content);
RelativeLayout.LayoutParams viewParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParam.setMargins(200, 400, 0, 0);
TextView myTextView = new TextView(this);
myTextView.setText("Hello this is my text");
myTextView.setTextSize(
TypedValue.COMPLEX_UNIT_PX, 50);
myTextView.setGravity(Gravity.CENTER);
myTextView.setLayoutParams(viewParam);
viewGroup.addView(myTextView);
}

Related

method returning Layout or View

The app I am trying to make, has got a lot of similar LinearLayouts and textViews that need to be created programmatically and placed on the screen in a specific order.
So I decided to define a method which returns one element, and for the furthur uses,I will put the method in some loop to produce the others. but when I create a view or layout this way, nothing shows up or sometimes the app crashes, as if it's been sent null to addView(). It only works when I create the View/Layout in onCreate() and then I use it right there afterwards.So , any ideas that I can use the method to creat my Layout/View? Because they are too many and it's not possible to create them one by one in onCreate()
Here's the method:
public LinearLayout createLinearLayout(){
TextView tv_day = new TextView(this);
tv_day.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
tv_day.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
tv_day.setGravity(Gravity.END);
tv_day.setText("27");
LinearLayout ll_horizontal = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams ll_horizontal_params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll_horizontal.setLayoutParams(ll_horizontal_params);
ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
ll_horizontal.addView(tv_day);
return ll_horizontal;
}
and this is onCreate() which doesn't add any linear layouts with a textView in it :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_month_view);
LinearLayout ll= createLinearLayout();
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.activity_month_view);
mainLayout.addView(ll);
}
I think this should help
- add an empty linear layout in XML with some id.
- reference that layout in code
- add elements to that layout dynamically
Hey was just checking your code. Its working perfectly now just try this method.
public LinearLayout createLinearLayout(){
TextView tv_day = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tv_day.setLayoutParams(layoutParams);
tv_day.setGravity(Gravity.CENTER);
tv_day.setText("27");
LinearLayout ll_horizontal = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams ll_horizontal_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll_horizontal.setLayoutParams(ll_horizontal_params);
ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
ll_horizontal.addView(tv_day);
return ll_horizontal;
}

setPivotY() behave strange on Android

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().

How to show ImageView Over Background programatically

relativeLayout.setBackgroundResource(R.drawable.b);
setContentView(relativeLayout);
img1.setImageResource(R.drawable.d);
LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(100,100);
img1.setLayoutParams(parms);
setContentView(img1, parms);
(Part of them)
I set a default background and want to change this b.png after 5 seconds. These lines are in after 5 secs handler method. The above 2 lines make me see new b.png at background. But there is an error when I set imageview over it. I also tried ImageView by adding on xml at origin and to appear as view.visible. But it can't. Only background image is available and no overlapping image on it is denied.
Try this
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout relativeLayout = new RelativeLayout(this);
ImageView img1 = new ImageView(this);
relativeLayout.setBackgroundResource(R.drawable.b);
setContentView(relativeLayout);
img1.setImageResource(R.drawable.d);
RelativeLayout.LayoutParams parms = new RelativeLayout.LayoutParams(100,100);
img1.setLayoutParams(parms);
relativeLayout.addView(img1, parms);
}

Android - Layout in JAVA File

As per my requirement, i should give the orientation in horizontal only, if i give 3 sentences in 3 text views, if there is no space for third sentence then it should come in the next line in the first position ...
public class MainActivity extends Activity {
private LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 15, 10, 10);
layout.setOrientation(LinearLayout.HORIZONTAL);
android.view.ViewGroup.LayoutParams params;
TextView tvTextsecond = new TextView(this);
tvTextsecond.setText("Heywhatrudoingtoday");
tvTextsecond.setLayoutParams(layoutParams);
tvTextsecond.setBackgroundColor(Color.RED);
tvTextsecond.setTextColor(Color.WHITE);
TextView tvTextthird = new TextView(this);
tvTextthird.setText("Haiitssundaytowork");
tvTextthird.setLayoutParams(layoutParams);
tvTextthird.setBackgroundColor(Color.BLUE);
tvTextthird.setTextColor(Color.WHITE);
TextView tvTextfourth = new TextView(this);
tvTextfourth.setText("Owebullshitruuselessfellow");
tvTextfourth.setLayoutParams(layoutParams);
tvTextfourth.setBackgroundColor(Color.YELLOW);
tvTextfourth.setTextColor(Color.WHITE);
layout.addView(tvTextsecond);
layout.addView(tvTextthird);
layout.addView(tvTextfourth);
}
private void findViewById() {
layout = (LinearLayout) findViewById(R.id.flowLayout);
}
}
Linear Layout doesn't behave that way. If there is no space, it would go out of your display area. So, for your requirement, you can't use Linear Layout here.

Android custom view and visible button

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

Categories

Resources