I have created a dynamic layout for edittext with add and minus icons. When i click on add, layout should recreated again and in place of add image, minus should shown.Add icon is changed to minus on first click but minus is not changed to plus.When i clicked on minus, sub layout is removed but not able to change icon.
This is my dynamic layout:
public void dLayout(){
count++;
lay_frame = new LinearLayout(this);
lay_frame.setOrientation(LinearLayout.VERTICAL);
lay_frame.setId(count);
for (int i =0; i<numClass; i++){
lay_main = new LinearLayout(this);
lay_uncle = new LinearLayout(this);
lay_cousin = new LinearLayout(this);
lay_main.setOrientation(LinearLayout.VERTICAL);
lay_uncle.setOrientation(LinearLayout.HORIZONTAL);
lay_uncle.setGravity(Gravity.CENTER);
lay_cousin.setOrientation(LinearLayout.HORIZONTAL);
lay_cousin.setGravity(Gravity.CENTER);
txt_uncle = new TextView(this);
txt_uncle.setText("Uncle Name");
txt_uncle.setTextColor(Color.BLACK);
txt_uncle.setPadding(0, 20 , 0, 20);
txt_uncle.setTextSize(14);
txt_cousin = new TextView(this);
txt_cousin.setText("Cousin Name");
txt_cousin.setTextColor(Color.BLACK);
txt_cousin.setPadding(0, 20 , 0, 0);
txt_cousin.setTextSize(14);
img_add = new ImageView(this);
img_add.setImageResource(R.drawable.add01);
img_add.setPadding(8, 0, 0 ,0);
img_minus = new ImageView(this);
img_minus.setImageResource(R.drawable.minus);
img_minus.setPadding(8, 0, 0 ,0);
img_minus.setVisibility(View.GONE);
img_cousinadd = new ImageView(this);
img_cousinadd.setImageResource(R.drawable.add01);
img_cousinadd.setPadding(8, 0, 0 ,0);
img_cousinminus = new ImageView(this);
img_cousinminus.setImageResource(R.drawable.minus);
img_cousinminus.setPadding(8, 0, 0 ,0);
ed_uncle = new EditText(this);
ed_uncle.setInputType(InputType.TYPE_CLASS_TEXT);
ed_uncle.setPadding(12, 8 ,8 ,8);
ed_uncle.setTextColor(Color.BLACK);
ed_uncle.setTextSize(14);
ed_uncle.setBackgroundResource(R.drawable.border);
ed_cousin = new EditText(this);
ed_cousin.setInputType(InputType.TYPE_CLASS_TEXT);
ed_cousin.setPadding(12, 8 ,8 ,8);
ed_cousin.setTextColor(Color.BLACK);
ed_cousin.setTextSize(14);
ed_cousin.setBackgroundResource(R.drawable.border);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(80, 0, 0, 0);
ed_cousin.setLayoutParams(params);
txt_cousin.setLayoutParams(params);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 80, 8.5f
);
params.setMargins(40, 0, 0, 0);
ed_uncle.setLayoutParams(params1);
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
dLayout();
}
});
img_minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (count > 0) {
final LinearLayout temp = (LinearLayout) mainLayout.findViewById(count);
mainLayout.removeView(temp);
count--;
}
img_minus.setVisibility(View.GONE);
img_add.setVisibility(View.VISIBLE);
}
});
lay_main.addView(txt_uncle);
lay_uncle.addView(ed_uncle);
lay_uncle.addView(img_add);
lay_uncle.addView(img_minus);
lay_main.addView(lay_uncle);
lay_main.addView(txt_cousin);
lay_cousin.addView(ed_cousin);
lay_cousin.addView(img_cousinadd);
lay_main.addView(lay_cousin);
lay_frame.addView(lay_main);
}
mainLayout.addView(lay_frame);
}
Change your btn add click logic to this
img_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dLayout();
img_add.setVisibility(View.GONE);
img_minus.setVisibility(View.VISIBLE);
}
});
Because after setting visibility again your setting the minus button visibility gone in the dLayout(); which means the property is overridden by next value
In onClick listener for minus button,lay_frame LinearLayout been removed from the main layout. Minus button is child of lay_frame layout, which is no more attached to view tree. If you are removing the view on click on minus button,changing the icon from minus to plus image doesn't make any sense.
Related
i have created a layout programmatically and set it into secontentview within oncreate i am create chat layout using simple edittext and a send image button right from edittext whenever user click on edittext suddenly hide by softkeyboard i tried all option which is required to mention in menifest like adjustPan or screenresize etc but no effect the edittext still hidden by keyboard
here is my layout code that contain the edittext
private LinearLayout getTextChatViewTest() {
// MainActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// LibraryMainActivity.this.getActionBar().show();
LinearLayout linWrapperLayout;
LinearLayout.LayoutParams linWrapperLayoutParams;
LinearLayout linHeaderLayout;
LinearLayout.LayoutParams linHeaderParams;
TextView txtHeaderText;
linWrapperLayout = new LinearLayout(MainActivity.this);
linWrapperLayout.setOrientation(LinearLayout.VERTICAL);
linWrapperLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
linWrapperLayout.setLayoutParams(linWrapperLayoutParams);
linWrapperLayout.setBackgroundColor(Color.parseColor("#ffffff"));
linHeaderLayout = new LinearLayout(MainActivity.this);
linHeaderLayout.setBackgroundColor(Color.parseColor("#E3E9F3"));
linHeaderLayout.setPadding(pixToDp(10), pixToDp(5), pixToDp(10), pixToDp(5));
linHeaderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linHeaderLayout.setLayoutParams(linHeaderParams);
txtHeaderText = new TextView(MainActivity.this);
txtHeaderText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
txtHeaderText.setPadding(pixToDp(5), pixToDp(10), pixToDp(10), pixToDp(10));
txtHeaderText.setTextColor(Color.parseColor("#004A8F"));
linHeaderLayout.addView(txtHeaderText);
txtHeaderText.setText("Text Chat");
RelativeLayout rel = new RelativeLayout(MainActivity.this);
rel.setBackgroundColor(Color.parseColor("#ffffff"));
RelativeLayout.LayoutParams relPArams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
rel.setLayoutParams(relPArams);
linWrapperLayout.addView(rel);
rel.addView(linHeaderLayout);
txtChatArea = new TextView(MainActivity.this);
txtChatArea.setId(011);
txtChatArea.setBackgroundColor(Color.parseColor("#ffffff"));
txtChatArea.setGravity(Gravity.BOTTOM);
int scroll_amount = (int) (txtChatArea.getLineCount() * txtChatArea.getLineHeight()) - (txtChatArea.getBottom() - txtChatArea.getTop());
txtChatArea.setLinksClickable(true);
txtChatArea.setAutoLinkMask(Linkify.WEB_URLS);
txtChatArea.setText("Anything");
txtChatArea.scrollTo(0, scroll_amount);
txtChatArea.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
txtChatArea.setScrollY(0);
txtChatArea.setMovementMethod(new ScrollingMovementMethod());
/** for allowing copy from text chat area */
txtChatArea.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
txtChatArea.setCursorVisible(true);
return true;
}
});
// Button btn = new Button (this);
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
// btn.setHeight((int)(display.getHeight()*0.68));
int height = display.getHeight();
RelativeLayout.LayoutParams relImageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
(MainActivity.getPixelHeight(MainActivity.this))/2+60);
relImageParams.setMargins(pixToDp(15), pixToDp(100), pixToDp(15), 0);
relImageParams.addRule(RelativeLayout.BELOW, 111);
txtChatArea.setLayoutParams(relImageParams);
rel.addView(txtChatArea);
/* The following section was copied from incoming call view */
relIncomingBottomLayout = new RelativeLayout(MainActivity.this);
relIncomingBottomLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relIncomingBottomLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
//relIncomingBottomLayoutParams.addRule(RelativeLayout.BELOW, 011);
relIncomingBottomLayoutParams.setMargins(pixToDp(10), 0, pixToDp(10), pixToDp(10));
relIncomingBottomLayout.setLayoutParams(relIncomingBottomLayoutParams);
rel.addView(relIncomingBottomLayout);
/* Now add a linearlayout with vertical orientation, which will contain two linearlayouts with horizontal orientation */
LinearLayout linBottomHolder = new LinearLayout(MainActivity.this);
linBottomHolder.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams linHolderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linBottomHolder.setLayoutParams(linHolderParams);
relIncomingBottomLayout.addView(linBottomHolder);
/* an warning view on characters left for end users */
txtWarning = new TextView(MainActivity.this);
txtWarning.setBackgroundColor(Color.parseColor("#ffffff"));
txtWarning.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
txtWarning.setText("150 characters left");
txtWarning.setTextColor(Color.GRAY);
linBottomHolder.addView(txtWarning);
/* Now add the first linear layout to contain an edittext to get message and an imageview */
LinearLayout linWriteHolder = new LinearLayout(MainActivity.this);
linWriteHolder.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams linWriteHolderPArams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
linWriteHolder.setLayoutParams(linWriteHolderPArams);
linBottomHolder.addView(linWriteHolder);
/* Now add an edittext and an imageview inside this linearlayout */
GradientDrawable gd = new GradientDrawable();
gd.setStroke(2, Color.parseColor("#E3E9F3"));
gd.setShape(GradientDrawable.RECTANGLE);
gd.setColor(Color.WHITE);
gd.setCornerRadius(5);
edtWriteMesage = new EditText(MainActivity.this);
edtWriteMesage.setHint("Type text here");
edtWriteMesage.setBackgroundColor(Color.parseColor("#ffffff"));
edtWriteMesage.setBackground(gd);
edtWriteMesage.setSingleLine(true);
/** filtering inputs */
InputFilter[] filters = new InputFilter[1];
filters[0] = new InputFilter.LengthFilter(150);
edtWriteMesage.setFilters(filters); /** now a maximum of 150 characters can be taken as input */
edtWriteMesage.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int currentLength = edtWriteMesage.getText().toString().length();
int remainingLength = 150 - currentLength;
if(remainingLength <= 15){
txtWarning.setTextColor(Color.RED);
}else{
txtWarning.setTextColor(Color.GRAY);
}
txtWarning.setText(""+remainingLength + " characters left");
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
LinearLayout.LayoutParams linWriteParams = new LinearLayout.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.9f);
linWriteParams.gravity = Gravity.CENTER_VERTICAL;
edtWriteMesage.setLayoutParams(linWriteParams);
linWriteHolder.addView(edtWriteMesage);
/* Now add an imageview with an arrow in it by the edittext's side */
imgSendArrow = new ImageView(MainActivity.this);
byte[] sendArrow = Base64.decode(SecondImageStore.send, Base64.DEFAULT);
//byte[] sendArrow = Base64.decode(SecondImageStore.papersend, Base64.DEFAULT);
imgSendArrow.setImageBitmap(BitmapFactory.decodeByteArray(sendArrow, 0, sendArrow.length));
LinearLayout.LayoutParams linSendParams = new LinearLayout.LayoutParams(0,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.1f);
linSendParams.gravity = Gravity.CENTER;
linSendParams.setMargins(pxToDp(3), 0, pxToDp(2), 0);
imgSendArrow.setLayoutParams(linSendParams);
linWriteHolder.addView(imgSendArrow);
/* Now add the Second relative layout to contain two buttons */
LinearLayout linTextChatButtonHolder = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams linTextChatButtonHolderParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
linTextChatButtonHolderParams.setMargins(0, pixToDp(10), 0, 0);
linTextChatButtonHolder.setLayoutParams(linTextChatButtonHolderParams);
linBottomHolder.addView(linTextChatButtonHolder);
/* Button to end ongoing conference */
btnTextChatEndCall = new Button(MainActivity.this);
btnTextChatEndCall.setText("Call Held");
btnTextChatEndCall.setBackgroundColor(Color.parseColor("#C11E0F"));
btnTextChatEndCall.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
btnTextChatEndCall.setGravity(Gravity.CENTER);
btnTextChatEndCall.setTextColor(Color.parseColor("#ffffff"));
LinearLayout.LayoutParams linTextChatEndCallParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f);
linTextChatEndCallParams.setMargins(0, pixToDp(10), 0, 0);
btnTextChatEndCall.setLayoutParams(linTextChatEndCallParams);
linTextChatButtonHolder.addView(btnTextChatEndCall);
/* Button to display menu in chat mode */
btnTextChatMenu = new Button(MainActivity.this);
btnTextChatMenu.setText("Menu");
btnTextChatMenu.setGravity(Gravity.CENTER);
btnTextChatMenu.setBackgroundColor(Color.parseColor("#014A8E"));
btnTextChatMenu.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
btnTextChatMenu.setTextColor(Color.parseColor("#ffffff"));
LinearLayout.LayoutParams linTextChatMenuParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f);
linTextChatMenuParams.setMargins(pixToDp(10), pixToDp(10), 0, 0);
btnTextChatMenu.setLayoutParams(linTextChatMenuParams);
linTextChatButtonHolder.addView(btnTextChatMenu);
btnTextChatEndCall.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try{
}catch(Exception e){
e.printStackTrace();
}
}
});
edtWriteMesage.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
int result = actionId & EditorInfo.IME_MASK_ACTION;
switch (result) {
case EditorInfo.IME_ACTION_DONE:
break;
case EditorInfo.IME_ACTION_NEXT:
break;
}
return false;
}
});
return linWrapperLayout;
// setContentView(linWrapperLayout);
}
Try on this way to hide keyboard
// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
In your Manifest.xml add this to you activity
<activity
android:windowSoftInputMode="adjustResize|stateHidden" ... >
...
</activity>
for Click here for more details about handling Input Methods
i have just remove getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); after that edittext comming below the edittext whenever soft keyboard appear
I am getting a NullPointerException near the beginning of my onCreate method in my Android app. I am getting that the clock TextView variable in null, but I don't know why or how to fix it.
My code:
//Some of the variables declared outside of any method:
EditText filename;
TextView clock;
private Handler clockHandler = new Handler();
//onCreate and the onClick method that flips the view and starts the timer:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
createLayout();
flipper = (ViewFlipper) findViewById(R.id.marker_flipper);
flipper.setDisplayedChild(0);
time_marks = (LinearLayout) findViewById(R.id.time_marks);
stop_time_marks = (LinearLayout) findViewById(R.id.stop_time_marks);
times = new ArrayList<String>();
start = (Button) findViewById(R.id.start);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
flipper.setDisplayedChild(1);
clock = (TextView) findViewById(R.id.clock);
mark = (Button) findViewById(R.id.mark);
stop = (Button) findViewById(R.id.stop);
startTime = System.currentTimeMillis();
clockHandler.postDelayed(updateTimerThread, 0);
}
});
createLayout Method:
#SuppressWarnings("deprecation")
public void createLayout() {
// Define Screen Size
Display display = getWindowManager().getDefaultDisplay();
// Define LayoutParams
LayoutParams parentParams = new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams clockParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 8);
clockParams.gravity = (Gravity.CENTER);
// Root Element
ViewFlipper flipper = new ViewFlipper(this);
flipper.setLayoutParams(parentParams);
flipper.setId(R.id.marker_flipper);
// Child 0
com.jenglanddev.marktime.Scaling_Linear_Layout markerStart = new com.jenglanddev.marktime.Scaling_Linear_Layout(
this);
markerStart.setLayoutParams(parentParams);
markerStart.setOrientation(LinearLayout.VERTICAL);
// Children of markerStart
Button start = new Button(this);
start.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 2));
start.setText("Start Marking");
start.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
start.setTextSize(display.getHeight() / 4);
start.setId(R.id.start);
markerStart.addView(start);
flipper.addView(markerStart);
// Child 1
com.jenglanddev.marktime.Scaling_Linear_Layout marker = new com.jenglanddev.marktime.Scaling_Linear_Layout(
this);
marker.setLayoutParams(parentParams);
marker.setOrientation(LinearLayout.VERTICAL);
// Children of marker
Button mark = new Button(this);
mark.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
(display.getHeight() / 10) * 4));
mark.setText("Mark Time");
mark.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
mark.setTextSize(display.getHeight() / 5);
mark.setId(R.id.mark);
marker.addView(mark);
Button stop = new Button(this);
stop.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
display.getHeight() / 10));
stop.setText("Stop Marking");
stop.setTextSize(display.getHeight() / 20);
stop.setId(R.id.stop);
marker.addView(stop);
TextView clock = new TextView(this);
clock.setLayoutParams(clockParams);
clock.setGravity(Gravity.CENTER);
clock.setText("");
clock.setTextColor(Color.BLACK);
clock.setTextSize(display.getHeight() / 10);
clock.setId(R.id.clock);
marker.addView(clock);
ScrollView sv1 = new ScrollView(this);
sv1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
LinearLayout marks = new LinearLayout(this);
marks.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
marks.setOrientation(LinearLayout.VERTICAL);
marks.setId(R.id.time_marks);
sv1.addView(marks);
marker.addView(sv1);
flipper.addView(marker);
// Child 2
com.jenglanddev.marktime.Scaling_Linear_Layout markerStop = new com.jenglanddev.marktime.Scaling_Linear_Layout(
this);
markerStop.setLayoutParams(parentParams);
markerStop.setOrientation(LinearLayout.VERTICAL);
// Children of markerStop
Button save = new Button(this);
save.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 4));
save.setText("Save Times");
save.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
save.setTextSize(display.getHeight() / 8);
save.setId(R.id.save);
markerStop.addView(save);
Button reset = new Button(this);
reset.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, display.getHeight() / 4));
reset.setText("Reset");
reset.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD),
Typeface.BOLD);
reset.setTextSize(display.getHeight() / 8);
reset.setId(R.id.reset);
markerStop.addView(reset);
ScrollView sv2 = new ScrollView(this);
sv2.setLayoutParams(parentParams);
LinearLayout stopMarks = new LinearLayout(this);
stopMarks.setLayoutParams(parentParams);
stopMarks.setOrientation(LinearLayout.VERTICAL);
stopMarks.setId(R.id.stop_time_marks);
sv2.addView(stopMarks);
markerStop.addView(sv2);
flipper.addView(markerStop);
setContentView(flipper);
}
How can I fix this?
I have added buttons to the TableLayout programmatically usin TableRow. Each row should include three buttons. If number of buttons is multiple of three there is no problem. All buttons are placed with same size. However the number of buttons for last row is less than three, its size is not matched with other buttons.
Here is my code:
tableView.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams
.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams
.MATCH_PARENT, ScrollView.LayoutParams.WRAP_CONTENT,1));
TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams
(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT,1);
layoutParams.setMargins(0,20,0,20);
int numberOfButtonsForLastRow = masalar.size() - numberOfRows*3;
for (int i = 0; i < numberOfRows; i++)
{
tr = new TableRow(fragmentView.getContext());
tr.setLayoutParams(layoutParams);
for (int j= 0;j<3;j++)
{
btn = new Button(getActivity());
btn.setBackgroundResource(R.drawable.buttonstyle);
btn.setText(masalar.get(masaCounter));
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams
.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1);
params.setMargins(20, 0, 20, 0);
btn.setLayoutParams(params);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
tr.addView(btn);
masaCounter++;
}
tableView.addView(tr);
}
if(numberOfButtonsForLastRow>0)
{
tr = new TableRow(fragmentView.getContext());
tr.setLayoutParams(layoutParams);
for (int j= 0;j<numberOfButtonsForLastRow;j++)
{
btn = new Button(getActivity());
btn.setBackgroundResource(R.drawable.buttonstyle);
btn.setText(masalar.get(masaCounter));
TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams
.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 20, 0);
btn.setLayoutParams(params);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
tr.addView(btn);
masaCounter++;
}
tableView.addView(tr);
}
This is how the last button looks like. I want it to be same size with others. How can I do this?
Add a Layout weight in your TableRow.LayoutParams same as what you did for the first 2 rows so buttons are divided into your TableView.
TableRow.LayoutParams params = new TableRow.LayoutParams(btn.getWidth(), btn.getHeight());
You need weigths, you devide the space of one row between (0 .. 1)
TableLayout.LayoutParams rows = new
TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 0,
1.0f / (float) NUMBUTTONS);
row.setLayoutParams(rows););
i have an imageview
and i move it in timer with:
ImageView kembali = (ImageView) findViewById(idmusuh);
kembali.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(5, 0, 0, 0);
kembali.setLayoutParams(params);
so the imagview is move down 5px every timer tick
now if i click the button i want to put the imageview back to start position ..what is the code?
Try this:
kembali.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(0, 0, 0, 0);
kembali.setLayoutParams(params);
}
});
Given that you have moved the ImageView by increasing the margin...
How can I set the gravity and margin for auto-generated(within the code) TextViews and Buttons? They are inside a LinearLayout.
Here is my generation code:
for(int i=0; i<num_enter; i++){
final int cuco = i;
LinearLayout linlay = new LinearLayout(this);
linlay.setOrientation(0);
TextView text = new TextView(this);
text.setText(name[cuco] + " ");
linlay.addView(text);
TextView num = new TextView(this);
num.setId(cuco);
num.setText("" + current[cuco]);
linlay.addView(num);
Button minus = new Button(this);
minus.setText("-");
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] - 1;
current[cuco] = current[cuco] -1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
minus.setLayoutParams(p);
linlay.addView(minus);
Button plus = new Button(this);
plus.setText("+");
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int cval = current[cuco];
int currentF = current[cuco] + 1;
current[cuco] = current[cuco] + 1;
SetSql update = new SetSql(SpellCast.this);
update.open();
update.changeCurrent(currentF, cval, name[cuco]);
update.close();
((TextView) findViewById(cuco)).setText("" + currentF);
}
});
LinearLayout.LayoutParams w = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT);
plus.setHeight(LayoutParams.WRAP_CONTENT);
plus.setWidth(50);
plus.setLayoutParams(w);
linlay.addView(plus);
main.addView(linlay);
}
Hope you find a way to set the button gravity without changing it size.
1) Get Reference of TextView say tv.
2) tv.setGravity(Gravity.CENTER);
It is not sure work with LinearLayout.. Try Relative Or Absolute
Use View.setLayoutParams(ViewGroup.LayoutParams params). Look at http://developer.android.com/reference/android/R.styleable.html#ViewGroup_Layout for the LayoutParams.
Since nobody has addressed margins
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
p.setMargins(10, 10,10,10);
p.gravity = Gravity.CENTER;
textView.setLayoutParams(p);
When using setLayoutParams make sure the type of layout params matches the parent view which, in this case, is LinearLayout
setGravity(Gravity.CENTER); Will set the gravity of the text within the view