I have a edit text inside an alert Dialog for some user input. But when I click on the edit text it does not show the soft keyboard.
Whereas if the same edit text is placed inside a dialog it show the keyboard when the edit text is focused.
public class LaunchingActivity extends Activity {
Button addSubList = null;
TableLayout tabLayToDoList;
int textViewId = StaticContents.textViewId;
int edtTxtViewId = StaticContents.edtTxtViewId;
int ImgViewId = StaticContents.ImgViewId;
int relLayViewId = StaticContents.relLayViewId;
int count = StaticContents.count;
CustomDialog share;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println(textViewId + " " + edtTxtViewId + " " + ImgViewId
+ " " + relLayViewId);
addSubList = (Button) findViewById(R.id.btnAddSubItem);
tabLayToDoList = (TableLayout) findViewById(R.id.tabLayToDoList);
}
public void showSublistDialog() {
Dialog subList = new Dialog(this);
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
System.out.println("Display Width and Height :" + "width " + width
+ " height " + height);
subList.show();
// rel.setLayoutParams(layoutPrams);
}
#Override
protected void onPostResume() {
addSubList.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
count = 0;
share = new CustomDialog(LaunchingActivity.this);
share.show();
}
});
super.onPostResume();
}
private class CustomDialog extends Dialog {
public CustomDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
RelativeLayout relLayParent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addsubitem);
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
LayoutParams params = getWindow().getAttributes();
if(height>width){
//potrait mode
params.height = height - 150;
params.width = width-60;
}else{
params.height = height - 100;
params.width = width-100;
}
getWindow().setAttributes(
(android.view.WindowManager.LayoutParams) params);
relLayParent = (RelativeLayout) findViewById(R.id.relLayParent);
addDesScription(relLayParent, R.id.relLaySubListContent);
addDesScription(relLayParent, R.id.relLaySubListContent);
}
public void addDesScription(RelativeLayout relParent, int relLayoutId) {
TextView txtDescription = new TextView(LaunchingActivity.this);
EditText edtDescription = new EditText(LaunchingActivity.this);
ImageView imgaddDescription = new ImageView(LaunchingActivity.this);
edtDescription.setInputType(262144);
RelativeLayout relLay = new RelativeLayout(LaunchingActivity.this);
textViewParams(txtDescription);
edttxtDescriptionParams(edtDescription);
imageViewParams(imgaddDescription);
relLaySubListDescription(relLay, relLayoutId);
relLay.addView(txtDescription);
relLay.addView(edtDescription);
relParent.addView(relLay);
if (count != 0) {
relLay.addView(imgaddDescription);
} else {
relLay.setVisibility(View.GONE);
}
incrementIds();
}
public void textViewParams(TextView txtDescription) {
txtDescription.setText("Description:");
txtDescription.setTextColor(getResources().getColor(R.color.Blue));
txtDescription.setId(textViewId);
txtDescription.setMinLines(1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
txtDescription.setLayoutParams(params);
}
public void imageViewParams(ImageView addSubListDescription) {
addSubListDescription
.setImageResource(R.drawable.add_description_icon);
addSubListDescription.setId(ImgViewId);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
addSubListDescription.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
addDesScription(relLayParent, relLayViewId - 1);
return false;
}
});
addSubListDescription.setLayoutParams(params);
}
public void edttxtDescriptionParams(EditText edtDescription) {
edtDescription.setId(edtTxtViewId);
edtDescription.setMinLines(1);
edtDescription.setHint("Description");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF, ImgViewId);
params.addRule(RelativeLayout.RIGHT_OF, textViewId);
params.addRule(RelativeLayout.CENTER_VERTICAL);
edtDescription.setLayoutParams(params);
}
public void incrementIds() {
ImgViewId++;
textViewId++;
edtTxtViewId++;
relLayViewId++;
count++;
}
public void relLaySubListDescription(
RelativeLayout relLaySubListDescription, int relLayoutId) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width = android.widget.RelativeLayout.LayoutParams.FILL_PARENT;
relLaySubListDescription.setId(relLayViewId);
params.addRule(RelativeLayout.BELOW, relLayoutId);
relLaySubListDescription.setLayoutParams(params);
}
}
}
public class LaunchingActivity extends Activity {
Button addSubList = null;
TableLayout tabLayToDoList;
int textViewId = StaticContents.textViewId;
int edtTxtViewId = StaticContents.edtTxtViewId;
int ImgViewId = StaticContents.ImgViewId;
int relLayViewId = StaticContents.relLayViewId;
int count = StaticContents.count;
CustomDialog share;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
System.out.println(textViewId + " " + edtTxtViewId + " " + ImgViewId
+ " " + relLayViewId);
addSubList = (Button) findViewById(R.id.btnAddSubItem);
tabLayToDoList = (TableLayout) findViewById(R.id.tabLayToDoList);
}
public void showSublistDialog() {
Dialog subList = new Dialog(this);
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
System.out.println("Display Width and Height :" + "width " + width
+ " height " + height);
subList.show();
// rel.setLayoutParams(layoutPrams);
}
#Override
protected void onPostResume() {
addSubList.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
count = 0;
share = new CustomDialog(LaunchingActivity.this);
share.show();
}
});
super.onPostResume();
}
private class CustomDialog extends Dialog {
public CustomDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
RelativeLayout relLayParent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addsubitem);
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
LayoutParams params = getWindow().getAttributes();
if(height>width){
//potrait mode
params.height = height - 150;
params.width = width-60;
}else{
params.height = height - 100;
params.width = width-100;
}
getWindow().setAttributes(
(android.view.WindowManager.LayoutParams) params);
relLayParent = (RelativeLayout) findViewById(R.id.relLayParent);
addDesScription(relLayParent, R.id.relLaySubListContent);
addDesScription(relLayParent, R.id.relLaySubListContent);
}
public void addDesScription(RelativeLayout relParent, int relLayoutId) {
TextView txtDescription = new TextView(LaunchingActivity.this);
EditText edtDescription = new EditText(LaunchingActivity.this);
ImageView imgaddDescription = new ImageView(LaunchingActivity.this);
edtDescription.setInputType(262144);
RelativeLayout relLay = new RelativeLayout(LaunchingActivity.this);
textViewParams(txtDescription);
edttxtDescriptionParams(edtDescription);
imageViewParams(imgaddDescription);
relLaySubListDescription(relLay, relLayoutId);
relLay.addView(txtDescription);
relLay.addView(edtDescription);
relParent.addView(relLay);
if (count != 0) {
relLay.addView(imgaddDescription);
} else {
relLay.setVisibility(View.GONE);
}
incrementIds();
}
public void textViewParams(TextView txtDescription) {
txtDescription.setText("Description:");
txtDescription.setTextColor(getResources().getColor(R.color.Blue));
txtDescription.setId(textViewId);
txtDescription.setMinLines(1);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
txtDescription.setLayoutParams(params);
}
public void imageViewParams(ImageView addSubListDescription) {
addSubListDescription
.setImageResource(R.drawable.add_description_icon);
addSubListDescription.setId(ImgViewId);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
addSubListDescription.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View arg0, MotionEvent arg1) {
addDesScription(relLayParent, relLayViewId - 1);
return false;
}
});
addSubListDescription.setLayoutParams(params);
}
public void edttxtDescriptionParams(EditText edtDescription) {
edtDescription.setId(edtTxtViewId);
edtDescription.setMinLines(1);
edtDescription.setHint("Description");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF, ImgViewId);
params.addRule(RelativeLayout.RIGHT_OF, textViewId);
params.addRule(RelativeLayout.CENTER_VERTICAL);
edtDescription.setLayoutParams(params);
}
public void incrementIds() {
ImgViewId++;
textViewId++;
edtTxtViewId++;
relLayViewId++;
count++;
}
public void relLaySubListDescription(
RelativeLayout relLaySubListDescription, int relLayoutId) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width = android.widget.RelativeLayout.LayoutParams.FILL_PARENT;
relLaySubListDescription.setId(relLayViewId);
params.addRule(RelativeLayout.BELOW, relLayoutId);
relLaySubListDescription.setLayoutParams(params);
}
}
}
Related
a have some problem and didn't find a solution. The problem is, i have a image view that's lay on layout and this layout lay on window manager, this image view is clickable. This image view have to move to right and to the left and be clickable. Here is my code
`public class FloatingAnimationService extends Service {
private WindowManager windowManager;
private ImageView floatingUnit;
private boolean isClicked;
public void onCreate() {
super.onCreate();
isClicked = false;
floatingUnit = new ImageView(this);
//a unit as imageView
Picasso.with(this).load(new File(Environment.getExternalStorageDirectory().getPath() +"/" + "cat.png")).into(floatingUnit);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(AppMethods.getPxFromDp(120 ,this), AppMethods.getPxFromDp(120 , this));
floatingUnit.setLayoutParams(layoutParams);
final LinearLayout floatingUnitLayout = new LinearLayout(this);
floatingUnitLayout.addView(floatingUnit);
windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
//here is all the science of params
final LayoutParams myParams = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.TYPE_PHONE,
LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
myParams.gravity = Gravity.TOP | Gravity.LEFT;
myParams.x=0;
myParams.y=100;
windowManager.addView(floatingUnitLayout, myParams);
// add a floatingUnit icon in window
Animation animation = AnimationUtils.loadAnimation(this ,R.anim.floating_unit_animation);
floatingUnit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startQuestion();
stopSelf();
windowManager.removeViewImmediate(floatingUnitLayout);
isClicked = true;
}
});
floatingUnit.startAnimation(animation);
startAnimationTimer(floatingUnitLayout);
AnimationSet animationSet = new AnimationSet(true);
myParams.x = 100;
myParams.y = 100;
windowManager.updateViewLayout(floatingUnitLayout, myParams);
Animation animationToRight = AnimationUtils.loadAnimation(this ,R.anim.left_to_right_animation);
Animation animationToLeft = AnimationUtils.loadAnimation(this , R.anim.right_to_left_animation);
animationSet.addAnimation(animationToRight);
floatingUnit.startAnimation(animation);
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private void startQuestion (){
Intent intentQuestionActivity = new Intent(this, QuestionActivity.class);
intentQuestionActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentQuestionActivity);
}
private void startAnimationTimer(final View floatingUnitLayout){
long animationLifeTime = 10000;
new CountDownTimer(animationLifeTime, 1000) {
public void onTick(long millisUntilFinished) {
Log.i("animation remaining: " , Long.toString(millisUntilFinished / 1000));
}
public void onFinish() {
Log.i("animation: " , "DONE");
if(!isClicked) {
windowManager.removeViewImmediate(floatingUnitLayout);
stopSelf();
}
}
}.start();
}
`
Please help. Thanks !
Thanks all . Found the solution by myself. The trick is: to move a image that lay on window manager, you have to update the Layout Params.
here is the code below
private void startAnimationTimer(final View floatingUnitLayout) {
long animationLifeTime = AppData.first(AppData.class).getCountSecondsPicShow();
countDownTimer = new CountDownTimer(animationLifeTime, 100) {
public void onTick(long millisUntilFinished) {
Log.i("animation remaining: ", Long.toString(millisUntilFinished / 1000));
myParams.x = myParams.x + 2;
myParams.y = myParams.y + 2;
windowManager.updateViewLayout(floatingUnitLayout, myParams);
}
public void onFinish() {
Log.i("animation: ", "DONE");
if (!isClicked) {
windowManager.removeViewImmediate(floatingUnitLayout);
stopSelf();
}
}
}.start();
}
I create programmatically few layouts and I need get width one of them, recalculate this value and change. All this action I do in onGlobalLayoutListener. But when I set a new width value, he doesnt change.
private RelativeLayout batteryContainer;
private LinearLayout batteryLeftSide;
private LinearLayout batteryRightSide;
public void drawBattery(){
handler.post(new Runnable() {
#Override
public void run() {
//Контейнер всієї батарейки
batteryContainer = new RelativeLayout(activity);
RelativeLayout.LayoutParams batteryContainerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
batteryContainerParams.setMargins((int) convertDpToPixel(containerMarginLeft), (int) convertDpToPixel(containerMarginTop), (int) convertDpToPixel(containerMarginRight), (int) convertDpToPixel(containerMarginBottom));
if(layoutBelow != null){
batteryContainerParams.addRule(RelativeLayout.BELOW, layoutBelow.getId());
}
if(layoutAbove != null){
batteryContainerParams.addRule(RelativeLayout.ABOVE, layoutAbove.getId());
}
batteryContainer.setLayoutParams(batteryContainerParams);
batteryContainer.setBackgroundColor(Color.parseColor("#505050"));
batteryContainer.setId(containerId);
int leftWidth = 0;
int rightWidth = 0;
//Ліва частина батарейки
batteryLeftSide = new LinearLayout(activity);
batteryLeftSide.setOrientation(LinearLayout.HORIZONTAL);
RelativeLayout.LayoutParams batteryLeftSideParams = new RelativeLayout.LayoutParams((int)convertDpToPixel(leftWidth), (int)convertDpToPixel(sideHeight));
batteryLeftSideParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
batteryLeftSide.setLayoutParams(batteryLeftSideParams);
batteryLeftSide.setBackgroundColor(Color.parseColor("#900000"));
batteryLeftSide.setId(leftSideId);
//Права частина батарейки
batteryRightSide = new LinearLayout(activity);
batteryRightSide.setOrientation(LinearLayout.HORIZONTAL);
RelativeLayout.LayoutParams batteryRightSideParams = new RelativeLayout.LayoutParams((int)convertDpToPixel(rightWidth), (int)convertDpToPixel(sideHeight));
batteryRightSideParams.addRule(RelativeLayout.RIGHT_OF, batteryLeftSide.getId());
batteryRightSide.setLayoutParams(batteryRightSideParams);
batteryRightSide.setBackgroundColor(Color.parseColor("#009900"));
batteryRightSide.setId(rightSideId);
//Додамо праві та ліві сторони в контейнер
batteryContainer.addView(batteryLeftSide);
batteryContainer.addView(batteryRightSide);
//Додамо контейнер в кореневий Layout на активності
rootLayout.addView(batteryContainer);
//Обчислення яка сторона батарейки займе 50%
ViewTreeObserver observer = rootLayout.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int batteryContainerWidth = batteryContainer.getMeasuredWidth();
int halfPlace = batteryContainerWidth * 50 / 100;
trustFromMe = halfPlace;
if(trustToMe > trustFromMe){
batteryLeftSide.getLayoutParams().width = halfPlace;
}
if(trustToMe < trustFromMe){
batteryRightSide.getLayoutParams().width = halfPlace;
}
if(trustToMe == trustFromMe){
batteryLeftSide.getLayoutParams().width = halfPlace;
batteryRightSide.getLayoutParams().width = halfPlace;
}
}
});
//but width not change
}
});
}
Try adding this to the end of the GlobalLayoutListener:
rootLayout.requestLayout();
This should force the layout to redraw it's size. If this doesn't work you may want to try creating and setting new LayoutParams as well.
Hiii
i have to create circular menu after R & D i got the following output which is display in image 1 but i want to give effect like image 2 i will put it my code here so you can check it
My current Output image 1 in this button and text display horizontally i want to convert this to arc effect both button and text like below image 2
i want to make this buttons like this image
MainActivity.java
public class MainActivity extends MenuActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override public void createButtons() {
for(int i = 0 ; i < 3 ; i++) {
RotativeButton button = new RotativeButton(this);
if(i==0)
{
button.setBackgroundResource(R.drawable.green);
//button.setBackgroundColor(Color.GREEN);
button.setText("Insurance Green");
button.setPosition(i);
button.setLabel("OPTION "+(i+1));
button.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
}
if(i==1)
{
button.setBackgroundResource(R.drawable.red);
//button.setBackgroundColor(Color.RED);
button.setText("Insurance Red");
button.setPosition(i);
button.setLabel("OPTION "+(i+1));
button.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
}
if(i==2)
{
button.setBackgroundResource(R.drawable.yellow);
//button.setBackgroundColor(Color.YELLOW);
button.setText("Insurance Yellow");
button.setPosition(i);
button.setLabel("OPTION "+(i+1));
button.setLayoutParams(new LinearLayout.LayoutParams(100, 100));
}
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("id", String.valueOf(i+1));
button.setIntent(intent);
mButtonList.add(button);
}
}
}
MenuActivity.java
public abstract class MenuActivity extends Activity {
protected List<RotativeButton> mButtonList = new ArrayList<RotativeButton>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.common_view);
// RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainView);
LinearLayout mainLayout=(LinearLayout)findViewById(R.id.mainView);
getLayoutInflater().inflate(R.layout.menu, mainLayout, true);
final RotativeMenuView customView = (RotativeMenuView) findViewById(R.id.rotativeMenuView);
final Button okBtn = (Button) customView.findViewById(R.id.okBtn);
customView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
public void onGlobalLayout()
{
if(okBtn.getVisibility()==View.VISIBLE)
{
customView.addButtons(mButtonList);
customView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
});
createButtons();
}
protected abstract void createButtons();
}
RotativeButton.java
public class RotativeButton extends Button {
private int position;
private Intent intent;
private String label;
public RotativeButton(Context context) {
super(context);
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public Intent getIntent() {
return intent;
}
public void setIntent(Intent intent) {
this.intent = intent;
}
}
RotativeMenuView.java
public class RotativeMenuView extends RelativeLayout
{
private List<RotativeButton> _buttonList;
private HashMap<Integer, Point > _pointForPosition = new HashMap<Integer, Point>();
private HashMap<Integer, RectF > _rectForPosition = new HashMap<Integer, RectF>();
private TextView titleTv;
public RotativeMenuView(Context context) {
super(context);
}
public RotativeMenuView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public RotativeMenuView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public void addButtons(List<RotativeButton> buttonList) {
final Button okBtn = (Button) findViewById(R.id.okBtn);
titleTv = (TextView) findViewById(R.id.labelTv);
okBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
RotativeButton activeButton = getActiveButton();
Intent i = activeButton.getIntent();
getContext().startActivity(i);
}
});
_buttonList = buttonList;
int numberOfButtons = buttonList.size();
final double angle = 360 / numberOfButtons;
int okWidth = okBtn.getMeasuredWidth();
int okHeight = okBtn.getMeasuredHeight();
Log.e("OK_WIDTH", String.valueOf(okWidth));
Log.e("OK_HEIGHT", String.valueOf(okHeight));
int okLeft = okBtn.getLeft();
int okTop = okBtn.getTop();
Log.e("okLeft", String.valueOf(okLeft));
Log.e("okTop", String.valueOf(okTop));
int distance = okWidth;
Log.e("distance", String.valueOf(distance));
final int[] point = new int[2];
okBtn.getLocationInWindow(point);
for (int i = 0; i < numberOfButtons; i++) {
RotativeButton rotativeButton = buttonList.get(i);
int position = rotativeButton.getPosition();
int buttonHeight = rotativeButton.getBackground()
.getIntrinsicHeight();
int buttonWidth = rotativeButton.getBackground()
.getIntrinsicWidth();
int diffHeight = (okHeight / 2) - (buttonHeight / 2);
int diffWidth = (okWidth / 2) - (buttonWidth / 2);
float newAngle = (float) angle * position;
Point nextPoint = nextPosition(okTop + diffHeight, okLeft
+ diffWidth, newAngle, distance);
int newLeft = nextPoint.x;
int newTop = nextPoint.y;
final RectF okRect = new RectF(point[0] - newLeft
- okBtn.getWidth() + diffWidth, point[1] - newTop
- okBtn.getHeight() + diffHeight, (point[0] - newLeft)
+ okBtn.getWidth() + diffWidth, (point[1] - newTop)
+ okBtn.getHeight() + diffHeight);
rotativeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final RotativeButton clickedButton = (RotativeButton) v;
int position = clickedButton.getPosition();
if (position == 0) {
return;
}
int total = _buttonList.size();
final int clickedPosition = total - position;
double angle = 360 / total;
titleTv.setVisibility(INVISIBLE);
for (int i = 0; i < total; i++) {
final RotativeButton currentButton = _buttonList.get(i);
position = currentButton.getPosition();
RectF rectF = _rectForPosition.get(position);
float startAngle = (float) angle * position;
Path aPath = new Path();
aPath.addArc(rectF, startAngle, (float) angle
* clickedPosition);
Animation anim = new PathAnimation(aPath);
anim.setDuration(500);
anim.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {
currentButton.setClickable(false);
okBtn.setClickable(false);
}
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
public void onAnimationEnd(Animation animation) {
currentButton.setClickable(true);
okBtn.setClickable(true);
updatePosition(currentButton, clickedPosition);
currentButton.clearAnimation();
}
});
anim.setFillBefore(true);
currentButton.startAnimation(anim);
}
}
});
LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.topMargin = newTop;
params.leftMargin = newLeft;
addView(rotativeButton, params);
if (position == 0) {
applyLabel(rotativeButton.getLabel());
}
_pointForPosition.put(position, nextPoint);
_rectForPosition.put(position, okRect);
}
}
public void updatePosition(final RotativeButton currentButton, int diffPosition){
int total = _buttonList.size();
int currentPosition = currentButton.getPosition();
int newPosition = currentPosition + diffPosition;
if(newPosition >= total ){
newPosition -= total;
}
currentButton.setPosition(newPosition);
Point pointForPosition = _pointForPosition.get(newPosition);
int newLeft = pointForPosition.x;
int newTop = pointForPosition.y;
Log.d("CustomView", "Button: "+ currentButton.getPosition()+ "("+currentButton.getLabel()+") , New Left:" +newLeft+" top: "+ newTop);
currentButton.layout(newLeft, newTop,
newLeft + currentButton.getMeasuredWidth(), newTop + currentButton.getMeasuredHeight());
Log.d("CustomView", "Moving from:" +currentPosition+" to: "+currentButton.getPosition() + "code: " +currentButton.getLabel());
if (newPosition == 0) {
applyLabel(currentButton.getLabel());
}
}
protected void applyLabel( String label) {
titleTv.setText(label.toUpperCase());
titleTv.setVisibility(VISIBLE);
}
private Point nextPosition(int okTop, int okLeft, double angle, int distance) {
int x = okLeft + (int) (distance*Math.cos(Math.toRadians(angle)));
int y = okTop + (int) (distance*Math.sin(Math.toRadians(angle)));
Log.e("X", String.valueOf(x));
Log.e("Y", String.valueOf(y));
Point p = new Point();
p.set(x, y);
return p;
}
private RotativeButton getActiveButton()
{
for (RotativeButton aButton : _buttonList)
{
int position = aButton.getPosition();
if(position == 0)
{
return aButton;
}
}
return null;
}
}
PathAnimation.java
public class PathAnimation extends Animation {
private PathMeasure measure;
private float[] pos = new float[2];
public PathAnimation(Path path) {
measure = new PathMeasure(path, false);
}
#Override
protected void applyTransformation(float interpolatedTime, Transformation t){
measure.getPosTan(measure.getLength() * interpolatedTime, pos,null);
t.getMatrix().setTranslate(pos[0], pos[1]);
}
}
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.
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);