okay so I am trying to display album art for songs in my app . I am trying to do it by using Mediametadataretriever. But I am getting an IllegalStateException in line metaRetriver.setDataSource(Environment.getExternalStorageDirectory().getPath() );
I understand it might be because of using an invalid path but I can't figure out what is the valid path then . I am really newbie to android app development. Can anyone help me with it ?
PlayListActivity.java :
package com.example.dell_1.myapp3;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import java.io.IOException;
public class PlayListActivity extends Activity {
private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;
MediaMetadataRetriever metaRetriver;
byte[] art;
ImageView album_art;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_list);
mMediaPlayer = new MediaPlayer();
ListView mListView = (ListView) findViewById(R.id.list);
mMusicList = getAudioList();
ArrayAdapter<String> mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, mMusicList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int arg2,
long arg3) {
try {
playSong(mAudioPath[arg2]);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private String[] getAudioList() {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA}, null, null,
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
mAudioPath = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return songs;
}
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
setContentView(R.layout.activity_android_building_music_player);
Log.d("ringtone", "playSong :: " + path);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
//mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
acv();
}
public void acv() {
getInit();
metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource(Environment.getExternalStorageDirectory().getPath() );
try {
art = metaRetriver.getEmbeddedPicture();
Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length);
album_art.setImageBitmap(songImage);
} catch (Exception e) {
album_art.setBackgroundColor(Color.GRAY);
}
}
public void getInit() {
album_art = (ImageView) findViewById(R.id.coverart);
}
}
activity_play_list.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#242424"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
activity_android_building_music_player.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:id="#+id/player_header_bg"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:layout_alignParentTop="true"
android:background="#layout/bg_player_header"
android:paddingLeft="5dp"
android:paddingRight="5dp">
<TextView
android:id="#+id/songTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:paddingLeft="50dp"
android:text="The Good"
android:textColor="#04b3d2"
android:textSize="16dp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/btnPlaylist"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#null"
android:src="#drawable/btn_playlist" />
</LinearLayout>
<LinearLayout
android:id="#+id/songThumbnail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/player_header_bg"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<ImageView
android:id="#+id/coverart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/player_footer_bg"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#layout/bg_player_footer"
android:gravity="center">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:weightSum="1">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.40"
android:layout_alignParentLeft="true"
android:paddingLeft="10dp"
android:background="#null"
android:src="#drawable/btn_previous" />
<ImageButton
android:id="#+id/btnPlay1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#null"
android:src="#drawable/btn_play"
android:onClick="buttonAction1"/>
<ImageButton
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="250dp"
android:background="#null"
android:src="#drawable/btn_next" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<SeekBar
android:id="#+id/songProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/player_footer_bg"
android:layout_alignLeft="#+id/timerDisplay"
android:layout_alignStart="#+id/timerDisplay"
android:layout_marginBottom="10dp"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:progressDrawable="#drawable/seekbar_progress"
android:thumb="#drawable/download8" />
<LinearLayout
android:id="#+id/timerDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/songProgressBar"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp">
<TextView
android:id="#+id/songCurrentDurationLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:textColor="#eeeeee"
android:textStyle="bold" />
<TextView
android:id="#+id/songTotalDurationLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textColor="#04cbde"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#id/timerDisplay"
android:gravity="center">
<ImageButton
android:id="#+id/btnRepeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#null"
android:src="#drawable/btn_repeat" />
<ImageButton
android:id="#+id/btnShuffle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="#null"
android:src="#drawable/btn_shuffle" />
</LinearLayout>
</RelativeLayout>
Actually you are playing some some in one file path and you are trying to fetch the art from external storage file path, so you should send the path to that method,
private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
setContentView(R.layout.activity_android_building_music_player);
Log.d("ringtone", "playSong :: " + path);
mMediaPlayer.reset();
mMediaPlayer.setDataSource(path);
//mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
acv(path);
}
public void acv(String path) {
getInit();
metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource(path);
try {
art = metaRetriver.getEmbeddedPicture();
Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length);
album_art.setImageBitmap(songImage);
} catch (Exception e) {
album_art.setBackgroundColor(Color.GRAY);
}
}
And few suggestions,
You should not use setContentView multiple time in an activity I understand you are using 2 layouts, but in that case, you should use 2 different activity.
You playing something songs and user will not be in your app always they like to move around the application so try to use service to run the song in background
For more reference, you can get fantastic opensource app will be available in Github look into that.
Well you are trying to get a thumbnail from a specific file, but you are putting in the path of the External Storage Directory. So by using
metaRetriver.setDataSource(Environment.getExternalStorageDirectory().getPath() );
You are pointing to a folder, not a specific media file. But this:
metaRetriver.setDataSource(Environment.getExternalStorageDirectory().getPath() + "yourFileName.mp3" );
Is more in line with what you would need.
Related
How can a menu be made with this logic? what is logic? I used ExpandableListview this time, it made all of them as drop-down menus, I tried with recycler, and I couldn't make the drop-down menu part of it. what should i use?
you need create a custom-compound view and use animation for fadein/fadeout or dropDown/dropUp effect here is my sample code you can optimize it as you need:
Result:
activity_main.xml:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.example.junk3.Menu
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</com.example.junk3.Menu>
</androidx.constraintlayout.widget.ConstraintLayout>
drop_down_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<LinearLayout
android:id="#+id/box_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/item_1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="item_1 (click me)"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/item_1_detail"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="1dp"
android:background="#FFFFFF"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item_1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="item_1_1"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="item_1_2"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="item_1_3"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/box_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/box_1">
<LinearLayout
android:id="#+id/item_2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="horizontal">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="item_2 (click me)"
android:textColor="#000000" />
</LinearLayout>
<LinearLayout
android:id="#+id/item_2_detail"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#FFFFFF"
android:orientation="horizontal">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:text="item_2_1"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</merge>
MainActivity.java:
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import com.example.junk3.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
lateinit var text:TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Menu.java :
import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Menu extends LinearLayout {
private Context ctx;
private LinearLayout item_1,item_2,item_1_detail,item_2_detail;
public Menu(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
initView(ctx);
}
public Menu(Context context) {
this(context, null);
ctx = context;
initView(ctx);
}
public Menu(Context context, AttributeSet attrs,int defSyle){
super(context,attrs,defSyle);
ctx = context;
initView(ctx);
}
public void initView(Context context){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.drop_down_menu,this);
}
#Override
public void onFinishInflate() {
super.onFinishInflate();
item_1 = (LinearLayout) findViewById(R.id.item_1);
item_2 = (LinearLayout) findViewById(R.id.item_2);
item_1_detail = (LinearLayout) findViewById(R.id.item_1_detail);
item_2_detail = (LinearLayout) findViewById(R.id.item_2_detail);
item_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(item_1_detail.getLayoutParams().height>0){
Log.e("detail_1","1");
slideView(item_1_detail, item_1_detail.getLayoutParams().height, 0);
}else{
slideView(item_1_detail, 0, dp2px(ctx.getResources(),120));
}
}
});
item_2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(item_2_detail.getLayoutParams().height>0){
slideView(item_2_detail, item_2_detail.getLayoutParams().height, 0);
}else{
slideView(item_2_detail, 0, dp2px(ctx.getResources(),40));
}
}
});
}
public static void slideView(View view, int currentHeight, int newHeight) {
Log.e("here3","ddd");
ValueAnimator slideAnimator = ValueAnimator
.ofInt(currentHeight, newHeight)
.setDuration(500);
/* We use an update listener which listens to each tick
* and manually updates the height of the view */
slideAnimator.addUpdateListener(animation1 -> {
Integer value = (Integer) animation1.getAnimatedValue();
view.getLayoutParams().height = value.intValue();
view.requestLayout();
});
/* We use an animationSet to play the animation */
AnimatorSet animationSet = new AnimatorSet();
animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
animationSet.play(slideAnimator);
animationSet.start();
}
public static int dp2px(Resources resource, int dp) {
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,resource.getDisplayMetrics()
);
}
}
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">
...
I have a simple LinearLayout with several buttons, whos state color/text change based on the state of an underlying service, thats working fine.
However the buttons, are only clickable on the right corner ???
The button allSystemServicesToggleButton which i have included the implementation for in this post and only be clicked on the right side/right corner???
Here is my fragment xml layout & Actual screen shot with “Show Layout bounds” set to true:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:text="All System Services"
android:textColor="#000000"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="0dp"
android:singleLine="true"
android:onClick="onClick"/>
<Button
android:id="#+id/allSystemServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
android:enabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:text="#string/shutdown_all_services"
android:textColor="#000000"
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="0dp"
android:singleLine="true"
android:onClick="onClick"/>
<Button
android:id="#+id/shutdownAllServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/shutdown"
android:layout_weight="1"
android:layout_width="0dp"
android:enabled="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Networks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider1"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Bluetooth Service"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/btServicesToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!--TODO: get requirements for showing paired devices & pairing devices-->
<TextView
android:id="#+id/textPairedText"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Paired Bluetooth Devices"
android:textColor="#000000"
android:singleLine="true"
android:textSize="20sp"
android:layout_width="0dp"
/>
<TextView
android:id="#+id/textViewNumberOfConnectedDevices"
android:layout_height="wrap_content"
android:text="0"
android:layout_width="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/btDevicesToggleButton"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="2"
android:text="Pair"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="MQTT Service"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/MQTTserviceToggleButton"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
android:text="#string/stopped" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Location Services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider3"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="GPS"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/gpsServiceToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="Command Services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="32sp"
android:textColor="#000000"/>
<View
android:id="#+id/viewServicesDivider4"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#808080"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Voice Recognition"
android:textColor="#000000"
android:textSize="20sp"
android:singleLine="true"/>
<Button
android:id="#+id/voiceRecognitionToggleButton"
android:layout_height="wrap_content"
android:text="#string/stopped"
android:layout_weight="1"
android:layout_width="0dp"
android:backgroundTint="#color/stoppedServiceColor"
/>
</LinearLayout>
Relevant fragment java:
package x.core.fragments;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import x.core.Application.NgfrApp;
import x.core.R;
import x.core.helpers.Util;
import x.core.services.BluetoothService;
import x.core.services.LocationService;
import x.core.services.MqttBrokerService;
import x.core.services.ServicesStateBroadcastReceiver;
import x.core.services.SpeechRecognitionService;
import x.core.services.UIService;
public class ServicesFragment extends Fragment implements View.OnClickListener {
private static final String TAG = "ServicesFragment";
public static ServicesFragment newInstance() {
return new ServicesFragment();
}
private static Button btServicesToggleButton;
private static Button mqttServicesToggleButton;
private static Button gpsServiceToggleButton;
private static Button voiceServiceToggleButton;
private static Button allServiceToggleButton;
private static String stopped = null;
private static String running = null;
private static int runningColorId, stoppedColorId = -1;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_services, container, false);
btServicesToggleButton = rootView.findViewById(R.id.btServicesToggleButton);
mqttServicesToggleButton = rootView.findViewById(R.id.MQTTserviceToggleButton);
gpsServiceToggleButton = rootView.findViewById(R.id.gpsServiceToggleButton);
voiceServiceToggleButton = rootView.findViewById(R.id.voiceRecognitionToggleButton);
allServiceToggleButton = rootView.findViewById(R.id.allSystemServicesToggleButton);
stopped = getResources().getString(R.string.stopped);
running = getResources().getString(R.string.running);
runningColorId = getResources().getColor(R.color.runningServiceColor);
stoppedColorId = getResources().getColor(R.color.stoppedServiceColor);
allServiceToggleButton.setEnabled(true);
allServiceToggleButton.setClickable(true);
allServiceToggleButton.setOnClickListener(this);
return rootView;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.allSystemServicesToggleButton:
if (ServicesStateBroadcastReceiver.BT_SERVICE_STATE_VALUE==false || ServicesStateBroadcastReceiver.MQTT_STATE_VALUE==false || ServicesStateBroadcastReceiver.NGFR_GPS_SERVICE_STATE_VALUE==false || ServicesStateBroadcastReceiver.VOICE_SERVICE_STATE_VALUE==false)
{
Toast.makeText(NgfrApp.getContext(),NgfrApp.getContext().getResources().getString(R.string.restarting_services),Toast.LENGTH_SHORT).show();
//restartingServices();
}
else
{
Toast.makeText(NgfrApp.getContext(),NgfrApp.getContext().getResources().getString(R.string.all_already_running),Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
MainActivity.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/"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<android.support.design.widget.TabLayout
android:id="#+id/activity_main_tabLyout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/activity_main_viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
MainActivity.java, I only included relevant code:
public class MainActivity extends AppCompatActivity {
private static String TAG = "Main";
private static final int CHECK_BT_CODE = 1;
private static final int CHECK_TTS_CODE = 2;
//global boolean flags that will communicate the state of the system at all times
//bluetooth related flags
public boolean isBleSupported = false;
public boolean isBluetoothEnabled = false;
public boolean accessBluetoothManager= false;
public boolean nearbyDevices = false;
//configuration data related
public boolean isConfigurationLoadedCorrectly = false;
//text to speech related
public boolean isTextToSpeechSupported = false;
private Context context = null;
private ServicesStateBroadcastReceiver servicesStateBroadcastReciever = null;
private ViewPager mainViewPager;
private TabLayout tabLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "Activity started!!");
context = this;
setContentView(R.layout.activity_main);
MainActivityViewPager adapter = new MainActivityViewPager(getSupportFragmentManager());
mainViewPager = (ViewPager) findViewById(R.id.activity_main_viewPager);
mainViewPager.setAdapter(adapter);
tabLayout = (TabLayout) findViewById(R.id.activity_main_tabLyout);
tabLayout.setupWithViewPager(mainViewPager );
}
}
The adapter for my fragments, FragmentStatePagerAdapter:
package x.core.views;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import x.BiometricsFragment;
import x.ServicesFragment;
public class MainActivityViewPager extends FragmentStatePagerAdapter {
public MainActivityViewPager(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
Fragment returnFragment;
switch(position) {
case 0:
returnFragment = ServicesFragment.newInstance();
break;
case 1:
returnFragment = BiometricsFragment.newInstance();
break;
default:
return null;
}
return returnFragment;
}
#Override
public int getCount() {
return 2;
}
public CharSequence getPageTitle(int position) {
CharSequence title;
switch (position) {
case 0:
title = "Services";
break;
case 1:
title = "Biometrics";
break;
default:
return null;
}
return title;
}
}
Thanks
only for corner clicking use this kind of logic
<FrameLayout
android:layout_width="50dp"
android:layout_height="50dp">
<TextView
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/colorAccent" />
<TextView
android:id="#+id/tvtttt"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="right"
android:background="#F00" />
</FrameLayout>
I am a new Android programmer. I am entering data into an SQLite database, and I want my ListView to then refresh and show the information that I have just added to the database. The ListView is below the add button. The logcat error shows a NullPointerException.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout6"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.34"
android:text="Registration No:"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/vehicle_reg"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:ems="10"
/>
<requestFocus />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout7"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.43"
android:text="Mileage:"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/vehicle_mileage"
android:layout_width="114dp"
android:layout_height="wrap_content"
android:layout_weight="0.29"
android:ems="10" >
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout8"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_weight="0.51"
android:text="Budget Amount:"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/vehicle_budget"
android:layout_width="167dp"
android:layout_height="wrap_content"
android:layout_weight="0.13"
android:ems="10"
android:inputType="numberDecimal" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout9"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.37"
android:text="Tank Capacity: "
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/vehicle_capacity"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/add_button_vehicle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Vehicle" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.43"
android:orientation="vertical" >
<ListView
android:id="#+id/vehicleList_reg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>
</LinearLayout>
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import com.example.budgetfuel.databasemodel.Car;
import com.example.budgetfuel.databasemodel.DBhelper;
import com.example.budgetfuel.databasemodel.DatabaseHelper;
import com.example.budgetfuel.databasemodel.SQLController;
public class VehicleSetup extends FragmentActivity {
Button addvehicle;
ListView lv;
SQLController dbcon;
Button btnAdd;
EditText reg, milge, cap, budgt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_settings);
// get action bar
ActionBar actionBar = getActionBar();
// Enabling Up / Back navigation
actionBar.setDisplayHomeAsUpEnabled(true);
dbcon = new SQLController(this);
dbcon.open();
addvehicle = (Button) findViewById(R.id.add_button_vehicle);
lv = (ListView) findViewById(R.id.listViewVehicles);
reg = (EditText) findViewById(R.id.vehicle_reg);
milge = (EditText) findViewById(R.id.vehicle_mileage);
cap = (EditText) findViewById(R.id.vehicle_budget);
budgt = (EditText) findViewById(R.id.vehicle_capacity);
// Attach The Data From DataBase Into ListView Using Crusor Adapter
Cursor cursor = dbcon.readData();
String[] from = new String[] {DBhelper.VEHICLE_ID,DBhelper.VEHICLE_REG, DBhelper.VEHICLE_BUDGET, DBhelper.VEHICLE_CAPACITY };
int[] to = new int[] { R.id.vehicle_id, R.id.vehicle_reg, R.id.vehicle_mileage, R.id.vehicle_budget,
R.id.vehicle_capacity };
#SuppressWarnings("deprecation")
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
VehicleSetup.this, R.layout.view_car_entry, cursor, from, to);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
//
addvehicle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String registration = reg.getText().toString();
int mileage = Integer.parseInt(milge.getText().toString());
int capacity = Integer.parseInt(cap.getText().toString());
double budget = Double.parseDouble(budgt.getText().toString());
dbcon.insertData(registration, mileage, budget, capacity);
reg.setText("");
milge.setText("");
cap.setText("");
budgt.setText("");
}
});
}
try to put adapter.notifyDataSetChanged(); after lv.setAdapter(adapter);
I have made a CustomTypeDialog class and what I want is to use EditText which is not in the active layout. I get a nullpointer exception when I try to click one of the buttons, which I think is because they are not in the active layout. Can you help me solve this? The dialog is called in an activity from another class.
package dk.droidrun.droidrunapp;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
public class CustomTypeDialog extends Dialog {
ImageButton routeType;
EditText txtType;
Button imageRun, imageBike, imageWalk;
public CustomTypeDialog(final Context context) {
super(context);
this.setContentView(R.layout.customtype_dialog);
routeType = (ImageButton)findViewById(R.id.saveRoute_activityType);
txtType = (EditText)findViewById(R.id.saveRoute_typeTxt);
imageRun = (Button)findViewById(R.id.dialog_btn1);
imageBike = (Button)findViewById(R.id.dialog_btn2);
imageWalk = (Button)findViewById(R.id.dialog_btn3);
setTitle("Select activity type");
show();
imageRun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtType.setText("Run");
routeType.setBackgroundResource(R.drawable.track_run);
}
});
imageBike.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtType.setText("Bike");
routeType.setBackgroundResource(R.drawable.track_bike);
}
});
imageWalk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtType.setText("Walk");
routeType.setBackgroundResource(R.drawable.track_walk);
}
});
}
}
This is my customtype_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AutoMode"
android:background="#color/black" >
<RelativeLayout
android:id="#+id/dialog_relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_above="#+id/dialog_relativeLayout2"
android:layout_centerHorizontal="true" >
<Button
android:id="#+id/dialog_btn1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/track_run"
android:layout_alignRight="#+id/dialog_relativeLayout1"
android:layout_alignTop="#+id/dialog_relativeLayout1"
/>
<Button
android:id="#+id/dialog_btn2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:background="#drawable/track_bike"
android:layout_alignTop="#+id/dialog_relativeLayout1"
android:layout_toRightOf="#+id/dialog_btn1"
/>
<Button
android:id="#+id/dialog_btn3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="3dp"
android:layout_toRightOf="#+id/dialog_btn2"
android:background="#drawable/track_walk"
/>
</RelativeLayout>
</RelativeLayout>
saveroutes.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"
android:orientation="vertical"
android:background="#color/black"
tools:context=".SaveRouteActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="#string/saveRoute"
android:textColor="#color/white"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_marginTop="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Enter a name for the route" />
<EditText
android:id="#+id/saveRoute_nameRoute"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="#string/saveRoute_name"
android:textColor="#color/white"
android:background="#4e4751"
android:inputType="textPersonName" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Describe your route" />
<EditText
android:id="#+id/saveRoute_desciptionTxt"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:hint="#string/saveRoute_description"
android:textColor="#color/white"
android:background="#4e4751"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7a0100"
android:text="Activity type (e.g. running)"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal" >
<EditText
android:id="#+id/saveRoute_typeTxt"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginTop="17dip"
android:ems="10"
android:layout_marginLeft="30dp"
android:hint="#string/saveRoute_type"
android:textColor="#color/white"
android:background="#4e4751" >
<requestFocus />
</EditText>
<ImageButton
android:id="#+id/saveRoute_activityType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/track_walk" />
</LinearLayout>
<Button
android:id="#+id/saveRoute_saveBtn"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#color/white"
android:text="#string/saveRoute_savebutton" />
<Button
android:id="#+id/saveRoute_cancelBtn"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:background="#color/white"
android:text="#string/saveRoute_cancel" />
</LinearLayout>
You cannot access views from another layout (your activity) using findViewById within the dialog view.
You need to add a callback listener for when the buttons on your dialog are clicked:
public interface OnDialogClickListener {
void onDialogImageRunClick();
}
public class CustomTypeDialog extends Dialog {
private final OnDialogClickListener listener;
public CustomTypeDialog(final Context context, OnDialogClickListener listener) {
this.listener = listener;
}
....
imageRun.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listener.onDialogImageRunClick();
}
);
}
Then when you create your dialog in your Activity where you have access to the view:
new CustomTypeDialog(context, new CustomTypeDialog.OnDialogClickListener() {
#Override
public void onDialogImageRunClick() {
txtType.setText("Run");
routeType.setBackgroundResource(R.drawable.track_run);
}
});