Unable to click the right to left DrawerLayout items - android

I have an activity that displays a drawer layout when a menu item is clicked thus its direction is from right to left. I manage to display the navigation menu using the following code.
public class Forecast_details extends ActionBarActivity implements OnChartValueSelectedListener {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//return false;
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if(id==R.id.Options){
if (mDrawerlayout.isDrawerOpen(Gravity.END)){
mDrawerlayout.closeDrawer(Gravity.END);
}else
mDrawerlayout.openDrawer(Gravity.END);
}
return super.onOptionsItemSelected(item);
}
My Navigation drawer contains a static list so I did not bother to make a listview out of it as seen below,
However I cannot click my items inside the Drawer Layout, or anything in the view even v4.widget.DrawerLayout in my xml is unclickable. Below is the two xml file that I used.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout_right"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<include layout="#layout/nav_drawer_right" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.36" >
....
</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
nav_drawer_right.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView_right"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#2c3e50"
android:paddingTop="20dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container_fragment2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="1dp" >
</FrameLayout>
<LinearLayout
android:id="#+id/linearLayout_menuconatiner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="2dp"
android:paddingRight="2dp" >
<ImageView
android:id="#+id/imageView_forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:clickable="true"
android:onClick="SendClick"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_forward" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_request" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_reloadvnet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_updatevnet" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_reloadvsms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_updatevsms" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_feedback" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
Please help.. I tried a lot of things to make it work like using a listview instead but I still have no luck.. please do help, Im new at using drawer layout so there may be some more things I need to know.. Thank you in advance..

To prevent return OnClickListener to parent layout require set android:clickable="true" in drawerLayout container.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout_right"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.36" >
....
</ScrollView>
<include layout="#layout/nav_drawer_right" />
</android.support.v4.widget.DrawerLayout>
nav_drawer_right.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView_right"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#2c3e50"
android:paddingTop="20dp" >
....
</ScrollView>
in your Activity
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawerLayout mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout_right);
ScrollView sV = (ScrollView) findViewById(R.id.scrollView1);
mDrawerLayout.openDrawer(Gravity.RIGHT);
sV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("drawer click >>>>>>>>>>>>>>>>>>");
}
});
}
}

Related

CardView NullPointerException onClickListener

I am trying to implement a setOnClickListener method on CardView but I keep getting a null exception whenever I switch orientation of device (works fine if I start by either portrait or landscape mode):
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.navajoinc.navajoinc.navfeedbackapp/com.navajoinc.navajoinc.navfeedbackapp.feedBackCardView}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.LinearLayout.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
My Java Class is:
public class feedBackCardView extends FragmentActivity {
CardView neutralCustomer;
CardView happyCustomer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feed_back_card_view);
neutralCustomer = findViewById(R.id.neutralCustomer);
happyCustomer = findViewById(R.id.happyCard);
neutralCustomer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(feedBackCardView.this, thank_you_positive.class);
startActivity(intent);
}
});
happyCustomer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(feedBackCardView.this, thank_you_positive.class);
startActivity(intent);
}
});
}
public void unsatisfiedCutomerSelectionMethod(View v){
singleChoiceClass unsatisfied_customer_dialog = new singleChoiceClass();
unsatisfied_customer_dialog.show(getFragmentManager(), "unsatisfied_customer_dialog");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main_menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.contactus:
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:800.525.5097"));
startActivity(callIntent);
break;
case R.id.visitsite:
Intent siteIntent = new Intent(Intent.ACTION_VIEW);
siteIntent.setData(Uri.parse("http://navajoinc.com"));
startActivity(siteIntent);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
Activity XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".feedBackCardView"
android:padding="10dp"
android:background="#color/colorWhiteSmoke"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
android:layout_gravity="center"
android:text="Please tap a response :"
android:textColor="#color/colorLightGrey" />
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:clickable="false"
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/happyCard"
android:foreground="?android:selectableItemBackground"
android:clickable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
app:cardElevation="3dp"
app:cardBackgroundColor="#color/cardview_light_background"
app:cardCornerRadius="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorWhite"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/happy_face"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#color/colorLightGrey"
android:text="Happy Customer"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cardview_light_background"
android:layout_margin="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="11dp"
android:textColor="#color/colorTextGrey"
android:text="I have had a happy experience"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:id="#+id/neutralLayout"
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="#+id/neutralCustomer"
android:foreground="?android:selectableItemBackground"
android:clickable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
app:cardElevation="3dp"
app:cardBackgroundColor="#color/cardview_light_background"
app:cardCornerRadius="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorWhite"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/neutral_face"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#color/colorLightGrey"
android:text="Neutral Customer"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cardview_light_background"
android:layout_margin="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="11dp"
android:textColor="#color/colorTextGrey"
android:text="My experience was neutral"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:foreground="?android:selectableItemBackground"
android:clickable="true"
android:onClick="unsatisfiedCutomerSelectionMethod"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
app:cardElevation="3dp"
app:cardBackgroundColor="#color/cardview_light_background"
app:cardCornerRadius="3dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorWhite"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#drawable/angry_face"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:textColor="#color/colorLightGrey"
android:text="Unsatisfied Customer"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cardview_light_background"
android:layout_margin="2dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="11dp"
android:textColor="#color/colorTextGrey"
android:text="My experience was unpleasant"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I have tried many solutions on Stackoverflow and other websites but none of it has worked ? Could you please help with this issue?
I've tried the code and it's working. Even after rotation. However, I changed the implementation of the onclick methods to simply show a toast rather that launch an intent since you didn't provide the classes. I also ommited all the code related to the menu.
Also, why are you setting the clickable=true property on your card view if you are setting an onClickListener within your code? It's redundant. Also, I don't see any reason to assert clickable=false in your <LinearLayout> tags.
I don't think you are providing enough information to locate the error, or it's related to the classes that haven't been provided. Is the app crashing simply on rotation, or after you try clicking on something? Are you sure the crash is related to rotation, and not an attempt to launch a new activity?

Android actionbar won't show title or settings

Little confused on how to show an actionbar on my second activity. Hopefully I didn't provide too much. Basically to clarify, the actionbar works perfect on my MainActivity (title shows and settings button), but I can only see the action bar on my other activities (NO title or buttons) I also took a look in menu_main.xml and noticed the context was set to my MainActivity, so I made menu_attractions.xml and changed the context to the proper activity, but that still didn't show the title or settings button. any help would be greatly appreciated!
Nothing fancy for my MainActivity (this is where I assume the magic happens for the title/buttons??)
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context=".MainActivity">
<include layout="#layout/content_main" />
<include layout="#layout/app_bar"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
And for the MainActivity class, I search for the toolbar below... and inflate the menu
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
attractionButton = (Button) findViewById(R.id.activityButton);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Here's my actionbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
</android.support.v7.widget.Toolbar>
Now... here's my second activity
public class AttractionsActivity extends AppCompatActivity {
private Button scheduleButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.d_activities);
Toolbar toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
scheduleButton = (Button) findViewById(R.id.activies_Schedule);
scheduleButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view){
Intent scheduleIntent = new Intent(AttractionsActivity.this, ScheduleActivity.class);
startActivity(scheduleIntent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_attractions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
here's my layout for my second activity activity
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/img_252413"
tools:context="AttractionsActivity"
tools:showIn="#layout/d_activities">
<include layout="#layout/app_bar"
android:id="#+id/include" />
<LinearLayout
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/include">
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Schedule"
android:id="#+id/activies_Schedule" />
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Maps"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Performers"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Sponsors"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Statics"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Exibitors"/>
</FrameLayout>
<Space
android:layout_width="0dp"
android:layout_height="10dp" />
<FrameLayout
android:background="#ffffff"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginTop="-5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="-5dp"
android:layout_marginLeft="-10dp"
android:layout_marginRight="-10dp"
android:layout_marginBottom="-5dp"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Food"/>
</FrameLayout>
</LinearLayout>
</RelativeLayout>
It seems You Have Changed the Theme for Your Second Activity.Try the following
Step 1: Add this in Your Styles.xml
<style name="AppTheme.AttractionsActivity" parent="Theme.AppCompat.Light.DarkActionBar">
Step 2: Add this in Manifest.Xml
<activity
android:name=".AttractionsActivity"
android:theme="#style/AppTheme.AttractionsActivity"
/>

couldnt save which view has focus?

my app contains "Settings" activity which contains 2 imagebuttons "back" and "about", whenever i press any of these buttons i get:
"couldnt save which view has focus because the focused view android.widget.LinearLayout#4055a9e0 has no id"
the xml code for my_settings:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll2"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="15" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="8dp"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/settings" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|end"
android:orientation="horizontal" >
<ToggleButton
android:id="#+id/tb1"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="#string/sett1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/border2"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/ll_set_back"
android:layout_margin="3dp"
android:gravity="center"
android:layout_weight="1"
android:orientation="vertical" >
**<ImageButton
android:id="#+id/st_ibt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/action_btn"
android:src="#drawable/back_ic" />**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/back"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/ll_set_about"
android:layout_margin="3dp"
android:gravity="center"
android:layout_weight="1"
android:orientation="vertical" >
**<ImageButton
android:id="#+id/st_ibt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/action_btn"
android:src="#drawable/about_ic" />**
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/about"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
dont pay attention to the "scrollview" (i dont think the problem is there), just look at the end of the code for the 2 imagebuttons..
and this the java code for Settings:
public class Settings extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.my_settings);
//code...
ImageButton goBack=(ImageButton)findViewById(R.id.st_ibt1);
ImageButton about=(ImageButton)findViewById(R.id.st_ibt2);
goBack.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Settings.this, MainActivity.class);
startActivity(intent);
//finish();
}
});
about.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent=new Intent(Settings.this, About.class);
startActivity(intent);
//finish();
}
});
}
so, how to fix this? i dont understand what this error means.. any suggestions
I think the problem is :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:orientation="vertical" >
You enable focus to this LinearLayout, but you didn't provide any id for it.

Button inside DrawerLayout is not clickable

I've been searching for a while with this issue and i cant find solution yet.
For my design of DrawerLayout, I will not be using listview but linearlayout as container for my views. I test it first by adding a button inside the linearlayout but i cant click it.
Heres the XML.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/left_drawer"
android:background="#color/white"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="250dp"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_test"
android:text="TEST"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<FrameLayout
android:id="#+id/container_fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
>
</FrameLayout>
In java, I also tried to put text in the button once it is being loaded and its working.The only thing that is not working is when i click it. the drawer will just close.
/**Init Drawer*/
private void initDrawer(){
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerContainer = (LinearLayout) findViewById(R.id.left_drawer);
//mDrawerContainer.setOnClickListener(new DrawerItemClickListener());
mDrawerContainer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"initDrawer",Toast.LENGTH_SHORT).show();
}
});
btn_test = (Button)mDrawerContainer.findViewById(R.id.btn_test);
btn_test.setOnClickListener(new View.OnClickListener() { //this is not working,i cant click the button
#Override
public void onClick(View v) {
Log.e("MyActivity","test");
Toast.makeText(context,"test",Toast.LENGTH_SHORT).show();
}
});
btn_test.setText("hooray"); //this one is working,it change the text to hooray once loaded
}
Im looking forward for your input guys,
Thanks a lot.
Its so ironic, I just change the positions of my layout. I transferred the linearlayout below the fragment and its now working.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container_fragment2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="1dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
>
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer"
android:background="#color/white"
android:orientation="vertical"
android:layout_gravity="start"
android:layout_width="250dp"
android:layout_height="match_parent">
<Button
android:id="#+id/btn_test"
android:text="TEST"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Check this ! reference
you have to set a TouchListener for your drawer layout.
eg/
gestureDetector = new GestureDetector(this, new CustomGestureListener());
mDrawerLayout.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
Remove the OnClickListener that you've set on the DrawerLayout itself.
check it----
cast your button with layout where u keep the button
here is my xml
<RelativeLayout
android:id="#+id/whatYouWantInLeftDrawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#EBF4FA">
<RelativeLayout
android:id="#+id/top_view_rel"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#color/blue_color">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="10dp"
android:visibility="visible">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/drawer_driver_name"
android:textColor="#color/text_color"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_marginLeft="135dp"
android:text="name"
android:textAllCaps="true"
android:textColor="#color/text_color"
android:textSize="16sp"
android:visibility="gone" />
<Button
android:id="#+id/edit"
android:layout_width="30dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:background="#color/colorTransparent"
android:drawableRight="#drawable/edit1"
/>
in activity--
leftDrawerRel = (RelativeLayout)findViewById(R.id.whatYouWantInLeftDrawer);
edit= (Button)leftDrawerRel.findViewById(R.id.edit);
edit.setOnClickListener(this);

How to populate the same view with layout inflater?

I have a layout and every time my button is pressed I want to add a group of views stored in another layout. This is the way I tried it so far. The error in logcat is:
"E/AndroidRuntime(7900): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first."
Code:
material_cost.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/materialCostText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Κόστος Υλικών"
android:textColor="#FF0000"
android:textSize="16sp"
android:textStyle="bold"
android:gravity="center_horizontal"
android:layout_margin="5dp"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="4dp"
android:background="#33B5E5" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/materialWrapper">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp">
<Button
android:id="#+id/btnMaterialSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Αποθήκευση"
android:layout_weight="50"/>
<Button
android:id="#+id/btnMaterialAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Προσθήκη πεδίου"
android:layout_weight="50"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
material_cost_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/materialItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/materialText"
android:text="Υλικό"
android:textSize="16sp"
android:textStyle="italic"
android:layout_weight="50"
android:layout_marginLeft="5dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/materialText"
android:text="Τιμή μονάδας"
android:textSize="16sp"
android:textStyle="italic"
android:layout_weight="50"
android:layout_marginLeft="5dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:id="#+id/materialText"
android:text="Ποσότητα"
android:textSize="16sp"
android:textStyle="italic"
android:layout_weight="50"
android:layout_marginLeft="5dp"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="50"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="4dp"
android:background="#33B5D8" />
</LinearLayout>
</LinearLayout>
And finally my class:
public class MaterialActivity extends Activity implements OnClickListener {
Button btnAdd;
LinearLayout rootLayout;
View layoutItem;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.material_cost);
btnAdd = (Button) findViewById(R.id.btnMaterialAdd);
rootLayout = (LinearLayout) findViewById(R.id.materialWrapper);
layoutItem = getLayoutInflater().inflate(R.layout.material_cost_item, rootLayout,false);
rootLayout.addView(layoutItem);
btnAdd.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.btnMaterialAdd:{
inflateEntry();
break;
}
}
}
private void inflateEntry() {
// TODO Auto-generated method stub
Log.v("debug","pressed");
rootLayout.addView(layoutItem);
}
}
you need to inflate the view again every time you click the button:
private void inflateEntry()
{
// TODO Auto-generated method stub
Log.v("debug","pressed");
View layoutItem = getLayoutInflater().inflate(R.layout.material_cost_item, null);
rootLayout.addView(layoutItem);
}

Categories

Resources