Android popup window - back button - java

I'm making application where I'm trying to add custom popup window with ability to close it when I click back button. The problem is I don't know how to add this back button. I'm attaching a file which shows what I have now and want I want to achieve.
I'm making application where I'm trying to add custom popup window with ability to close it when I click back button. The problem is I don't know how to add this back button. I'm attaching a file which shows what I have now and want I want to achieve.
image
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.activity_transaction, null);
final PopupWindow popupWindow = new PopupWindow(popupView, 565, 760, true);
popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
Toolbar toolbar = popupView.findViewById(R.id.tool_bar);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setTitle("New transaction");
int currentDay = 1;
int currentMonth = Integer.parseInt(actualChosenMonth.getText().toString().substring(0, 2)) - 1;
int currentYear = Integer.parseInt(actualChosenMonth.getText().toString().substring(3, 7));
SimpleDateFormat simpledateformat = new SimpleDateFormat("EE");
Date date = new Date(currentYear, currentMonth, currentDay - 1);
String currentDayOfWeek = simpledateformat.format(date);
String currentDateAsString = convertDateToString(currentDay, currentMonth, currentYear, currentDayOfWeek);
chooseTransactionDate = popupView.findViewById(R.id.chooseTransactionDate);
chooseTransactionDate.setText(currentDateAsString);
chooseTransactionDate.setOnClickListener(this);
chooseTodayTomorrow = popupView.findViewById(R.id.chooseTodayTommorow);
chooseTodayTomorrow.setOnClickListener(this);
chooseTransactionType = popupView.findViewById(R.id.chooseTransactionType);
chooseTransactionType.setOnCheckedChangeListener(this);
chosenTransactionExpense = popupView.findViewById(R.id.chosenTransactionExpense);
chosenTransactionIncome = popupView.findViewById(R.id.chosenTransactionIncome);
transactionCategoryImage = popupView.findViewById(R.id.transactionCategoryImage);
chooseTransactionCategory = popupView.findViewById(R.id.chooseTransactionCategory);
ArrayAdapter<String> chooseCategoryAdapter = new ArrayAdapter<>(this.getActivity(), R.layout.popup_spinner_layout, getResources().getStringArray(R.array.list_of_expenses));
chooseTransactionCategory.setAdapter(chooseCategoryAdapter);
chooseTransactionCategory.setOnItemSelectedListener(this);
chooseTransactionAmount = popupView.findViewById(R.id.chooseTransactionAmount);
chooseTransactionAmount.requestFocus();
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
chooseTransactionName = popupView.findViewById(R.id.chooseTransactionName);
saveTransactionButton = popupView.findViewById(R.id.saveTransactionButton);
saveTransactionButton.setOnClickListener(this);
LAYOUT
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bacgroundColorPopup"
tools:context=".TransactionActivity">
<Button
android:id="#+id/saveTransactionButton"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="#+id/chooseTransactionName"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/rounded_button"
android:text="#string/save_transaction"
android:textSize="15sp" />
<EditText
android:id="#+id/chooseTransactionAmount"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="#id/transactionCategoryImage"
android:layout_alignParentLeft="true"
android:textColor="#color/BrownGrey"
android:paddingLeft="10dp"
android:hint="Amount"
android:maxLength="5"
android:textColorHint="#color/BrownGreyBrighter"
android:inputType="numberDecimal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/custom_edit_text"
android:drawableRight="#drawable/calculator"
android:textSize="20sp" />
<EditText
android:id="#+id/chooseTransactionName"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_below="#id/chooseTransactionAmount"
android:layout_alignParentLeft="true"
android:textColor="#color/BrownGrey"
android:layout_marginTop="6dp"
android:layout_marginLeft="10dp"
android:paddingLeft="10dp"
android:hint="Notes"
android:textColorHint="#color/BrownGreyBrighter"
android:layout_marginRight="10dp"
android:background="#drawable/custom_edit_text"
android:drawableRight="#drawable/calculator"
android:textSize="20sp" />
<ImageView
android:id="#+id/transactionCategoryImage"
android:layout_margin="6dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#id/chooseTransactionType"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp" />
<LinearLayout
android:id="#+id/dateButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<Button
android:id="#+id/chooseTransactionDate"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="62dp"
android:layout_marginRight="2dp"
android:layout_weight="1.6"
android:background="#drawable/rounded_button"
android:elevation="4dp"
android:padding="7dp"
android:text=""
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />
<Button
android:id="#+id/chooseTodayTommorow"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="62dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#drawable/rounded_button"
android:elevation="4dp"
android:padding="10dp"
android:text="#string/yesterday"
android:textAllCaps="false"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
<RadioGroup
android:id="#+id/chooseTransactionType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/dateButtons"
android:orientation="horizontal">
<RadioButton
android:id="#+id/chosenTransactionExpense"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#drawable/radio_selector"
android:button="#android:color/transparent"
android:checked="true"
android:elevation="4dp"
android:gravity="center"
android:padding="7dp"
android:text="#string/expense"
android:textColor="#drawable/text_color"
android:textSize="16sp" />
<RadioButton
android:id="#+id/chosenTransactionIncome"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#drawable/radio_selector"
android:button="#android:color/transparent"
android:elevation="4dp"
android:gravity="center"
android:padding="7dp"
android:text="#string/income"
android:textColor="#drawable/text_color"
android:textSize="16sp" />
</RadioGroup>
<Spinner
android:id="#+id/chooseTransactionCategory"
android:popupBackground="#color/bacgroundColorPopup"
android:layout_margin="6dp"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_below="#+id/chooseTransactionType"
android:layout_alignParentRight="true"
android:textAlignment="textEnd" />
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar" />
</RelativeLayout>

Related

FATAL EXCEPTION : main (Android std)

While I run this on my device it will crash and my logcat is as below:
2020-12-19 17:03:28.204 24798-24798/com.example.emergencynotificationhealthcare E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.emergencynotificationhealthcare, PID: 24798
java.lang.RuntimeException: Unable to start activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2799)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1573)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6605)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:999)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:889)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.emergencynotificationhealthcare.Login.onCreate(Login.java:42)
at android.app.Activity.performCreate(Activity.java:6964)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
Here is my activity_register.xml:
<ImageView
android:layout_width="101dp"
android:layout_height="69dp"
android:transitionName="logo_logo"
android:src="#drawable/logo_new"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
android:fontFamily="#font/bungee"
android:text="Welcome"
android:transitionName="logo_text"
android:textColor="#ff8e88"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register to start"
android:textSize="16sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
app:counterEnabled="true"
app:counterMaxLength="15"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:transitionName="user_tran"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/phoneNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:passwordToggleEnabled="true"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:transitionName="password_tran"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Type"
android:layout_marginLeft="5dp"
android:textSize="17dp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Guardian"/>
<Button
android:id="#+id/login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#ff8e88"
android:text="Register"
android:textColor="#fff"
android:transitionName="button_tran"/>
<Button
android:id="#+id/register_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Already have an account? LOGIN"
android:background="#00000000"
android:transitionName="login_register_tran"/>
Here is my Login.java:
Button callRegister, login_btn;
ImageView image;
TextView logoText, sloganText;
TextInputLayout username,password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
callRegister = findViewById(R.id.register_screen);
image = findViewById(R.id.imageView);
logoText = findViewById(R.id.logo_name);
sloganText = findViewById(R.id.slogan_name);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
login_btn = findViewById(R.id.login_btn);
callRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Login.this,Register.class);
Pair[] pairs = new Pair[7];
pairs[0] = new Pair<View,String>(image,"logo_logo");
pairs[1] = new Pair<View,String>(logoText,"logo_text");
pairs[2] = new Pair<View,String>(sloganText,"logo_desc");
pairs[3] = new Pair<View,String>(username,"user_tran");
pairs[4] = new Pair<View,String>(password,"password_tran");
pairs[5] = new Pair<View,String>(login_btn,"button_tran");
pairs[6] = new Pair<View,String>(callRegister,"login_register_tran");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(Login.this,pairs);
startActivity(intent, options.toBundle());
}
}
});
}
Here is my activity_login.xml
<ImageView
android:layout_width="101dp"
android:layout_height="69dp"
android:src="#drawable/logo_new"
android:transitionName="logo_logo"/>
<TextView
android:id="#+id/logo_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/bungee"
android:text="Hi, Welcome Back!"
android:transitionName="logo_text"
android:textColor="#ff8e88"
android:textSize="40sp"/>
<TextView
android:id="#+id/slogan_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign in to continue"
android:transitionName="logo_desc"
android:textSize="18sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/username"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/password"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
style="#style/Widget.AppCompat.CompoundButton.CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Me" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="3dp"
android:background="#00000000"
android:elevation="0dp"
android:text="Forget Password" />
<Button
android:id="#+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#ff8e88"
android:text="Log In"
android:textColor="#fff" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="5dp"
android:background="#00000000"
android:elevation="0dp"
android:fontFamily="#font/antic"
android:text="New Member? REGISTER"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
Did you inflate the correct layout file? activity_register is the name of the file you said, but your code uses
setContentView(R.layout.activity_login);.
Try replacing that with setContentView(R.layout.activity_register); if that's not the case.

Showing null pointer exception even though no error is showing up [duplicate]

This question already has answers here:
What is a stack trace, and how can I use it to debug my application errors?
(7 answers)
Closed 4 years ago.
This is the code MainActivity.java file. I think there should be no mistake in this code. But it shows the message
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference.
I don't know why it is showing up there? Please help me to figure out.
package com.example.android.practiceset2;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.*;
import android.view.*;`
public class MainActivity extends AppCompatActivity {
int score=0,wickets=0;
String value=null,number=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Button b1=(Button)findViewById(R.id.b_i1);//b_i1 stands for increase in 1 run taken by team B and similarly other names are to the ids of different button.
Button b2=(Button)findViewById(R.id.b_i2);
Button b3=(Button)findViewById(R.id.b_i3);
Button b4=(Button)findViewById(R.id.b_i4);
Button b6=(Button)findViewById(R.id.b_i6);
Button bw=(Button)findViewById(R.id.b_w);
Button bwide=(Button)findViewById(R.id.b_wide);
Button a1=(Button)findViewById(R.id.a_i1);
Button a2=(Button)findViewById(R.id.a_i2);
Button a3=(Button)findViewById(R.id.a_i3);
Button a4=(Button)findViewById(R.id.a_i4);
Button a6=(Button)findViewById(R.id.a_i6);
Button aw=(Button)findViewById(R.id.a_w);
Button awide=(Button)findViewById(R.id.a_wide);
TextView ta= (TextView)findViewById(R.id.a);
TextView tb= (TextView)findViewById(R.id.b);
public void aorb(View view{
if(getResources().getResourceEntryName((view.getId())).startsWith("a"))
{ value="a";}
else{value="b";}
number=getResources().getResourceEntryName((view.getId())).substring(2);
score();}
public void a_enabled(Boolean b)
{
a1.setEnabled(b);
a2.setEnabled(b);
a3.setEnabled(b);
a4.setEnabled(b);
a6.setEnabled(b);
aw.setEnabled(b);
awide.setEnabled(b);
}
public void b_enabled(Boolean b)
{
b1.setEnabled(b);
b2.setEnabled(b);
b3.setEnabled(b);
b4.setEnabled(b);
b6.setEnabled(b);
bw.setEnabled(b);
bwide.setEnabled(b);
}
public void display(String s){
if(value.equals("a"))
{
ta.setText(s);
b_enabled(false);
a_enabled(true);
}
else {
tb.setText(s);
a_enabled(false);
b_enabled(true);
}
}
public void score(){
int i=0;
if(number.equals("i1"))
i=1;
if(number.equals("i2"))
i=2;
if(number.equals("i3"))
i=3;
if(number.equals("i4"))
i=4;
if(number.equals("i6"))
i=6;
if(number.equals("wide"))
i=1;
score+=i;
if(number.equals("w"))
wickets+=1;
if(wickets==10)//if a team loses all its 10 wickets , the score is finalised and next team is invited for play
{
display(score+"/"+wickets);
if(value.equals("a"))
{
value="b";
}
else{value="a";}
score=0;
wickets=0;
display(score+"/"+wickets);
}
display(score+"/"+wickets);
}
public void reset(View view){
ta.setText("0/0");
tb.setText("0/0");
a_enabled(true);
b_enabled(true);
}}
This is the code for my content_main.xml file.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="horizontal"
android:background="#FFFFFF">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team A"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:textColor="#616161"
android:layout_margin="16dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="0/0"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:textSize="56dp"
android:textColor="#000000"
android:id="#+id/a"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="aorb"
android:id="#+id/a_i1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:onClick="aorb"
android:id="#+id/a_i3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="aorb"
android:id="#+id/a_i6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="aorb"
android:id="#+id/a_i2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="aorb"
android:id="#+id/a_i4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out"
android:onClick="aorb"
android:id="#+id/a_w"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="Wide"
android:onClick="aorb"
android:id="#+id/a_wide"/>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team B"
android:textAlignment="center"
android:fontFamily="sans-serif-medium"
android:textSize="20sp"
android:textColor="#616161"
android:layout_margin="16dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:text="0/0"
android:textAlignment="center"
android:fontFamily="sans-serif-light"
android:textSize="56dp"
android:textColor="#000000"
android:id="#+id/b"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="aorb"
android:id="#+id/b_i1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:onClick="aorb"
android:id="#+id/b_i3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
android:onClick="aorb"
android:id="#+id/b_i6"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="aorb"
android:id="#+id/b_i2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:onClick="aorb"
android:id="#+id/b_i4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Out"
android:onClick="aorb"
android:id="#+id/b_w"
/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:text="Wide"
android:onClick="aorb"
android:id="#+id/b_wide"
/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:onClick="reset"
android:layout_marginBottom="32dp"
/>
</RelativeLayout>
Here is the image of it should look like it is the screenshot the preview
All these findViewById must be included in your onCreate() method. You can keep the declarations out of onCreate(). Your views do not exist before setContentView(R.layout.activity_main);
I mean before onCreate() you can have Button b1;
and after setContentView(R.layout.activity_main); in onCreate() you must have b1=(Button)findViewById(R.id.b_i1);
for every view.
I have made the corrections in your and now the app is not crashing. Try using the below code snippet
MainActivity
public class MainActivity extends AppCompatActivity {
int score = 0, wickets = 0;
String value = null, number = null;
Button b1;
Button b2;
Button b3;
Button b4;
Button b6;
Button bw;
Button bwide;
Button a1;
Button a2;
Button a3;
Button a4;
Button a6;
Button aw;
Button awide;
TextView ta;
TextView tb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
b1 = (Button) findViewById(R.id.b_i1);//b_i1 stands for increase in 1 run taken by team B and similarly other names are to the ids of different button.
b2 = (Button) findViewById(R.id.b_i2);
b3 = (Button) findViewById(R.id.b_i3);
b4 = (Button) findViewById(R.id.b_i4);
b6 = (Button) findViewById(R.id.b_i6);
bw = (Button) findViewById(R.id.b_w);
bwide = (Button) findViewById(R.id.b_wide);
a1 = (Button) findViewById(R.id.a_i1);
a2 = (Button) findViewById(R.id.a_i2);
a3 = (Button) findViewById(R.id.a_i3);
a4 = (Button) findViewById(R.id.a_i4);
a6 = (Button) findViewById(R.id.a_i6);
aw = (Button) findViewById(R.id.a_w);
awide = (Button) findViewById(R.id.a_wide);
ta = (TextView) findViewById(R.id.a);
tb = (TextView) findViewById(R.id.b);
}
public void aorb(View view) {
if (getResources().getResourceEntryName((view.getId())).startsWith("a")) {
value = "a";
} else {
value = "b";
}
number = getResources().getResourceEntryName((view.getId())).substring(2);
score();
}
public void a_enabled(Boolean b) {
a1.setEnabled(b);
a2.setEnabled(b);
a3.setEnabled(b);
a4.setEnabled(b);
a6.setEnabled(b);
aw.setEnabled(b);
awide.setEnabled(b);
}
public void b_enabled(Boolean b) {
b1.setEnabled(b);
b2.setEnabled(b);
b3.setEnabled(b);
b4.setEnabled(b);
b6.setEnabled(b);
bw.setEnabled(b);
bwide.setEnabled(b);
}
public void display(String s) {
if (value.equals("a")) {
ta.setText(s);
b_enabled(false);
a_enabled(true);
} else {
tb.setText(s);
a_enabled(false);
b_enabled(true);
}
}
public void score() {
int i = 0;
if (number.equals("i1"))
i = 1;
if (number.equals("i2"))
i = 2;
if (number.equals("i3"))
i = 3;
if (number.equals("i4"))
i = 4;
if (number.equals("i6"))
i = 6;
if (number.equals("wide"))
i = 1;
score += i;
if (number.equals("w"))
wickets += 1;
if (wickets == 10)//if a team loses all its 10 wickets , the score is finalised and next team is invited for play
{
display(score + "/" + wickets);
if (value.equals("a")) {
value = "b";
} else {
value = "a";
}
score = 0;
wickets = 0;
display(score + "/" + wickets);
}
display(score + "/" + wickets);
}
public void reset(View view) {
ta.setText("0/0");
tb.setText("0/0");
a_enabled(true);
b_enabled(true);
}
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif-medium"
android:text="Team A"
android:textAlignment="center"
android:textColor="#616161"
android:textSize="20sp" />
<TextView
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="sans-serif-light"
android:text="0/0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="56dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/a_i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="1" />
<Button
android:id="#+id/a_i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="3" />
<Button
android:id="#+id/a_i6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/a_i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="2" />
<Button
android:id="#+id/a_i4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="4" />
<Button
android:id="#+id/a_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="Out" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/a_wide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="aorb"
android:text="Wide"
android:textAlignment="center" />
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"></View>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:fontFamily="sans-serif-medium"
android:text="Team B"
android:textAlignment="center"
android:textColor="#616161"
android:textSize="20sp" />
<TextView
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:fontFamily="sans-serif-light"
android:text="0/0"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="56dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/b_i1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="1" />
<Button
android:id="#+id/b_i3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="3" />
<Button
android:id="#+id/b_i6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="#+id/b_i2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="2" />
<Button
android:id="#+id/b_i4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="4" />
<Button
android:id="#+id/b_w"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="aorb"
android:text="Out" />
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/b_wide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="aorb"
android:text="Wide"
android:textAlignment="center" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"
android:onClick="reset"
android:text="Reset"
android:textAlignment="center" />
</RelativeLayout>
</RelativeLayout>
Hope this helps.

Horizontally Scrolling Text Performance Issues

My app contains a few textViews which are supposed to be scrolling horizontally. This works when my layout is first loaded but after clicking on a button to load another layout and then re-click on that button to load back the first layout those textViews start scrolling after a "big" delay(like 20 secs+). I'm trying to figure out the source of this problem with the Layout Inspector, Hierarchy Viewer etc. but no luck.
UPDATE: I noticed something strange today.When i go from the 1st layout to the 2nd one and vice versa, although the TextViews stop scrolling, if i close and re-open the phone's screen, the scrolling works like a charm. This seems totally strange to me, do you know what's causing this and why?
CardViewActivity.java:
public class CardViewActivity extends AppCompatActivity {
private ImageView cardArtImageView;
private TextView leaderSkillDescText;
private TextView superAttackTitleText;
private TextView superAttackDescText;
private TextView passiveSkillTitleText;
private TextView passiveSkillDescText;
private TextView hpText;
private TextView attText;
private TextView defText;
private TextView costText;
private Button arrowButton;
private int selectedItemPosition;
private boolean isBtnClicked = false;
// Listener member field for each layout's button. This listener will be used recursively
private View.OnClickListener arrowButtonListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
// When the arrowButton is clicked, choose the right layout based on the button's state
int resID = isBtnClicked ? R.layout.cardview_refined : R.layout.cardview_expand_details;
setContentView(resID);
// If we're in the first layout, initialize the cardArtImageView field
if(isBtnClicked) {
cardArtImageView = findViewById(R.id.cardArtImageView);
}
viewDefinitions(!isBtnClicked);
initCardViewData(selectedItemPosition);
setSelectedViewsInit();
// Set the arrowButton's listener to this listener (recursively)
arrowButton.setOnClickListener(arrowButtonListener);
// toggle our flag field
isBtnClicked = !isBtnClicked;
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardview_refined);
// Retrieving the data sent over from MainActivity
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
if (bundle != null) {
selectedItemPosition = bundle.getInt("Card Index");
}
//Toast.makeText(this, "WIDTH: " + SCREEN_WIDTH, Toast.LENGTH_SHORT).show();
// Initializing our views
cardArtImageView = findViewById(R.id.cardArtImageView);
viewDefinitions(false);
setSelectedViewsInit();
initCardViewData(selectedItemPosition);
arrowButton.setOnClickListener(arrowButtonListener);
}
/**
* Sets the required textViews as selected to allow automatic scrolling
*/
private void setSelectedViewsInit() {
leaderSkillDescText.setSelected(true);
superAttackTitleText.setSelected(true);
superAttackDescText.setSelected(true);
if (passiveSkillTitleText != null && passiveSkillDescText != null) {
passiveSkillTitleText.setSelected(true);
passiveSkillDescText.setSelected(true);
}
}
/**
* Adds the views's definitions
*
* #param initPassiveInfo used to decide whether or not the passiveSkillDesc & ..Title != null
* so that they can be defined
*/
private void viewDefinitions(boolean initPassiveInfo) {
leaderSkillDescText = findViewById(R.id.leaderSkillDesc);
superAttackTitleText = findViewById(R.id.superAttackTitle);
superAttackDescText = findViewById(R.id.superAttackDesc);
if (initPassiveInfo) {
passiveSkillTitleText = findViewById(R.id.passiveSkillTitle);
passiveSkillDescText = findViewById(R.id.passiveSkillDesc);
} else {
Log.d("Definitions", "Passive info == null");
}
hpText = findViewById(R.id.HP);
attText = findViewById(R.id.ATT);
defText = findViewById(R.id.DEF);
costText = findViewById(R.id.COST);
arrowButton = findViewById(R.id.arrowButton);
}
/**
* Initialize the cardViewActivity's views with the data from the CardInfoDatabase.java class
*
* #param selectedItemPosition Used to initialize this activity's views if the intent was called from the MainScreen Fragment
*/
private void initCardViewData(int selectedItemPosition) {
if (cardArtImageView != null) {
cardArtImageView.setImageResource(CardInfoDatabase.cardArts[selectedItemPosition]);
}
leaderSkillDescText.setText(CardInfoDatabase.leaderSkills[selectedItemPosition]);
superAttackTitleText.setText(CardInfoDatabase.superAttacksName[selectedItemPosition]);
superAttackDescText.setText(CardInfoDatabase.superAttacksDesc[selectedItemPosition]);
if (passiveSkillTitleText != null && passiveSkillDescText != null) {
passiveSkillTitleText.setText(CardInfoDatabase.passiveSkillsName[selectedItemPosition]);
passiveSkillDescText.setText(CardInfoDatabase.passiveSkillsDesc[selectedItemPosition]);
}
hpText.setText(CardInfoDatabase.hp[selectedItemPosition].toString());
attText.setText(CardInfoDatabase.att[selectedItemPosition].toString());
defText.setText(CardInfoDatabase.def[selectedItemPosition].toString());
costText.setText(CardInfoDatabase.cost[selectedItemPosition].toString());
}
}
firstLayout.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"
android:orientation="vertical"
android:background="#drawable/card_view_bg"
tools:layout_editor_absoluteY="25dp">
<ImageView
android:id="#+id/cardArtImageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/cardDetailsImageView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="#string/card_image" />
<!--
<ImageView
android:id="#+id/cardDetailsImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/card_details_box" /> -->
<!-- Implement scrolling text
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="225dp"
android:background="#drawable/card_details_closed">
<TextView
android:id="#+id/COST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/leaderSkillDesc"
android:layout_marginBottom="9dp"
android:layout_toEndOf="#+id/superAttackDesc"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="00"
android:textColor="#color/white"
android:textSize="17sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<TextView
android:id="#+id/HP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="33dp"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
<TextView
android:id="#+id/ATT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="73dp"
android:layout_toEndOf="#+id/HP"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
<TextView
android:id="#+id/DEF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="42dp"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
</RelativeLayout>
<TextView
android:id="#+id/leaderSkillDesc"
android:layout_width="250dp"
android:layout_height="15dp"
android:layout_above="#+id/superAttackTitle"
android:layout_alignParentEnd="true"
android:layout_marginBottom="13dp"
android:layout_marginEnd="37dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#color/white"
android:textSize="13sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintVertical_bias="0.626" />
<TextView
android:id="#+id/superAttackTitle"
android:layout_width="245dp"
android:layout_height="15dp"
android:layout_above="#+id/superAttackDesc"
android:layout_alignStart="#+id/superAttackDesc"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#android:color/holo_blue_light"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/superAttackDesc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<TextView
android:id="#+id/superAttackDesc"
android:layout_width="255dp"
android:layout_height="15dp"
android:layout_alignEnd="#+id/leaderSkillDesc"
android:layout_alignParentBottom="true"
android:layout_marginBottom="74dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<Button
android:id="#+id/arrowButton"
android:layout_width="60dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/leaderSkillDesc"
android:layout_marginBottom="7dp"
android:layout_marginStart="75dp"
android:background="#drawable/arrow_up"
android:textOff=""
android:textOn="" />
</RelativeLayout>
</LinearLayout>
SecondLayout.xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/card_view_bg"
tools:layout_editor_absoluteY="25dp">
<!-- Implement scrolling text
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:scrollHorizontally="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
-->
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/card_details_open">
<TextView
android:id="#+id/COST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout3"
android:layout_marginTop="48dp"
android:layout_toEndOf="#+id/superAttackDesc"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:text="00"
android:textColor="#color/white"
android:textSize="17sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="#+id/relativeLayout3">
<TextView
android:id="#+id/HP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="33dp"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
<TextView
android:id="#+id/ATT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="73dp"
android:layout_toEndOf="#+id/HP"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
<TextView
android:id="#+id/DEF"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="42dp"
android:fontFamily="monospace"
android:text="0000"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
tools:text="0000" />
</RelativeLayout>
<TextView
android:id="#+id/leaderSkillDesc"
android:layout_width="250dp"
android:layout_height="15dp"
android:layout_alignParentEnd="true"
android:layout_below="#+id/COST"
android:layout_marginEnd="36dp"
android:layout_marginTop="16dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#color/white"
android:textSize="13sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintVertical_bias="0.626" />
<TextView
android:id="#+id/superAttackTitle"
android:layout_width="245dp"
android:layout_height="15dp"
android:layout_alignStart="#+id/leaderSkillDesc"
android:layout_below="#+id/leaderSkillDesc"
android:layout_marginTop="23dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#android:color/holo_blue_light"
android:textSize="12sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/superAttackDesc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/superAttackDesc"
android:layout_width="255dp"
android:layout_height="15dp"
android:layout_alignStart="#+id/superAttackTitle"
android:layout_below="#+id/superAttackTitle"
android:layout_marginTop="5dp"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/passiveSkillTitle"
android:layout_width="255dp"
android:layout_height="17dp"
android:layout_alignStart="#+id/superAttackDesc"
android:layout_below="#+id/superAttackDesc"
android:layout_marginBottom="3dp"
android:layout_marginStart="8dp"
android:layout_marginTop="23dp"
android:background="#color/passiveSkillNameBackground"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:paddingLeft="7dp"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="viewStart"
android:textColor="#android:color/holo_blue_light"
android:textColorHighlight="#android:color/black"
android:textSize="13sp"
android:textStyle="italic"
android:visibility="visible"
tools:layout_editor_absoluteX="207dp"
tools:layout_editor_absoluteY="543dp" />
<TextView
android:id="#+id/passiveSkillDesc"
android:layout_width="250dp"
android:layout_height="15dp"
android:layout_alignStart="#+id/passiveSkillTitle"
android:layout_below="#+id/passiveSkillTitle"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:fontFamily="monospace"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="13sp"
android:textStyle="italic"
android:visibility="visible"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="602dp" />
<Button
android:id="#+id/arrowButton"
android:layout_width="60dp"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/leaderSkillDesc"
android:layout_marginBottom="7dp"
android:layout_marginStart="75dp"
android:background="#drawable/arrow_down"
android:textOff=""
android:textOn="" />
</RelativeLayout>
</RelativeLayout>
I'd suggest using a RecyclerView with a custom Adapter, RecyclerView can be used vertically or horizontally.

Android textview text underline unwanted

I'm having a strange trouble and didn't find any solutions on the web.
I'm adding a TextView dynamically into a RelativeLayout, with custom fonts, and sometimes the text appears underlined.
public void bouton_valide_ajout_texte(View v)
{
new_text_view.setText(((TextView) dialog.findViewById(R.id.edit_ajoute_texte)).getText());
RelativeLayout rl_newtext_1 = (RelativeLayout)findViewById(R.id.rl_newtext_1);
rl_newtext_1.addView(new_text_view);*/
TextView text_view = new TextView(this);
text_view.setText(((TextView) dialog_ajoute_texte.findViewById(R.id.edit_ajoute_texte)).getText());
ListView liste_view_police = (ListView) dialog_ajoute_texte.findViewById(R.id.list_view_dialog_polices);
text_view.setTextSize(TypedValue.COMPLEX_UNIT_SP,12);
text_view.setTypeface(listePolices.get_typeface(position_item_police));
text_view.setTextColor(Color.argb(255, 0, 0, 0));
text_view.clearComposingText();
container_calques.addView(text_view);
Object[] tab_calque = new Object[5];
tab_calque[0]="texte";
tab_calque[1]=text_view;
tab_calque[2]=12;
tab_calque[3]=0;
liste_calques.add(tab_calque);
calque_selectionne=liste_calques.size()-1;
text_view.setOnTouchListener(new TouchListener_calque(liste_calques.size()-1));
dialog_ajoute_texte.hide();
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ebm="http://schemas.android.com/apk/res-auto"
xmlns:sat="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="350dp"
android:layout_gravity="center"
android:id="#+id/rl_container_tshirt">
<ImageView
android:id="#+id/tshirt_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/tee_shirt_blanc">
</ImageView>
<RelativeLayout
android:id="#+id/container_calques"
android:layout_width="120dp"
android:layout_height="220dp"
android:layout_marginLeft="88dp"
android:layout_marginTop="85dp"
>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout_reglages_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
>
<SeekBar
android:id="#+id/seekbar_taille"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:layout_alignParentLeft="true"
android:max="40"
android:progress="20"
/>
<SeekBar
android:id="#+id/seekbar_rotation"
android:layout_width="170dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:layout_alignParentLeft="true"
android:max="360"
android:progress="180"
/>
<Button
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Supprimer"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:onClick="bouton_supprimer_calque"/>
<Button
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Couleur"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:layout_marginTop="40dp"
android:onClick="bouton_choisir_couleur"/>
<Button
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="Police"
android:layout_alignParentRight="true"
android:layout_marginRight="8dp"
android:layout_marginTop="80dp"
android:onClick="bouton_choisir_police"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
>
<Button
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="ajouter texte"
android:id="#+id/ajouter_calque"
android:onClick="bouton_ajoute_texte"/>
<Button
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="ajouter image"
android:id="#+id/ajouter_image"
android:onClick="bouton_ajoute_image"
android:layout_alignParentRight="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="enregistrer création dans galerie"
android:id="#+id/enregistrer_creation"
android:onClick="bouton_enregistrer_creation"
android:layout_below="#+id/ajouter_calque"
/>
</RelativeLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.view.ext.SatelliteMenu
android:id="#+id/menu_flottant_gauche"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:layout_margin="8dp"
sat:satelliteDistance="170dp"
sat:mainImage="#drawable/ic_launcher"
sat:totalSpacingDegree="90"
sat:closeOnClick="true"
sat:expandDuration="200"
/>
</FrameLayout>
</LinearLayout>
Solved
I replaced
text_view.setText(((TextView);
dialog_ajoute_texte.findViewById(R.id.edit_ajoute_texte)).getText());
By
text_view.setText(((EditText);
dialog_ajoute_texte.findViewById(R.id.edit_ajoute_texte)).getText().toString());
The possible underline could by the ListView as it puts a divider between its items. Look at this answer to get rid of the divider
Please make sure you don't have any inputType to your textview as follows
android:inputType="something"

ListView row not clickable (after adding two buttons)

Strangely, my listView's onClick no longer works after I added two Buttons: Submit and Show Result.
Here is the list view xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".CulturalActivity"
android:background="#EFEFEF"
android:id="#+id/AdLayout"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="3dp"
>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:divider="#android:color/transparent"
android:descendantFocusability="blocksDescendants"
android:scrollbars="none"
android:dividerHeight="10dp"
android:listSelector="#drawable/list_selector" />
</RelativeLayout>
Here is the layout of each list row:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="315dp"
android:clickable="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="7dp"
android:background="#drawable/bg_card"
android:orientation="horizontal"
android:padding="5dip" >
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="-60dp"
android:background="#2093CD"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="52dp"
android:text="Audit"
android:textColor="#android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/relativeLayout2" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Farm/Grp"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_alignTop="#+id/tile"
android:layout_toRightOf="#+id/textView4" />
<!--thumbnail-->
<LinearLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/thumbnail_image"
android:layout_marginTop="35dp"
android:gravity="center"
android:id="#+id/tile">
<ImageView
android:id="#+id/thumbImage"
android:layout_width="50dp"
android:clickable="false"
android:layout_height="50dp"
android:src="#drawable/opened" />
</LinearLayout>
<TextView
android:id="#+id/crophead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Crop"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_alignBottom="#+id/tile"
android:layout_alignLeft="#+id/title" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Certification"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_below="#+id/textView4"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/certification"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView2"
android:layout_marginLeft="22dp"
android:layout_toRightOf="#+id/title"
android:text="Test"
android:textColor="#343434"
android:textSize="17sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audit Type"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_below="#+id/tile"
android:layout_alignLeft="#+id/textView2"
android:layout_marginTop="20dp" />
<TextView
android:id="#+id/audittype"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignLeft="#+id/certification"
android:maxLines="2"
android:text="ES"
android:textColor="#343434"
android:textSize="17sp" />
<View
android:layout_width="fill_parent"
android:layout_height="3px"
android:layout_marginTop="200dp"
android:background="#21265b"
android:id="#+id/view" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Date"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_below="#+id/view"
android:layout_alignLeft="#+id/view"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_alignParentBottom="false" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="End Date"
android:textColor="#000000"
android:typeface="sans"
android:textSize="17sp"
android:textStyle="bold"
android:layout_marginRight="15dp"
android:layout_alignTop="#+id/textView6"
android:layout_alignRight="#+id/startdate" />
<TextView
android:id="#+id/startdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#8B1A1A"
android:text="30/05/1992"
android:textSize="17sp"
android:layout_alignTop="#+id/enddate"
android:layout_toRightOf="#+id/relativeLayout2"
android:layout_marginLeft="6dp"
android:layout_alignParentBottom="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#8B1A1A"
android:text="2/13/14"
android:textSize="17sp"
android:id="#+id/enddate"
android:layout_below="#+id/textView7"
android:layout_alignRight="#+id/textView7" />
<TextView
android:id="#+id/farm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/audittype"
android:maxLines="3"
android:layout_alignTop="#+id/title"
android:text="Tea Estate Nagarcoil Tamil Nadu, India "
android:textColor="#343434"
android:textSize="17sp" />
<TextView
android:id="#+id/crop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/farm"
android:layout_alignTop="#+id/crophead"
android:maxLines="2"
android:text="Wheat without its chaff and barn but not brown"
android:textColor="#343434"
android:textSize="17sp" />
<Button
android:id="#+id/upsync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/blue_thumb"
android:layout_below="#+id/enddate"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Show Results"
android:textColor="#android:color/white" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/upsync"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_below="#+id/enddate"
android:background="#drawable/blue_thumb"
android:text="Submit"
android:textColor="#android:color/white" />
</RelativeLayout>
Here is the click snippet of the list view:
listview.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view,
int position, long id) {
long audit_id;
try{
audit_id = (new CustomListAdapter(HomeList.this, c).getAuditID(position));
changeTileStatus(audit_id,"opened");
System.out.println("listClicked");
sendAuditAndIntent(audit_id);
}catch(Exception e)
{
System.out.println("ERROR # HOMELIST list onClick: "+e);
}
}
private void sendAuditAndIntent(long audit_id) { //sends audit id and api key
Intent intent = new Intent(HomeList.this,ChapterActivity.class );
Bundle extras= new Bundle();
extras.putString("audit_id",String.valueOf(audit_id));
extras.putString("api_key", api_key);
intent.putExtras(extras);
startActivity(intent);
}
private void changeTileStatus(long audit_id, String value) {
AuditTableManager tile= new AuditTableManager(HomeList.this);
tile.open();
tile.updateEntry(audit_id, value);
tile.close();
}
});
OnItemClickListener is not called if cells contain clickable Views. It is an Android feature. You can handle clicks by OnClickListener then - set it to your two buttons (not the listView itself).
In your adapter:
public View onCreateView(..., final int position){
...
convertView.findViewById(R.id.button1).setOnClickListener(new OnClickListener(){
public void onClick(View view){
itemClickedAction1(position);
}
});
convertView.findViewById(R.id.button2).setOnClickListener(new OnClickListener(){
public void onClick(View view){
itemClickedAction2(position);
}
});
...
}
Try android:Focusable="false" and also android:clickable="false" in the custom row TextView

Categories

Resources