Child view of a cutom RelativeLayout doesn't come to front - android

I have a custom Relative Layout(a class extended to RelativeLayout). I draw some bitmaps on a canvas using the method dispatchDraw(). And I want to show an Edit text box on top this canvas. So I create a new instance of EditText and bring it to front. EditText adds to the layout but doesn't come to front. When I remove canvas drawing code the edit text appears.
Help me if you know a way to bring the EditText to the front of the canvas.
public class EditCardLayout extends RelativeLayout {
public EditCardLayout (Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
editText = new EditText(context);
editText.setText("My Text");
addView( editText );
editText.bringToFront();
}
#Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
// some bitmaps are drawn here...
}
}
XML :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#eceadb"
android:gravity="center">
<editCard.EditCardLayout
android:id="#+id/cardEditView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
</RelativeLayout>

What order are you adding the elements in? According to the docs, the z-order is affected by the order in which the elements are drawn, with the last thing drawn on top. Try adding the edittext last.

Related

Android custom input method: layout_alignParentBottom causes input area to fill most of screen

I created a custom input method that changes layouts dynamically. I want to align the last view to the bottom of the input area. I set the layout_alignParentBottom property for the last view, but then the input area expands to fill almost the entire screen, as seen here.
Without layout_alignParentBottom set
With layout_alignParentBottom set
I do have an android:windowBackground style set, but even without it, it does the same thing. I inflate my XML layout in my custom InputMethodService class in onCreateInputView(), and I don't adjust any layout settings in the onFinishInflate() method of MyClass. Here is my code. Any ideas?
my_class_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<com.package.MyClass xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:text="#string/login" />
</com.package.MyClass>
MyClass.java
package com.package;
<imports>
public class MyClass extends RelativeLayout {
private final MyClassInputMethodService mInputMethodServiceContext;
private Button mLoginButton;
public MyClass(Context context, AttributeSet attrs) {
super(context, attrs);
mInputMethodServiceContext = (MyClassInputMethodService)context;
}
public MyClass( MyClassInputMethodService context ) {
super(context);
mInputMethodServiceContext = (MyClassInputMethodService)context;
}
#Override
protected void onFinishInflate() {
// Login button
mLoginButton = (Button)findViewById(R.id.login_button);
}
MyClassInputMethodService.java
package com.package;
<imports>
public class MyClassInputMethodService extends InputMethodService {
private static final String TAG = MyClassInputMethodService.class.getSimpleName();
private static MyClassInputMethodService mContext;
private View mInputView;
#Override
public void onCreate() {
// Set Theme
setTheme(UserManagement.getCurrentUser().getTheme().getResourceId());
super.onCreate();
mContext = this;
}
#Override
public View onCreateInputView() {
mInputView = (MyClassLogin) getLayoutInflater().inflate(R.layout.fast_fill_login, null);
return mInputView;
}
}
EDIT
I tried a smaller-sized image for the windowBackground drawable, and it did make the default input method area smaller.
<style name="Theme.Blue" >
<item name="android:windowBackground">#drawable/background_blue</item>
</style>
So that leads me to another possibility. Do I have to use the smallest possible background image size that would fit the least number of views that will be shown, or can I make the windowBackground drawable dynamic, so it will only use as much height as the input method views use?

Draw lines between ListView rows

I have ListView with ImageView in rows.
I need to draw lines between these ImageViews.
So one line is started at first row at ImageView position and ends at second row at ImageView position. And so on.
What is the best way to draw these lines?
Thank you.
UPDATE. I am sorry, I can't upload image and I speak english not very well.
It is not horizontal divider between rows. If to simplify it: line starts at vertical center of one row at ImageView position and ends with vertical center of next row at ImageView position.
I think I should try:
To override row view.
At Adapter's GetView where I create row view pass to this row view coordinates of adjacent rows.
In view's onDraw method calculate coordinates and draw line.
To override ListView. In it's OnDraw method try to get ImageView's positions and draw all lines.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="#+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#FFCC00"
android:dividerHeight="4px"/>
</LinearLayout>
Have a listview seperator.
I am not sure if I understood your question correctly, but this should make really nice gradient lines between rows :
mListView = getListView();
mAdapter = new ServersListAdapter(this, new String[] {},
new String[] {}, new String[] {});
int[] colors = { Color.parseColor("#D3D3D3"), Color.parseColor("#D3D3D3"), Color.parseColor("#D3D3D3") };
mListView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
This is an example of a view you can draw a line in. Create this class, refresh your palette if you need to.
public class LineTextView extends AppCompatTextView {
private Paint paint = new Paint();
public LineTextView(Context context) {
super(context);
}
public LineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.parseColor("#000000"));
paint.setStrokeWidth(2);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(0, 0, 0, getHeight(), paint);
}
}
Now you can use it in your layout
<LinearLayout
android:layout_width="2dp"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.taskmodel.view.LineTextView
android:id="#+id/lineTextViewTopHalf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"/>
<com.example.taskmodel.view.LineTextView
android:id="#+id/lineTextViewBottomHalf"
android:layout_width="2dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

How to scroll bitmaps?

I have one image view (assigned one bitmap to it) and another bitmap (Transparent) for painting. How it is possible to scroll this bitmap and background bitmap at the same time to be painted at different places.
From what I understood you are trying to draw on top of a image. If thats the case, create a custom view and get the image using BitmapFactory. After you get a bitmap object, use its copy method and use that copy for the canvas of the view.
Now you can override the onDraw method of a custom view and draw anything on top of it.
This view can be added to the layout, and scrolling will happen to the view.
Edit: Sample Code
Ok this is some code that might help you. I dont have the time to go through all your code.
But I tried to understand your requirement.
I am showing the code for an activity, its xml and custom view
Activity.java
public class DrawDemo extends Activity {
private FrameLayout container;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.draw_demo_layout);
container = (FrameLayout)findViewById(R.id.sc);
container.addView(new DrawView(this));
}
public void drawHandler(View target){
container.addView(new DrawView(this));
}
public void clearHandler(View target){
if(container.getChildCount() != 1){
container.removeViewAt(container.getChildCount()-1);
}
}
}
draw_demo_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="0dip"
android:id="#+id/sc" android:scrollbars="horizontal"
android:layout_weight="1.0">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/chips"/>
</FrameLayout>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Draw"
android:onClick="drawHandler"/>
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Clear"
android:onClick="clearHandler"/>
</LinearLayout>
DrawView.java
public class DrawView extends View {
public DrawView(Context context) {
super(context);
setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT
,LinearLayout.LayoutParams.FILL_PARENT));
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = new Paint();
p.setColor(Color.RED);
p.setStrokeWidth(5);
canvas.drawLine(50, 50, 100, 150, p);
canvas.drawLine(100, 150, 20, 50, p);
}
}
This activity has a framelayout which has a imageview as the base which holds the bitmap.
We add a custom draw view on top of it and do our drawings on it.When we want to clear the drawing we remove the drawview and add another one if we need to draw again.
This might not be the most efficient way. But should get you started.

Android: FrameLayout subclass does not draw

This is, what works:
a) FrameLayout with two ImageViews in main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="#+id/frameLayout1"
android:layout_height="wrap_content">
<ImageView android:src="#drawable/radar_background"
android:id="#+id/background" android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageView>
<ImageView android:src="#drawable/radar_sector" android:id="#+id/sector"
android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</FrameLayout>
b) RotateAnimation on the background, whereas the foreground sector remains unchanged
Because I need to do a bit more on the background image I put this into a FrameLayout subclass and changed main.xml accordingly:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="#+id/frameLayout1"
android:layout_height="wrap_content">
<com.decades.SensorTest.RadarView
android:id="#+id/background" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView android:src="#drawable/radar_sector" android:id="#+id/sector"
android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</FrameLayout>
This is the new RadarView.java:
public class RadarView extends FrameLayout {
private Bitmap mRadar;
public RadarView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public RadarView(Context context) {
super(context);
init();
}
private void init() {
mRadar = BitmapFactory.decodeResource(getResources(), R.drawable.radar_background);
}
#Override
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
canvas.drawBitmap(mRadar, 0, 0, null);
}
}
What happens:
a) The constructor is called during setContentView(R.layout.main);
b) The dispatchDraw override is called
c) The image does not appear on the screen...
Does anybody see, why?
I realize that this question is old, but it was never answered properly.
Implementing a View to put into the view group is a reasonable way to go, but sometimes you don't want to do that (say you have a view group class that arranges the contained views in some way).
In this case you need to draw from the layout (view group) view class.
I think that the given code should work, from reading through it, aside from the actual dispatchDraw function. You missed canvas.save() and canvas.restore().
Example that should be easy to get working quickly:
#Override
public void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
canvas.save();
canvas.drawCircle(cX, cY, bgRadius, bgPaint);
canvas.restore();
}
This has worked for me, at least, in my context. Hopefully it helps others who have the same problem. :)
Have you tried implementing onDraw() instead?
dispatchDraw() has to do with child Views.
I think you should extend the View class and override the onMeasure() and onDraw() methods.
More information here:
http://developer.android.com/guide/topics/ui/custom-components.html
Example:
http://www.barebonescoder.com/2010/06/android-development-simple-2d-graphics-part-1/
I'm guessing that you wanted to implement onDraw(), but that is probably not enough. To my understanding the layouts may not call onDraw if they have nothing to draw besides their children. You might need to "trick" the layout to draw itself (so you'll get the onDraw call). An easy yet not efficient way we did this (involved guesswork...) is to set the background color of the layout.
A better way is probably to read the layout code and do it properly :)

ondraw view and xml in android

How can I draw an dynamic view on top of mainscreen in android.
For example I display the main screen using setContentView(R.layout.main);
This draws many widgets and includes an image defined in main.xml this way :
<ImageView
android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/sinewave"/>
Now if I would like to draw rectangle on this image, how can I do it?
Thank you for your time.
To draw something on screen instead of using a pre-defined Widget:
You create a custom view class.
public class yourCustomView extends View {
}
You add that custom view to your xml layout.
<view class="your.package.path.yourCustomView"
android:id="#+id/myView"
android:layout_width="fill_parent"
android:layout_height="50dip"
/**you can add other xml attributes that are defined for View**/
/>
You override the onDraw() method to make it draw stuff you want on its canvas.
#Override
protected void onDraw(Canvas canvas) {
/**draw stuff here**/
}
Make sure you use this constructor if using the XML layout:
public yourCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
}

Categories

Resources