I created a DrawCircle View Class to draw a Circle. I like to add this DrawCircle View Class to a fragment.
The DrawCircle View class is as follow.
public class DrawCircle extends View {
private Paint paint;
public DrawCircle(Context context) {
super(context);
// create the Paint and set its color
paint = new Paint();
paint.setColor(Color.WHITE);
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
canvas.drawCircle(200, 200, 100, paint);
}
}
Inside my fragment, I have a Layout already inflated.
public class GripForce extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_grip_force, container, false);
final Button buttonAcce = (Button) v.findViewById(R.id.gripbutton);
buttonAcce.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (vibrate == false) {
vibrate = true;
buttonAcce.setText("VIBRATE STOP");
// Vibrate for 300 milliseconds
timer = new Timer();
myTimerTask = new MyTimerTask();
timer.schedule(myTimerTask, 1, 80000);
mVibrator.vibrate(100000);
} else {
timer.cancel();
timer.purge();
mVibrator.cancel();
vibrate = false;
buttonAcce.setText("VIBRATE");
}
}
});
mVibrator = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
return v;
}
}
My question is how to get this DrawCirle View on top of the existing View.
Thanks in advance.
EDIT:
I set the View in the xml.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.forcetest.GripForce">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal|top">
<com.abbott.forcetest.DrawCircle
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/circleView"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_gravity="center_horizontal|bottom">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="VIBRATE"
android:id="#+id/gripbutton"
android:background="#e71919"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="20dp" />
</LinearLayout>
</FrameLayout>
Then in the onCreateView, the program crashed at the line
v = inflater.inflate(R.layout.fragment_grip_force, container, false);
What is worng?
EDIT2:
I changed my xml to
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abbott.forcetest.GripForce"
android:id="#+id/gripLayout">
<!--<LinearLayout-->
<!--android:orientation="vertical"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:layout_gravity="center_horizontal|top">-->
<!--<com.abbott.forcetest.DrawCircle-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:id="#+id/circleView"-->
<!--android:layout_weight="1"/>-->
<!--</LinearLayout>-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_gravity="center_horizontal|bottom">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="VIBRATE"
android:id="#+id/gripbutton"
android:background="#e71919"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="20dp" />
</LinearLayout>
</FrameLayout>
Then in the program,
final FrameLayout l_out = (FrameLayout) v.findViewById(R.id.gripLayout);
DrawCircle circleVIew = new DrawCircle(this.getContext());
l_out.addView(circleVIew);
It works now.
Just add it like you would add any other view dynamically.
Just create it -
DrawCircle circle = new DrawCircle(context);
Then add it to your your parent layout -
yourParentLayout.addView(circle);
Set any properties you want while adding the circle view to place it accordingly.
See here how to add views dynamically.
You are trying to self-define view. And now assuming that you already make everything related to the view drawing fine( I haven't check your code related to view drawing), and now what you need to do to place that view is just simply code it in your layout file.
Like this:
<com.example.yourprojectname.DrawCircle
android:layout_width=xxxx
android:layout_height=xxx
/>
Like what you do to TextView or Button, code it in layout xml file and remember to findViewById in you java code. It will appear.
And of course you could do it programmingly, try addView method in your code. But write it in xml file is a little bit easier, also you can define the position you want that view to show up.
Related
I have problems with setting margin to a custom made linear layout class that I use multiple times in a GridLayout. The Gridlayout is placed in a fragment.
This is the code of fragment_grid.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="app_a_tize.expressme.Fragment.GridFragment"
android:layout_gravity="center">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/orange"
android:layout_margin="5dp"
android:id="#+id/gridlayout_grid"></GridLayout>
</FrameLayout>
This is the code of the GridFragment.java:
public class GridFragment extends Fragment {
public GridFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_grid, container, false);
}
#Override
public void onStart() {
super.onStart();
GridLayout grid = (GridLayout) getView().findViewById(R.id.gridlayout_grid);
grid.setRowCount(3);
int tileHeight = (CategoryTileActivity.gridContentHeight -3 * 10) / 3;
int amountofColumns = (int) CategoryTileActivity.gridContentWidth / tileHeight;
grid.setColumnCount(amountofColumns);
grid.setMinimumWidth((amountofColumns * tileHeight) + (5 * 20 ));
for (int i = 0; i < 3 * amountofColumns; i++) {
//fill the grid with the custom LinearLayout:
grid.addView(new TileClass(getActivity(), tileHeight, tileHeight, "ToBeImplemented", "Button"));
}
}
}
This is the code of the custom LinearLayout:
public class TileClass extends LinearLayout {
public TileClass(Context context, int height, int width, String image, String text) {
super(context);
this.setBackgroundResource(R.drawable.tile_button); //creates rounded layouts
this.setMinimumHeight(height);
this.setMinimumWidth(width);
this.setOrientation(LinearLayout.VERTICAL);
ImageView tileImage = new ImageView(context);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.tilephoto);
Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, 100, 100, true);
tileImage.setImageBitmap(bMapScaled);
tileImage.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tileText = new TextView(context);
tileText.setText(text);
tileText.setTextColor(Color.WHITE);
tileText.setGravity(Gravity.CENTER);
addView(tileImage);
addView(tileText);
}
}
When I run the Activity, I get this as result:
The code I showed above is responsible for the orange area in the middle. What I need: the blue "buttons"/LinearLayouts, in the orange area in the middle, to have a margin of 5dp. So the rest of the orange space is be taken by the custom LinearLayouts.
I don't know how to fix that, I tried a lot of options but they don't seem to work out for me.. Everything from MarginLayoutParams to params.setMargins(5,5,5,5); On almost every layout in my code.
I use Android Studio 2.1.2, supporting minimum of API 15.
Every help is appreciated!
For your imagination, this must be the end result, I need the margin like this:
You have to make custom view of gridview item as below:-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/categoryHeight"
android:layout_marginLeft="#dimen/margin_5dp"
android:layout_marginRight="#dimen/margin_5dp"
android:layout_marginTop="#dimen/margin_7dp"
android:background="#drawable/rounded_bg"
>
<ImageView
android:id="#+id/llRowItem"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scaleType="fitXY"
android:gravity="bottom"/>
<TextView
android:id="#+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black_light"
android:padding="#dimen/margin_5dp"
android:layout_gravity="bottom"
android:singleLine="true"
android:textColor="#color/white"
android:textSize="#dimen/font_size_16sp" />
</FrameLayout>
and inside adapter set color of text view, background, text or image of imageview whatever you want to set.
Here's a challenge for all you android master programmers out there! I am currently putting 2 surface views into a fragment (LeftPanel and MySurfaceView), below a 'banner' edit text area and with 1 button below the two surface views in a 'footer ' position.
The layout is described in the fragment3.xml file see below :-
<?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:orientation="horizontal"
android:background="#eeeeee"
>
<EditText
android:id="#+id/textF3"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:text=" Fragment 3 "
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#null"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textF3"
android:layout_alignParentStart="true"
android:weightSum="1"
>
<com.fragmentnavigationtestbed.LeftPanel
android:id="#+id/leftPanel"
android:layout_width="wrap_content"
android:layout_height="633dp"
android:layout_weight="0.5"
/>
<com.fragmentnavigationtestbed.MySurfaceView
android:id="#+id/view"
android:layout_width="wrap_content"
android:layout_height="633dp"
android:layout_weight="0.5"
/>
</LinearLayout>
<Button
android:id="#+id/button32"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:text="GO BACK TO FRAGMENT 1"
android:onClick="goto_fragment1"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
/>
</RelativeLayout>
This fragment is inflated by the onCreateView() method in the Fragment3.java file where the 2 surfaceViews (SVs) are instantiated and have their inTouchListeners set inside the onActivityCreated Method.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment3, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
panel = new LeftPanel(this.getActivity());
panel.setOnTouchListener(this);
myView = new MySurfaceView(this.getActivity());
myView.setOnTouchListener(this);
}
I have a simple draw method (doDraw) which draws a square on a coloured background, I cannot get this to create any visible change to the SV LeftPanel, unless its within the onTouchEvent() method of the LeftPanel.java file and only when the user clicks the surface of the Leftpanel SV. (This problem applies to the other SV MySurfaceView as well !).
public void doDraw() {
Paint paint = new Paint();
paint.setColor(Color.GREEN);
holder = getHolder();
Canvas canvas = holder.lockCanvas();
if (canvas != null) {
canvas.drawColor(Color.CYAN);
canvas.drawRect(100, 100, 200, 200, paint);
holder.unlockCanvasAndPost(canvas);
}
}
If you don't click the SV you see just black.
I've tried putting the draw method in the Fragment3 onActivityCreated() method, and in LeftPanel's run() method, but though the method is evoked nothing happens on the SV. It only works in the onTouchEvent() method
As long as I click on my Leftpanel SV, I get my onDraw() method result!
My problem is I want to visibly display, the results of my OnDraw() method before any user action has occurred, can anyone give me some help here?
I've searched a lot of the S/O answers, but none go anywhere near this area.
I have a problem when declaring a button.
I will try to explain as specific as possible.
In my main Layout I have a Fragment containing a secondary Layout. In which I have several buttons.
My intention is that my main fichero.java to declare the buttons within the fragment.
Here we put the main Layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="josejuansosarodriguez.radioecca.conocecanarias.TrueoFalseFragment"
android:id="#+id/fragmentTrueFalse"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Grp1"
android:id="#+id/textGrp1"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"/>
<fragment
android:layout_width="fill_parent"
android:layout_height="450dp"
android:name="josejuansosarodriguez.radioecca.conocecanarias.Grp1FragmentP1"
android:id="#+id/fragmetaskGRP1"
android:layout_below="#+id/textGrp1"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp" />
</RelativeLayout>
Here we put the secondary Layout that has the buttons:
<?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/fragment_TrueorFalse">
<Button
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/buttontrue"
android:id="#+id/buttontrue"
android:layout_marginLeft="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" />
<Button
android:layout_width="120sp"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="#string/buttonfalse"
android:id="#+id/buttonfalse"
android:layout_marginRight="40dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" />
</RelativeLayout>
And here I leave the my main activity:
public class Grp1Fragment extends Fragment {
private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;
private View view;
public Grp1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
buttonTrue = (Button) view.findViewById(R.id.buttontrue);
buttonTrue.setOnClickListener(this);
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grp1, container, false);
return view;
}
My problem I find in the following line:
buttonTrue.setOnClickListener (this);
The message reads: View can not be in Applied to josejuansosarodriguez.radioecca.conocecanarias.Grp1Fragment
I hope I have spread far.
Thank you very much for everything, this forum is amazing.
Try this:
Note that you have to FIRST inflate the layout to make accesible the widgets of the layout.
Then you can "bind" the widgets, and finallly in this case, set the listeners to the buttons.
I set you a Log, if you need change it to Toast, or code
public class Grp1Fragment extends Fragment {
private Button buttonTrue;
private Button buttonFalse;
private Button buttonNextAsk;
private View view;
public Grp1Fragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_grp1, container, false);
//Declare your widgets (Buttons)
buttonTrue = (Button) view.findViewById(R.id.buttontrue);
buttonFalse = (Button) view.findViewById(R.id.buttonfalse);
//Set the Listeners
buttonTrue.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast or Log, or whateber you need
Log.d("Fragment1" , "Button: True");
}
});
buttonFalse.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast or Log, or whateber you need
Log.d("Fragment1" , "Button: FAlse");
}
});
//return the view
return view;
}
void setOnClickListener(View.OnClickListener l)
requires a OnClickListener, which Grp1Fragment is not.
Use something like
buttonTrue.setOnClickListener(new View.OnClickListener() {
void onClick(View v) {
... // reaction on the button
}
});
I try to use setContentView (R.layout.main) to display both an imageview and a Custom View. Somehow only the imageview can be shown but the Custom view is not visible (i.e. Bitmap ButtonStart not visible). I tested my code for onDraw and Texture class (responsible to load a bitmap object) somewhere else and it works fine. So I dunno what goes wrong...
Code for Custom view
public class TitleView extends View {
private Bitmap ButtonStart;
public TitleView (Context context, AttributeSet attrs) {
super (context, attrs);
Texture t2 = new Texture(context);
t2.loadFromAsset("button_Start.png");
ButtonStart = t2.getBitmap();
}
#Override
protected void onDraw (Canvas canvas) {
canvas.drawBitmap (ButtonStart, 0, 0, null);
}
}
MainActivity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature (Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main_screen_image);
}
}
main_screen_image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainScreenLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ImageView
android:id="#+id/mainScreenImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="#drawable/screenimage"
>
</ImageView>
<com.lowbband.chimera.TitleView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/titleview"/>
</LinearLayout>
With the code above, only screenimage was shown...
<ImageView
android:id="#+id/mainScreenImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent" //it should be wrap_content otherwise it will take whole space
android:scaleType="centerCrop"
android:src="#drawable/screenimage"
>
</ImageView>
<com.lowbband.chimera.TitleView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/titleview"/>
And if the image is large and still fills the screen then put ScrollView outside LinearLayout
I have a class called Panel like this:
public class Test extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Panel(this));
...
}
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private LinearLayout layout;
public Panel(Context context) {
super(context);
layout = //get layout resource in layouts folder
}
public void onDraw(Canvas canvas) {
//I want to draw my xml layout
layout.draw(canvas);
}
}
I have tried to use LayoutInflater in order to get my layout:
LayoutInflater inflater = LayoutInflater.from(context);
layout = new LinearLayout(getApplicationContext());
inflater.inflate(R.layout.mylayout, layout);
... but I cant see anything on the screen, only a black background :S
as far as I know you can't draw a linear layout inside the SurfaceView you can draw anything you want in your SurfaceView if you want to add elements to your view like buttons or something like that I recommend using the XML and putting all your elements there. Something like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<SurfaceView android:id="#+id/mySurfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</SurfaceView>
<Button android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="myButton"/>
</LinearLayout>
and then you may set your content of your view with setContentView(R.layout.my_xml);