I have a custom view extending RelativeLayout (could be any other viewgroup). In this view I have several framelayouts added with addView(). No XML exists for this view! The view constructer adds everything from code.
I want to use this custom view as any other content view for an activity.
I have this fragment activity:
MyView myview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myview = new MyView(this);
myview.getSomeContainedFrameLayoutOfView().setId(12345);
setContentView(myview);
}
Then I do this in onResume
#Override
protected void onResume() {
super.onResume();
View u = findViewById(12345); // test, returns null
u = myview.findViewById(12345); // test, also null
SomeFragment f = new SomeFragment();
FragmentManager m = getSupportFragmentManager();
FragmentTransaction ft = m.beginTransaction();
ft.replace(12345, f); // ofcourse error (no view found for id)
ft.commit();
}
Why is findViewById null? If I change setContentView() line to
setContentView(R.layout.myframelayouts);
Everything works. So somehow I suspect
setId(12345)
does something different from:
android:id="#+id/someid"
What am I missing?
Is the view returned by getSomeContainedFrameLayoutOfView() actually contained in the RelativeLayout? That is, somewhere in MyView have you called AddView(theTargetView)?
If the target view is simply a member of MyView, then the RelativeLayout, which implements findViewById unless you override it, doesn't know about the target view.
I have something like this:
public class MojLayout extends LinearLayout {
private FrameLayout lijeviFrame;
private FrameLayout glavniFrame;
private int layoutWidth;
private int panel;
private boolean glavniFrameFullScreen = false;
public MojLayout(final Context context, final float leftWeight) {
super(context);
lijeviFrame = new FrameLayout(context);
glavniFrame = new FrameLayout(context);
post(new Runnable() {
#Override
public void run() {
layoutWidth = getWidth();
panel = (int) (layoutWidth / leftWeight);
init(context);
}
});
}
private void init(Context context) {
lijeviFrame.setId(R.id.lijeviFrame); //defined in /res/values/ids.xml
glavniFrame.setId(R.id.glavniFrame);
LinearLayout container = new LinearLayout(context);
container.setOrientation(HORIZONTAL);
....
container.addView(lijeviFrame);
container.addView(glavniFrame);
addView(container);
}
In onCreate() method I tried myView.findViewbyId(R.id.lijeviFrame) -> null, without myView, same.
I don't get it
Related
I am using this circular menu for my App.
https://github.com/oguzbilgener/CircularFloatingActionMenu
It is working. But when i open my Navigation Drawer. This Menu is in the front. How can i make the Menu to go Back to the Navigation Drawer.
How Can i Bring my Navigation Drawer to Front ?
public class Home extends MainActivity implements OnClickListener
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
this.application = (Remtt) this.getApplication();
this.preferences = this.application.getPreferences();
super.onCreate(savedInstanceState);
this.checkOnCreate();
int redActionButtonSize = getResources().getDimensionPixelSize(R.dimen.red_action_button_size);
int redActionButtonMargin = getResources().getDimensionPixelOffset(R.dimen.action_button_margin);
int redActionButtonContentSize = getResources().getDimensionPixelSize(R.dimen.red_action_button_content_size);
int redActionButtonContentMargin = getResources().getDimensionPixelSize(R.dimen.red_action_button_content_margin);
int redActionMenuRadius = getResources().getDimensionPixelSize(R.dimen.red_action_menu_radius);
int blueSubActionButtonSize = getResources().getDimensionPixelSize(R.dimen.blue_sub_action_button_size);
int blueSubActionButtonContentMargin = getResources().getDimensionPixelSize(R.dimen.blue_sub_action_button_content_margin);
ImageView fabIconStar = new ImageView(this);
fabIconStar.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_headphones));
FloatingActionButton.LayoutParams starParams = new FloatingActionButton.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
starParams.setMargins(redActionButtonMargin, redActionButtonMargin, redActionButtonMargin, redActionButtonMargin);
fabIconStar.setLayoutParams(starParams);
FloatingActionButton.LayoutParams fabIconStarParams = new FloatingActionButton.LayoutParams(redActionButtonContentSize, redActionButtonContentSize);
fabIconStarParams.setMargins(redActionButtonContentMargin, redActionButtonContentMargin, redActionButtonContentMargin, redActionButtonContentMargin);
FloatingActionButton leftCenterButton = new FloatingActionButton.Builder(this).setContentView(fabIconStar, fabIconStarParams).setBackgroundDrawable(R.drawable.button_action_red_selector).setPosition(FloatingActionButton.POSITION_TOP_CENTER).setLayoutParams(starParams).build();
// Set up customized SubActionButtons for the right center menu
SubActionButton.Builder lCSubBuilder = new SubActionButton.Builder(this);
lCSubBuilder.setBackgroundDrawable(getResources().getDrawable(R.drawable.button_action_blue_selector));
FrameLayout.LayoutParams blueContentParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
blueContentParams.setMargins(blueSubActionButtonContentMargin, blueSubActionButtonContentMargin, blueSubActionButtonContentMargin, blueSubActionButtonContentMargin);
lCSubBuilder.setLayoutParams(blueContentParams);
// Set custom layout params
FrameLayout.LayoutParams blueParams = new FrameLayout.LayoutParams(blueSubActionButtonSize, blueSubActionButtonSize);
lCSubBuilder.setLayoutParams(blueParams);
ImageView lcIcon1 = new ImageView(this);
ImageView lcIcon2 = new ImageView(this);
ImageView lcIcon3 = new ImageView(this);
ImageView lcIcon4 = new ImageView(this);
ImageView lcIcon5 = new ImageView(this);
ImageView lcIcon6 = new ImageView(this);
ImageView lcIcon7 = new ImageView(this);
ImageView lcIcon8 = new ImageView(this);
ImageView lcIcon9 = new ImageView(this);
lcIcon1.setImageDrawable(getResources().getDrawable(R.drawable.anim));
lcIcon2.setImageDrawable(getResources().getDrawable(R.drawable.anim));
lcIcon3.setImageDrawable(getResources().getDrawable(R.drawable.anim));
lcIcon4.setImageDrawable(getResources().getDrawable(R.drawable.anim));
lcIcon5.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_headphones));
lcIcon6.setImageDrawable(getResources().getDrawable(R.drawable.anim));
lcIcon7.setImageDrawable(getResources().getDrawable(R.drawable.anim));
lcIcon8.setImageDrawable(getResources().getDrawable(R.drawable.anim));
lcIcon9.setImageDrawable(getResources().getDrawable(R.drawable.anim));
// Build another menu with custom options
FloatingActionMenu leftCenterMenu = new FloatingActionMenu.Builder(this).addSubActionView(lCSubBuilder.setContentView(lcIcon1, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon2, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon3, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon4, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon5, blueContentParams).build())
.addSubActionView(lCSubBuilder.setContentView(lcIcon6, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon7, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon8, blueContentParams).build()).addSubActionView(lCSubBuilder.setContentView(lcIcon9, blueContentParams).build()).setRadius(redActionMenuRadius).setStartAngle(0).setEndAngle(360).attachTo(leftCenterButton).build();
}
}
}
This is very similar to my answer here, with only minor changes to the FloatingActionButton instantiation.
The library code assumes that the Button and Menu are to appear on top of an Activity's content View, and everything therein. The addition of a container ViewGroup member in the FloatingActionButton class allows us to specify a child ViewGroup to be the parent instead. Please note that the following changes will only work if a FloatingActionButton is used with the Menu.
The additions to the FloatingActionButton class:
public class FloatingActionButton extends FrameLayout {
...
private ViewGroup containerView;
...
public FloatingActionButton(Activity activity,
LayoutParams layoutParams,
int theme,
Drawable backgroundDrawable,
int position,
View contentView,
FrameLayout.LayoutParams contentParams
// Note the addition of the following
// constructor parameter here
, ViewGroup containerView) {
...
setClickable(true);
// This line is new. The rest of the constructor is the same.
this.containerView = containerView;
attach(layoutParams);
}
...
public View getActivityContentView() {
if(containerView == null) {
return ((Activity)getContext())
.getWindow().getDecorView().findViewById(android.R.id.content);
} else {
return containerView;
}
}
public ViewGroup getContainerView() {
return containerView;
}
// The following setter is not strictly necessary, but may be of use
// if you want to toggle the Button's and Menu's z-order placement
public void setContainerView(ViewGroup containerView) {
this.containerView = containerView;
}
...
public static class Builder {
...
private ViewGroup containerView;
...
public Builder setContainerView(ViewGroup containerView) {
this.containerView = containerView;
return this;
}
public FloatingActionButton build() {
return new FloatingActionButton(activity,
layoutParams,
theme,
backgroundDrawable,
position,
contentView,
contentParams,
// New argument
containerView);
}
}
...
}
And the changes to the FloatingActionMenu class:
public class FloatingActionMenu {
...
public View getActivityContentView() {
if(mainActionView instanceof FloatingActionButton &&
((FloatingActionButton) mainActionView).getContainerView() != null) {
return ((FloatingActionButton) mainActionView).getContainerView();
} else {
return ((Activity)mainActionView.getContext())
.getWindow().getDecorView().findViewById(android.R.id.content);
}
}
...
}
Then, assuming your Activity's layout is similar to:
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
...>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
...
</android.support.v4.widget.DrawerLayout>
In your Home Activity, you would build and instantiate leftCenterButton like so:
public class Home extends Activity implements OnClickListener {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
...
FrameLayout container = (FrameLayout) findViewById(R.id.container);
FloatingActionButton leftCenterButton = new FloatingActionButton.Builder(this)
.setContentView(fabIconStar, null)
.setBackgroundDrawable(R.drawable.ic_launcher)
.setPosition(FloatingActionButton.POSITION_TOP_CENTER)
.setLayoutParams(starParams)
// The new method call is added here
.setContainerView(container)
.build();
...
}
...
}
I am new to android development. I have create a view that I want to display in a fragment within an activity.
Here is an excerpt of my view:
public class CalendarView extends View implements View.OnTouchListener {
#Override
protected void onDraw(Canvas canvas) {
displayTitleDate(canvas);
displayNavigation(canvas);
displayDays(canvas);
displayNumbers(canvas);
highlightRect(canvas);
invalidate();
}
}
Here is an excerpt of the fragment:
public class MyCalendarFragment extends Fragment {
private CalendarView calendarView;
public static MyCalendarFragment newInstance()
{
return new MyCalendarFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
calendarView = new CalendarView(getActivity());
return calendarView;
}
}
And finally the excerpt of my activity code:
public class EditPersonActivity extends Activity {
private Fragment calendarFrag;
private Fragment currentFrag;
private FragmentTransaction fragTrans;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_person);
calendarFrag = MyCalendarFragment.newInstance();
fragTrans = getFragmentManager().beginTransaction();
fragTrans.add(R.id.edit_layout, calendarFrag);
fragTrans.commit();
}
}
My view does not show up at all. I guess this is a lifecycle issue. I have also tried to override the onMeasure method in the view but this does not change anything. Besides, it works when the view is embedded in an activity instead of a fragment. Any help is very welcome. Thx in advance.
Finally, I have solved my issue by using a Custom View directly into the xml file describing the fragment.
<com.mycompany.view.CalendarView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
In the java code implementing the view, I had to override one of the constructor this way:
public CalendarView(Context context, AttributeSet attrs) {
super(context, attrs);
//Other code here
}
And finally, I had to override the onMeasure(...) method:
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width, height;
Point size = new Point();
WindowManager w = ((Activity)getContext()).getWindowManager();
w.getDefaultDisplay().getSize(size);
width = size.x;
height = (int) (size.x * 1.5);
setMeasuredDimension(width, height);
}
Et voila, it works. Thx for your help.
In Kotlin you can replace doOnPreDraw -> (on) -> doOnLayout.
This is what i am trying to do, this my main class file
private View TicTacStart;
//onCreate
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TicTacStart = new Game(this);
//reference to base layout..
LinearLayout baseView = (LinearLayout)findViewById(R.id.baseView);
//I cant INFLATE THE VIEW SINCE I AM NOT USING XML AND RATHER USING ANOTHER CLASS GAME WHICH EXTENDS VIEW
LayoutInflater vi = (LayoutInflater)thisActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View thisScreensView = vi.inflate(TicTacStart, null);
//add the view to the base view...
baseView.addView(thisScreensView);
}
This is game class
public class Game extends View {
private Cell[][] singlesquare = null;
int x = 3;
int y = 3;
private int l;
private int a;
private boolean whatdrawn = false;
private int playerwin = 3;
private Paint caneta;
Data Data MORE classes
}
so basically what I want to do is add Game view class into baseview in my main class file. Is there someway i can make this work as i don't want to use xml's now.
Yuu can directly add GameView into baseview for ex:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
TicTacStart = new Game(this);
//reference to base layout..
LinearLayout baseView = (LinearLayout)findViewById(R.id.baseView);
baseView.addView(TicTacStart );
}
I am new on android development, and i was currently creating an application using tabhost and a TabActivity to switch between different activities. This application was working fine, but I recently discover that TabActivity are deprecated and I should use fragment instead.
So, I follow this great tutorial explaining how to do so :
http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/
Most of it works fine, but there is my problem some of my old activities which are now fragments were inflating ImageButton using this kind of methods :
private void displayLevelButtons() {
LayoutInflater lf = getLayoutInflater();
btns = lf.inflate(R.layout.mallmapactivity_level_buttons, null, false);
mBtnLevelDown = (ImageButton) btns.findViewById(R.id.btn_level_down);
mBtnLevelUp = (ImageButton) btns.findViewById(R.id.btn_level_up);
mBtnLevelDown.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelDown();
updateLevelButtons();
}
});
mBtnLevelUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelUp();
updateLevelButtons();
}
});
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;
addContentView(btns,params);
}
In order to be able to compile this method inside a fragment, I changed:
LayoutInflater lf = getLayoutInflater();
to
LayoutInflater lf = getActivity().getLayoutInflater();
and
addContentView(btns,params);
to
getActivity().addContentView(btns,params);
But using my method displayLevelButtons() like this make my buttons appears in the right bottom of my general view, not in the right bottom of my fragment.
I also tried to specify a root when inflating my layout with image buttons, my all fragment class looking like this :
public class MyFragment extends Fragment {
private View btns;
private ImageButton mBtnLevelUp;
private ImageButton mBtnLevelDown;
private ViewGroup myViewGroup;
#Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (container == null) {
return null;
}
myViewGroup = container;
v = inflater.inflate(R.layout.mallmaptile, container, false);
return v;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
//Do some stuff and then :
displayLevelButtons();
}
private void displayLevelButtons() {
LayoutInflater lf = getActivity().getLayoutInflater();
btns = lf.inflate(R.layout.mallmapactivity_level_buttons, myViewGroup, false);
mBtnLevelDown = (ImageButton) btns.findViewById(R.id.btn_level_down);
mBtnLevelUp = (ImageButton) btns.findViewById(R.id.btn_level_up);
mBtnLevelDown.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelDown();
updateLevelButtons();
}
});
mBtnLevelUp.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
levelUp();
updateLevelButtons();
}
});
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL | Gravity.RIGHT;
getActivity().addContentView(btns,params);
}
Doing so my buttons are still display in the bottom of my general view not in the bottom of my fragment, and if I try to change the attachToRoot boolean value to true :
btns = lf.inflate(R.layout.mallmapactivity_level_buttons, myViewGroup, true);
I get the error : java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I am sure, I missed something important but I don't what. Any suggestions would be really appreciated.
Thanks in advance,
If you are facing problem in inflating layout then you can use following code:
View headerView = View.inflate(this, R.layout.layout_name, null);
this will give you complete parent layout then you can use that layout to fetch whatever view you want or you can get layout param and then set layout param to any views (In your case button I think).
I had same problem and find my answer in this link:
problem with inflater
you can do it without any dependency
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.view_with_merge_tag, this);
I am trying to add Views to a custom ViewGroup. The ViewGroup gets drawn, but no Views that were added to it are visible. LineView's (which extends a View) onDraw() method does not get called. What am I doing wrong?
public class TestActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ShapeView shapeView = new ShapeView(this);
shapeView.setBackgroundColor(Color.RED);
drawContainer = (RelativeLayout)findViewById(R.id.draw_container);
drawContainer.addView(shapeView);
}
}
public class ShapeView extends ViewGroup {
private LineView mLineView;
public ShapeView (Context context) {
super(context);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(200, 200);
this.setLayoutParams(p);
mLineView = new LineView(context);
this.addView(mLineView);
}
}
I maybe wrong but should you not be adding the shapeView to your layout "main"? What is drawContainer? I can't see it being instantiated and not in the docs. I assume main contains a relative/linear layout.
Access it by:
RelativeLayout rel = (RelativeLayout) findViewById(R.id.container);
(where the id is changed to match your id)
then:
rel.addView(shapeView);