I am going nuts over this.
I did not find any working solution (tried a few from stackoverflow)
Scenario (this is an actual screenshot what is already done):
I have a Activity that has a View as his Attribute.
This view adds another view via View.addView(myView).
I now want to add a Button to myView (to be specific: after MotionEvent.ACTION_UP the button should appear in the right lower corner (this will start the robot to drive the track))
Here is a shortcut of my code:
public class ModeRouting extends View {
public ModeRouting(Context context) {
super(context);
Button asuroStartButton = new Button(context) //does not work
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int actionevent = event.getAction();
if (actionevent == MotionEvent.ACTION_UP
|| actionevent == MotionEvent.ACTION_CANCEL) {
asuroStartButton.visible=true;
view.add(asuroStartButton);
}
return true;
}
}
and my Activity:
//in constructor
contentView = (FrameLayout) findViewById(R.id.content);
onClickListenerFacade(routingMode, route);
//this removes all views from stack and places the new one on the view
private void onClickListenerFacade(View v, final View target) {
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
contentView.removeAllViews();
contentView.setBackgroundColor(0xff000000);
contentView.addView(target);
modeSelectorAnimation();
}
});
}
I tried to create a button in my mainactivity.xml and instantiate in my mainactivity.
I am missing some point in here but i am not sure which.
Since my view is purely dynamic (no layout.xml) i dont think i should use a layout.xml (maybe thats my mind-blockage) but instead set the button attributes dynamically too.
Any hint is appreciated!
You want to extend ViewGroup rather than just a View (LinearLayout, RelativeLayout, FrameLayout, etc) - they handle child views for you.
I think maybe you need to refresh the whole view/activity. Try to do this in the onResume methode, maybe this helps. But as you don't use a layout.xml, I'm not sure if this helps you much..
#Override
protected void onResume(){
super.onResume();
setContentView(R.layout.activity_main);
}
Related
I have a structure where a search view resides at the top and a frame layout does below. SearchAFragment is the initial fragment of frame layout.
I am managing frame layout like;
search_box.setOnSearchClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
changeFragment(new SearchBFragment(), id);
}
});
search_box.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
changeFragment(new SearchAFragment(), id);
//search_box.clearFocus();
return false;
}
});
I am clicking search view and then searching things and everything is all right. After searching, I click close button of search view to switch to SearchAFragment and then the problem occurs. It switches SearchAFragment but when I try to open search view and search things again, SearchBFragment never comes to front. How can I handle this problem?
I have a survey app which is implemented using a viewpager and an arraylist of fragments. The fragments have different view types, some have radio buttons, some input boxes. I want the save the entered data to a shared variable in the parent activity when the user navigates from one fragment to another. When the user reaches the last fragment i want to display the summary of the data. i was thinking of saving the data when the user navigates from one fragment to the next. Also not sure if it is the best way to go about it.
List<Question> questions = new SurveyServiceImpl(getApplicationContext()).getSurveyQuestions(1);
ArrayList<Fragment> questionFragments = new ArrayList<>();
questionFragments.add(HomeFragment.newInstance("", ""));
for (int i = 0; i < questions.size(); i++) {
switch (questions.get(i).getQuestionType()) {
case SELECT:
if (questions.get(i).getMaximumOptionsRequired() == 1)
questionFragments.add(QuestionTypeSelect.newInstance("", questions.get(i)));
else
questionFragments.add(QuestionTypeCheckBox.newInstance("", questions.get(i)));
break;
case INPUT:
questionFragments.add(QuestionTypeInput.newInstance("", questions.get(i)));
break;
default:
}
}
questionFragments.add(EndFragment.newInstance("", ""));
final ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), questionFragments));
Use OnPageChangeListener for: all scrolls; page centered; page scroll changes (start drag, finish center animation)
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
#Override public void onPageSelected(int position) { }
#Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
//User started moving current page
}});
Off the top of my head:
Save the data as soon as it is changed in each fragment :
For EditTexts, use TextWatcher : (example) to save the text when it changes in afterTextChanged().
For RadioButtons, use radioButton.setOnClickListener(/**Save data in onClick*/), or radioGroup.setOnCheckedChangeListener if you're using a RadioGroup (example).
Might not be the most efficient solution, but works :) .
Hmm. Well unless you are doing some sort of save checkmarks along the wizard flow that you want to represent that all data for that page was saved, I would not go that route personally. You can easily just recognize pageChanged listener
vpMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//Generated method stub
}
#Override
public void onPageSelected(int position) {
setCurrentPage(position);
PAGE_TO_RETURN_TO = position;
vpMain.setCurrentItem(PAGE_TO_RETURN_TO);
validate data and allow to scroll or save
}
#Override
public void onPageScrollStateChanged(int state) {
//Generated Method Stub
}
});
but I would prefer to see onPause() of the parent activity handle saving the data personally. But it really depends on your need for the app.
This Should be easy and simple right?!
So, i have a Gameobject as a Button i reference it on a GamePanel that extents SurfaceView like this :
bby = new Buttons(BitmapFactory.decodeResource(getResources(), R.drawable.babybutton),123 ,114 ,1);
bby is drawn and work fine,
my problem is How can i change the Resource (R.drawble.babybutton) to a different resource for e.g (R.drawble.babybutton2) on Touch?
just like pressing buttons animations!
Thanks in advance
(If my question look stupid please don't dislike! i'm very new to this).
if you want to change the button's drawable on touch on the surfaceview. than you can override this method in your surfaceview class.
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
//here you can set an another drawable to your button or can do something else
break;
}
return true;
}
In your activity set a touchListener on the surfaceview.
after initialize the surfaceView. pass this surfaceview to the
surfaceView.setOnTouchListener(surfaceView)
I may have misunderstood you, but if you want to change the button's rescource, this is how you should do it:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
runOnUiThread(new Runnable() {
#Override
public void run() {
button.setBackgroundResource(R.drawable.icon);
}
});
}
});
public class troopcard extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.troopcard);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.pekkacard:
setContentView(R.layout.pekkacard);
break;
case R.id.dragcard:
setContentView(R.layout.dragcard);
}
}
}
The question here is why isnt the set content view working?as in the xml isnt opening of which i want. as making a intent then directing to a class then setting content view will take a hell load of time. I thought by directly setting the content view it might work. it isnt. What could be the solution. i really dont want to make a separate class for this as i have 40 + xmls. so is there a way to directly set content view without having to make a class?
This is not a true programming architecture. If you have more than one layout you should use more than one activity or fragment.
Switching on R.id won't work. You are never actually calling
setContentView(R.layout.pekkacard);
or
setContentView(R.layout.dragcard);
Try:
Override
public void onClick(View v) {
setContentView(R.layout.pekkacard);
}
to see if it displays the other layout.
I have two pages (Fragments) in my app that each have their own interaction. A long click on either page should toggle the color scheme to black and white and back again. This is accomplished using a separate java class that is called from either page. This works perfectly when run from the second page, but when run from the first page, the second page isn't changed. Here's some of my code, simplified for length:
Page 1:
firstView = inflater.inflate(R.layout.page1, container, false);
secondView = inflater.inflate(R.layout.page2, container);
//A bunch of code and then....
element1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
pm = sharedPreferences.getBoolean("pm", false);
if (pm) {
performance.PerfModeOff();
pm = false;
sharedPreferences.edit().putBoolean("pm", pm).apply();
} else {
performance.PerfModeOn(mContext);
pm = true;
sharedPreferences.edit().putBoolean("pm", pm).apply();
}
return true;
}
});
Page 2:
secondView= inflater.inflate(R.layout.page2, container, false);
firstView = inflater.inflate(R.layout.page1, container);
//A bunch of code and then...
element2.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
pm = sharedPreferences.getBoolean("pm", false);
if (pm) {
performance.PerfModeOff();
pm = false;
sharedPreferences.edit().putBoolean("pm", pm).apply();
} else {
performance.PerfModeOn(mContext);
pm = true;
sharedPreferences.edit().putBoolean("pm", pm).apply();
}
return true;
}
});
The class that's changing the colors:
public Performance(View firstView, View secondView,LayoutInflater inflater, ViewGroup container) {
mfirstView = inflater.inflate(R.layout.page1, container,false);
msecondView = inflater.inflate(R.layout.page2, container,true);
uparrow= (ImageView) firstView.findViewById(R.id.UpArrow);
//**A lot of lines like above
public void PerfModeOn(Context context) {
mfirstView.setBackgroundColor(Color.BLACK);
msecondView.setBackgroundColor(Color.BLACK);
uparrow.setBackgroundTintList(ColorStateList.valueOf(context.getResources().getColor(R.color.DarkTint)));
//** More lines to change colors
public void PerfModeOff() {
//**Lines to change colors back
Of course if you need to see some more code, just let me know. What can I do to fix this? Thanks for your help!
Your second view never changes because PerfModeOn() calls mfirstView.setBackgroundColor(Color.BLACK); naively. You need to pass which view to set the background of, so it sets the correct background.
In other words, your first view will only ever be the view changed because of how explicit you were when setting the background.
An alternative would be:
public void perfModeOn(Context context, View viewToSetBackgroundOf) {
viewToSetBackgroundOf.setBackgroundColor(Color.BLACK);
...
}
An even better approach would be to implement an OnLongClickListener that both of your views use. Then you get which view was clicked in your onLongClick() method. Then a simple if statement on which view was clicked, and set the background of the opposite view. This would get rid of your redundant code, and some unneeded methods.