I'm using the sliding menu library here: https://github.com/jfeinstein10/SlidingMenu/
and I have an activity that inherits from SlidingMenuActivity with a sliding menu that works perfectly, but I also want to add SlidingMenus to each row in a list fragment that is shown as part of this SlidingMenuActivity subclass. It seems that the way I'm doing it doesn't work at all; the touches get intercepted and they don't let me click on a list row, but I can't swipe the sliding menu into appearance, nor can I see the SlidingMenu when it's closed.
This is the code that I'm using to add the sliding menu to each list row:
private void makeSlidingMenu(View view) {
FrameLayout menuClosedFrame = // ... the above view
RelativeLayout menuLayout = // ... the behind view
SlidingMenu slidingMenu = new SlidingMenu(view.getContext());
slidingMenu.setContent(menuClosedFrame);
slidingMenu.setMenu(menuLayout);
slidingMenu.setBackgroundColor(Color.RED);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setTouchModeBehind(SlidingMenu.TOUCHMODE_FULLSCREEN);
slidingMenu.setBehindScrollScale(1.0f);
slidingMenu.setFadeDegree(0.0f);
RelativeLayout layout = (RelativeLayout)view;
layout.addView(slidingMenu, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT) {{
addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
}});
}
It's not pretty, but it seems to get the job done on a normal activity that doesn't have a list view and is not a SlidingMenuActivity. It just doesn't work in a nested scenario with a ListView for me. Is there anything else I could be doing wrong? If posting more code would help let me know. Thanks!
Figured it out; the way I was adding the sliding menu to the row was causing it to be sized improperly I think; I fixed it by adding it to a framelayout instead that also contains the contents I want to show since i want the cell's main content to remain static.
Related
I'm creating a quiz app. Users are shown clues and have to guess the answer.
I have an activity with two layouts nested inside the "parent" constraint layout. The reason for this is that I needed a custom layout so I could pull rows from three columns from an SQLite database and display them in the app. These are the clues.
The top part of the activity is therefore this layout and the bottom part of the activity is another constraint layout that contains the buttons (refresh button to refresh the clues and check answer button) and the text view allowing the user to enter their text.
When I enter my activity, everything works fine. Clues are displayed and all buttons work.
When I press the refresh button (to refresh the clues), it refresh's the clue and then stops the buttons in the bottom layout from working, i.e I press them but nothing happens. My theory is that when I run the method that, it switches everything to the top layout and that stays (disabling the buttons in the bottom layout).
The bit of code that refresh's the clues, and therefore is causing the issue is this:
public void getPlayerHistory() { // Method used to get player history from DatabaseAccess.java,
// assign to list and place in the ListView
setContentView(R.layout.view_player_history); //the main layout everything is being displayed in
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getApplicationContext());
databaseAccess.open();
userList = new ArrayList<>();
Cursor data = databaseAccess.getListContents();
int numRows = data.getCount();
while (data.moveToNext()){
user = new User(data.getString(0), data.getString(1),
data.getString(2));
userList.add(user);
}
databaseAccess.close();
com.example.transfergame.ThreeClass adapter =
new com.example.transfergame.ThreeClass(this, R.layout.list_adapter_view, userList);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
Log.i("get player history", "playerHistoryFunction Run");
}
I think the issue is coming from this line
com.example.transfergame.ThreeClass adapter =
new com.example.transfergame.ThreeClass(this, R.layout.list_adapter_view, userList);
where I switch to my custom layout, but I'm not exactly sure what to code to then switch back to my parent layout.
Can someone point me in the right direction?
The issue actually came from
public void getPlayerHistory() {
setContentView(R.layout.view_player_history);
For some reason, setting this within this method, and not the onCreate() method was causing the issue. I have taken this out and put it in the onCreate() method and now everything runs fine.
I am trying to manipulate the visibility of RelativeLayout on a certain click event, using the visibility attribute.
After adding event handlers to each list item, I can check that the visibility status is changing, but in the Android emulator, the screen doesn't change at all (and I have tested it with visibility="visible" in the XML to make sure that it would show up).
Here's the code for the click handler:
someHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater layoutSeed = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View baseView = layoutSeed.inflate(R.layout.activity_listings, null, false);
RelativeLayout popup = (RelativeLayout) baseView.findViewById(R.id.popupContainer);
Log.d("Tag1",Integer.toString(popup.getVisibility()));
popup.setVisibility(View.VISIBLE);
Log.d("Tag2",Integer.toString(popup.getVisibility()));
}
});
Logcat shows the status change. I've also tried to invalidate() and postInvalidate() on both baseView and popup, as well as popup.bringToFront() and the combinations of these and so far nothing's working.
Any suggestions or possible routes to investigate?
If the RelativeLayout you are trying to change visibility is already in the screen, you don't need to inflate it again. Please make a reference to already inflated one and change its visibility
holder.relativeLayout.setVisibility(View.GONE);
Gone will remove the view from your layout. Use Invisible instead of gone.
when you click on button your View is replace by button's OnClick(View v) method.
so you just write down
v.popup(View.GONE)
Look at this line of code:
View baseView = layoutSeed.inflate(R.layout.activity_listings, null, false);
RelativeLayout popup = (RelativeLayout) baseView.findViewById(R.id.popupContainer);
Log.d("Tag1",Integer.toString(popup.getVisibility()));
Actually, you're trying to create a new baseView and set it gone. If you want to set the row item gone, you should remove it from your adapter temporary instead such as (if you set itemView to gone, you should be careful with the resuing view of the adapter):
removeItemAt(getAdapterPosition())
notifyDataRemoved(getAdapterPosition())
So the Negative and Positive buttons of my AlertDialog are greyed-out, but they shouldn't be.
greyed-out text screen
I suspect it has something to do with Context, becouse once i had identical problem with my ListView. I have repaired that by changing argument in ArrayAdapter's reference from getApplicationContext() to getBaseContext(). Can someone explain it to me? I don't really understand the 'Context'
This is my code
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("What do you want to do with " + getArrayList("ListOfRecipes").get(position) );
builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
List<String> list = new ArrayList<>(getArrayList("ListOfRecipes"));
Toast.makeText(getBaseContext(), list.get(position) + "has been removed", Toast.LENGTH_SHORT).show();
list.remove(position);
saveList(list, "ListOfRecipes");
}
});
builder.setNegativeButton("Modify", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
List<String> list = new ArrayList<>(getArrayList("ListOfRecipes"));
SharedPreferences sp = getSharedPreferences("Recip", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("Recip", list.get(position));
editor.apply();
startActivity(new Intent(getBaseContext(), ManageRecipeActivity.class));
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
First of all, if that piece of code is inside an activity, you can simply declare context with "this" (which is what you have done by passing MainActivity.this) to the dialog builder.
What I'm suspecting is that it may be that your MainActivity is getting a theme for the AlertDialog that is making the buttons look gray. You could check that out in your styles.xml (if there is a style defined for the dialog) and in the AndroidManifest file for the theme you are passing to your MainActivity.
If you don't find anything wrong/don't want to change the theme, I can think of two ways to solve that problem.
First way - Changing the button color (less work, but less flexible)
The first is actually changing the dialog button color as it's done in this post to whatever color you want.
Second way - inflating a custom view that meets your needs (more work, but more flexible)
The second way would be to inflate a view and pass it to the dialog. Actually, you don't really have to use the standard dialog style at all, you can inflate your own view inside it to fit your needs.
To do that, you must:
1) Inflate a chosen view
As an example:
LayoutInflater factory = LayoutInflater.from(this);
final View view = factory.inflate(R.layout.image_dialog_layout, null);
2) Pass the inflated view to your dialog builder:
final AlertDialog dialog = new AlertDialog.Builder(this);
dialog.setView(view);
//Additional code to set click listeners, i.e.
dialog.create().show();
}
That way, you'll be inflating whatever layout you want, so you can just put the buttons you want inside it (with the color, size, font type you want).
It is important to notice that, even after inflating a view to it, you can still use methods setPositiveButton and setNegativeButton, they will appear below your inflated layout in the dialog. So, beware inflating buttons AND using those methods, because the buttons will appear duplicated.
Since, in this case, you don't want them to be gray, you want to put buttons inside your layout, with whatever style you want, and inflate them (and reference them in your code through findViewById).
The biggest advantage of the second way is that you can inflate whatever you want, with the styles you want. You can even put images inside it, if you wish.
Hope it helps, let me know if it worked for you.
Context is an interesting topic in android. And one thing to understand is Application Context and Activity Context are different. You should make sure that any thing that is related to UI, you should be using Activity Context.
This can be things like
Showing a dialog
Starting another activity
Inflating a new layout
This is because Activity is the only Context on which the themes defined in your manifest are actually attached.
I also recommend Context, What Context article to get a complete picture.
Happy Coding :)
Hi I tried to change the content of a textview of another xml.
Unfortunaly it does not work, my code looks like this:
View inflatedView = getLayoutInflater().inflate(R.layout.fragment_main, null);
TextView testView= (TextView) inflatedView.findViewById(R.id.section_label);
TextView test = (TextView) inflatedView.findViewById((R.id.testext));
testView.setText("adasd");
test.setText("adjhsajdH");
Does anyone know a solution.
When you are inflating a layout, it creates a new layout. It means your first line of code does not retrieve the layout of your fragment, but creates a new one.
That' why you don't see the change in your fragment view.
If you want to change the values displayed by the fragment, create a new method in which fragment that will do the job internally.
try this. remove the old layout from the parent view.
parentView.removeView(urtextview)
then add the new textview u just edited
parentView.add(urTextView)
Is it possible to use the SlidingMenu (seen https://github.com/jfeinstein10/SlidingMenu) without the use of fragments? In my app i am using GreenDroid, and seeing it doesn't yet have support for GDFragmentActivities or anything of the sort. So i am just using the example of attaching the SlidingMenu to my activity using the following code:
SlidingMenu slide = new SlidingMenu(this );
slide.setMode(SlidingMenu.LEFT);
slide.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
slide.setTouchModeBehind(SlidingMenu.TOUCHMODE_FULLSCREEN);
slide.setBehindOffset(150);
slide.setFadeDegree(0.3f);
slide.setShadowWidth(5);
slide.setOnClickListener(this);
slide.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
slide.setMenu(R.layout.simpleframelayout);
ListView v = (ListView)slide.getMenu().findViewById(R.id.simpleFrameLayoutList);
v.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, new String[]{"Hello"}));
My problem is that though this will make the SlidingMenu appear properly, the ListView will be completely unresponsive to any touches. I was wondering if there were anything i could possibly be missing or if in fact i definitely do need to use fragments?
Cheers
Instead of this
(ListView)slide.getMenu().findViewById(R.id.simpleFrameLayoutList);
Try this
(ListView)findViewById(R.id.simpleFrameLayoutList);
It worked for me