Hide icons in action bar in android - android

I have an action bar which is created programmatic-ally:
public class ActionBarPdfView extends Activity{
public ImageView action_bar_home_icon, action_bar_back_icon, action_bar_star, action_bar_search;
public RelativeLayout relative;
public ActionBar action;
public RelativeLayout.LayoutParams params;
private DatabaseManager dbManager;
public static int VIEW_ID = 3;
public int getViewId(){
return VIEW_ID;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
action_bar_home_icon = new ImageView(getApplicationContext());
params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 20, 0);
action_bar_home_icon.setLayoutParams(params);
action_bar_home_icon.setId(1);
action_bar_star = new ImageView(getApplicationContext());
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, action_bar_home_icon.getId());
params.setMargins(0, 0, 20, 0);
action_bar_star.setLayoutParams(params);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
action = getActionBar();
relative = new RelativeLayout(getApplicationContext());
action_bar_back_icon = new ImageView(getApplicationContext());
action_bar_search = new ImageView(getApplicationContext());
dbManager = new DatabaseManager(getApplicationContext());
dbManager.waitForTurn();
ObjectsDBAdapter objects = dbManager.getObjectsDbAdapter();
objects.setupImageView(R.drawable.home_android, action_bar_home_icon);
// action_bar_home_icon.setImageResource(R.drawable.home_android);
// params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
// params.addRule(RelativeLayout.RIGHT_OF, action_bar_home_icon.getId());
// params.setMargins(0, 0, 20, 0);
// action_bar_back_icon.setImageResource(R.drawable.back_android);
// action_bar_back_icon.setLayoutParams(params);
// action_bar_back_icon.setId(2);
objects.setupImageView(R.drawable.sternbutton_android, action_bar_star);
params = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.setMargins(0, 0, 20, 0);
objects.setupImageView(R.drawable.searchtext_android, action_bar_search);
action_bar_search.setLayoutParams(params);
action_bar_search.setId(3);
action.setDisplayShowCustomEnabled(true);
action.setDisplayShowHomeEnabled(false);
action.setDisplayShowTitleEnabled(false);
relative.addView(action_bar_home_icon);
relative.addView(action_bar_back_icon);
relative.addView(action_bar_star);
relative.addView(action_bar_search);
relative.setGravity(Gravity.CENTER);
action.setCustomView(relative);
dbManager.closeObjectsDbAdapter();
return true;
}
}
I have some icons on the left side and some icons on right side. What I want is, when there are more icons from right and they cover some icons on left side, to hide this icons in menu.
I know that in xml I can do that using android:showAsAction. I would like to know how I can do that programmatic-ally? Any ideas?

Related

Why this button setText cannot show text in my custom dialog?

My code:
public class MyBottomDialog extends Dialog {
LinearLayout container;
public MyBottomDialog(Context context) {
super(context);
container = new LinearLayout(context);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
container.setLayoutParams(params);
container.setOrientation(LinearLayout.VERTICAL);
// container.setBackgroundColor(Color.TRANSPARENT);
int padding = context.getResources().getDimensionPixelSize(R.dimen.navigation_padding_bottom);
container.setPadding(padding, padding, padding, padding);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(container);
// setContentView(button);
setCancelable(true);
setCanceledOnTouchOutside(true);
getWindow().setGravity(Gravity.BOTTOM);
// getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
getWindow().setWindowAnimations(R.style.BottomDialogAnimation);
}
public void addMenuItems(String[] menuItemTitles, int[] menuItemTextColors) {
for (int i = 0; i < menuItemTitles.length; i++) {
// Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.bottom_menu_item, container, false);
// button.setText(menuItemTitles[i]);
// button.setTextColor(menuItemTextColors[i]);
// container.addView(button);
addMenuItem(menuItemTitles[i], menuItemTextColors[i]);
}
}
public void addMenuItem(String menuItemTitle, int menuItemTextColor) {
// Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.bottom_menu_item, container, false);
Button button = produceButton();
button.setText(menuItemTitle);
button.setTextColor(menuItemTextColor);
container.addView(button);
}
private Button produceButton() {
Button button = new Button(getContext());
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setGravity(Gravity.CENTER);
// button.setBackground(getContext().getResources().getDrawable(R.drawable.bottom_menu_button2));
float[] outerRadii = new float[]{24, 24, 24, 24, 24, 24, 24, 24};
RoundRectShape rect = new RoundRectShape(outerRadii, null, null);
ShapeDrawable drawable = new ShapeDrawable(rect);
drawable.getPaint().setColor(Color.WHITE);
drawable.getPaint().setStyle(Paint.Style.FILL);
button.setBackground(drawable);
return button;
}
}
then I use it:
MyBottomDialog dlg = new MyBottomDialog(this);
dlg.addMenuItems(new String[]{"delete", "cancel"}, new int[]{android.R.color.holo_red_light, android.R.color.holo_blue_light});
dialog.show();
but there is no any text in these two button in dialog, why?

Edittext hidden by softkeyboard

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

How to add a whole layout withing main layout in android

I've tried following code to add two layouts within main layout
but this gives error while setting layout to frame and while adding it into view
public class MainActivity extends Activity {
FrameLayout rootLayout;
FrameLayout frame1,frame2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootLayout=new FrameLayout(getApplicationContext());
frame1=(FrameLayout)findViewById(R.layout.frame1);
frame2=(FrameLayout)findViewById(R.layout.frame2);
LayoutParams param1=new FrameLayout.LayoutParams(180,300);
LayoutParams param2=new FrameLayout.LayoutParams(180,300);
param1.gravity=Gravity.LEFT;
param2.gravity=Gravity.RIGHT;
param1.setMargins(0, 0, 30, 30);
param2.setMargins(30, 0, 0, 30);
frame1.setLayoutParams(param1);
frame2.setLayoutParams(param2);
rootLayout.addView(frame1);
rootLayout.addView(frame2);
}
}
You have to remove both frame1 and frame2 Views from their parent View...
and this may help you...
FrameLayout rootLayout;
FrameLayout frame1, frame2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootLayout = new FrameLayout(this);
frame1 = (FrameLayout) getLayoutInflater().inflate(R.layout.frame1,null);
frame2 = (FrameLayout) getLayoutInflater().inflate(R.layout.frame2, null);
FrameLayout.LayoutParams param1 = new FrameLayout.LayoutParams(180, 300);
FrameLayout.LayoutParams param2 = new FrameLayout.LayoutParams(180, 300);
param1.gravity = Gravity.LEFT;
param2.gravity = Gravity.RIGHT;
param1.setMargins(0, 0, 30, 30);
param2.setMargins(30, 0, 0, 30);
frame1.setLayoutParams(param1);
frame2.setLayoutParams(param2);
rootLayout.addView(frame1);
rootLayout.addView(frame2);
setContentView(rootLayout);
}

Adding TextView to RelativeLayout inside AsyncTask does not work

I am adding few TextViews to a RelativeLayout. I have extended RelativeLayout. I could add the TextView in the constructor definition. I could see the item on the viw. But the same code, when I use in onPostExecute of AyncTask or use post() method, TextView is not seen added to the layout. Same coe is executed both place. I just changed the top margin.
Here is my code
TextView tv = new TextView(context);
addView(tv);
tv.setHeight(49);
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) 0;
params.topMargin = (int) 600;
tv.setLayoutParams(params);
================ Implementation ================================
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
DateDetailsScrollView sc = new DateDetailsScrollView(container.getContext());
new SyncInThread().execute(sc);
return sc;
}
================= AsyncTask ===================================
private static class SyncInThread extends AsyncTask<Object, Void, String> {
private ArrayList<EVT> _mEvents = new ArrayList<EVT>();
DateDetailsScrollView _lv = null;
#Override
protected String doInBackground(Object... cal) {
_lv = (DateDetailsScrollView)cal[0];
//The SQL cannot be shared here... Please forgive me.
boolean f = cursor.moveToFirst();
while(f) {
EVT evt = new EVT();
evt.loadFromCursor(cursor);
f = cursor.moveToNext();
_mEvents.add(evt);
cc++;
}
return "";
}
#Override
protected void onPostExecute(String result) {
for(EVT ev : _EVT) {
_lv.addEvent(ev);
}
}
}
public class DateDetailsScrollView extends ScrollView {
private EventRelativeLayout _mEventLayout = null;
public DateDetailsScrollView(Context context) {
super(context);
_mEventLayout = new EventRelativeLayout(context);
ll.addView(_mEventLayout);
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
addView(ll);
}
public void addEvent(final VEVENT evt) {
_mEventLayout.addEvent(evt);
invalidate();
this.refreshDrawableState();
}
}
public class EventRelativeLayout extends RelativeLayout {
public EventRelativeLayout(Context context) {
//This works fine.
super(context);
TextView tv = new TextView(context);
addView(tv);
int[] colors = { 0xFF052d42, 0xFF096da0 };
GradientDrawable drawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
tv.setBackgroundColor(0xFF290202);
tv.setBackgroundDrawable(drawable);
tv.setHeight(49);
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = (int) 0;
params.topMargin = (int) 100;
tv.setLayoutParams(params);
}
public void addEvent(EVT evt) {
TextView tv = new TextView(getContext());
addView(tv);
int[] colors = { 0xFF052d42, 0xFF096da0 };
GradientDrawable drawable = new GradientDrawable(
GradientDrawable.Orientation.TOP_BOTTOM, colors);
tv.setBackgroundColor(0xFF290202);
tv.setBackgroundDrawable(drawable);
tv.setHeight(100);
int dm = em -eh;
int dh = eh - sh;
if( dm < 0) {
dm += 60;
dh--;
}
tv.setWidth(100);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = 0;
params.topMargin = 600;
tv.setHeight(49);
tv.setLayoutParams(params);
}
}
I got the issue. It was a programmatic mistake and due to a wrong calculation, the veiw was added to a different page.

Pop up android app

I have a pop up that displays an image upon clicking a button. I got to display the pop up but the image is so small and I want it to occupy the 90% of the screen and add a close button to it.
Here is my code:
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmi);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.animalbite);
WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int fullScreenWidth = display.getWidth();
int dialogWidth = (int) (fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);
imageView.setLayoutParams(params);
//tv.setText("Hi this is a sample text for popup window");
layout.addView(imageView, params);
popUp.setContentView(layout);
Button ViewBmi = (Button) findViewById(R.id.ViewBmi);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10 ,10);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Here is the display:
ANy ideas?
Try this...
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params,params2;
ImageView imageView;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
final int height = (display.getHeight()*90)/100;
final int width = (display.getWidth()*90)/100;
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
tv = new TextView(this);
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.ic_launcher);
params = new LayoutParams(width,height-50);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(imageView, params);
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
layout.addView(button,params2);
popUp.setContentView(layout);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
popUp.dismiss();
click = true;
}
});
Button ViewBmi = (Button) findViewById(R.id.button1);
ViewBmi.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.CENTER, 10, 10);
popUp.update(10, 20, width,height);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
}
Try This....
Button button = new Button(this);
button.setText("Close");
params2 = new LayoutParams(100,50);
--->params2.setMargins(100, 0, 0, 0);
layout.addView(button,params2);
I'd do it like this:
height = (int)(0.9*getWindow().getWindowManager().getDefaultDisplay().getHeight());
params.height = height;
Try:
params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
...
layout.addView(imageView, params);
This way the ImageView will be as wide, as possible.
If the underlying dialog is not wide enough, then you can set it's width also full screen:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = LayoutParams.MATCH_PARENT;
getWindow().setAttributes(params);
Or if you want it to be for example 90% of the screen, then:
// get the width of the whole screen
Display display = getWindow().getDefaultDisplay();
int fullScreenWidth = display.getWidth();
//then set 90% of it to the width of the Dialog:
int dialogWidth = (int) fullScreenWidth * 0.9);
WindowManager.LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.WRAP_CONTENT;
params.width = dialogWidth;
getWindow().setAttributes(params);

Categories

Resources