Design Edit Text Code Editor with line number not scrollable - java

I am trying to design a code editor with the help of Android Edit Text. I am using a text view parallel to it which is showing line number. It is working fine but the problem is after going down more then 30 lines it text view remains same but edit text becomes scroll able I can make the text view scroll able but it will not going to scroll with respect to edit text. The main problem is of scrolling. If anyone can help me to correct this or some other way to use similar kind of simple activity to code with line numbers, So that further I will be able to save that text in a file.
Below is the Activity and xml code I have tried.
MainActivity.java
package com.rk.codeareadesignworkingedittextline;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ed=(EditText)findViewById(R.id.editText);
final TextView tv=(TextView)findViewById(R.id.textView);
ed.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
int lines=ed.getLineCount();
String lineText="";
for (int i=1;i<=lines;i++){
lineText=lineText+i+"\n";
tv.setText(lineText);
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
}
Below is the xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="51dp"
android:layout_height="587dp"
android:background="#eeeeee"
android:gravity="center_horizontal"
android:padding="12dp"
android:text="1"
android:textSize="18dp" />
<EditText
android:id="#+id/editText"
android:layout_width="350dp"
android:layout_height="595dp"
android:ems="10"
android:gravity="left|top"
android:inputType="textMultiLine"
android:text=""
android:textSize="18dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>

Put your EditText in a ScrollView. And let the EditText and TextView expand as much as it can with wrap_content.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="51dp"
android:layout_height="wrap_content"
android:background="#eeeeee"
android:gravity="center_horizontal"
android:padding="12dp"
android:text="1"
android:textSize="18dp" />
<EditText
android:id="#+id/editText"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="left|top"
android:inputType="textMultiLine"
android:text=""
android:textSize="18dp" />
</LinearLayout>
</ScrollView>

Try this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="51dp"
android:layout_height="match_parent"
android:background="#eeeeee"
android:gravity="center_horizontal"
android:padding="12dp"
android:text="1"
android:textSize="18dp" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:textAlignment="textStart"
android:inputType="textMultiLine"
android:text=""
android:textSize="18dp" />
</LinearLayout>
</ScrollView>

Related

Rating Bar not appearing

I am trying to use a rating bar in Android Studio but it just isn't appearing in my app.
I am not receiving any errors and can't understand what I am doing wrong.
Has anyone else experienced this issue?
Here is my java file;
package com.example.myopenlounge;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.analytics.FirebaseAnalytics;
public class RatingActivity extends AppCompatActivity {
private RatingBar mRatingBar;
private TextView mRatingScale;
private EditText mFeedback;
private Button mSendFeedback;
private int star1 = 0;
private int star2 = 0;
private int star3 = 0;
private int star4 = 0;
private int star5 = 0;
public static final String TAG = "TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rating);
mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
mRatingScale = (TextView) findViewById(R.id.tvRatingScale);
mFeedback = (EditText) findViewById(R.id.etFeedback);
mSendFeedback = (Button) findViewById(R.id.btnSubmit);
mRatingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
mRatingScale.setText(String.valueOf(rating));
switch((int) ratingBar.getRating()){
case 1:
mRatingScale.setText("Awful!");
star1++;
break;
case 2:
mRatingScale.setText("Not great...");
star2++;
break;
case 3:
mRatingScale.setText("Good");
star3++;
break;
case 4:
mRatingScale.setText("Great");
star4++;
break;
case 5:
mRatingScale.setText("Fantastic!");
star5++;
break;
default:
mRatingScale.setText("");
}
}
});
mSendFeedback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mFeedback.getText().toString().isEmpty()){
Toast.makeText(RatingActivity.this, "Please fill in feedback text box",
Toast.LENGTH_LONG).show();
}
else{
mFeedback.setText("");
mRatingBar.setRating(0);
Toast.makeText(RatingActivity.this, "Thank you for sharing your feedback :) ",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
and then my xml file looks like this;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RatingActivity"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="We hope you enjoyed your meal with us today"
android:textSize="18sp"
android:textStyle="italic" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"
android:rating="5"/>
<TextView
android:id="#+id/tvRatingScale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Awesome. I love it"
android:textSize="16sp"
android:textStyle="bold"/>
<EditText
android:id="#+id/etFeedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5"
android:hint="Tell us a little about why you have given us this rating"
android:gravity="top" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#e57373"
android:text="Send feedback"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="17dp"
tools:layout_editor_absoluteY="46dp" />
</LinearLayout>
The text "We hope you enjoyed your meal with us today" is all that appears on the screen when the onCreate method is called. I can't seem to find a reason why.
This is because you sets orientation horizontal
android:orientation="horizontal"
you need to update it to vertical
android:orientation="vertical"
Here is your updated xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RatingActivity"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="We hope you enjoyed your meal with us today"
android:textSize="18sp"
android:textStyle="italic" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1"
android:rating="5"/>
<TextView
android:id="#+id/tvRatingScale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Awesome. I love it"
android:textSize="16sp"
android:textStyle="bold"/>
<EditText
android:id="#+id/etFeedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:lines="5"
android:hint="Tell us a little about why you have given us this rating"
android:gravity="top" />
<Button
android:id="#+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#e57373"
android:text="Send feedback"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
tools:layout_editor_absoluteX="17dp"
tools:layout_editor_absoluteY="46dp" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RatingActivity"
android:orientation="vertical">
...

Radio button as tabs highlight when selected (Android Studio)

I am trying to create radio buttons that allows the users to select one of the three options. Once selected, the option should have a border, while the others do not.
Here is an image of what I am trying to create:
I'm not sure how to go about this. Any suggestions are greatly appreciated.
You could create a group of objects (which are the options) and programmatically ensure that only one of them can be selected.
selection could be presented as you wish it would be, I'd give a code example that imitates approximately the image you've attached. here's the structure:
XML file: CardViews with base of invisible FrameLayout (will be functioning as stroke), above the base is the cards' content
Java Class: setChecked(MaterialCardView selected) method that selects the chosen card (making it's 'stroke' visible) and checkedCard() that returns the selected card
so here's the code:
activity_main.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:background="#color/parentBackground"
tools:context=".MainActivity">
<com.google.android.material.card.MaterialCardView
android:id="#+id/public_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/public_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="visible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card1Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_public" />
<TextView
android:id="#+id/card1Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card1Icon"
android:text="Public Profile"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card1Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card1Label"
android:layout_alignStart="#id/card1Label"
android:text="Everyone can see your account. \nIncluding non followers."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/friends_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/public_card"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/friends_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card2Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_friends" />
<TextView
android:id="#+id/card2Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card2Icon"
android:text="Friends Only"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card2Label"
android:layout_alignStart="#id/card2Label"
android:text="Connections nly. Only your friends \nand friends of friends can see your account."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/private_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/friends_card"
android:layout_margin="10dp"
app:cardBackgroundColor="#color/cardBackground"
app:cardCornerRadius="5dp">
<FrameLayout
android:id="#+id/private_stroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/selectedCardStroke"
android:visibility="invisible" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#color/cardBackground">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/card3Icon"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_centerVertical="true"
android:layout_margin="20dp"
android:src="#mipmap/ic_private" />
<TextView
android:id="#+id/card3Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/card3Icon"
android:text="Private"
android:textAllCaps="false"
android:textColor="#color/cardLabelColor"
android:textSize="35sp" />
<TextView
android:id="#+id/card3Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/card3Label"
android:layout_alignStart="#id/card3Label"
android:text="Only you can see your account."
android:textAllCaps="false"
android:textColor="#color/cardTextColor"
android:textSize="15sp" />
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
MainActivity.java:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.google.android.material.card.MaterialCardView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public MaterialCardView cPublic, cFriends, cPrivate;
public FrameLayout sPublic, sFriends, sPrivate;
public ArrayList<MaterialCardView> cards;
public ArrayList<FrameLayout> strokes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cards = new ArrayList<>();
strokes = new ArrayList<>();
cPublic = findViewById(R.id.public_card);
cFriends = findViewById(R.id.friends_card);
cPrivate = findViewById(R.id.private_card);
cards.add(cPublic);
cards.add(cFriends);
cards.add(cPrivate);
sPublic = findViewById(R.id.public_stroke);
sFriends = findViewById(R.id.friends_stroke);
sPrivate = findViewById(R.id.private_stroke);
strokes.add(sPublic);
strokes.add(sFriends);
strokes.add(sPrivate);
cPublic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cPublic);
}
});
cFriends.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cFriends);
}
});
cPrivate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setChecked(cPrivate);
}
});
}
public void setChecked(MaterialCardView selected) {
int index = cards.indexOf(selected);
FrameLayout stroke = strokes.get(index);
stroke.setVisibility(View.VISIBLE);
for (FrameLayout s : strokes) {
if (!s.equals(stroke)) {
s.setVisibility(View.INVISIBLE);
}
}
}
public MaterialCardView checkedCard() {
int index = 0;
for (FrameLayout s : strokes) {
if (s.getVisibility() == View.VISIBLE) {
index = strokes.indexOf(s);
}
}
return cards.get(index);
}
}
Hope it could help you (:

JSON text not autosizing in textview (uniformly in horizontal and vertical direction)

I'm trying to fetch text from a JSON file uploaded in my Firebase database, but when the text lines are fetched, they aren't auto-sized (wrt x and y-axis), although I used autoSizeTextType = uniform along with added parameters. Blank spaces are still present on the right side (as one can see from the snapshots). I've used 3rd party Gradle dependencies, but, they also don't work out well. So, pls help me out and tell me solution based on my code.
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CourseDetail">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing"
android:layout_width="match_parent"
android:layout_height="350dp"
android:fitsSystemWindows="true"
app:contentScrim="#262628"
app:expandedTitleTextAppearance="#android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/img_course"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#null"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_collapseMode="parallax"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:title="Course Name"
app:titleTextColor="#ffffff" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="DESCRIPTION"
android:textColor="#262628"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<TextView
android:id="#+id/course_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:autoSizeTextType="uniform"
android:fitsSystemWindows="true"
android:lineSpacingMultiplier="1.5"
android:text="Description "
android:textColor="#android:color/black"
android:autoSizeMinTextSize="15sp"
android:autoSizeMaxTextSize="20sp"
android:autoSizeStepGranularity="1sp"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="DEGREE INFROMATION"
android:textColor="#262628"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<TextView
android:id="#+id/degree_information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="Description"
android:textColor="#android:color/black"
android:textSize="14sp" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:padding="12dp"
android:text="JOB SCOPE"
android:textColor="#262628"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="1dp"
app:cardUseCompatPadding="true">
<TextView
android:id="#+id/job_scope"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:lineSpacingMultiplier="1.5"
android:padding="12dp"
android:text="Description"
android:textColor="#android:color/black"
android:textSize="14sp" />
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
Activity.java
package com.example.shubhojit.careersafter10th;
import android.support.annotation.NonNull;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.shubhojit.careersafter10th.Model.Courses_After10th;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
public class CourseDetail extends AppCompatActivity {
TextView course_description,deg_info,job_scope;
ImageView courseImage;
CollapsingToolbarLayout collapsingToolbarLayout;
String courseId="";
FirebaseDatabase database;
DatabaseReference coursedetail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_course_detail);
database = FirebaseDatabase.getInstance();
coursedetail = database.getReference("Courses_After10th");
course_description = (TextView)findViewById(R.id.course_description);
deg_info = (TextView)findViewById(R.id.degree_information);
job_scope = (TextView)findViewById(R.id.job_scope);
courseImage = (ImageView)findViewById(R.id.img_course);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing);
collapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppbar);
collapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppbar);
if(getIntent() != null)
courseId = getIntent().getStringExtra("CourseId");
if(!courseId.isEmpty())
{
getDetailCourse(courseId);
}
}
private void getDetailCourse(String courseId) {
coursedetail.child(courseId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
Courses_After10th course = dataSnapshot.getValue(Courses_After10th.class);
Picasso.with(getBaseContext()).load(course.getImage()).into(courseImage);
collapsingToolbarLayout.setTitle(course.getName());
deg_info.setText(course.getDegree_Information());
job_scope.setText(course.getJob_Scope());
course_description.setSingleLine(false);
course_description.setText(course.getDescription());
course_description.setEllipsize(TextUtils.TruncateAt.END);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
TextView does not provide Justify kinda alignment available in Word kinda tools.
To Justify text, instead use WebView with HTML content. Convert your text into HTML tags with proper alignment and styling. And then load the content in WebView as HTML.

Managing onClickListeners in more then one xml file

I created an app that adds forms dynamically when a user clicks the add butting and deletes it when he clicks the cancel button. I have two cancel buttons, one in the Field.xml file and the other in the activity_school_search_setup file.
They both have the same ID but the one in the Field.xml file does not delete the field. It appears as though the onclicklistener does not function for the delete button.
Main Java class file
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;
public class SchoolSearchSetup extends AppCompatActivity implements View.OnClickListener {
private EditText searchSchoolID;
private Button searchSchoolButtonID;
private ListView listOfSchoolsID;
private Button openNewschoolID;
private LinearLayout schoolSetupLayout;
private Button addNewClass;
private EditText classNameEditText;
private Button deleteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_search_setup);
findAllViewsID();
initializeListenners();
}
public void findAllViewsID(){
classNameEditText = findViewById(R.id.classNameText);
addNewClass = findViewById(R.id.addNewClassButton);
schoolSetupLayout = findViewById(R.id.schoolSetupLayout);
searchSchoolID = findViewById(R.id.searchSchoolID);
searchSchoolButtonID = findViewById(R.id.searchSchoolButtonID);
listOfSchoolsID = findViewById(R.id.listOfSchoolsID);
openNewschoolID = findViewById(R.id.openNewschoolID);
deleteButton = findViewById(R.id.delete_button);
}
public void initializeListenners(){
openNewschoolID.setOnClickListener(SchoolSearchSetup.this);
addNewClass.setOnClickListener(SchoolSearchSetup.this);
deleteButton.setOnClickListener(SchoolSearchSetup.this);
}
public void addNewClass(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.openNewschoolID:
displaySchoolSetUpForms();
break;
case R.id.addNewClassButton:
addNewClass();
break;
case R.id.delete_button:
schoolSetupLayout.removeView((View) view.getParent());
}
}
private void displaySchoolSetUpForms() {
schoolSetupLayout.setVisibility(View.VISIBLE);
}
}
Here is the main XML activity file
activity.school_search_setup.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="wrap_content"
tools:context="com.example.demeainc.demea.MainActivity"
android:orientation="vertical"
android:background="#color/backgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<ImageView
android:id="#+id/backArrowClassView"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_arrow_class"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:text="Search your school."
android:gravity="center"
android:textSize="25dp" />
<android.support.v7.widget.CardView
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp">
<EditText
android:id="#+id/searchSchoolID"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="#drawable/ic_search_black_24dp"
android:hint="Search"/>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<Button
android:id="#+id/searchSchoolButtonID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:text="Search"
android:textColor="#ffff"
android:background="#color/colorPrimary"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listOfSchoolsID"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#android:color/transparent"
android:cacheColorHint="#color/ligtherDarkGrey"
android:divider="#CCCCCC"
android:dividerHeight="2dp"
android:paddingLeft="2dp" >
</ListView>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="#+id/openNewschoolID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:background="#color/colorPrimary"
android:text="Open New School"
android:textColor="#ffff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/schoolSetupLayout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/schoolSetupText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="School Setup"
android:textSize="20dp"
android:layout_marginLeft="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="#+id/addNewClassButton"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Add new class"
android:textColor="#ffff"
android:background="#color/colorPrimary"/>
<Button
android:id="#+id/nextButton"
android:layout_marginTop="25dp"
android:layout_marginLeft="8dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Next"
android:textColor="#ffff"
android:background="#color/green"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Here is the field.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/schoolSetupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
</LinearLayout>
You either should add a clickListener on
View rowView = inflater.inflate(R.layout.field, null);
Or add android:onClick within an element of field.xml if you want some click event to happen on it.
initializeListenners(); is only called once, during the initial layout, not on dynamic view additions.
Implement OnClickListener for Field.xml button after you inflate that layout.
Here is example:
public void addNewClass(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
final Button btnDelete = rowView.findViewById(R.id.delete_button);
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your stuff
}
});
schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}
Hope that help :)

Dynamically generated delete button does not respond to an OnclickListener(Android JAVA)

I created an app that dynamically adds an edittext field to the view by clicking an Add button and deletes it by clicking a delete button. I created a separate xml file called field.xml that is called up when a new edittext field is added to the view. But the delete button does not remove this edittext field when clicked. What am I missing in the code I have tried most options but to no avail.
Here is the main java file
SchoolSearchSetup.java
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.View;
import android.widget.Toast;
public class SchoolSearchSetup extends AppCompatActivity implements View.OnClickListener {
private EditText searchSchoolID;
private Button searchSchoolButtonID;
private ListView listOfSchoolsID;
private Button openNewschoolID;
private LinearLayout schoolSetupLayout;
private Button addNewClass;
private EditText classNameEditText;
private Button deleteButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_school_search_setup);
findAllViewsID();
initializeListenners();
}
public void findAllViewsID(){
classNameEditText = findViewById(R.id.classNameText);
addNewClass = findViewById(R.id.addNewClassButton);
schoolSetupLayout = findViewById(R.id.schoolSetupLayout);
searchSchoolID = findViewById(R.id.searchSchoolID);
searchSchoolButtonID = findViewById(R.id.searchSchoolButtonID);
listOfSchoolsID = findViewById(R.id.listOfSchoolsID);
openNewschoolID = findViewById(R.id.openNewschoolID);
deleteButton = findViewById(R.id.delete_button);
}
public void initializeListenners(){
openNewschoolID.setOnClickListener(SchoolSearchSetup.this);
addNewClass.setOnClickListener(SchoolSearchSetup.this);
deleteButton.setOnClickListener(SchoolSearchSetup.this);
}
public void addNewClass(){
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.field, null);
schoolSetupLayout.addView(rowView, schoolSetupLayout.getChildCount() -1);
}
#Override
public void onClick(View view) {
switch(view.getId()){
case R.id.openNewschoolID:
displaySchoolSetUpForms();
break;
case R.id.addNewClassButton:
addNewClass();
break;
case R.id.delete_button:
schoolSetupLayout.removeView((View) view.getParent());
}
}
private void displaySchoolSetUpForms() {
schoolSetupLayout.setVisibility(View.VISIBLE);
}
}
The main XML file
activity.school_search_setup.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="wrap_content"
tools:context="com.example.demeainc.demea.MainActivity"
android:orientation="vertical"
android:background="#color/backgroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<ImageView
android:id="#+id/backArrowClassView"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_arrow_class"
android:layout_marginLeft="5dp"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="50dp"
android:text="Search your school."
android:gravity="center"
android:textSize="25dp" />
<android.support.v7.widget.CardView
android:foreground="?attr/selectableItemBackground"
android:clickable="true"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp">
<EditText
android:id="#+id/searchSchoolID"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableLeft="#drawable/ic_search_black_24dp"
android:hint="Search"/>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<Button
android:id="#+id/searchSchoolButtonID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:text="Search"
android:textColor="#ffff"
android:background="#color/colorPrimary"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp">
<android.support.v7.widget.CardView
android:layout_width="300dp"
android:layout_height="wrap_content">
<ListView
android:id="#+id/listOfSchoolsID"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#android:color/transparent"
android:cacheColorHint="#color/ligtherDarkGrey"
android:divider="#CCCCCC"
android:dividerHeight="2dp"
android:paddingLeft="2dp" >
</ListView>
</android.support.v7.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical">
<Button
android:id="#+id/openNewschoolID"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_marginLeft="60dp"
android:background="#color/colorPrimary"
android:text="Open New School"
android:textColor="#ffff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="#+id/schoolSetupLayout"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<TextView
android:id="#+id/schoolSetupText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="School Setup"
android:textSize="20dp"
android:layout_marginLeft="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center">
<Button
android:id="#+id/addNewClassButton"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Add new class"
android:textColor="#ffff"
android:background="#color/colorPrimary"/>
<Button
android:id="#+id/nextButton"
android:layout_marginTop="25dp"
android:layout_marginLeft="8dp"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Next"
android:textColor="#ffff"
android:background="#color/green"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Here is the Field.xml file for the edit text to be added. Both the field.xml and the activity.school_search_setup.xml share the same IDs.
Field.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/schoolSetupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/classNameText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:hint="Class name, e.g Grade one" />
<Button
android:id="#+id/delete_button"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="1"
android:background="#android:drawable/ic_delete"/>
</LinearLayout>
</LinearLayout>
Have you tried using
setVisibility(View.GONE);
Update:
Try this code block:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
btn.setBackgroundColor(Color.rgb(80, 80, 90));
linear.addView(btn, params);
btn1 = ((Button) findViewById(id_));
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});

Categories