i am new to Android Development.I have created an applicaiton which will display the corresponding textview on clicking a textview.
For that i have given a if-else-if ladder condition statement to display the textview. Since i have lot of textviews(around 100), while building the project android studio is saying CODE IS TOO LONG.
How to solve this issue. Is it possible to use the MAP to iterate through the textview IDS?
Please help me.
public void click(View v) {
switch (v.getId()) {
case R.id.fam:
// check if a child is set to a specific String
String toSearchFor = "badaatext";
boolean found = false;
ArrayList<View> allViewsWithinMyTopView = getAllChildren(findViewById(R.id.linear));
for (View child : allViewsWithinMyTopView) {
if (child instanceof TextView) {
TextView childTextView = (TextView) child;
int intName = childTextView.getId();
System.out.println("The Child TextView is : " + childTextView.getResources().getResourceEntryName(intName));
if (childTextView.getResources().getResourceEntryName(intName).startsWith("badaatext")) {
found = true;
System.out.println("The text is foudn inside the layout");
System.out.println("And the Textview ID is :" + childTextView.getResources().getResourceEntryName(intName));
childTextView.setVisibility(View.GONE);
String BadaaString = childTextView.getResources().getResourceEntryName(intchildName);
System.out.println("The Big TextView is : " + BadaaString);
String ChotaString = BadaaString.substring(9, 12);
System.out.println("The Small Textview is : " + ChotaString);
if (ChotaString.equalsIgnoreCase("rel")) {
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.rel);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("nam")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.nam);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("mom")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.mom);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("dad")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.dad);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("elb")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.elb);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("els")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.els);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("yos")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.yos);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("sil")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.sil);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("yob")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.yob);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("hus")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.hus);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("wif")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.wif);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("fre")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.fre);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("tea")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.tea);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("ser")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.ser);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("mai")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.mai);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("wom")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.wom);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("stm")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.stm);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("man")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.man);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("boy")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.boy);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gir")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gir);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gue")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gue);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("cus")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.cus);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("hoi")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.hoi);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gif")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gif);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("son")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.son);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("dau")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.dau);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gpa")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gpa);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gma")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gma);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gdp")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gdp);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gdm")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gdm);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("und")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.und);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("aud")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.aud);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("maa")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.maa);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("mam")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.mam);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("mad")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.mad);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("ata")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.ata);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("fla")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.fla);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("mla")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.mla);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("sod")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.sod);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("gru")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.gru);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("som")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.som);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("grm")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.grm);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("unp")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.unp);
Engtxt1.setVisibility(View.VISIBLE);
}else if(ChotaString.equalsIgnoreCase("aup")){
//Display the Small TextView:
TextView Engtxt1 = (TextView) findViewById(R.id.aup);
Engtxt1.setVisibility(View.VISIBLE);
}else{
System.out.println("No Matches found");
}
}
}
}
if (!found) {
//fail("Text '" + The text is not available in the layout");
System.out.println("The TextView is not present inside the layout");
}
//Make the Clicked View to be Invisible
TextView Engfam = (TextView) findViewById(R.id.fam);
Engfam.setVisibility(View.GONE);
//Adding the corresponding dynamic textview
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear);
linearLayout.setOrientation(LinearLayout.VERTICAL);
/* Add textview 1 */
TextView textViewFamily = new TextView(lessonSix.this);
// textView1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
// LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams textviewLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textViewFamily, 100,600, 2, TypedValue.COMPLEX_UNIT_SP);
textviewLayoutParams.setMargins(0, 9, 0, 0);
// linearLayout.addView(textView1, 1, textviewLayoutParams);
textViewFamily.setId(R.id.badaatextFam);
textViewFamily.setLayoutParams(textviewLayoutParams);
textViewFamily.setBackgroundResource(R.drawable.shapelineinsideroundedrect
);
textViewFamily.setText("FAMILY - குடும்பம்");
textViewFamily.setGravity(Gravity.TOP | Gravity.CENTER);
textViewFamily.setTextSize(20);
textViewFamily.setTypeface(null, Typeface.BOLD);
textViewFamily.setPadding(0, 25, 0, 25);
textViewFamily.setVisibility(View.VISIBLE);
linearLayout.addView(textViewFamily, 1);
break;
case R.id.gir:
// check if a child is set to a specific String
found = false;
ArrayList<View> allViewsWithinMyTopView21 = getAllChildren(findViewById(R.id.linear));
for (View child : allViewsWithinMyTopView21) {
if (child instanceof TextView) {
childTextView = (TextView) child;
intchildName = childTextView.getId();
System.out.println("The Child TextView is : " + childTextView.getResources().getResourceEntryName(intchildName));
if (childTextView.getResources().getResourceEntryName(intchildName).startsWith("badaatext")) {
found = true;
System.out.println("The text is foudn inside the layout");
System.out.println("And the Textview ID is :" + childTextView.getResources().getResourceEntryName(intchildName));
childTextView.setVisibility(View.GONE);
String BadaaString = childTextView.getResources().getResourceEntryName(intchildName);
System.out.println("The Big TextView is : " + BadaaString);
String ChotaString = BadaaString.substring(9, 12);
System.out.println("The Small Textview is : " + ChotaString);
//if (ChotaString.equalsIgnoreCase("fam")) {
// //Display the Small TextView:
// TextView Engtxt1 = (TextView) findViewById(R.id.fam);
//Engtxt1.setVisibility(View.VISIBLE);
//}
int textViewId = context.getResources().getIdentifier(ChotaString, "id", context.getPackageName());
TextView Engtxt1 = (TextView) findViewById(textViewId);
Engtxt1.setVisibility(View.VISIBLE);
}
}
}
if (!found) {
//fail("Text '" + The text is not available in the layout");
System.out.println("The TextView is not present inside the layout");
}
//Make the Clicked View to be Invisible
TextView Enggir = (TextView) findViewById(R.id.gir);
Enggir.setVisibility(View.GONE);
//Adding the corresponding dynamic textview
LinearLayout linearLayout21 = (LinearLayout) findViewById(R.id.linear);
linearLayout21.setOrientation(LinearLayout.VERTICAL);
/* Add textview 1 */
TextView textViewGirl = new TextView(lessonSix.this);
//textView1.setLayoutParams(new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
//LinearLayout.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams textviewLayoutParams21 = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(textViewGirl, 100,600, 2, TypedValue.COMPLEX_UNIT_SP);
textviewLayoutParams21.setMargins(0, 9, 0, 0);
//linearLayout.addView(textView1, 1, textviewLayoutParams);s
textViewGirl.setId(R.id.badaatextGir);
textViewGirl.setLayoutParams(textviewLayoutParams21);
textViewGirl.setBackgroundResource(R.drawable.shapelineinsideroundedrect);
textViewGirl.setText("GIRL - சிறுமி");
textViewGirl.setGravity(Gravity.TOP | Gravity.CENTER);
textViewGirl.setTextSize(20);
textViewGirl.setTypeface(null, Typeface.BOLD);
textViewGirl.setPadding(0, 25, 0, 25);
textViewGirl.setVisibility(View.VISIBLE);
linearLayout21.addView(textViewGirl, 40);
break;
The Relevant XML Code:
<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/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/blue"
tools:context=".lessonSix">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--<TextView-->
<!--android:id="@+id/badaatextOne"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="130dp"-->
<!--android:layout_marginTop="5dp"-->
<!--android:paddingTop="25dp"-->
<!--android:background="@drawable/shapelineinsideroundedrect"-->
<TextView
android:id="@+id/fam"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginTop="5dp"
android:background="@drawable/shaperoundedcornerrect"
android:text="FAMILY"
android:onClick="click"
android:clickable="true"
android:gravity="center"
android:textSize="20dip"
android:textStyle="bold"
tools:ignore="OnClick" />
<TextView
android:id="@+id/rel"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginTop="5dp"
android:background="@drawable/shaperoundedcornerrect"
android:text="RELATION"
android:clickable="true"
android:onClick="click"
android:gravity="center"
android:textSize="20dip"
android:textStyle="bold" />
<TextView
android:id="@+id/gir"
android:layout_width="match_parent"
android:layout_height="65dp"
android:layout_marginTop="5dp"
android:background="@drawable/shaperoundedcornerrect"
android:text="GIRL"
android:clickable="true"
android:onClick="click"
android:gravity="center"
android:textSize="20dip"
android:textStyle="bold"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Adding the Exception : FATAL EXCEPTION: main Process: com.seekho.noushad.dictionary, PID: 30013 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:4788) at android.view.View$PerformClick.run(View.java:19923) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5401) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java Caused by: java.lang.reflect.InvocationTargetException Application terminated.
New Log(Exception):
D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.seekho.noushad.dictionary, PID: 24918 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:4788) at android.view.View$PerformClick.run(View.java:19923) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5401) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:714) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:4788) at android.view.View$PerformClick.run(View.java:19923) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5401) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:714) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference at com.seekho.noushad.dictionary.lessonSix.click(lessonSix.java:4893) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:4788) at android.view.View$PerformClick.run(View.java:19923) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5401) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:919) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:714) I/Process: Sending signal. PID: 24918 SIG: 9 Application terminated.