i am making a activity that takes int position from another activity to get a class formula that have the formula, some variables and the title , so i want to create the activity by taking that formula and setting the title , making some edit text views for variables so that the user add values for them and then i return the result by a dialogue the problem is that the views that i add programitically are not displayed, plz help
public class DisplayFomula extends AppCompatActivity {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_fomula);
DB db = new DB(this);
Bundle extra = getIntent().getExtras();
int pos = extra.getInt("id");
final Formula form = db.getFormula(pos);
RelativeLayout layout=new RelativeLayout(this);
TextView tw1 =(TextView) findViewById(R.id.textView11);
tw1.setText(form.title);
final String[] var = form.var;
int i=0;
final EditText[] ed = new EditText[50];
for(;i<var.length;i++){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
ed[i]= new EditText(this);
ed[i].setId(i);
ed[i].setHint(var[i]);
ed[i].setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
ed[i].setLayoutParams(params);
if(i!=0) params.addRule(RelativeLayout.BELOW,ed[i-1].getId());
else params.addRule(RelativeLayout.BELOW,R.id.textView11);
layout.addView(ed[i],params);}
Button btn = new Button(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
btn.setId(i);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
btn.setLayoutParams(params);
btn.setText("Go!");
layout.addView(btn,params);
params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
addContentView(layout,params);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double[] varin=new double[var.length];
for(int i =0;i<var.length;i++){
Double dbl = Double.parseDouble(ed[i].getText().toString());
varin[i]=dbl;
}
String res = String.valueOf(form.result(varin));
}
});
}
}
You are adding all the views to RelativeLayout layout. But i think u have'nt add layout view to any activity_display_fomula view.
get a viewgroup from activity_display_fomula and add your RelativeLayout layout to it..
Related
At the click of a button, I create one textview programmatically and add them to flexboxlayout. Why is only one view added?
mButton.setOnClickListener(new View.OnClickListener() {
int i = 1;
#Override
public void onClick(View v) {
flexboxLayout.setFlexDirection(FlexDirection.ROW_REVERSE);
flexboxLayout.setFlexWrap(FlexWrap.WRAP);
flexboxLayout.setJustifyContent(JustifyContent.CENTER);
final TextViewR textViewR = new TextViewR(getApplicationContext(), "Text " + Integer.toString(i++)); //setting text here
FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
lp.setOrder(1);
textViewR.setLayoutParams(lp);
flexboxLayout.addView(textViewR);
}
});
I assume TextViewR is your custom TextView.
You should change
`FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);`
to
`FlexboxLayout.LayoutParams lp = new FlexboxLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);`
since textViewR.setLayoutParams(lp); is setting the flexboxlayout parameters to the textview and your parameters are telling the textview to match parent which is the flexbox.
My RelativeLayout Dynamically adds check boxes and sets text to each of them but they all overlap each other even though i'm using the RelativeLayout.BELOW parameter.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bible_progress);
Bundle progressBundle = getIntent().getExtras();
int positionBook = progressBundle.getInt("position");
if(positionBook == 0) {
for (int i = 0; i < 10; i++) {
RelativeLayout progressLayout = (RelativeLayout) findViewById(R.id.progress_layout);
CheckBox progressBox = new CheckBox(this);
progressBox.setText("Dynamic Checkbox " + i);
progressBox.setId(i + 10);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, progressBox.getId());
progressBox.setLayoutParams(params);
progressLayout.addView(progressBox);
}
}
}
Im not getting any errors so there is no stack trace.
you are providing the wrong id to the rule
params.addRule(RelativeLayout.BELOW, progressBox.getId());
this way you are providing the id of the view you just created, to the rule
I have set notification listener in my code. Whenever I receive a notification I want to put a Text view in my linear layout activity which has nothing initially.
When second notification arrives I want to add one more text view below the previous text view.
public class InformationActivity extends Activity
{
public static VideoInformationClass vidInfo = new VideoInformationClass() ;
public static LinearLayout lv ;
public static LayoutParams textViewParams;
public static TextView tv ;
public static TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7,tv8,tv9,tv10;
static int fieldFrequency;
static int numberOfFrameLines;
static int numberOfVisibleLines;
static int numberOfVisiblePixels;
static int interlace;
static int imageFormat;
static int videoCoding;
static int scanType;
VideoPropertiesParams GetParams;
VideoPropertiesParams VP;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.information_activity);
Context context = getBaseContext();
LinearLayout lv = new LinearLayout(this);
lv.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// tv = new TextView(this);
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
onFieldFrequencyChanged();
onImageFormatChanged();
onInterlacedChanged();
/*vidInfo.RegisterVidPropertyListener();
vidInfo.RegisterSignalAndPresenceListener();
vidInfo.RegisterThreeDChangeListener();*/
if (ControlUnit.flag) {
GetParams = vidInfo.GetVideoProperty();
Log.i("TvPlayerFunctionalTestApp","Get Video Property called");
fieldFrequency = GetParams.fieldFrequency;
numberOfFrameLines = GetParams.numberOfFrameLines;
numberOfVisibleLines = GetParams.numberOfVisibleLines;
numberOfVisiblePixels = GetParams.numberOfVisiblePixels;
interlace = GetParams.interlace;
imageFormat = GetParams.imageFormat;
videoCoding = GetParams.videoCoding;
scanType = GetParams.scanType;
}
/* tv.setText("Something");
tv.setLayoutParams(textViewParams);
lv.addView( tv );*/
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
/*vidInfo.UnRegisterSignalAndPresenceListener();
vidInfo.UnRegisterThreeDChangeListener();
vidInfo.UnRegisterVidPropertyListener();*/
}
public void onFieldFrequencyChanged(){
String info = "On field frequency changed , value is " + vidInfo.ChangedFrequency;
tv = new TextView(this);
tv.setText(String.valueOf(info));
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(textViewParams);
lv.addView(tv);
}
public void onImageFormatChanged() {
String info = "On Image Format changed , value is " + vidInfo.ChangedFormat;
tv1 = new TextView(this);
tv1.setText(String.valueOf(info));
LinearLayout.LayoutParams textViewParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv1.setLayoutParams(textViewParams);
lv.addView(tv1 );
}
public void onInterlacedChanged() {
String info = "On Interlaced changed , value is " + vidInfo.InterlaceChange;
tv.setText(String.valueOf(info));
LinearLayout.LayoutParams textView
Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(textViewParams);
lv.addView(tv);
}
Just declare your Linear layout with vertical orientation
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
LinearLayout lv = (LinearLayout)findViewById(R.id.linear);
TextView tv = new TextView(this);
tv.setLayoutParams(InformationActivity.textViewParams);
tv.setText(String.valueOf(info));
lv.addView(tv );//not InformationActivity.tv just write tv
It will automatically add next view below the the another
Make sure your LinearLayout has orientation set to vertical. Then just add one more item:
LinearLayout layout = // your layout
TextView textView = // new text view
layout.addView(textView);
Just make a Linear Layout whose oreientation is vertacal and add TextView to it.
You need a object from the main ViewGroup(RelativeLayout, LinearLayout etc.),
TextView text = new TextView(this); RelativeLayout r = (RelativeLayout)findViewById(Id);r.add(text);
this lines of code will add a new textView object to the end of the r viewGroup.
I have an activity with some dynamic content.
For each item of an array, there is a different content (number of textViews, checkboxes, ..)
The user clicks on a "save" button and then the screen has to refresh itself to display the content of the next item.
I was wondering if it was a good practice to reload my activity like this :
Intent refresh = new Intent(this, conflictActivity.class);
startActivity(refresh)
finish()
instead of removing all the views in my layouts, etc ...
I think it's easier to reload it.
Is there another way to do it ?
EDIT : added Code.
public class ConflicFieldTest extends Activity {
private int nbField;
private int nbChecked;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.conflict);
final Button saveButton = (Button) findViewById(R.id.save);
final LinearLayout leftLayout = (LinearLayout) findViewById(R.id.leftLayout);
final LinearLayout rightLayout = (LinearLayout) findViewById(R.id.rightLayout);
final TextView fieldName = new TextView(this);
final LinearLayout editLayoutRight = new LinearLayout(this);
final LinearLayout editLayoutLeft = new LinearLayout(this);
final LinearLayout fieldRight = new LinearLayout(this);
final LinearLayout fieldLeft = new LinearLayout(this);
final ArrayList<String> allIdInConflict = getAllIdInConflict();
for (int i = 0; i < allIdInConflict.size(); i++) {
new AccountSyncTask() {
#Override
public void onPostExecute(
final ArrayList<ArrayList<String>> result) {
nbField = 0;
nbChecked = 0;
final Map<String, Object> fieldsInConflict = conflict
.conflictWithFields(memoAccountMap,
serverAccountMap, modifiedAccountMap);
for (Iterator<Map.Entry<String, Object>> field = fieldsInConflict
.entrySet().iterator(); field.hasNext();) {
final Map.Entry<String, Object> entry = field.next();
fieldName.setText(entry.getKey() + " : ");
final EditText[] editTextArrayRight = new EditText[fieldsInConflict
.size()];
final EditText[] editTextArrayLeft = new EditText[fieldsInConflict
.size()];
final CheckBox[] checkboxRight = new CheckBox[fieldsInConflict
.size()];
final CheckBox[] checkboxLeft = new CheckBox[fieldsInConflict
.size()];
checkboxRight[nbField] = new CheckBox(
ConflicFieldTest.this);
checkboxLeft[nbField] = new CheckBox(
ConflicFieldTest.this);
editLayoutLeft.setGravity(Gravity.CENTER_HORIZONTAL);
editLayoutRight.setGravity(Gravity.CENTER_HORIZONTAL);
fieldRight.setGravity(Gravity.CENTER_HORIZONTAL);
fieldLeft.setGravity(Gravity.CENTER_HORIZONTAL);
editLayoutRight.setOrientation(LinearLayout.HORIZONTAL);
fieldRight.setOrientation(LinearLayout.VERTICAL);
fieldLeft.setOrientation(LinearLayout.VERTICAL);
rightLayout
.addView(
editLayoutRight,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
leftLayout
.addView(
editLayoutLeft,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editTextArrayRight[nbField] = new EditText(
ConflicFieldTest.this);
editTextArrayRight[nbField].setText((String) entry
.getValue());
editTextArrayRight[nbField].setTextColor(Color.BLACK);
editTextArrayRight[nbField].setFocusable(false);
editTextArrayRight[nbField].setClickable(false);
editTextArrayRight[nbField].setWidth(180);
editTextArrayRight[nbField].setPadding(0, 10, 0, 0);
editTextArrayLeft[nbField] = new EditText(
ConflicFieldTest.this);
editTextArrayLeft[nbField]
.setText((String) serverAccountMap.get(entry
.getKey()));
editTextArrayLeft[nbField].setTextColor(Color.BLACK);
editTextArrayLeft[nbField].setFocusable(false);
editTextArrayLeft[nbField].setClickable(false);
editTextArrayLeft[nbField].setWidth(180);
editTextArrayLeft[nbField].setPadding(0, 10, 0, 0);
final int i1 = nbField;
checkboxLeft[i1]
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
checkboxRight[i1]
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
editLayoutLeft.addView(fieldName);
editLayoutRight
.addView(
editTextArrayRight[nbField],
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editLayoutLeft
.addView(
editTextArrayLeft[nbField],
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
editLayoutRight.addView(checkboxRight[nbField]);
editLayoutLeft.addView(checkboxLeft[nbField]);
nbField++;
}
saveButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//save data & update UI
}
}
.execute(appState.getSessionName(), id, "getObjectOnServer");
}
}
Sorry for the code, it is a litle bit dirty (i have deleted some parts), i am reworking it.
int flag = 0;
public void updateUI() {
if(flag == 0) {
//your normal codes...
} else {
//display view with user given data
}
}
Call this function in oncreate. At first flag will be set to zero. When u enter the data in textview and tick checkboxes, update the flag with some value. Then on save Button click call this function again. Now flag is set. So it will display the updated UI.
I think this will give u an idea..
I have a weird problem. I derived MessageRow from TableRow. I populated a table with one TableRow (Header) and 100 MessageRows. But when I add the table to the HorizontalScroll view only the Messagerows are showing.
If I inspect the table in the debugger, all the rows are there, including the Header, with the right children and text.
This is the simplified code:
public class MessageRow extends TableRow {
public TextView tvData1;
public TextView tvData2;
public TextView tvData3;
public MessageRow(Context context) {
this(context, null);
}
public MessageRow(Context context, AttributeSet attrs) {
super(context, attrs);
tvData1 = new TextView(context);
tvData2 = new TextView(context);
tvData3 = new TextView(context);
}
public void setData(String data1, String data2, String data3) {
tvData1.setText(data1);
tvData2.setText(data2);
tvData3.setText(data3);
addView(tvData1);
addView(tvData2);
addView(tvData3);
}
}
The activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewer);
mTable = new TableLayout(this);
HorizontalScrollView hview = (HorizontalScrollView) findViewById(R.id.hscroll);
mTable.setBackgroundColor(Color.WHITE);
populate(mTable);
hview.addView(mTable);
}
public void setHeader(TableLayout tl) {
TableRow mHeader = new TableRow(getContext());
mHeader.addView(getColumnHeader("Data#1"));
mHeader.addView(getColumnHeader("Data#2"));
mHeader.addView(getColumnHeader("Data#3"));
mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mHeader);
}
public void populate(TableLayout tl) {
setHeader(tl);
for(int i = 0; i < 100; i++) {
MessageRow mr = new MessageRow(getContext());
mr.setMessage("xxx"+i,"yyy"+i,"zzz"+i);
mr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mr);
}
}
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.rightMargin = params.leftMargin = 2;
bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
Now, I tried bypassing the getColumnHeader() function like this:
public void setHeader(TableLayout tl) {
TableRow mHeader = new TableRow(getContext());
//mHeader.addView(getColumnHeader("Data#1"));
--> Button bt1 = new Button(getContext());
--> bt1.setText("Data#1");
--> mHeader.addView(bt1);
//mHeader.addView(getColumnHeader("Data#2"));
--> Button bt2 = getColumnHeader("Data#2");
--> mHeader.addView(bt2);
mHeader.addView(getColumnHeader("Data#3"));
mHeader.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tl.addView(mHeader);
}
and then bt1 (Data#1) is displayed!!! but not Data#2 or Data#3. This hints that somehow the function getColumnHeader() is bad. But not acording to the debugger, when I inspect bt1 and bt2 they seem good, I cannot detect what is the difference between the manually instantiated button bt1 and the returned button bt2.
Side note: I don't know if this is the best way, but to compare the two variables I just right-clicked and Copy Variable, then pasted it to notepad++ in two different files (one for bt1 and one for bt2) and used the compare plugin.
Some more info. I managed to display the buttons, further debug showed that setting the Layout params for each button in the function getColumnHeader() caused the problem. Still I don't know why.
The button member mLayoutParams being null is OK, but when created (.setLAyoutParams()) and assigned values (for example width and height) the suddenly the button is not displayed.
This is the patch,... until I figure out why.
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
//LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//params.rightMargin = params.leftMargin = 2;
//bt.setLayoutParams(params);
bt.setText(name);
return bt;
}
Last piece of the puzzle.
I tried many variations and finally it worked!
Replacing LayoutParams with TableRow.LayoutParams did the trick.
Their structure is very similar, probably even TableRow.LayoutParams is derived from LayoutParams.
I will leave the mystery here, until some expert sheds light on why this happened.
The final code:
private Button getColumnHeader(String name) {
Button bt = new Button(getContext());
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.MATCH_PARENT);
params.rightMargin = params.leftMargin = 2;
bt.setLayoutParams(params);
bt.setText(name);
return bt;
}