Custom ActionBar setCustomView() has extra margins - java

I am not really good when it comes to designing. I already have an ActionBar I just want to customize it.
As you can see in the screenshot it is not properly aligned. The yellow one is the default ActionBar and the Gray is the customized one. What should I do?
Here is my xml code for the ActionBar:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/grey_700" >
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textStyle="bold" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
app:srcCompat="#drawable/pic" />
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:background="#null"
android:src="#android:drawable/ic_menu_rotate" />
And inside my onCreate()
ActionBar mActionBar = getSupportActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.st_actionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText("My Own Title");
ImageButton imageButton = (ImageButton) mCustomView
.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);

This ActionBar is out of date, and if you don't have to use ActionBar I recommend you use toolbar.You can have a look at the official web site, https://developer.android.com/reference/android/support/v7/widget/Toolbar.html

Related

Custom alert dialog view does not show things set dynamically

I have made an alert dialog which shows a custom view,
Java Code
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.about_me_alert, null);
CircleImageView imageView = view.findViewById(R.id.image);
Glide.with(view).load("https://example.com/example.jpg").into(imageView);
TextView title = view.findViewById(R.id.title);
title.setText("This is a title");
TextView note = view.findViewById(R.id.note);
note.setText("This is a note");
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setView(view);
builder.setPositiveButton("Okay", (dialog, which) -> dialog.dismiss()).show();
XML Layout- about_me_alert.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/cardbackground"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/image"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_centerHorizontal="true"
android:layout_margin="8dp" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:textColor="#color/white"
android:textSize="16sp" />
<TextView
android:id="#+id/note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_margin="8dp"
android:textColor="#color/white"
android:textSize="16sp" />
</RelativeLayout>
When alert is shown, it reserves space for the imageview, and two textviews but does not set text or image.
NOTE: This only happens when I make a production apk for release, not while I am testing on my phone.

How to creat Dialog PopUp window with custom Layout?

I'm trying to create a dialog popup window with a custom layout, but my custom layout height and popup window height is not matching. Also, the popup window is hiding a button. What should I do, how can I match my popup window height with a custom layout?
Please help me.
I want my popup window to look like this
But it is looking like this
My custom layout
<?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="336dp"
android:layout_height="156dp"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|center"
android:background="#drawable/rectangle"
android:layout_marginBottom="88dp">
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="58dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/bikepic" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginEnd="64dp"
android:layout_marginBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/delivery" />
<TextView
android:id="#+id/biketextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="69dp"
android:layout_marginTop="17dp"
android:background="#drawable/biketextview"
android:gravity="center"
android:text="Bike"
android:textColor="#FFFFFF"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/deliverytextView"
android:layout_width="64dp"
android:layout_height="18dp"
android:layout_marginTop="17dp"
android:layout_marginEnd="64dp"
android:background="#drawable/deliverytextview"
android:gravity="center"
android:text="Delivery"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView7"
android:layout_width="23dp"
android:layout_height="14dp"
android:layout_marginStart="78dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="74"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView5" />
<TextView
android:id="#+id/textView8"
android:layout_width="23dp"
android:layout_height="14dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="80dp"
android:gravity="center"
android:text="39"
android:textSize="12sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView6" />
popup dialog code
popAddPost = new Dialog(this);
popAddPost.setContentView(R.layout.pop_up_confirm_pickup);
//popAddPost.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//popAddPost.getWindow().setLayout(Toolbar.LayoutParams.MATCH_PARENT,Toolbar.LayoutParams.WRAP_CONTENT);
popAddPost.getWindow().getAttributes().gravity = Gravity.BOTTOM;
In your xml, the parent layout is using RelativeLayout LayoutParams on a ConstraintLayout
Remove android:layout_alignParentLeft="true" and android:layout_alignParentEnd="true"
ConstraintLayout also doesn't support layout_gravity so you can also remove that.
<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"
android:background="#drawable/rectangle"
android:layout_marginBottom="88dp">
Code
popAddPost = new Dialog(this);
popAddPost.setContentView(R.layout.pop_up_confirm_pickup);
Objects.requireNonNull(popAddPost.getWindow()).setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
popAddPost.show();
EDIT
To position the dialog at the bottom use this:
Window window = popAddPost.getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.gravity = Gravity.BOTTOM;
window.setAttributes(params);
This is how i do it:
private AlertDialog alertDialog;
alertDialog = buildDialog();
alertDialog.show();
private AlertDialog buildDialog(){
final AlertDialog dialogBuilder = new AlertDialog.Builder(context).create();
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = inflater.inflate(R.layout.dialog, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setButton(DialogInterface.BUTTON_NEGATIVE, "CLOSE", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialogBuilder.dismiss();
}
});
return dialogBuilder;
}
I think dialog is not a good solution for your problem as a dialog would have its own window with overlay.
As I can see you want the dialog to be at the bottom of the screen, which makes the BottomSheet as a perfect match for your problem.
You can use the following to get started with:
https://www.androidhive.info/2017/12/android-working-with-bottom-sheet/
https://material.io/develop/android/components/bottom-sheet-behavior/
https://medium.com/#droidbyme/android-bottom-sheet-7e9cfcec6427
With BottomSheet, you would get a separate window just like dialog without the overlay you are facing.

How to set title and back arrow button in toolbar for each fragment?

//Here is my fragment
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white">
<RelativeLayout
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="#dimen/_420sdp"
android:background="#EAEAEA"
>
<RelativeLayout
android:id="#+id/home_bg_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/_400sdp"
android:background="#drawable/sama">
<Button
android:id="#+id/bookAppointmentBtn"
android:layout_width="match_parent"
android:layout_height="#dimen/_55sdp"
android:layout_marginLeft="#dimen/_30sdp"
android:layout_marginRight="#dimen/_30sdp"
android:fontFamily="#string/roboto_light"
android:textSize="#dimen/_20sdp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="#dimen/_10sdp"
android:background="#drawable/book_appointmentbg"
android:textColor="#color/white"
android:text="#string/bookappointment" />
</RelativeLayout>
</RelativeLayout>
<Button
android:id="#+id/ContactNoBtn"
android:layout_width="match_parent"
android:layout_height="#dimen/_46sdp"
android:text="#string/shop_number"
android:textColor="#ffffff"
android:textSize="#dimen/_25sdp"
android:layout_below="#+id/discountsTv"
android:textAllCaps="false"
android:layout_marginRight="#dimen/_30sdp"
android:layout_marginTop="#dimen/_5sdp"
android:layout_marginLeft="#dimen/_30sdp"
android:background="#drawable/black_background"/>
<LinearLayout
android:id="#+id/servicesLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:layout_marginLeft="#dimen/_20sdp"
android:layout_marginRight="#dimen/_20sdp"
android:orientation="horizontal"
android:layout_below="#+id/servicesupperview"
>
<LinearLayout
android:id="#+id/menLayout"
android:layout_width="#dimen/_108sdp"
android:layout_height="#dimen/_27sdp"
android:layout_margin="#dimen/_10sdp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#string/roboto_medium"
android:textColor="#535353"
android:layout_marginStart="#dimen/_17sdp"
android:layout_marginTop="#dimen/_5sdp"
android:textSize="#dimen/_12sdp"
android:text="#string/mens"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/womenLayout"
android:layout_width="#dimen/_108sdp"
android:layout_height="#dimen/_27sdp"
android:layout_margin="#dimen/_10sdp"
android:layout_weight="1"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#string/roboto_medium"
android:textSize="#dimen/_12sdp"
android:layout_marginStart="#dimen/_10sdp"
android:layout_marginTop="#dimen/_5sdp"
android:textColor="#535353"
android:text="#string/womens"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/childrenLayout"
android:layout_width="#dimen/_108sdp"
android:layout_height="#dimen/_27sdp"
android:layout_margin="#dimen/_10sdp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/_12sdp"
android:fontFamily="#string/roboto_medium"
android:textColor="#535353"
android:layout_marginStart="#dimen/_20sdp"
android:layout_marginTop="#dimen/_5sdp"
android:text="#string/children_caps"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I am having toolbar in MainActivity. This is my fragment i want to set title, image and back button inside the toolbar. Here i am using bottom navigation to change the fragments. Each and every fragment i want to set different title and image. I have to set title as center of the toolbar and back button as right corner of the toolbar.
In your MainActivity add these lines in OnCreate() method
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("My Activity Title");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
To use Back arrow you can write this code outside onCreate()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
Toast.makeText(this, "Back button pressed", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
In you fragment you can change title of Toolbar
getActivity().setTitle("My fragment Title");
You can Create custom Toolbar with ImageView and TextView with your arrow .
include_tool_bar.xml
<com.mastercard.wallet.ui.widget.CustomTextView
android:id="#+id/toolbar_title"
style="#style/Toolbar.TitleText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|left"
android:textSize="#dimen/px_48"
app:fontName="InterStateLight"
customviews:fontName="InterStateLight" />
<ImageView
android:id="#+id/toolbar_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="#string/adaCitiPayLogoText"
android:src="#drawable/ab_logo"
android:visibility="gone" />
And Add Toolbar
Toolbar toolbar = (Toolbar) appCompatActivity.findViewById(R.id.toolbar);
ImageView logo = (ImageView) toolbar.findViewById(R.id.toolbar_logo);
CustomTextView textView = (CustomTextView) toolbar.findViewById(R.id.toolbar_title);
logo.setVisibility(View.VISIBLE);
logo.setImageResource(logoResourceId);
textView.setVisibility(View.GONE);
appCompatActivity.setSupportActionBar(toolbar);
appCompatActivity.getSupportActionBar().setDisplayShowTitleEnabled(false);
title of the toolbar can be changed from fragment calling this in onActivityCreated
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((MainActivity) getActivity()).getSupportActionBar().setTitle(getString(R.string.menu_about_us));
((MainActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
}
in your activity make sure to add a toolbar like this.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
and set it in your activity
setSupportActionBar(findViewById(R.id.toolbar));
and for the back press just add a listener and do the needful.
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// pop up fragment or take another action
}
});

Android: made a text in RelativeLayout and can't change text in activity?

<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="#string/text"
android:textColor="#F0F8FF"
android:textSize="50sp"
android:textStyle="bold" />
This is declaration of the text field in activity_main.xml.
And the activity:
TextView text = (TextView) findViewById(R.id.textView1);
text.setText(napis);
Program crashes when I call setText() in onReasume() and it doesn't do anything when I call setText() in onCreate().
Whole xml file as requested:
<?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"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</FrameLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/zajebiste1" />
<Chronometer
android:textColor="#F0F8FF"
android:id="#+id/chronometer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="46dp"
android:gravity="center"
android:textSize="40sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="#string/text"
android:textColor="#F0F8FF"
android:textSize="50sp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
Activity:
text = (TextView) findViewById(R.id.textView1);
text.setText("Your Text");
Can't show more, cause something doesn't work... xf
onCreate():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView) findViewById(R.id.textView1);
napis="Witaj!";
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//,........
text = (TextView) findViewById(R.id.textView1);
text.setText("Your Text");
senSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
senAccelerometer = senSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
senSensorManager.registerListener(this, senAccelerometer , SensorManager.SENSOR_DELAY_NORMAL);
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
onReasume():
#Override
protected void onResume() {
super.onResume();
senSensorManager.registerListener(this, senAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
First of all, what is up with
//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.activity_main); ??
Can you try doing
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_main, this);
TextView text = (TextView) layout.findViewById(R.id.textView1);
text.setText(napis);
setContentView(layout);
set the contentView only once, towards the end of the method..
Try this, then post logcat if it doesn't work

Change textview dynamically in Viewflipper

pretty easy question here, but I am not a java superuser quite yet.
I am using a ViewFlipper to provide a number of images, and a sliding-drawer to contain text specific to each image. I would like to have the text of the sliding drawer change to a specific string dependent on which child view is currently displayed.
Here is the 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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prev" />
<Button
android:id="#+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="#drawable/image1"></ImageView>
<ImageView android:id="#+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="#drawable/image2"></ImageView>
</ViewFlipper>
</LinearLayout>
here is the java:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper = (ViewFlipper)findViewById(R.id.flipper);
next = (Button) findViewById(R.id.next);
previous = (Button) findViewById(R.id.previous);
imageview1 = (ImageView)findViewById(R.id.imageView1);
imageview2 = (ImageView)findViewById(R.id.imageView2);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == next) {
flipper.setInAnimation(inFromRightAnimation());
flipper.setOutAnimation(outToLeftAnimation());
flipper.showNext();
}
if (v == previous) {
flipper.setInAnimation(inFromLeftAnimation());
flipper.setOutAnimation(outToRightAnimation());
flipper.showPrevious();
}
}
I'm sure the answer is really obvious, but that's why I need your help....THANKS!
When you call showNext() and showPrevious(), update your TextView to match.

Categories