I was using BottomSheetDialog for taking some input from the customer, my view contains TextInputLayout and button below the TextInputLayout when the user clicks on TextInputLayout the below button is getting hide by keyboard, so I need to that button above the keyboard, tried many possibilities but not able to fix it.
Attaching the pics regarding it.
Java file(activity) code:
BottomSheetDialog customTipAmountBottomSheetDialog;
private void showCustomTipAmountBottomSheet() {
customTipAmountBottomSheetDialog = new BottomSheetDialog(OrderActivity.this);
View customTipAmountBottomSheetView = getLayoutInflater().inflate(R.layout.custom_tip_amount_bottom_sheet, null);
customTipAmountBottomSheetDialog.setContentView(customTipAmountBottomSheetView);
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.showSoftInput(binding.invoiceDialog.submit, InputMethodManager.RESULT_HIDDEN);
TextInputLayout customTipAmountBSTIL = customTipAmountBottomSheetView.findViewById(R.id.customTipAmountBSTIL);
EditText customTipAmountBSET = customTipAmountBottomSheetView.findViewById(R.id.customTipAmountBSET);
ImageView submit_custom_tip_amount = customTipAmountBottomSheetView.findViewById(R.id.submit_custom_tip_amount);
submit_custom_tip_amount.setOnClickListener(view -> {
String amount = customTipAmountBSET.getText().toString();
if (amount.length() > 0 && Integer.parseInt(amount) > 0) {
int tipAmount = Integer.parseInt(amount);
if (tipAmount > 0 && tipAmount < 51) {
binding.invoiceDialog.customTipAmountTv.setText(tipAmount);
captainTipAmount = tipAmount;
customTipAmountBottomSheetDialog.dismiss();
} else {
customTipAmountBSTIL.setError("Amount should be less than 50");
}
} else {
customTipAmountBSTIL.setError("Requires a valid amount");
}
});
customTipAmountBottomSheetDialog.show();
}
layout file custom_tip_amount_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:layout_height="match_parent">
<TextView
android:id="#+id/customTipAmountCaptainNameTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="Tip amount for Rapido Captain"
android:textColor="#color/black"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.TextInputLayout
android:id="#+id/customTipAmountBSTIL"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:hint="Enter amount"
app:boxBackgroundColor="#color/grey_100"
app:boxCornerRadiusTopEnd="8dp"
app:boxCornerRadiusTopStart="8dp"
app:errorEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/customTipAmountCaptainNameTv">
<EditText
android:id="#+id/customTipAmountBSET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="1234567890"
android:ems="2"
android:focusable="true"
android:imeOptions="actionDone"
android:inputType="phone" />
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="#+id/submit_custom_tip_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="submitFeedbackConfirmationPopUp"
android:padding="#dimen/margin_8"
app:layout_constraintBottom_toBottomOf="#+id/customTipAmountBSTIL"
app:layout_constraintEnd_toEndOf="#+id/customTipAmountBSTIL"
app:layout_constraintTop_toTopOf="#+id/customTipAmountBSTIL"
app:srcCompat="#drawable/ic_arrow_forward_black_24dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit_custom_tip_amount" />
in Manifest file for activity file:
android:windowSoftInputMode="adjustPan"
Tried searching alot, but still not able to fix it. i got one answer related to my problem but it was with BottomSheetFragment()keyboard hides BottomSheetFragment, but i need with BottomSheetDialog. Please someone help me regarding this.
Let me know anything else info is required.
On onCreate() method in your activity java class use the below method.
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Few days ago, i was faced same problem. Then using this portion of code into my onCreate() method, then the problem was solved.
Sample:
There are ways to achieve this:
1. Write below code into your Android Manifest file under <activity> tag:
android:windowSoftInputMode="stateHidden|adjustResize"
2. Write below code into your onCreate() method of activity:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
I hope its work for you.
Related
I'm currently working on a recyclerView that will list a few itens that have a checkbox on each item.
On the checkbox itself, it only needs one click to check, but on the card (only for the first time), it needs two clicks on the card to check, after that, it recognizes each click separetely.
Basically, I need two click two times for the first time in each item in the card to the checkbox be checked for the first time and then, it works normally.
Tried focusableInTouchMode in true and false and the problem occurred anyways.
Sorry for the typo mistakes here.
My item XML:
<?xml version="1.0" encoding="utf-8"?>
<layout 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_marginVertical="#dimen/half_default_size"
android:background="#drawable/team_member_rounded">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="false"
app:cardCornerRadius="#dimen/half_default_size"
app:cardElevation="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/members_card"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:padding="#dimen/default_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.cardview.widget.CardView
android:id="#+id/cv_member"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cl_checkbox"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="#+id/checkbox_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/text_member_name"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_size"
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#id/checkbox_member"
app:layout_constraintTop_toTopOf="parent"
tools:text="C012345 TESTE" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>`
</layout>
My setOnClickListener:
holder.checkBox.setChecked(membersSelected.contains(agent));
holder.checkBox.setOnClickListener(view -> {
if (membersSelected.contains(agent)) {
membersSelected.remove(agent);
} else {
membersSelected.add(agent);
}
});
holder.cardMember.setOnClickListener(view -> {
holder.checkBox.setChecked(membersSelected.contains(agent));
if (membersSelected.contains(agent)) {
membersSelected.remove(agent);
} else {
membersSelected.add(agent);
}
});
holder.cardView.setOnClickListener(view -> {
holder.checkBox.setChecked(membersSelected.contains(agent));
if (membersSelected.contains(agent)) {
membersSelected.remove(agent);
} else {
membersSelected.add(agent);
}
});
holder.itemView.setOnClickListener(view -> {
holder.checkBox.performClick();
holder.cardMember.performClick();
holder.cardView.performClick();
});
Tried using focusableInTouchMode="false" and "true", tried adding every component in the item to be clickable and so on.
I managed to solve the problem.
First of all, I've noticed that I was using a ConstraintLayout inside a CardView inside in another ConstraintLayout that was inside in another CardView. First of all, I've removed 2 of them from the XML and included android:background="?android:selectableItemBackground"> in the ConstraintLayout, makint it like this:
<?xml version="1.0" encoding="utf-8"?>
<layout 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_marginVertical="#dimen/half_default_size"
android:background="#drawable/team_member_rounded">
<androidx.cardview.widget.CardView
android:id="#+id/cv_member"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
app:cardCornerRadius="#dimen/half_default_size"
app:cardElevation="4dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/members_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:padding="#dimen/default_size"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="?android:selectableItemBackground">
<CheckBox
android:id="#+id/checkbox_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/text_member_name"
style="#style/TextAppearance.AppCompat.Body1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/half_default_size"
android:ellipsize="end"
android:maxLines="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#id/checkbox_member"
app:layout_constraintTop_toTopOf="parent"
tools:text="C012345 TESTE" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</layout>
And for the the adapter, on the onBindViewHolder, I changed to:
holder.checkBox.setChecked(membersSelected.contains(agent));
holder.checkBox.setOnClickListener(view -> {
if (membersSelected.contains(agent)) {
membersSelected.remove(agent);
} else {
membersSelected.add(agent);
}
});
holder.cardMember.setOnClickListener(view -> {
holder.checkBox.performClick();
});
With this, the cardView recognize the first click and check the checkbox.
Create model named members
public class Member{
int clickCount = 0;
String members;
//getters and setters
}
then add all members to the list of member
List<Member> memberList = new ArrayList();
for(int i=0;i<members.size();i++){
Member member = new Member();
member.setMember(members.get(i));
memberList.add(member);
}
then pass the memberList to the adapter and increment the value of clickCount on click of the cardview, and on 2 clickCount you can set checkBox as checked
then
I recently made a small app that should change the background color & text of the application when a certain amount of money is generated (here 100000). However, whenever I go past 10000, the application crashes. I am not sure why and can not get around it. I saw that this problem is with background color only because it is otherwise displaying text correctly.
Here is the XML for activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="#+id/Trial"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EC5353"
tools:context=".MainActivity">
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
android:gravity="center"
android:text="#string/Title"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:text="#string/moneyPresent"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Title" />
<Button
android:id="#+id/moneyGenerator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:background="#FFFFFF"
android:backgroundTint="#732424"
android:backgroundTintMode="multiply"
android:text="#string/generateMoneyButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/money" />
<TextView
android:id="#+id/richTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="#string/grind_boi"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/moneyGenerator" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my Java Code (Omitted the packages as it would take a lot of space. Do tell me if anyone wants to read it.)
public class MainActivity extends AppCompatActivity {
private Button makeMoney;
private int totalMoney=0;
private TextView moneyText;
private TextView richAlert;
private ConstraintLayout background;
NumberFormat currency=NumberFormat.getCurrencyInstance(new Locale("en","in"));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
makeMoney.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
totalMoney+=1000;
moneyText.setText(currency.format(totalMoney));
Log.d("MoneyTag", "Money Made: "+totalMoney);
Toast.makeText(getApplicationContext(),"Total Money= \u20B9"+totalMoney,Toast.LENGTH_LONG)
.show();
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro 😁");
background.setBackgroundColor(16776960);
}
}
});
}
}
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
You're finding all the views, except background(ConstraintLayout), that's why it is throwing NullPointerException (Most likely, because you haven't attached logs).
You need to find the layout first
background = findViewById(R.id.Trial);
in onCreate.
you haven't defined "background" view in the above.
to use .setBackgroundColor you need to use it with some view
try making object of layout view then change the color of that layout
mConstraintLayout =
(ConstraintLayout)findViewById(R.id.Trial);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro 😁");
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.yellow));
background = findViewById(R.id.Trial);
just find layout and null pointer excpetion will removed
I'm new to android studio, bear with any misunderstandings on my part. I want to display a string of musical lyrics to a text view called lyrics_view which resides in my fragment_lyrics.xml file. I've looked around a lot and followed some similar examples but am not having any luck solving my issue. Here is some of my code. When I hit the play button, a song plays via the Spotify api and I also want to display those lyrics to lyrics_view using the musixMatch api. I can provide more code if needed. Am I even going about displaying this string correctly? Or is there a completely different way I should be doing it? Thank you for making effort with a long post.
playSongButton() which is in my MainActivity class. This is responsible for playing songs and making a call to a calculateLyrics() method.
public void playSongButton(View view) {
try {
mPlayer.playUri(null, "spotify:track:15vzANxN8G9wWfwAJLLMCg", 0, 0);
//where i believe i am having issues
TextView textView = null;
TextView displayTextView = (TextView) findViewById(R.id.lyrics_view);
String lyricString = songLyrics.calculateLyrics();
displayTextView.setText(lyricString);
} catch (Exception e) {
e.printStackTrace();
}
}
CalculateLyrics() which is in SongLyrics class
public SongLyrics(String songName, String artistName) {
this.songName = "Paris";
this.artistName = "The Chainsmokers";
}
public String calculateLyrics() {
try {
musixMatch = new MusixMatch(key.getKey());
track = musixMatch.getMatchingTrack(songName, artistName);
trackData = track.getTrack();
trackId = trackData.getTrackId();
lyrics = musixMatch.getLyrics(trackId);
lyricString = lyrics.getLyricsBody();
} catch (Exception e) {
e.printStackTrace();
}
return lyricString;
}
activity_main.xml
<?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"
android:orientation="horizontal"
android:weightSum="1"
app:layout_collapseParallaxMultiplier="1.0">
<EditText
android:id="#+id/songName"
android:layout_width="0dp"
android:layout_height="56dp"
android:ems="10"
android:hint="#string/enter_song"
android:inputType="text"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
android:layout_marginTop="88dp"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="6dp" />
<EditText
android:id="#+id/artistName"
android:layout_width="0dp"
android:layout_height="55dp"
android:ems="10"
android:hint="#string/enter_artist"
android:inputType="text"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Display1"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
tools:layout_editor_absoluteX="0dp"
app:layout_constraintTop_toBottomOf="#+id/songName" />
<TextView
android:text="Enter Song Info"
android:layout_width="wrap_content"
android:layout_height="69dp"
android:id="#+id/enterSongInfo"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="80dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="80dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintHorizontal_bias="1.0"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp" />
<Button
android:text="Play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/playButton"
android:onClick="playSongButton"
android:layout_marginStart="296dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="296dp"
android:layout_marginTop="88dp"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:text="Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pauseButton"
android:onClick="pauseSongButton"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/playButton"
android:layout_marginStart="296dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="296dp" />
<fragment
android:id="#+id/lyrics"
android:layout_width="353dp"
android:layout_height="329dp"
android:layout_marginStart="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="8dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginTop="208dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.71"
class="com.example.joeberg.jams.LyricsFragment"/>
/>
</android.support.constraint.ConstraintLayout>
fragment_lyrics.xml
<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"
>
<TextView
android:text="Lyrics"
android:layout_width="93dp"
android:layout_height="59dp"
android:id="#+id/lyrics_heading"
class="com.example.joeberg.jams.LyricsFragment"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="280dp"
android:textColor="#color/common_google_signin_btn_text_dark_focused" />
<TextView
class="com.example.joeberg.jams.LyricsFragment"
android:layout_width="335dp"
android:layout_height="271dp"
android:id="#+id/lyrics_view"
app:layout_constraintTop_toBottomOf="#+id/lyrics_heading"
android:textAppearance="#style/TextAppearance.AppCompat"
android:layout_marginStart="32dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="32dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintHorizontal_bias="0.52" />
</android.support.constraint.ConstraintLayout>
You need to filter the results of your query to musixmatch. When I run a curl command with artist The Chainsmokers and track Paris you recieve 20 tracks returned from musixmatch (mostly club remixes). You have to write some code to find/filter the results to the one you want to display using the filter parameters mentioned in user documentation (f_ input parameters)
curl:
curl --url "http://api.musixmatch.com/ws/1.1/track.search?q_artist=The%20Chainsmokers&q_track=Paris&apikey
partial output:
{"message":{"header":{"status_code":200,"execute_time":0.019639015197754,"available":20},"body":{"track_list":[{"track":{"track_id":173322884,"track_name":"Paris (Mixed)","track_name_translation_list":[],"track_rating":1,"commontrack_id":96669304,"instrumental":0,"explicit":0,"has_lyrics":0,"has_subtitles":0,"has_richsync":0,"num_favourite":0,"album_id":32901374,"album_name":"Let's Dance, Vol. 2 (DJ Mix)","artist_id":26490468,"artist_name":"The Chainsmokers","track_share_url":"https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed?utm_source=application&utm_campaign=api&utm_medium=","track_edit_url":"https://www.musixmatch.com/lyrics/The-Chainsmokers/Paris-Mixed/edit?utm_source=application&utm_campaign=api&utm_medium=","restricted":0,"updated_time":"2019-06-12T15:47:28Z","primary_genres":{"music_genre_list":[]}}},{"track":{"track_id":126251071,"track_name":"Paris (Sbm X Geru Remix)","track_name_translation_list":[],"track_rating":5,"commontrack_id":69979021,"instrumental":0,"explicit":0,"has_lyrics":1,"has_subtitles":0,"has_richs
I have two TextView which I want to put a custom font on.
In the OnCreate of my MainActivity, after the setContentView call, I wrote the following code:
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/BernerBasisschrift1.ttf");
TextView welcomeTextView = (TextView)findViewById(R.id.welcome_message);
TextView introTextView = (TextView)findViewById(R.id.introduction_message);
welcomeTextView.setTypeface(myTypeface);
introTextView.setTypeface(myTypeface);
But for some reason, the font stays default.
The app runs on Galaxy S6 Edge device (Lollipop 5.0.2).
I read here that there's a problem with custom fonts on Lollipop, and in order to fix it I converted my font with the tool provided in the thread to ttx and vice versa, but even this didn't help.
Any ideas what can I do?
Edit:
Tested on Jellybean (4.3), not working either.
Another edit:
It works on a clean new project! Cant figure out the difference.
Putting the MainActivity and its' XML for help:
public class MainActivity extends FragmentActivity {
private LoginFragment loginFragment;
private TextView welcomeTextView;
private TextView introTextView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
hideActionBar();
setContentView(R.layout.activity_main);
welcomeTextView = (TextView)findViewById(R.id.welcome_message);
introTextView = (TextView)findViewById(R.id.introduction_message);
Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/BernerBasisschrift1.ttf");
welcomeTextView.setTypeface(myTypeface);
introTextView.setTypeface(myTypeface);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
loginFragment = new LoginFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, loginFragment, "facebookFragment").commit();
}
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
// get all views on screen
ArrayList<View> views = new ArrayList<View>()
{{
add(findViewById(R.id.welcome_message));
add(findViewById(R.id.introduction_message));
add(findViewById(R.id.below_login));
add(findViewById(R.id.above_login));
add(findViewById(R.id.authButton));
add(findViewById(R.id.logo));
}};
// set animations
for(View view : views)
YoYo.with(Techniques.Tada).duration(700).playOn(view);
}
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void hideActionBar(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
getActionBar().hide();
}
And the activity's XML:
<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:background="#drawable/main_background">
<ImageView
android:id="#+id/logo"
android:layout_width="256dp"
android:layout_height="101dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:background="#drawable/logo" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp">
<TextView
android:id="#+id/welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/welcome_message"
android:textColor="#color/White"
android:textSize="25sp"
style="#style/ShadowStyleText"
android:gravity="center" />
<TextView
android:id="#+id/introduction_message"
android:layout_below="#+id/welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/introduction_message"
android:textColor="#color/White"
android:textSize="18sp"
android:gravity="center"
style="#style/ShadowStyleText"
android:layout_marginTop="5dp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/bottom_login_chunk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:layout_marginStart="50dp"
android:layout_marginLeft="50dp"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp">
<TextView
android:id="#+id/above_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/above_login_button"
android:textColor="#color/White"
android:textSize="18sp"
style="#style/ShadowStyleText"
android:gravity="center" />
<com.facebook.widget.LoginButton
android:id="#+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/above_login"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp" />
<TextView
android:id="#+id/below_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/authButton"
android:layout_centerHorizontal="true"
android:text="#string/below_login_button"
android:textColor="#color/White"
style="#style/ShadowStyleText"
android:textSize="14sp" />
</RelativeLayout>
</RelativeLayout>
Ok so I found out recently that the facebook login button caused the problem, since everything worked when it was commented out.
So basically to solve this problem use another method to show the button instead using support fragment manager; sdk docs has lots of examples.
I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.
<LinearLayout
android:id="#+id/layout_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator"
android:layout_margin="5dp"
android:background="#color/yellow_background"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_check_empty" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tv_add_team_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:text="Add a team member"
android:textColor="#color/text_green"
android:textSize="15dp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_add_team_member"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/iv_create_assignment_attach"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_flag_alt" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="15"
android:text="Due"
android:textSize="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="65"
android:hint="None"
android:selectAllOnFocus="true" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_calendar_empty" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_create_assignment_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:text="Trash" />
<Button
android:id="#+id/btn_create_assignment_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:text="Done" />
</LinearLayout>
</LinearLayout>
Here is the Java code, the onClickListener implementation of the TextView.
tvAddTeamMember.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
EditText et = new EditText(getActivity());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Text");
ll.addView(et);
}
You may proceed this way ! This is not what you're looking for. But your problem will get resolved.
1) By default add the EditText view in your XML and make it invisible.
2) On Clicking the TextView, make it visible.
enjoy !
Hi #Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
Try to change the line :
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
With this:
android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Then do your work
If you know you might be adding a layout, you could always declare ViewStub. After that you simply find the viewStub and call inflate() on it:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
This is the promoted way to do that in Android SDK. Thanks to that you can define the infalte layout in an xml file.
Try with this code.
replace ur line ll.addView(et);
by::
ll.addView(et,p);
and delete ur line et.setLayoutParams(p);
A simple way to do this.
Button add = (Button) findViewById(R.id.add_checkpoint);
final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);
// layout has assigned weight = "1.0" in xml & it's a child of ScrollView
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater oLayoutInflater = getLayoutInflater();
// oView is declared globally & Layout "row_checkpoint" contains a EditText
oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);
final EditText speciesEditText = (EditText) oView
.findViewById(R.id.checkpoint);
speciesEditText.setId(layout.getChildCount());
// Perform whatever required over EditText, same way you can add more elements.
layout.addView(oView);
}
});
Hope it will help.