Refreshing a LinearLayout after adding a view - android

I'm trying to add views dynamically to a linearlayout.
I see through getChildCount() that the views are added to the layout, but even calling invalidate() on the layout doesn't give me the childs showed up.
Am I missing something?

A couple of things you can check in your code:
On the View that is being added, check that you call its setLayoutParameter method with an appropriate ViewGroup.LayoutParameter.
When you adding the new Views, make sure you are doing it on the UI thread. To do this, you can use the parent View's post method.
This self contained example adds a TextView after a short delay when it starts:
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class ProgrammticView extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));
setContentView(layout);
// This is just going to programatically add a view after a short delay.
Timer timing = new Timer();
timing.schedule(new TimerTask() {
#Override
public void run() {
final TextView child = new TextView(ProgrammticView.this);
child.setText("Hello World!");
child.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
// When adding another view, make sure you do it on the UI
// thread.
layout.post(new Runnable() {
public void run() {
layout.addView(child);
}
});
}
}, 5000);
}
}

I had the same problem and noticed that my overriden onMeasure() method wasn't called after the invalidate. So I created my own subroutine in the LinearLayout and called it before the invalidate() method.
Here is the code for an vertical LinearLayout:
private void measure() {
if (this.getOrientation() == LinearLayout.VERTICAL) {
int h = 0;
int w = 0;
this.measureChildren(0, 0);
for (int i = 0; i < this.getChildCount(); i++) {
View v = this.getChildAt(i);
h += v.getMeasuredHeight();
w = (w < v.getMeasuredWidth()) ? v.getMeasuredWidth() : w;
}
height = (h < height) ? height : h;
width = (w < width) ? width : w;
}
this.setMeasuredDimension(width, height);
}

I spent a lot of time to solve this problem too. And I found a simple method of refresh LinearLayout in 3 lines of code
You must set transperent color in style.xml
<color name="transparent">#00000000</color>
And in the code just call to set background
LinearLayout ll = (LinearLayout) findViewById(R.id.noteList);
ll.setBackgroundColor(getResources().getColor(R.color.transparent));
ll.invalidate();
If you has drawable background call
ll.setBackgroundResource(R.drawable.your_drawable);

Related

How to use LinearLayout.LayoutParams to adjust weights in ScrollView (Android Studio)

I've been following [this] answer to try to correct the heights of some image buttons in a scroll view, since I can't use linear layout weights. This is the code I've come up with:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_plant_list);
final ImageButton buttonPlant1= (ImageView) findViewById(R.id.buttonPlant1);
final ScrollView scrollViewPlant = (ScrollView) findViewById(R.id.scrollViewPlant);
scrollViewPlant.post(new Runnable() {
#Override
public void run() {
int scrollPlantHeight = scrollViewPlant.getHeight();
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, scrollPlantHeight / 3.69);
buttonPlant1.setLayoutParams(layoutParams);
}
});
}
I keep getting an alert in my code telling me that I need to import a class before the "LayoutParams.MATCH_PARENT."
When I import a class (I'm not even sure which one to import, but all I've tried is LinearLayout.LayoutParams), I get another alert telling me that it "Cannot resolve constructor 'LayoutParams(int, double)'" at the LayoutParams.MATCH_PARENT.
What class should I import? If any? Or is there something else I'm missing?
LayoutParams Class doesn't have constructor with (int, double).
Try casting the second parameter to integer.
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, (int)(scrollPlantHeight / 3.69));

Android. Change margin of LinearLayout on runtime

I am trying to move a LinearLayout view by modifying it's margins using an interface and following code:
#Override
public void onListScroll(int offset) {
tabBarOffset += offset;
if (tabBarOffset < 0) tabBarOffset = 0;
if (tabBarOffset > 50) tabBarOffset = 50;
View tabBar = findViewById(R.id.movingTabBar);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) tabBar.getLayoutParams();
params.topMargin = tabBarOffset;
}
the code works with the first call -- when the activity is created. But whenever the code block is called after, no changes in the view are being happened. I can confirm that the margin parameter is trully being changed, because of three facts :
1) the code works on start up
2) logging the marginTop value always gives new values ( it is being changed)
3) in the Hierarchy View i could see the new margin value
so i am thinking i just have to call some method to update the view itself? to make it redraw?
or may be i have to call some code on the UI thread? because this code is being run from interface callback.
try this:
#Override
public void onListScroll(final int offset) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
tabBarOffset += offset;
if (tabBarOffset < 0) tabBarOffset = 0;
if (tabBarOffset > 50) tabBarOffset = 50;
ViewGroup tabBar = findViewById(R.id.movingTabBar);
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) tabBar.getLayoutParams();
params.topMargin = tabBarOffset;
tabBar.requestLayout();
}
});
}

Adjust size and gap between FancyButton placed on LinearLayout

I have placed programmatically generated FancyButtons on LinearLayout. But, the generated buttons are placed too compactly, in other words, there is no separation between two successive buttons. Also, I want the buttons to stretch entire with of the layout. I tried btnWordList.setMinimumWidth(MATCH_PARENT) without any result. Please find the code below.
FlexDict.java
package in.dipanjan.flexdict;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.graphics.Color;
import android.content.Intent;
import android.widget.LinearLayout;
import android.graphics.PixelFormat;
import mehdi.sakout.fancybuttons.FancyButton;
import android.support.v7.app.ActionBarActivity;
public class FlexDict extends ActionBarActivity implements View.OnClickListener {
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int listCount, wordLists = 5;
/* http://stackoverflow.com/questions/19078461/android-null-pointer-exception-findviewbyid */
setContentView(R.layout.activity_flex_dict);
LinearLayout container = (LinearLayout)findViewById(R.id.container);
for(listCount = 1; listCount <= wordLists; listCount++)
{
/* https://github.com/medyo/fancybuttons */
FancyButton btnWordList = new FancyButton(this);
btnWordList.setId(listCount);
btnWordList.setText("WordList " + listCount);
btnWordList.setBackgroundColor(Color.parseColor("#3b5998"));
btnWordList.setFocusBackgroundColor(Color.parseColor("#5474b8"));
btnWordList.setTextSize(20);
btnWordList.setIconResource("\uf04b");
btnWordList.setRadius(10);
btnWordList.setOnClickListener(this);
container.addView(btnWordList);
}
setContentView(container);
}
#Override
public void onClick(View view) {
int wordList = view.getId();
/*
* http://www.java-samples.com/showtutorial.php?tutorialid=1525
* http://stackoverflow.com/questions/7980627/pressing-back-button-did-not-go-back-to-previous-activity-android
*/
Bundle params = new Bundle();
params.putInt("WordList", wordList);
Intent intent = new Intent(this, ShowList.class);
intent.putExtras(params);
startActivity(intent);
}
}
activity_flex_dict.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/radialback">
</LinearLayout>
UI
http://s26.postimg.org/rkb0r4ys9/Fancy_Button.png
You need to setLayoutParams on your View.
i.e. btnWordList.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
You need to set layout params for any view even if instantiated in XML or Programatically.
http://developer.android.com/reference/android/view/View.html#setLayoutParams(android.view.ViewGroup.LayoutParams)
Set the layout parameters associated with this view. These supply parameters to the parent of this view specifying how it should be arranged. There are many subclasses of ViewGroup.LayoutParams, and these correspond to the different subclasses of ViewGroup that are responsible for arranging their children.
You need to set a margin between your buttons to make a gab between your buttons. and set your width to match_parent to make the button stretch to the width of the layout
int marginBottom = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
5,
r.getDisplayMetrics()
);
for(listCount = 1; listCount <= wordLists; listCount++)
{
/* https://github.com/medyo/fancybuttons */
FancyButton btnWordList = new FancyButton(this);
btnWordList.setId(listCount);
LayoutParams params = new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(0, 0, 0, marginBottom);
btnWordList.setLayoutParams(params);
btnWordList.setText("WordList " + listCount);
btnWordList.setBackgroundColor(Color.parseColor("#3b5998"));
btnWordList.setFocusBackgroundColor(Color.parseColor("#5474b8"));
btnWordList.setTextSize(20);
btnWordList.setIconResource("\uf04b");
btnWordList.setRadius(10);
btnWordList.setOnClickListener(this);
container.addView(btnWordList);
}
You can use LayoutParam with margin
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
lp.bottumMargin = 2;
// or
lp.setMargins(0,0,0,2);
btnWordList.setLayoutParams(lp);

Android - removing layout programmatically

I have an activity that makes a layout programmatically from a Shared Preference using a for loop. The text views and buttons are enclosed in a linear layout. The user can input as many views as he wants. Now, the button will be a delete button. When pressed, I want to delete the linear layout the button and the other textviews are contained. How do I do this?
HERE IS MY CODE:
package com.dirkjan.myschools;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
LinearLayout subjectLeft, subjectRight;
Button addSubj;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subjectLeft = (LinearLayout) findViewById(R.id.llSubjectLeft);
subjectRight = (LinearLayout) findViewById(R.id.llSubjectRight);
//Load the saved subjects
SharedPreferences getSubjects = getSharedPreferences("SubjectInfo_Prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = getSubjects.edit();
int subjectCount = getSubjects.getInt("count", 0);
if (subjectCount > 0 ){
for (int i = 1; i <= subjectCount; i++){
//Set the linear layout for each subject
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
float scale = getResources().getDisplayMetrics().density;
//SET BOTTOM MARGIN
float margin = 5; //RESIZE MARGIN HERE!
int margs = (int) (margin * scale + 0.5f);
//SET PADDING IN DP
float padding = 5; //RESIZE PADDING HERE!
int pads = (int) (padding * scale +0.5f);
llParams.setMargins(0,0,0,margs);
//SETTING THE LINEARLAYOUT PARAMS
ll.setLayoutParams(llParams);
ll.setPadding(pads, pads, pads, pads);
//SETTING THE BACKGROUND COLOR OF THE LINEAR LAYOUT
String chosenColor = getSubjects.getString("chosenColor" + i, "BLUE");
if (chosenColor.equals("Green")){
ll.setBackgroundResource(R.color.HoloGreen);
}else if (chosenColor.equals("Blue")){
ll.setBackgroundResource(R.color.HoloBlue);
}else if (chosenColor.equals("Gray")){
ll.setBackgroundResource(R.color.HoloGray);
}else if (chosenColor.equals("Orange")){
ll.setBackgroundResource(R.color.HoloOrange);
}else {
ll.setBackgroundResource(R.color.HoloYellow);
}
//ADDING THE LAYOUT TO THE APPROPRIATE CONTAINER (LEFT OR RIGHT)
if (i % 2 == 1){
subjectLeft.addView(ll);
} else {
subjectRight.addView(ll);
}
//SETTING THE SUBJECT NAME TEXTVIEW
TextView SubjectName = new TextView(this);
SubjectName.setText(getSubjects.getString("subjectName" + i, "Error"));
SubjectName.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
SubjectName.setTextSize(22);
SubjectName.setTypeface(Typeface.DEFAULT_BOLD);
//SETTING THE SUBJECT NUMB TEXT VIEW
TextView SubjectNumber = new TextView(this);
SubjectNumber.setText(getSubjects.getString("subjectNumb" + i, "Error"));
SubjectNumber.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
SubjectNumber.setTextSize(16);
//Creating the divider line
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2);
divider.setLayoutParams(dividerParams);
divider.setBackgroundResource(R.color.Black);
//Add Views into the Layout
ll.addView(SubjectNumber);
ll.addView(SubjectName);
ll.addView(divider);
}
}
addSubj = (Button) findViewById(R.id.buttonPlusSubject);
addSubj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent toAddSubj = new Intent(MainActivity.this,
AddSubjectActivity.class);
startActivity(toAddSubj);
finish();
}
});
}
#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;
}
}
Please do take note that no id is assigned for each layout. It would help if there is a code to identify the parent of the parent of the button (The button is in a relative layout, which is in a linear layout where the linear layout must be removed by clicking the button.
First find your parent layout using
ll = (LinearLayout) findViewById(R.id.main_linearlayout);
get the child layout using
final LinearLayout child = (LinearLayout) ll.findViewById(count);
now to remove the whole layout you can use removeview() method as below
ll.removeView(child);
to only remove all views from the particular layout(here for eg. child) you can use
child.removeAllViews();
You can call view.setVisiblility(View.GONE) if you want to remove it from the layout, or view.setVisibility(View.INVISIBLE) if you just want to hide it.
You can remove a Child View from a parent by calling removeView(View view), for example like this :
parent.removeView(child);
Supposing that your LinearLayout ID is my_linear_layout, just do this in your onClickListener:
findViewById(R.id.my_linear_layout).setVisibility(View.GONE);
In your XML, be sure to put the ID:
<LinearLayout
android:id="#+id/my_linear_layout"
...>
</LinearLayout>
you can do this like get the id of the currently clicked item
and assigned in root layout
LinearLayout layout = (LinearLayout) v.getParent();
And remove using this code given below:
linearLayout.removeView(layout);

Android, RelativeLayout restarts Marquee-TextView when changing ImageView in same RelativeLayout

I have not found a solution for my problem, maybe you can help me here.
I am using a RelativeLayout with an ImageView and a TextView as children. The TextView contains a large text and should scroll from right to left. But everytime when I set a new image to the ImageView, the marquee starts from beginning.
I think that by setting a new image the TextView looses its focus and so the marquee starts again. How can I prevent that or is there something else I am doing wrong? Would be great if someone could point me to the correct solution.
Thanks a lot!
You can reproduce my problem with this code:
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils.TruncateAt;
import android.view.Display;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class RelativeLayoutActivity extends Activity {
private ImageView mImageView;
private TextView mTextView;
private Handler mHandler = new Handler();
private int mCurrentImage = 0;
private Runnable mCallback = new Runnable() {
#Override
public void run() {
toggleImage();
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initLayout();
setLongText();
mHandler.postDelayed(mCallback, 5000);
}
private void initLayout() {
RelativeLayout layout = (RelativeLayout) findViewById(R.id.parentLayout);
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
mImageView = new ImageView(this);
mImageView.setScaleType(ScaleType.FIT_XY);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(screenWidth,screenHeight);
mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.red));
layout.addView(mImageView, rlp);
// marquee text at the bottom
mTextView = new TextView(this);
mTextView.setSingleLine();
mTextView.setEllipsize(TruncateAt.MARQUEE);
mTextView.setMarqueeRepeatLimit(-1);
mTextView.setHorizontallyScrolling(true);
mTextView.setFocusable(true);
mTextView.setFocusableInTouchMode(true);
mTextView.setBackgroundColor(Color.BLACK);
mTextView.setTextColor(Color.WHITE);
rlp = new RelativeLayout.LayoutParams(screenWidth, 50);
rlp.topMargin = screenHeight-100;
layout.addView(mTextView, rlp);
}
private void setLongText() {
StringBuilder sb = new StringBuilder();
for (int i=0; i<50; i++) {
sb.append(" ");
}
for (int i=0; i<50; i++) {
sb.append("A");
}
for (int i=0; i<50; i++) {
sb.append("B");
}
for (int i=0; i<50; i++) {
sb.append("C");
}
mTextView.setText(sb.toString());
mTextView.setSelected(true);
}
private void toggleImage() {
mCurrentImage++;
if (mCurrentImage % 2 == 0) {
mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.red));
} else {
mImageView.setImageDrawable(this.getResources().getDrawable(R.drawable.green));
}
mHandler.postDelayed(mCallback, 5000);
}
}
I had a same problem and I fixed it just now:)
If your TextView which in layout XML contains layout_weight
android:layout_weight="1"
Remove it!This attibute cause marquee restart.
Hope helpful:)
The marquee reset problem is because of loss of focus of the TextView on which it is running. To overcome these issues you can simply surround your TextView with another RelativeLayout so that it is in a different ViewGroup as your other views.
You have another option: Create a new class that is a subclass of TextView and override onFocusChanged and onWindowFocusChanged to prevent loss of focus for the said textview.
That's it. Using these techniques your marquee won't restart every time another element gains focus.
See my answer here : https://stackoverflow.com/a/13841982/1823503
The idea is :
to fix the width and height programmatically (you did it)
to setSelected (you did it)
to Override your TextView to fix the focus problems
just a simple Fix..:) no need to worry much...just fix width of textview as some 800dp or too higher width. it will solve reset issue
<TextView
android:id="#+id/adv_txt_view"
android:layout_width="800dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:singleLine="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:marqueeRepeatLimit="marquee_forever"
android:focusableInTouchMode="true"
android:focusable="true"
android:scrollHorizontally="true"
android:textColor="#color/black"
android:text="large text to scroll!!" />

Categories

Resources