I have a following layout in main_view.xml which contains a ImageButton in it, button when i click/touch the button the ButtonListener function never gets called.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayoutFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/mapArea"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</fragment>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/locate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="#80000000"
android:gravity="center"
android:padding="10dp"
android:text="#string/findingLocation"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<ImageButton
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="#dimen/rightLeftMargin"
android:layout_marginLeft="#dimen/rightLeftMargin"
android:background="#null"
android:clickable="true"
android:contentDescription="#string/button1text"
android:src="#drawable/ic_feed_active" />
<ImageButton
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/rightLeftMargin"
android:background="#null"
android:contentDescription="#string/button2"
android:src="#drawable/ic_button2" />
<ImageButton
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="#dimen/rightLeftMargin"
android:layout_marginRight="#dimen/rightLeftMargin"
android:background="#null"
android:contentDescription="#string/button3View"
android:src="#drawable/ic_settings_active" />
</RelativeLayout>
And following code in MainActivity.Java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
User usrObj = new User();
if (usrObj.authenticateUser())
setContentView(R.layout.main_view);
else
setContentView(R.layout.login);
}
public void ButtonListener() {
ImageButton oggleButton = (ImageButton) findViewById(R.id.Button1);
ToggleButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "ImageButton is clicked!",
Toast.LENGTH_SHORT).show();
}
});
}
}
please help.
you forget to call ButtonListener method when usrObj.authenticateUser() is true. do it as:
if (usrObj.authenticateUser())
{
setContentView(R.layout.main_view);
ButtonListener (); << call here
}
else
setContentView(R.layout.login);
and also attach setOnClickListener to oggleButton instead of ToggleButton
apart from above suggestions from ρяσѕρєя K, here is one more thing you have to take care is.
instead of this below code.
oggleButton.setOnClickListener(new OnClickListener() {
use this one
oggleButton.setOnClickListener(new View.OnClickListener() { // use View.oncli......
Related
The textinputlayout I created is for a login screen, but whenever the activity is launched, everything else shows except for that. Clicking on the space where it should be also does nothing (the keyboard does not come up) so it seems like its visibility has been set to "gone" even though it has not.
Layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/rootView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.thelatinschool.canvasgrades.SplashScreenActivity">
<ImageView
android:id="#+id/logo"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_centerInParent="true"
android:contentDescription="#null"
android:src="#mipmap/ic_launcher_round"
android:visibility="visible" />
<ProgressBar
android:id="#+id/loadingProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="12dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="-4dp"
android:foregroundGravity="bottom"
android:indeterminate="true"
android:padding="0dp"
android:theme="#style/ProgressBarStyle"
android:visibility="visible" />
<RelativeLayout
android:id="#+id/afterAnimationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:layout_marginTop="130dp"
android:layout_marginEnd="20dp"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:id="#+id/WelcomeTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Highlands Latin School"
android:textColor="#color/colorPrimary"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/readItTogetherTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/WelcomeTextView"
android:layout_marginTop="10dp"
android:text="Canvas Grades"
android:textColor="#color/colorAccent"
android:textSize="15sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/loginButton"
android:layout_below="#+id/readItTogetherTextView"
android:gravity="center"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:hint="Email"
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorAccent"
android:textSize="15sp"
app:boxBackgroundMode="outline" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="25dp"
android:hint="Password"
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorAccent"
android:textSize="15sp"
app:boxStrokeColor="#000000" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="5dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center_vertical"
android:background="#FFFFFF"
android:text="Forgot Password?"
android:textColor="#color/colorAccent"
android:textSize="14sp"
android:textStyle="bold" />
</FrameLayout>
</LinearLayout>
<Button
android:id="#+id/loginButton"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_above="#+id/skipTextView"
android:layout_marginBottom="5dp"
android:background="#drawable/button_drawable"
android:text="Login"
android:textAllCaps="false"
android:textColor="#FFFFFF"
android:textSize="16sp" />
<TextView
android:id="#+id/skipTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:padding="12dp"
android:text="Incorrect username or password!"
android:textColor="#B53737"
android:textSize="15sp"
android:visibility="invisible" />
</RelativeLayout>
</RelativeLayout>
And the corresponding java:
public class SplashScreenActivity extends AppCompatActivity {
public String theme1;
private ProgressDialog progressDialog;
private boolean animationStarted = false;
private ImageView bookIconImageView;
private TextView bookITextView;
private ProgressBar loadingProgressBar;
int progress;
private RelativeLayout rootView, afterAnimationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_activity);
bookIconImageView = findViewById(R.id.logo);
loadingProgressBar = (ProgressBar)findViewById(R.id.loadingProgressBar);
rootView = findViewById(R.id.rootView);
afterAnimationView = findViewById(R.id.afterAnimationView);
Thread thred = new Thread(new Runnable() {
#Override
public void run() {
doWork();
startApp();
}
public void doWork() {
for (progress=10;progress<100;progress=progress+10){
try {
Thread.sleep(350);
loadingProgressBar.setProgress(progress);
} catch (InterruptedException e) {
e.printStackTrace();
}} }
public void startApp(){
runOnUiThread(new Runnable() {
#Override
public void run() {
loadingProgressBar.setVisibility(GONE);
rootView.setBackgroundColor(ContextCompat.getColor(SplashScreenActivity.this, R.color.splashscreen));
bookIconImageView.setImageResource(R.mipmap.ic_launcher_round);
}
});
startAnimation();
};
});
thred.start();
}
private void startAnimation() {
ViewPropertyAnimator viewPropertyAnimator = bookIconImageView.animate();
viewPropertyAnimator.x(50f);
viewPropertyAnimator.y(100f);
viewPropertyAnimator.setDuration(1000);
viewPropertyAnimator.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
afterAnimationView.setVisibility(VISIBLE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
// Intent myIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
// SplashScreenActivity.this.startActivity(myIntent);
}
I have an animation for the splash screen that displays right before the login screen, I haven't touched anything to do with the text input box in java but maybe I made a mistake somewhere there.
Most probably you miss adding app level material design dependency
// material Design support library - androidx
implementation 'com.google.android.material:material:1.0.0'
// material Design support library - support library
implementation 'com.android.support:design:28.0.0'
UPDATE
You're missing adding TextInputEditText within TextInputLayout, so you won't expect to see something, also move android:hint, android:textSize, android:textColorHint & android:textColor attributes into TextInputEditText instead of TextInputLayout
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="50dp"
app:boxBackgroundMode="outline">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:textColor="#color/colorPrimary"
android:textColorHint="#color/colorAccent"
android:textSize="15sp" />
</com.google.android.material.textfield.TextInputLayout>
I have problem with drawerLayout. in drawerLayout, i have two Linearlayouts. one is the main menu that shows directly, the otherone is active with drawer slide. when i slide the drawer menü i cant click the buttons. but when i remove the second linearLayout then the buttons in the drawer work. how can i solve it?
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=""
android:clickable="true"
android:contextClickable="true">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="left|start"
android:background="#color/list_back"
android:layout_width="250dp"
android:layout_height="match_parent"
android:onClick="setContentView"
android:contextClickable="true"
android:clickable="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ana Sayfa"
android:id="#+id/buttonAnaSayfa"
android:onClick="setContentView"
android:clickable="false"
android:contextClickable="false"
android:nestedScrollingEnabled="false" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kalori Tablosu"
android:id="#+id/buttonKalori"
android:onClick="setContentView"
android:clickable="true"
android:contextClickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Glisemik İndeks Tablosu"
android:id="#+id/buttonGITablosu"
android:onClick="setContentView"
android:clickable="true"
android:contextClickable="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vücut Kütle İndeksi"
android:id="#+id/buttonVucutKutle"
android:onClick="setContentView"
android:clickable="true"
android:contextClickable="true" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="center">
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonKaloriListesi"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/kalori_listesi_button"
android:background="#null"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonGITablosu"
android:src="#drawable/gi_tablosu_button"
android:layout_below="#+id/imageButtonKaloriListesi"
android:layout_alignParentStart="true"
android:background="#null"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonBugunKaloriHesabi"
android:src="#drawable/bugun_kac_kalori_aldin_button"
android:background="#null"
android:layout_above="#+id/imageButtonVucutKutleIndeks"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonVucutKutleIndeks"
android:src="#drawable/vucut_kutle_indeksi_button"
android:background="#null"
android:layout_centerVertical="true"
android:layout_alignStart="#+id/imageButtonBugunKaloriHesabi"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"
android:id="#+id/buttontest1"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_gravity="center_horizontal"
android:id="#+id/ad"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"></LinearLayout>
</LinearLayout>
here is my .java ;
package com.azelirbrevo.glisemikindeks
public class "" extends AppCompatActivity {
//DrawerLayout drawerLayout;
//ActionBarDrawerToggle toggle;
//CharSequence actionBarTitle, appTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstaftersplash);
ImageButton ImageButtonKaloriListesi = (ImageButton) findViewById(R.id.imageButtonKaloriListesi);
ImageButton imageButtonGITablosu = (ImageButton) findViewById(R.id.imageButtonGITablosu);
ImageButton imageButtonVucutKutleIndeks = (ImageButton) findViewById(R.id.imageButtonVucutKutleIndeks);
ImageButton imageButtonBugunKaloriHesabi = (ImageButton) findViewById(R.id.imageButtonBugunKaloriHesabi);
ImageButtonKaloriListesi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(firstAfterSplash.this, KaloriListesi.class);
startActivity(intent);
}
});
imageButtonGITablosu.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(firstAfterSplash.this, GITablosu.class);
startActivity(intent);
}
});
imageButtonVucutKutleIndeks.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(firstAfterSplash.this, VucutKutleIndexHesaplama.class);
startActivity(intent);
}
});
imageButtonBugunKaloriHesabi.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(firstAfterSplash.this, bugunKacKaloriAldim.class);
startActivity(intent);
}
});
//drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
//actionBarTitle = appTitle = getSupportActionBar().getTitle();
Button buttonAnasayfa = (Button) findViewById(R.id.buttonAnaSayfa);
Button buttonKalori = (Button) findViewById(R.id.buttonKalori);
Button buttonGı = (Button) findViewById(R.id.buttonGITablosu);
Button buttonVucutKutle = (Button) findViewById(R.id.buttonVucutKutle);
Button test = (Button) findViewById(R.id.buttontest1);
test.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(firstAfterSplash.this, TEST.class);
startActivity(intent);
}
});
buttonAnasayfa.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "tiklandi", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(firstAfterSplash.this, GITablosu.class);
startActivity(intent);
}
});
buttonKalori.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "tiklandi", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(firstAfterSplash.this, GITablosu.class);
startActivity(intent);
}
});
buttonGı.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "tiklandi", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(firstAfterSplash.this, GITablosu.class);
startActivity(intent);
}
});
buttonVucutKutle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "tiklandi", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(firstAfterSplash.this, GITablosu.class);
startActivity(intent);
}
});
}}
this is the design
You can create a application with Navigation drawer Activity here you can see custom layout inside drawer layout and inside layout you can set any type of widgets as button, TextView and any type what ever you want.
In xml file, my drawer menu items (LinearLayout and Buttons) were over my main LinearLayout. I just cut-paste it under the main LinearLayout and it helped.
here is the updated xml file of my project, hope will help someone.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=""
android:clickable="true"
android:contextClickable="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foregroundGravity="center">
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonKaloriListesi"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/kalori_listesi_button"
android:background="#null"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonGITablosu"
android:src="#drawable/gi_tablosu_button"
android:layout_below="#+id/imageButtonKaloriListesi"
android:layout_alignParentStart="true"
android:background="#null"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonBugunKaloriHesabi"
android:src="#drawable/bugun_kac_kalori_aldin_button"
android:background="#null"
android:layout_above="#+id/imageButtonVucutKutleIndeks"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<ImageButton
android:layout_width="360dp"
android:layout_height="wrap_content"
android:id="#+id/imageButtonVucutKutleIndeks"
android:src="#drawable/vucut_kutle_indeksi_button"
android:background="#null"
android:layout_centerVertical="true"
android:layout_alignStart="#+id/imageButtonBugunKaloriHesabi"
android:layout_marginTop="10dp"
android:foregroundGravity="center"
android:gravity="center"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_gravity="center_horizontal"
android:id="#+id/ad"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"></LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_gravity="left|start"
android:layout_width="250dp"
android:layout_height="match_parent"
android:onClick="setContentView"
android:contextClickable="true"
android:clickable="true"
android:background="#85c7f1">
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/ButtonDrawerAnaSayfa"
android:background="#null"
android:layout_gravity="left"
android:gravity="left"/>
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/ButtonDrawerKalori"
android:background="#null"
android:layout_gravity="left"
android:gravity="left"/>
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/ButtonDrawerGI"
android:background="#null"
android:layout_gravity="left"
android:gravity="left"/>
<Button
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/ButtonDrawerVucutKutle"
android:background="#null"
android:layout_gravity="left"
android:gravity="left"/>
</LinearLayout>
public class AddActivity extends Activity implements OnClickListener{
String[] info = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
ImageButton submit = (ImageButton)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
submit.setOnClickListener((OnClickListener) this);
add1.setOnClickListener((OnClickListener) this);
add2.setOnClickListener((OnClickListener) this);
add3.setOnClickListener((OnClickListener) this);
int id = v.getId();
switch(id){
case R.id.submit:{
submitEntry(info);
break;
}
case R.id.add1:{
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
break;
}
}
}
}
This is the code.
<?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" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/key_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<EditText
android:id="#+id/keyString"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/site_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new" />
<EditText
android:id="#+id/site1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="invisible" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site3"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/submit_buttom" />
</LinearLayout>
</ScrollView>
</LinearLayout>
And this is the XML. The add1, add2, add3 and submit ImageButtons are all in a ScrollView.
When I press the add1 ImageButton, I want the add2 and site2 ImageButtons to become visible but instead, it throws the following error.
Motion event has invalid pointer count 0; value must be between 1 and 16.
What am I doing wrong?
PS: All the findViewById() calls are in the onClick() method because a NullPointerExeption is thrown if I call them in the onCreate().
Those findViewByIdcalls in onClickdon't make sense. Not sure why you are getting a null pointer exception calling them in onCreate.onClick is never called in this instance because nothing in the creation of the Activity is assigning the buttons to look at your onClick method; the buttons will default to having no listener assigned. It also doesn't look like a good idea to use the Activity as the onClickListener as well.
Your code should look something like this:
public class AddActivity extends Activity {
// https://source.android.com/source/code-style.html
// info -> mInfo; non-public, non-static field!
String[] mInfo = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
Button submit = (Button)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
add1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
}
});
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submitEntry(mInfo);
}
});
}
The findViewById() calls and especially the setOnClickListener() calls should have been inside the onCreate(). With setOnClickListener() inside the onClick() i doubt the onClick was ever called.
We would need more logs to find the exact issue.
I have several Views, text views, and a button that have the android:visibility="invisible" attribute. My goal is to click a button that resides above these 'invisible' widgets, so that these widgets will become visible. I created another java class called 'VisibilityActivity.java" and tried the following method. But for some reason when I run the app, the button doesn't do anything. I don't know what I'm missing.
Here's the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class VisibilityActivity extends Activity {
private View mVictim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mVictim = findViewById(R.id.horizontalRule1);
mVictim = findViewById(R.id.TextView03);
mVictim = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_background"
android:isScrollContainer="true"
android:orientation="vertical"
android:paddingTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/ratingsInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/yourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp" />
<Button
android:id="#+id/submitRatingButton"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#drawable/custom_button"
android:text="#string/submitRating"
android:textColor="#color/black1" />
<View
android:id="#+id/horizontalRule1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/summaryInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1"
android:visibility="invisible" />
<View
android:id="#+id/horizontalRule2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/ourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="10dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/overallRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar3"
android:color="#color/black1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="40dp"
android:visibility="invisible" />
<Button
android:id="#+id/saveContinueButton3"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:background="#drawable/custom_button"
android:text="#string/saveContinue"
android:textColor="#color/black1"
android:onClick="onSaveAndContinue3Clicked"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Thanks. Help would be greatly appreciated!
I am updating user936414's answer.
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
if( mText.getVisibility() == View.INVISIBLE )
mText.setVisibility(View.VISIBLE);
else
mText.setVisibility(View.INVISIBLE);
if( mRule1.getVisibility() == View.INVISIBLE )
mRule1.setVisibility(View.VISIBLE);
else
mRule1.setVisibility(View.INVISIBLE);
if( mRule2.getVisibility() == View.INVISIBLE )
mRule2.setVisibility(View.VISIBLE);
else
mRule2.setVisibility(View.INVISIBLE);
}
};
Also you might want to experiment with View.GONE.
findViewById(R.id.ratingBar3).setVisibility(View.VISIBLE);
findViewById(R.id.saveContinueButton3).setVisibility(View.VISIBLE);
you made it invisible view invisible again.. try the above code
Try
public class VisibilityActivity extends Activity {
private TextView mText;
private View mRule1, mRule2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mText= (TextView)findViewById(R.id.horizontalRule1);
mRule1 = findViewById(R.id.TextView03);
mRule2 = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mText.setVisibility(View.VISIBLE);
mRule1.setVisibility(View.VISIBLE);
mRule2.setVisibility(View.VISIBLE);
}
};
}
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(new View.onClickListener)
{
#Override
public void onClick(View v)
{
//Insert your code here
}
}
I'd like to create a menu for my widget configuration similar like GWT Stack Panel
Is it posible to create similar type of Menu in android.? Any Help or suggestions are welcome.
use this layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Bt1"
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearLayout1">
<ImageView
android:layout_width="wrap_content"
android:id="#+id/imageView1"
android:layout_height="wrap_content"
android:src="#drawable/icon"></ImageView>
</LinearLayout>
<Button
android:text="Bt2"
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearLayout2">
<ImageView
android:layout_width="wrap_content"
android:id="#+id/imageView2"
android:layout_height="wrap_content"
android:src="#drawable/icon"></ImageView>
</LinearLayout>
<Button
android:text="Bt3"
android:id="#+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></Button>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/linearLayout3">
<ImageView
android:layout_width="wrap_content"
android:id="#+id/imageView3"
android:layout_height="wrap_content"
android:src="#drawable/icon"></ImageView>
</LinearLayout>
</LinearLayout>
and use this code :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.linearLayout1).setVisibility(View.GONE);
findViewById(R.id.linearLayout2).setVisibility(View.GONE);
findViewById(R.id.linearLayout3).setVisibility(View.GONE);
Button bt1=(Button) findViewById(R.id.button1);
Button bt2=(Button) findViewById(R.id.button2);
Button bt3=(Button) findViewById(R.id.button3);
bt1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.linearLayout1).setVisibility(View.VISIBLE);
findViewById(R.id.linearLayout2).setVisibility(View.GONE);
findViewById(R.id.linearLayout3).setVisibility(View.GONE);
}
});
bt2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.linearLayout1).setVisibility(View.GONE);
findViewById(R.id.linearLayout2).setVisibility(View.VISIBLE);
findViewById(R.id.linearLayout3).setVisibility(View.GONE);
}
});
bt3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.linearLayout1).setVisibility(View.GONE);
findViewById(R.id.linearLayout2).setVisibility(View.GONE);
findViewById(R.id.linearLayout3).setVisibility(View.VISIBLE);
}
});
}
you can use your layout in the linearlayout1,linearlayout2 and linearlayout3 for build stack panel for android.