Sliding Menu without moving header - android

I am creating a sliding menu drawer in my application...the problem is when i am sliding the menu it is sliding the heading too wheres i just want the content to move with the slider. how can i prevent header from moving. i am using fragments for my code:
MainActivity :
setContentView(R.layout.layout_home);
ButterKnife.inject(this);
btnMenu.setVisibility(View.VISIBLE);
homeScreenFragment = new HomeScreenFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frameLayout, homeScreenFragment).commit();
// configure the SlidingMenu menu = new SlidingMenu(this);
menu=new SlidingMenu(this);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.menu_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, new HomeNavFragment()).commit();
findViewById(R.id.imgBtnMenu).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
menu.toggle(true);
}
});
HomeScreenFragment:
public final String[] podName = new String[] { "NO Text", "NO Text",
"NO Text", "NO Text" };
public final Integer[] images = { R.drawable.pod_img1, R.drawable.pod_img2,
R.drawable.pod_img3, R.drawable.pod_img1 };
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_homescreen, container,
false);
ButterKnife.inject(this, view);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
addItemsToListView();
}
private void addItemsToListView() {
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < podName.length; i++) {
RowItem item = new RowItem(images[i], podName[i]);
// RowItem item = new RowItem(imageUrl[i], deityName[i]);
rowItems.add(item);
}
setAdapter();
}
private void setAdapter() {
CustomBaseAdapter adapter = new CustomBaseAdapter(getActivity(), rowItems);
podListView.setAdapter(adapter);
}
Navigation Fragment:
public static final String[] nav_title = new String[] { "CLE & Events",
"Pictorial Roster", "Publications", "On-Demand CLE", "Court Info",
"LBA Alerts", "My Profile", "Notification Settings",
"App Support & Feedback", "Share this App", "Developer", " ", " " };
public static final int[] nav_icon = { R.drawable.ic_menu_calender,
R.drawable.ic_menu_roster, R.drawable.ic_menu_publication,
R.drawable.ic_menu_filebox, R.drawable.ic_menu_court,
R.drawable.ic_menu_bubble, R.drawable.ic_menu_profile,
R.drawable.ic_menu_reminder, R.drawable.ic_menu_share,
R.drawable.ic_menu_developer, R.drawable.ic_menu_developer,
R.drawable.ic_menu_developer };
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_homescreen, container,
false);
ButterKnife.inject(this, view);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
setAdapter();
/*addItemsToListView();*/
}
private void setAdapter() {
NavigationMenuAdapter adapter = new NavigationMenuAdapter(getActivity(),nav_title,nav_icon);
podListView.setAdapter(adapter);
}
Xml:
layout_home:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include
android:id="#+id/header_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
layout="#layout/include_header" />
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header_home">
</FrameLayout>
</RelativeLayout>
layout_homescreen
<ListView
android:id="#+id/podListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:listSelector="#android:color/transparent" >
</ListView>
</LinearLayout>
menu.xml

Since you want to move your content area with sidepanel + ActionBar should remain static, best way would be to implement v4 widget SlidingPaneLayout (android.support.v4.widget.SlidingPaneLayout).
here is a good example that explains how to implement SlidingPaneLayout
And please go through Developer's site for proper understanding.
Hope it helps..!! :)

Related

ListPopupWindow jumps to top on certain devices when scrolled

I'm building a search field using a ListPopupWindow.
On certain devices the ListPopupWindow changes it's position when scrolled. (It jumps to the top):
Before scrolling:
After scrolling:
I tested this on a Nexus 6P.
On other devices it works normally, it does not change it's initial position when scrolled. This was captured on a Samsung Galaxy A5.
Here's the code:
The Activiyt:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
EditText itemName;
ListPopupWindow listPopupWindow;
String[] items = {"Item1", "Item2", "Item3", "Item4",
"Item5", "Item6", "Item7", "Item8", "Item9", "Item10"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
itemName = (EditText) findViewById(
R.id.item_name);
listPopupWindow = new ListPopupWindow(
MainActivity.this);
listPopupWindow.setAdapter(new ArrayAdapter<>(
MainActivity.this,
R.layout.list_item, items));
listPopupWindow.setAnchorView(itemName);
listPopupWindow.setWidth(ListPopupWindow.MATCH_PARENT);
listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
listPopupWindow.setModal(true);
listPopupWindow.setOnItemClickListener(MainActivity.this);
itemName.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listPopupWindow.show();
}
});
}
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
itemName.setText(items[position]);
listPopupWindow.dismiss();
}
}
The layout file of the activity:
<?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">
<EditText
android:id="#+id/item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter item name"
android:textSize="26sp"/>
</LinearLayout>
And the list item layout:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
android:textSize="50sp"
android:textStyle="bold"/>
Any ideas how to solve this?
Instead of doing this:
listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
you have to set the height of your listPopupWindow explicitly. In order to do this, you need to calculate the available height and find the right moment for setting that value.
Get rid of the above line.
Add this to your xml LinearLayout:
android:id="#+id/root"
Add this to your MainActivity:
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
int popupHeight = findViewById(R.id.root).getHeight() - itemName.getHeight();
listPopupWindow.setHeight(popupHeight);
}
}
Inside a fragment, you don't need onWindowFocusChanged. You can simply post a Runnable to the view:
Activity activity;
EditText itemName;
ListPopupWindow listPopupWindow;
LinearLayout linearLayout;
String[] items = {"Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10"};
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Activity) {
activity = (Activity) context;
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View view = inflater.inflate(R.layout.fragment_main, container, false);
linearLayout = view.findViewById(R.id.root);
itemName = view.findViewById(R.id.item_name);
listPopupWindow = new ListPopupWindow(activity);
listPopupWindow.setAdapter(new ArrayAdapter<>(activity, R.layout.list_item, items));
listPopupWindow.setAnchorView(itemName);
listPopupWindow.setWidth(ListPopupWindow.MATCH_PARENT);
listPopupWindow.setModal(true);
listPopupWindow.setOnItemClickListener(this);
itemName.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
listPopupWindow.show();
}
});
view.post(new Runnable() {
#Override
public void run() {
int popupHeight = linearLayout.getHeight() - itemName.getHeight();
listPopupWindow.setHeight(popupHeight);
}
});
return view;
}
This worked for me
val popup = ListPopupWindow(context)
try {
val setListItemExpandMax: Method = popup.javaClass.getDeclaredMethod("setListItemExpandMax", Integer.TYPE)
setListItemExpandMax.isAccessible = true
setListItemExpandMax.invoke(popup, 5)
} catch (e: Exception) {
when (e) {
is NoSuchMethodException, is InvocationTargetException, is IllegalAccessException -> {
e.printStackTrace()
}
else -> throw e
}
}

onClickListener is not working within onCreateView method

Im using the following code to create Floating Action button. Its within a relative layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/tools"
android:background="#drawable/form_bg">
<android.support.design.widget.FloatingActionButton
android:id="#+id/myFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/fab_bg_mini"
app:elevation="4dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/trans_white"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<WebView
android:id="#+id/chart"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Here is how Im setting the onClickListener for the floating action button within onCreateView method.
public class TrendChart extends Fragment {
private View mRootview;
private static final String URL = "file:///android_asset/taskstatus.html";
private WebView chart;
class InnerWebView extends WebViewClient {
InnerWebView() {
}
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript: loadTrendChart();");
}
}
public static final TrendChart newInstance(String company_name) {
TrendChart f = new TrendChart();
f.setArguments(new Bundle(1));
return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
mRootview = inflater.inflate(R.layout.trend_chart, null);
this.chart = (WebView) mRootview.findViewById(R.id.chart);
this.chart.getSettings().setJavaScriptEnabled(true);
this.chart.setWebChromeClient(new WebChromeClient());
this.chart.setWebViewClient(new InnerWebView());
refreshWebView();
return mRootview;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FloatingActionButton myFab = (FloatingActionButton) getActivity().findViewById(R.id.myFAB);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("Clicked","Hello");
}
});
}
private void refreshWebView() {
this.chart.loadUrl(URL);
}
}
The problem is that onClickListener is not getting fired. Why is that so? I tried putting listener within onCreateView method too but still the problem persists. Is there any ways through which I can use floating button within a class that extends Fragments?
You have to put your code when the Activity is fully created as follows :
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
FloatingActionButton myFab = (FloatingActionButton) getActivity().findViewById(R.id.myFAB);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("Check","Clicked");
}
});
}
If you have your code on onCreateView() your code should look like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
FloatingActionButton myFab = (FloatingActionButton) view.findViewById(R.id.myFAB);
myFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do stuff
}
});
return view;
}
EDIT
Change this :
mRootview = inflater.inflate(R.layout.trend_chart, null);
to this :
mRootview = inflater.inflate(R.layout.trend_chart, container, false);

OnCreateView not called in a dialogfragment from a fragment

I have a fragment that executes or display a dialog fragment once a button is clicked. This dialog fragment displays a table generated by code, however when I try to click the button on this is displayed
And when I trace it in the LogCat the dialog fragments onCreateView is not called. Can somebody help or explain this to me?. Im not that good in android programming yet and I know i still have a lot to learn.
Here is the code of the fragment that calls the dialog fragment
public class fragment_schedule extends Fragment {
...............
public fragment_schedule(ArrayList<SubjSchedule> subj){
subject = subj;
}
#SuppressWarnings("deprecation")
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
..................
showlstbtn = (Button) rootView.findViewById(R.id.button_showlstschd);
showlstbtn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.addToBackStack(null);
DialogFragment dialog = ShowLstDialog.newInstance(subject);
dialog.show(ft, "dialog");
}
});
..........
and heres my dialog fragment
public class ShowLstDialog extends DialogFragment {
private static final String TAG = ShowLstDialog.class.getSimpleName();
public static Context mContext;
public static TableLayout tl;
public static TableRow trh;
private static ArrayList<SubjSchedule> subject= new ArrayList<SubjSchedule>();
public static DialogFragment newInstance(ArrayList<SubjSchedule> subj) {
// TODO Auto-generated method stub
DialogFragment f = new DialogFragment();
subject = subj;
Log.v(TAG, subject.size()+"");
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v(TAG, "OnCreateView: " + subject.size()+"");
View rootView = inflater.inflate(R.layout.fragment_dialog_showlst, container);
//mEditText = (EditText) view.findViewById(R.id.txt_your_name);
Log.v(TAG, "OnCreateView: " + subject.size()+"");
getDialog().setTitle("");
tl = (TableLayout) rootView.findViewById(R.id.tablelayout_schedlst);
trh = new TableRow(getActivity());
TextView[] tvh = new TextView[3];
for(int i=0; i<3; i++){
tvh[i] = new TextView(getActivity());
tvh[i].setBackgroundResource(R.drawable.green2);
tvh[i].setTextColor(getResources().getColor(R.color.LightCyan));
tvh[i].setGravity(Gravity.CENTER);
tvh[i].setPadding(30, 3, 30, 3);
//tvh[i].setLayoutParams(params);
trh.addView(tvh[i]);
}
tvh[0].setText("Subject");
tvh[1].setText("Description");
tvh[2].setText("Instructor");
TableRow[] tr = new TableRow[subject.size()];
for(int i=0; i<subject.size();i++){
tr[i] = new TableRow(getActivity());
TextView[] tv1 = new TextView[3];
for(int k=0; k<3; k++){
tv1[k] = new TextView(getActivity());
tv1[k].setBackgroundResource(R.drawable.btn_default_disabled_holo_dark);
tv1[k].setTextColor(getResources().getColor(R.color.list_background_pressed));
tv1[k].setGravity(Gravity.CENTER);
tv1[k].setPadding(30, 3, 30, 3);
//tvh[i].setLayoutParams(params);
tr[i].addView(tv1[i]);
}
tv1[0].setText(subject.get(i).getcn());
tv1[1].setText(subject.get(i).getd());
tv1[2].setText(subject.get(i).geti());
tl.addView(tr[i]);
}
return rootView;
}
}
and here is the xml file of the dialog fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#color/LightCyan"
android:scrollbars="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TableLayout
android:id="#+id/tablelayout_schedlst"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="*" >
</TableLayout>
</RelativeLayout>
</HorizontalScrollView>
</ScrollView>
</RelativeLayout>
thank you so much in advance.
Change this
public static DialogFragment newInstance(ArrayList<SubjSchedule> subj) {
// TODO Auto-generated method stub
DialogFragment f = new DialogFragment();
subject = subj;
Log.v(TAG, subject.size()+"");
return f;
}
to
public static ShowLstDialog newInstance(ArrayList<SubjSchedule> subj) {
// TODO Auto-generated method stub
ShowLstDialog f = new ShowLstDialog();
subject = subj;
Log.v(TAG, subject.size()+"");
return f;
}
And do read
http://developer.android.com/reference/android/app/DialogFragment.html
Instead of (or in addition to) implementing onCreateView(LayoutInflater, ViewGroup, Bundle) to generate the view hierarchy inside of a dialog, you may implement onCreateDialog(Bundle) to create your own custom Dialog object
So you should use:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
to replace:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
Instead of
public static DialogFragment newInstance(...) {
DialogFragment f = new DialogFragment();
return f;
}
write
public static DialogFragment newInstance(...) {
DialogFragment f = new ShowLstDialog();
return f;
}
and also use Bundle and setArguments, getArguments to pass arguments.

How to set onclick listener for a button in a fragment in android

My app contains a form as shown in the following image:
When I click on menu options button, the drawer opens as shown in the following image:
I want the drawer to open when the button select location is pressed.
My codes are
RegisterBlood.java
...
public class RegisterBlood extends Activity implements OnItemClickListener {
DrawerLayout dLayout, dLayout2;
ListView dList, dList2;
ArrayAdapter<String> adapter, adapter2;
String[] state_data2;
String city = "", state = "";
Button location;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectlocation);
Bundle args = new Bundle();
Fragment detail = new RegisterBloodFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, detail)
.commit();
...
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, state_data);
dList.setAdapter(adapter);
dList.setSelector(android.R.color.holo_blue_dark);
// dLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
dList.setOnItemClickListener(this);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent e) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
// your action...
if (!dLayout.isDrawerOpen(dList)) {
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, state_data2);
dList.setAdapter(adapter);
dLayout.openDrawer(dList);
dList.setOnItemClickListener(this);
}
return true;
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your action...
if (dLayout.isDrawerOpen(dList)) {
dLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
return true;
}
return super.onKeyDown(keyCode, e);
}
#Override
public void onItemClick(AdapterView<?> arg0, View v, final int position,
long id) {
// TODO Auto-generated method stub
...
dLayout2 = (DrawerLayout) findViewById(R.id.drawer_layout);
dList2 = (ListView) findViewById(R.id.left_drawer);
adapter2 = new ArrayAdapter<String>(RegisterBlood.this,
android.R.layout.simple_list_item_1, city_data);
dList2.setAdapter(adapter2);
dList2.setSelector(android.R.color.holo_blue_dark);
dLayout2.openDrawer(dList2);
dLayout2.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
dList2.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position2,
long id) {
dLayout2.closeDrawers();
state = state_data2[position];
city = city_data[position2];
Bundle args = new Bundle();
args.putString("Menu", city_data[position2] + " ("
+ state_data2[position] + ")");
Fragment detail = new RegisterBloodFragment();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, detail).commit();
}
});
}}
RegisterBloodFragment.java
...
public class RegisterBloodFragment extends Fragment implements OnClickListener {
Button location;
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.registerblood, container, false);
String menu = getArguments().getString("Menu");
location= (Button) view.findViewById(R.id.etlocation);
location.setText(menu);
//Context c=getActivity();
//location.setOnClickListener(c.getApplicationContext().set);
return view;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getActivity(),
"Yes",
Toast.LENGTH_SHORT).show();
}
}
registerblood.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="#color/silver"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="10dp" >
<EditText
android:id="#+id/bregetName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="Name" />
<EditText
android:id="#+id/bregetBloodGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:ems="10"
android:hint="Blood Group" />
<Button
android:id="#+id/etlocation"
style="#android:style/Widget.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="select location" >
</Button>
<Button
android:id="#+id/bregbtnSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10sp"
android:layout_marginTop="10sp"
android:background="#drawable/button"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Submit"
android:textColor="#FFFFFF"
android:textSize="24sp" />
</LinearLayout>
selectlocation.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</FrameLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#fff"/>
My question is:
How to add onclicklistener for select location button in RegisterBlood.java or call onClickListener which is in RegisterBlood.java from RegisterBloodFragment.java ?
Please help me out.
Since the button is a part of the fragment's layout, set the listener there:
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.registerblood, container, false);
String menu = getArguments().getString("Menu");
location = (Button) view.findViewById(R.id.etlocation);
location.setText(menu);
location.setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
RegisterBlood activity = (RegisterBlood) getActivity();
// Now you can contact your activity through activity e.g.:
activity.onKeyDown(KeyEvent.KEYCODE_MENU, null);
}
First of all remember to implements the OnClickListener interface:
public class YourClassName extends Fragment implements View.OnClickListener
public Button button;
Then, inside the method OnCreateView, inflate the button and set the listener to it, like this:
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle args) {
button = (Button) inflater.inflate(R.layout.registerblood, container, false).findViewById(R.id.your_button_id);
button.setOnClickListener(this);
}
Then #Override the function onClick and do whatever you gotta do:
#Override
public void onClick(View v) {
//YOUR CODE HERE
}
EDIT
If you're simply looking for a FocusListener (in that case your question needs an update) set the listener on your onCreateView method:
your_edittext.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Toast.makeText(getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
// OPEN THE DRAWER HERE
} else {
Toast.makeText(getApplicationContext(), "lost the focus", Toast.LENGTH_LONG).show();
}
}
or you can implement that listener to your class.
u need to inflate fragment like this for button click
View view = inflater.inflate(R.layout.fragment_blank3,
container, false);
Button bt1=(Button)view.findViewbyId(R.id.buttton);
bt1.setOnclick...
//then return in on create view.
return view;
// this works good, thankyou

Items on sliding menu(jfeinstein) uncliclable

i am doing sliding menu. Everything works well but items on sliding menu not clickable.Here is my code.
package kz.redone.labs.hook;
public class MainActivity extends SlidingFragmentActivity {
private SlidingMenu sm;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initSlidingMenu();
}
private void initSlidingMenu(){
setBehindContentView(R.layout.activity_menu);
sm = getSlidingMenu();
sm.setMode(SlidingMenu.LEFT);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
sm.setFadeDegree(0.35f);
sm.setShadowWidthRes(R.dimen.shadow_width);
// sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
}
}
here the MenuActivity
public class MenuActivity extends SlidingFragmentActivity implements OnClickListener {
private ListView lvMenu;
private SimpleAdapter sa;
private ArrayList<Map<String,Object>> data;
TextView tvMain;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_menu, container, false);
}
public void onViewCreated(final View view, Bundle savedInstanceState) {
tvMain = (TextView) view.findViewById(R.id.tvMain);
tvMain.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stubmsg
switch (v.getId()) {
case R.id.tvMain:
Log.d("tag", "message");
Toast.makeText(this, "Main", 1500).show();
break;
default:
break;
}
}
}
and here is activity_menu.xml
<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"
tools:context=".MenuActivity" >
<TextView
android:id="#+id/tvMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:clickable="true"
android:text="Main"/>
<TextView
android:id="#+id/tvSetting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tvMain"
android:textSize="30dp"
android:text="Settings"/>
</RelativeLayout>
activity_main.xml cosists only of textview.
Thanks!

Categories

Resources