I'm now implementing to drag & drop a relative_layout . I have a class that extends Relativelayout.
when I drag the relative layout to another layout , I would like to place the relative layout automatically in the center of the container (Relative Layout) from coding like(android:layout_centerInParent="true" from xml ).
This is my class extend Relative layout :
public class Test extends RelativeLayout {
ImageView image2;
public RL(Context context){
super(context);
TextView txtName= new TextView(context);
TextView bac_Number= new TextView(context);
ImageView image = new ImageView(context);
ImageView image1 = new ImageView (context);
ImageView image2 = new ImageView(context);
txtName.setText("First");
// bac.setId(1);
txtName.setText("Aung Aung");
image.setImageResource(R.drawable.fcb);
image1.setImageResource(R.drawable.upload);
image2.setImageResource(R.drawable.guest);
bac_Number.setText("8");
RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(txtName, lpSecond);
RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//lpFirst.addRule(RelativeLayout.RIGHT_OF, second.getId());
addView(bac_Number, lpFirst);
RelativeLayout.LayoutParams lpThird = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(image, lpThird);
}
}
This is the activity I called above class
public class MainActivity extends Activity {
Button btn_Choose;
int p;
String[] series = {"Photo","Name","Back Number"};
String[] series1 = {"myPhoto","GuestPhoto","Default"};
GridLayout layoutmain;
private static final String IMAGEVIEW_TAG = "The Android Logo";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RL r = new RL(this);
RL r1 = new RL(this);
RL r2 = new RL(this);
RL r3 = new RL(this);
RL r4 = new RL(this);
setContentView(R.layout.activity_main);
layoutmain = (GridLayout)findViewById(R.id.layoutmain);
layoutmain.addView(r);
r.setGravity(Gravity.CENTER);
layoutmain.addView(r1);
layoutmain.addView(r2);
layoutmain.addView(r3);
layoutmain.addView(r4);
btn_Choose = (Button)findViewById(R.id.btnselect);
r.setTag(IMAGEVIEW_TAG);
r1.setTag(IMAGEVIEW_TAG);
r2.setTag(IMAGEVIEW_TAG);
r3.setTag(IMAGEVIEW_TAG);
r4.setTag(IMAGEVIEW_TAG);
// myImage.setOnLongClickListener(new MyClickListener());
r.setOnLongClickListener(new MyClickListener());
r1.setOnLongClickListener(new MyClickListener());
r2.setOnLongClickListener(new MyClickListener());
r3.setOnLongClickListener(new MyClickListener());
r4.setOnLongClickListener(new MyClickListener());
findViewById(R.id.toplinear).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomlinear).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomlinear1).setOnDragListener(new MyDragListener());
findViewById(R.id.layoutlinear).setOnDragListener(new MyDragListener());
findViewById(R.id.layoutmain).setOnDragListener(new MyDragListener());
}
private final class MyClickListener implements OnLongClickListener {
#Override
public boolean onLongClick(View view) {
// TODO Auto-generated method stub
ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag( data,
shadowBuilder,
view,
0
);
return true;
}
}
class MyDragListener implements OnDragListener {
Drawable normalShape = getResources().getDrawable(R.drawable.normal_shape);
Drawable targetShape = getResources().getDrawable(R.drawable.target_shape);
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Toast.makeText(getApplicationContext(), "Start Drag ", Toast.LENGTH_LONG).show();
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackground(targetShape); //change the shape of the view
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackground(normalShape); //change the shape of the view back to normal
break;
case DragEvent.ACTION_DROP:
if(v.getClass().toString().equals("class android.widget.RelativeLayout")){
Log.i("class","Relative");
View view = (View) event.getLocalState();
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
RelativeLayout containView = (RelativeLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
}
else {
Log.i("CLAss", v.getClass()+"Cant drop");
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
Context context = getApplicationContext();
Toast.makeText(context, "You can't drop the image here",
Toast.LENGTH_LONG).show();
break;
}
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackground(normalShape); //go back to normal shape
default:
break;
}
return true;
}
}
public void goClick(View v){
if (v.getId() == R.id.btnselect){
RotationPickerDialog dp = new RotationPickerDialog(this, series, new RotationPickerListener() {
#Override
public void OnDoneButton(Dialog rotationdialog, String r, int position) {
// TODO Auto-generated method stub
rotationdialog.dismiss();
btn_Choose.setText(r);
p = position ;
}
});
WindowManager.LayoutParams WMLP = dp.getWindow().getAttributes();
WMLP.x = 100;
WMLP.y = 250;
dp.getWindow().setAttributes(WMLP);
dp.show();
Log.i("p", p+"");
Toast.makeText(this, "Opponent Team Name", Toast.LENGTH_LONG).show();
}
}
}
You can add rules to RelativeLayout.LayoutParams:
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)r.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
Try this .....
Rl.setGravity(RelativeLayout.CENTER_IN_PARENT);
Related
Here is the problem I am facing. On a empty relative layout, when touched textview is instantiated at the touched x y position. I got this far correct, but the problem is that when I touch on the empty space near already instantiated view, previous view and currently placed views are overlapped. I tried by the getting the child views of the layout and checking the current view and already placed view using rect data that if they intersect. How to solve this problem?
Here is the code:
public class MainActivity extends AppCompatActivity
{
private int id = 0;
private RelativeLayout root;
private RelativeLayout.LayoutParams params;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_designer);
root = (RelativeLayout) findViewById(R.id.rootlayout);
root.setOnTouchListener(new View.OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
instantiateView(v, event);
break;
}
return true;
}
});
}
private void instantiateView(View v, MotionEvent event)
{
int x = (int) event.getX();
int y = (int) event.getY();
TextView bt = new TextView(DesignerActivity.this);
bt.setText("1");
bt.setId(++id);
bt.setBackgroundColor(Color.BLACK);
bt.setTextColor(Color.WHITE);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
showDialog();
}
});
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(x, y, 0, 0);
bt.setLayoutParams(params);
//((ViewGroup) v).addView(bt);
if(root.getChildCount() <= 0)
{
((ViewGroup) v).addView(bt);
}
else
{
for (int i = 0; i < root.getChildCount(); i++)
{
if (!checkCollision(bt, root.getChildAt(i)))
{
if(bt != root.getChildAt(i))
{
((ViewGroup) v).addView(bt);
}
}
}
}
}
private void showDialog()
{
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_layout);
Button editBtn = (Button) dialog.findViewById(R.id.button1);
Button deleteBtn = (Button) dialog.findViewById(R.id.button2);
editBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
dialog.dismiss();
}
});
deleteBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
dialog.dismiss();
}
});
dialog.show();
}
private boolean checkCollision(View v1, View v2)
{
Rect r1 = new Rect(v1.getLeft(), v1.getTop(), v1.getRight(), v1.getBottom());
Rect r2 = new Rect(v2.getLeft(), v2.getTop(), v2.getRight(), v2.getBottom());
return r1.intersect(r2);
}
}
You are using Relative Layout that's why your Textviews are overlapping.
If you don't want the overlapping and want to place it next or somewhere else to the overlapped view , it is your decision. Just check if they intersect and take appropriate decision based on your requirement.
Below line is the problem in your code.
Rect r1 = new Rect(v1.getLeft(), v1.getTop(), v1.getRight(), v1.getBottom());
You set the params' Margin does not mean that you will get desired left,top,right, bottom values.You will get these values right after the inflation of your view hierarchy.
You can use this function:
private boolean checkCollision(View v1, View v2)
{
int leftMargin = ((RelativeLayout.LayoutParams) v1.getLayoutParams()).leftMargin;
int topMargin = ((RelativeLayout.LayoutParams) v1.getLayoutParams()).topMargin;
Rect r1 = new Rect(root.getPaddingLeft() + leftMargin, root.getPaddingTop() + topMargin,
root.getPaddingLeft() + leftMargin + v2.getWidth(), root.getPaddingTop() + topMargin + v2.getHeight());
Rect r2 = new Rect(v2.getLeft(), v2.getTop(), v2.getRight(), v2.getBottom());
return r1.intersect(r2);
}
After that use
params.addRule(); according to your requirement where you want to place your overlapping view.
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relativeLayoutHelper"
android:focusableInTouchMode="true"
android:focusable="true"
android:clickable="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LinearForPager"
android:focusableInTouchMode="true"
android:clickable="true"
android:focusable="true">
</LinearLayout>
</RelativeLayout>
In programm i add object ScreenPager in LinearLayout, it extends class ViewPager, and I add another LinearLayout with TextViews:
public class ModuleHelpOfGuide extends Activity {
private final String PATH_IMAGE = "helper";
float x1,y1, x2,y2;
ScreenPager screenPagerHelper;
LinearLayout linearLayoutHelper,linearLayoutHorizontalHelper;
RelativeLayout relativeLayoutHelper;
TextView pageTextView [];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_pager_layout);
LayoutInflater.from(this).setFactory(ScreenPager.getShortNameFactory());
screenPagerHelper = new ScreenPager(this);
addInScreen();
relativeLayoutHelper = (RelativeLayout)findViewById(R.id.relativeLayoutHelper);
linearLayoutHelper = (LinearLayout) findViewById(R.id.LinearForPager);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
screenPagerHelper.setLayoutParams(layoutParams);
linearLayoutHelper.addView(screenPagerHelper);
relativeLayoutHelper.setOnTouchListener(touchListenerRelativeHelper);
linearLayoutHorizontalHelper = new LinearLayout(this);
LinearLayout.LayoutParams layoutParamsHorizontal = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayoutHorizontalHelper.setLayoutParams(layoutParamsHorizontal);
LinearLayout.LayoutParams layoutParamsPageText = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParamsPageText.gravity = Gravity.BOTTOM;
String [] listAssets;
try {
listAssets = getAssets().list(PATH_IMAGE);
int count = listAssets.length;
pageTextView = new TextView [count];
for (int i = 0; i < count; i++) {
pageTextView[i] = createTexViewHelper();
pageTextView[i].setLayoutParams(layoutParamsPageText);
linearLayoutHorizontalHelper.addView(pageTextView[i]);
}
pageTextView[screenPagerHelper.getCurrentItem()].setTextColor(Color.WHITE);
} catch (IOException e) {
e.printStackTrace();
}
relativeLayoutHelper.setOnTouchListener(touchListenerRelativeHelper);
relativeLayoutHelper.addView(linearLayoutHorizontalHelper);
}
private void addInScreen (){
String [] listAssets;
try {
listAssets = getAssets().list(PATH_IMAGE);
if (listAssets.length > 0){
for (String pathFile : listAssets){
InputStream file = getAssets().open(PATH_IMAGE + "/" + pathFile);
Drawable drawable = Drawable.createFromStream(file,null);
ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(layoutParams);
imageView.setImageDrawable(drawable);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
screenPagerHelper.addScreen(imageView);
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
private TextView createTexViewHelper (){
TextView textViewHelper = new TextView(getApplicationContext());
textViewHelper.setText(".");
textViewHelper.setTextSize(65);
textViewHelper.setTypeface(null, Typeface.BOLD);
textViewHelper.setTextColor(android.graphics.Color.GRAY);
return textViewHelper;
};
View.OnTouchListener touchListenerRelativeHelper = new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
int current = screenPagerHelper.getCurrentItem();
for (int i = 0; i < pageTextView.length; i++){
pageTextView[i].setTextColor(android.graphics.Color.GRAY);
}
pageTextView[current].setTextColor(getResources().getColor(R.color.white));
return true;
}
if (event.getAction() == MotionEvent.ACTION_UP){
int current = screenPagerHelper.getCurrentItem();
for (int i = 0; i < pageTextView.length; i++){
pageTextView[i].setTextColor(android.graphics.Color.GRAY);
}
pageTextView[current].setTextColor(getResources().getColor(R.color.white));
return false;
}
return true;
}
};
}
Problem in TouchEvent, it's not working if use on Relative or Linear Layots, if i use it on ScreenPager than event begin work, but textView lag on 1 image in slaider. If i use TouchEvent in ScreenPager and return true, than slider doesnt work. How track event touchlistener on RelativeLayout?
I solved problem not good, but it works, how could, set on ScreenPager listener TouchListener and in touchlistener:
View.OnTouchListener touchListenerHelper = new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
int current;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
if (x1 > x2 ) {
current = screenPagerHelper.getCurrentItem()+1;
current = current == pageTextView.length ? pageTextView.length - 1 : current;
for (int i = 0; i < pageTextView.length; i++) {
pageTextView[i].setTextColor(android.graphics.Color.GRAY);
}
pageTextView[current].setTextColor(getResources().getColor(R.color.white));
} else if ( x1 < x2){
current = screenPagerHelper.getCurrentItem()-1;
current = current < 0 ? 0 : current;
for (int i = 0; i < pageTextView.length; i++) {
pageTextView[i].setTextColor(android.graphics.Color.GRAY);
}
pageTextView[current].setTextColor(getResources().getColor(R.color.white));
}
break;
}
return false;
}
};
becouse event of ViewPAger begin after my costume event, i add to current 1.
I have a RelativeLayout with 4 ImageViews that should have 2 on top, one on each side of the Layout, and 2 ImageViews below the first two, one on each side of the screen.
I have 2 problems, the two ImageViews that are supposed to be below, do not go below. And The 2 ImageViews that are aligned to be on the right of the screen should be one on top of the other but seem to be about 25dp out.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
RelativeLayout tl = (RelativeLayout)v.findViewById(R.id.l1);
tl.setBackgroundColor(Color.WHITE);
ImageView imageTopL = new ImageView(getActivity());
imageTopL.setImageResource(R.drawable.bell_dl_256);
imageTopL.setPadding(50, 0, 0, 0);
imageTopL.setId(0);
ImageView imageBottomL = new ImageView(getActivity());
imageBottomL.setImageResource(R.drawable.bell_dl_256);
imageBottomL.setPadding(50, 0, 0, 0);
imageBottomL.setId(1);
ImageView imageBottomR = new ImageView(getActivity());
imageBottomR.setImageResource(R.drawable.bell_dr_256);
imageBottomR.setPadding(0, 0, 50, 0);
imageBottomR.setId(2);
ImageView imageTopR = new ImageView(getActivity());
imageTopR.setImageResource(R.drawable.bell_dr_256);
imageTopR.setPadding(0, 0, 50, 0);
imageTopR.setId(3);
tl.addView(imageTopL);
tl.addView(imageTopR);
tl.addView(imageBottomL);
tl.addView(imageBottomR);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)imageTopL.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params = (RelativeLayout.LayoutParams)imageTopR.getLayoutParams();
params.addRule(RelativeLayout.RIGHT_OF, imageTopL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params = (RelativeLayout.LayoutParams)imageBottomL.getLayoutParams();
params.addRule(RelativeLayout.BELOW, imageTopL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params = (RelativeLayout.LayoutParams)imageBottomR.getLayoutParams();
params.addRule(RelativeLayout.BELOW, imageTopL.getId());
params.addRule(RelativeLayout.RIGHT_OF, imageBottomL.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
return v;
// try this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_hello_moon, parent, false);
RelativeLayout tl = (RelativeLayout) v.findViewById(R.id.l1);
tl.setBackgroundColor(Color.WHITE);
ImageView imageTopL = new ImageView(this);
imageTopL.setId(1);
imageTopL.setImageResource(R.drawable.ic_launcher);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
imageTopL.setAdjustViewBounds(true);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params1.setMargins(50,10,0,0);
imageTopL.setLayoutParams(params1);
imageTopL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Top Left",Toast.LENGTH_SHORT).show();
}
});
ImageView imageTopR = new ImageView(this);
imageTopR.setId(2);
imageTopR.setImageResource(R.drawable.ic_launcher);
imageTopL.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params2.setMargins(0,10,50,0);
imageTopR.setLayoutParams(params2);
imageTopR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Top Right",Toast.LENGTH_SHORT).show();
}
});
ImageView imageBottomL = new ImageView(this);
imageBottomL.setId(3);
imageBottomL.setImageResource(R.drawable.ic_launcher);
imageTopL.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params3.addRule(RelativeLayout.BELOW, imageTopL.getId());
params3.setMargins(50,10,0,0);
imageBottomL.setLayoutParams(params3);
imageBottomL.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Bottom Left",Toast.LENGTH_SHORT).show();
}
});
ImageView imageBottomR = new ImageView(this);
imageBottomR.setId(4);
imageBottomR.setImageResource(R.drawable.ic_launcher);
imageTopL.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params4.addRule(RelativeLayout.BELOW, imageTopR.getId());
params4.setMargins(0,10,50,0);
imageBottomR.setLayoutParams(params4);
imageBottomR.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(ImageTextListViewActivity.this,"Bottom Right",Toast.LENGTH_SHORT).show();
}
});
tl.addView(imageTopL);
tl.addView(imageTopR);
tl.addView(imageBottomL);
tl.addView(imageBottomR);
return v;
}
I have a LinearLayout into my PopupView, and a button into this LinearLayout. Event listener of this button works correctly, but animation of pressing (highlighting) is not starting. What I did wrong? Please, gelp!
public class PopupView extends View
{
private PopupWindow popUp;
private Button buttonOk;
private OnClickListener onClick;
private LayoutParams params;
private int popUpWidth = 0;
private int popUpHeight = 0;
private int popUpX, popUpY;
public PopupView(Context context, View parent)
{
super(context);
popUp = new PopupWindow(context);
popUpLayout = new LinearLayout(context);
buttonOk = new Button(context);
buttonOk.setText("OK");
buttonOk.setId(1);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popUpLayout.setOrientation(LinearLayout.VERTICAL);
popUp.setContentView(popUpLayout);
onClick = new OnClickListener()
{
public void onClick (View v)
{
switch (v.getId())
{
case 1:
System.out.println("OK pressed!");
break;
case 2:
break;
}
}
};
buttonOk.setOnClickListener(onClick);
popUpLayout.removeAllViews();
popUpLayout.addView(buttonOk, params);
popUpWidth = 200;
popUpHeight = 400;
popUpX = 300;
popUpY = 300;
popUp.showAtLocation(parent, Gravity.NO_GRAVITY, popUpX, popUpY);
popUp.update(popUpX, popUpY, popUpWidth, popUpHeight);
}
}
In default PopupView is not focusble. Need to call method popup.setFocusable(true). Thanks for all!
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.