I have an abstract class extended from the class View, and 4 subclasses to perform a series of vectorial drawings. Then I have a layout with four Buttons and a View to show the drawings. Depending on which button is clicked, different figures should be drawn in that View. The problem is that when I click one button, the figures are drawn filling the entire screen, the buttons disappear. How could this problem be solved?
public abstract class DrawFiguresWithDimensions extends View{
public DrawingFiguresWithDimensions(Context context)
//...
}
public class DrawRectangleWithDimensions extends DrawFiguresWithDimensions {..}
public class DrawTWithDimensions extends DrawFiguresWithDimensions {..}
public class DrawDobleTWithDimensions extends DrawFiguresWithDimensions {..}
public class DrawBoxWithDimensions extends DrawFiguresWithDimensions {..}
Here is the "activity_main.xml":
<LinearLayout
android:id="#+id/mainLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="10"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:weightSum="4"
>
<Button
android:id="#+id/btnSecRec"
android:text="Rec"
android:onClick="btnSecRec"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/btnSecT"
android:text="T"
android:onClick="btnSecT"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/btnSecDobleT"
android:text="DT"
android:onClick="btnSecDobleT"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="#+id/btnSecCajon"
android:text="Box"
android:onClick="btnSecCajon"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<View class="com.tari.drawfigureswithdimensions.DrawFiguresWithDimensions"
android:id="#+id/viewDatosSec"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
"MainActivity.java"
public class MainActivity extends Activity {
DrawRectangleWithDimensions sec1;
DrawTWithDimensions sec2;
DrawDoubleTWithDimensions sec3;
DrawBoxWithDimensions sec4;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnSecRec(View view){
sec1 = new DrawRectangleWithDimensions(this);
sec1.setFigure(10, 40);
setContentView(sec1);
}
public void btnSecT(View view){
sec2 = new DrawTWithDimensions(this);
sec2.setFigure(100, 20, 20, 70);
setContentView(sec2);
}
public void btnSecDobleT(View view){
sec3 = new DrawDoubleTWithDimensions(this);
sec3.setFigure(100, 20, 20, 90, 50, 25);
setContentView(sec3);
}
public void btnSecCajon(View view){
sec4 = new DrawBoxWithDimensions(this);
sec4.setFigure(100, 20, 20, 80);
setContentView(sec4);
}
You can't expect to instantiate an abstract class by referencing it in your xml layout.
You'll need to specify a concrete implementation in order for it to work properly.
So, instead, you might provide a ViewGroup of some sort in your layout to serve as the parent for whatever concrete DrawFiguresWithDimensions implementation you want to show based on what button is pressed.
Then, dynamically remove the current child associated with your parent ViewGroup and add the one corresponding to your pressed button.
Essentially, you'll want to replace this in your activity_main.xml layout:
<View class="com.tari.drawfigureswithdimensions.DrawFiguresWithDimensions"
android:id="#+id/viewDatosSec"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
with this:
<RelativeLayout
android:id="#+id/viewDatosSec"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
And then when handling the button click, do something like:
#Override
public void onClick(View v)
{
int id = v.getId();
if (id == R.id.btnSecCajon)
{
RelativeLayout parent = (RelativeLayout)findViewById(R.id.viewDatosSec);
parent.removeAllViews();
parent.addView(new DrawBoxWithDimensions(this));
}
}
Your custom view will be instantiate as soon as the layout will be load so NO abstract class allowed !
Here an exemple to include a custom view in your xml.
http://developer.android.com/training/custom-views/create-view.html
Related
I am new in android and tried to learn how to make an add button which add Views in a certain view dynamically when clicked. But i meet the problem that not sure how to set id for each item in the layout which i want to insert into another view.
Here is the main_view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/container_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/add_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/container_layout"
android:text="add"
/>
</RelativeLayout>
Here is my code:
public class MainActivity extends AppCompatActivity {
public int index_num;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button add_button = findViewById(R.id.add_button);
final LayoutInflater layoutInflater = getLayoutInflater();
final ViewGroup insertPoint = findViewById(R.id.container_layout);
index_num = 0;
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
View new_view = layoutInflater.inflate(R.layout.new_layout,insertPoint, false);
insertPoint.addView(new_view);
index_num++;
}
});
}
}
And here is the layout which i want to insert the main view which includes 3 Edittext:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
</LinearLayout>
Can someone teach me how to set different id for the three edittext when i pressed the add button?
As #Rasoul Miri said, you can use View.generateViewId() to set id for newly inserted views, and you should use a list to record them. And also you should set the id for those three items.
like these:
for the inserted view
<EditText
android:id="#+id/one_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:id="#+id/two_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:id="#+id/three_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
for the MainActivity
public ArrayList<InsertedView> list;
public void addView() {
View new_view = layoutInflater.inflate(R.layout.new_layout,insertPoint, false);
insertPoint.addView(new_view);
index_num++;
InsertedView insertedView = new InsertedView(View.generateViewId(), View.generateViewId(), View.generateViewId());
new_view.findViewById(R.id.one_view).setId(insertedView.firstId);
new_view.findViewById(R.id.two_view).setId(insertedView.secondId);
new_view.findViewById(R.id.three_view).setId(insertedView.thirdId);
list.add(insertedView);
}
class InsertedView {
public int firstId;
public int secondId;
public int thirdId;
public InsertedView(int one, int two, int three) {
firstId = one;
secondId = two;
thirdId = three;
}
}
you can use these ways
use View
view.setId(View.generateViewId());
use ViewCompat
ViewCompat.generateViewId()
use ConstraintLayout
ConstraintLayout.generateViewId()
We always have heard using multiple fragments with one activity. Is opposite possible? I am curious about this. Can we use same fragment for multiple activities. Please give ONE EXAMPLE.
How to reuse one Fragment in multiple Activities
The green background with two buttons is a single fragment that is reused among multiple activities.
1. Make your fragment class and layout
MyFragment.java
import android.support.v4.app.Fragment;
public class MyFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myLayout = inflater.inflate(R.layout.my_fragment_layout, container, false);
// add click listeners to the buttons in the fragment
Button buttonOne = myLayout.findViewById(R.id.button_1);
Button buttonTwo = myLayout.findViewById(R.id.button_2);
buttonOne.setOnClickListener(this);
buttonTwo.setOnClickListener(this);
return myLayout;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_1:
Toast.makeText(getContext(), "Button One", Toast.LENGTH_SHORT).show();
break;
case R.id.button_2:
Toast.makeText(getContext(), "Button Two", Toast.LENGTH_SHORT).show();
break;
}
}
}
my_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_dark"
android:orientation="vertical">
<Button
android:id="#+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"/>
<Button
android:id="#+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"/>
</LinearLayout>
2. Add the fragment to your activities
activity_blue.xml
<?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:background="#android:color/holo_blue_dark"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToRedActivityButtonClick"
android:text="Go to red activity"/>
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
activity_red.xml
<?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:background="#ff3636"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goToYellowActivityButtonClick"
android:text="Go to yellow activity"/>
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
activity_yellow.xml
<?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:background="#f9f478"
android:orientation="vertical">
<!-- reused fragment -->
<fragment
android:id="#+id/my_fragment"
android:name="com.example.onefragmentmultipleactivities.MyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Notes
For simplicity we added the fragment directly to the xml. You can also load fragments dynamically in code. See the documentation for help with that.
Yes, it is possible to have one fragment with multiple activities.
But you will need to program the layout with java using LayoutParams and embed them in every fragment instance.
On every activity, you need to call this fragment
Create your UI Components in Java, add them to the layout dynamically from Java Class i.e. Your Activities.
I would suggest this approach will not be easy to maintain, if you are not super comfortable with Java exclusively. You will need to forget XML for this approach as nothing will be there in it at all, everything will be done with Java classes only.
generic_error_msg_fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/White" >
<TextView
android:id="#+id/error_message_textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:textColor="#android:color/darker_gray"
android:textSize="#dimen/font_size_16sp" />
<Button
android:id="#+id/error_button_handler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="left|center_vertical"
android:textSize="#dimen/font_size_16sp"
/>
</RelativeLayout>
GenericErrorFragment.Java
public class GenericErrorFragment extends Fragment{
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View genericView = inflater.inflate(R.layout.generic_error_msg_fragment, null);
TextView errorText = (TextView) genericView.findViewById(R.id.error_message_textview);
errorText.setText("error msg" );
Button errorbutton = (Button) genericView.findViewById(R.id.error_button_handler);
errorbutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// your logic launch some other activity
}
});
return genericView;
}
}
you can load this fragment in any activity and can define your custom text for error and button handler
I will not write whole code but I can give you exact example you are looking for
Think of an application in which a person can register either as admin or as user
Now while working on it you make 3 fragments in admin registration activity asking
1.) personal information
2.) academic information
3.) admin details
Now for user registration activity say you make 2 fragments to get the following information
1.) personal information
2.) user details
here you used personal information fragment 2 times for 2 activities
code for this is child's play main thing is the concept
How to let marquee text to show in every activity. Just write at one place and the marquee text should be reflect in every activity. How to do this..please help
here is one way to do that.
first create a BaseActivity that will be extended by every activity whose marquee is similar for every activity.
public abstract class BaseWaalaMarquee extends Activity {
FrameLayout flBaseActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_layout_marquee);
appendView();
}
private void appendView() {
flBaseActivity = (FrameLayout) findViewById(R.id.flBaseActivity);
View mainView = getLayoutInflater().inflate(getLayout(), null);
flBaseActivity.addView(mainView);
}
public abstract int getLayout();
}
create a base layout for this activity named base_layout_marquee as below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/fact"
android:layout_width="200dp"
android:layout_height="40dip"
android:duplicateParentState="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="Loading... More text to see if it spans or not and want more">
<requestFocus
android:duplicateParentState="true"
android:focusable="true"
android:focusableInTouchMode="true" />
</TextView>
<FrameLayout
android:id="#+id/flBaseActivity"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
now create your own layout for the activity like below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f00f0f"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your custom Layout" />
</LinearLayout>
finally create you activity that extends BaseWaalaMarquee as below
package ram.materialnavigation.opengltravis;
import ram.materialnavigation.R;
/**
* Created by view9 on 12/16/15.
*/
public class ExtendedBaseActivity extends BaseWaalaMarquee {
#Override
public int getLayout() {
return R.layout.your_custom_layout;
}
}
Now invoke ExtendedBaseActivity and see the result.
I guess this will be quite helpful for you. cheers
Simple..Take a global string variable and change it accordingly and use it in all the activities.
I am new to android and
I am trying to create a simple drawing application, in which I want to draw with finger and add shapes(circle, rectangle etc).
So far I am able to draw with finger and by using
this answer I am able to create a rectangle shape.
What I am trying to achieve, is while drawing with finger, when I click on Draw Rectangle button, I get the rectangle shape, and I will be able to draw this shape.
But I am having problem in adding this rectangle shape on a DrawingView.
MainActivity.java
public class MainActivity extends Activity
{
private DrawingView drawView;
private DrawRectangleView drawRectView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = (DrawingView) findViewById(R.id.drawing);
}
public void rectangleClicked(View view)
{
Log.i("---Log---","Button clicked");
// how to call DrawRectangleView and add it to DrawingView ???
}
}
main_activity.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<com.example.test.DrawingView
android:id="#+id/drawing"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:layout_weight="1"
android:background="#FFFFFFFF" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="rectangleClicked"
android:text="Draw Rectangle" />
</LinearLayout>
</LinearLayout>
I am also new to android. This is what I have done, not sure if this is a good approach or not, but here it is...
In your button's onClick method do this:
public void rectangleClicked(View view)
{
Log.i("---Log---","Button clicked");
drawRectView.setValue("rectangle");
}
And in your DrawRectangleView class simply define a string variable and define a setter method:
public void setValue(String val) {
testVar = val;
}
After that simply if-else value of testVar. Hope this helps
P.S
I think you should use on custom view rather than using two different views.
I have the following layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:text="Height"
android:layout_height="wrap_content"
android:id="#+id/buttonHeight"
android:layout_width="wrap_content"
android:layout_weight="15"
android:onClick="OnClickHeight">
</Button>
<Button
android:text="Width"
android:layout_height="wrap_content"
android:id="#+id/buttonWidth"
android:layout_width="wrap_content"
android:layout_toRightOf="#id/buttonHeight"
android:layout_weight="1"
android:onClick="onClickWidth">
</Button>
</LinearLayout>
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="1px"
android:drawSelectorOnTop="false"/>
</RelativeLayout>
I then have a class that extends ListActivity in which I have my onClickWidth and onClickHeight methods, for example:
public void onClickWidth(View v)
{
// does stuff
}
However, these onClick events are not being handled. If I change the class to extend Activity, instead of ListActivity, and also remove the ListView from my layout, then it is detected!
I have tried to set android:clickable="true", played around with the android:focusable attribute, and various other things, but I just cannot get this to work. How can I resolve this, or is this simply not allowed?
Can you tell us what are you try to do? And why do you need ListActivity?
Sorry for edditing:P
You could take a look here: How to handle ListView click in Android
put OnClicklistener within adapter itself .
Rather then using the onClick stuff in the layout, have you tried something like this:
Button buttonWidth = (Button)findViewById(R.id.buttonWidth);
buttonWidth.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do stuff
}
});
Then do the same for buttonHeight.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_land);
Button btn = (Button) findViewbyid(R.id.buttonWidth);
btn.setOnClickListener(this);
}
public void onClickWidth(View v)
{
if(v == btn){
//your code...
}
}
set the android:descendantFocusability attribute of the parent layout which contains the ListView to value blocksDescendants like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants"> ...</LinearLayout>