Showing posts with label button. Show all posts
Showing posts with label button. Show all posts

Saturday, 20 October 2018

Make back button working in action bar in Android.

Recently I was trying to use working back button on my one of the project, but whatever I was done previously was not working. It was a crash every time with error message -


I am newbie to Android, but I was done back activity few day back but after updating my Android Studios and APIs nothing was working. I was also uses  inflated menu in my project, and debugger always stuck in onOptionsItemSelected(MenuItem item) method which I was used for option menu. I also tried many unsuccessful lines of code. Finally I got this solution for my problem -

1 .  In  onCreate(Bundle savedInstanceState) method use -

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

2 .  Override methos in your activity class

@Overridepublic boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}


3 . In onOptionsItemSelected(MenuItem item) method use -

switch (item.getItemId()) {  
       case android.R.id.home:
        Intent i = new Intent(this, Your_Parent_Activity.class);
        intent.addFlags(i.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
        return true;
}


Friday, 7 September 2018

How to customize/decorate a button in Android Studio?

How can we customize button in Android Studio?. It can be a background color, its shape, its fonts, image, etc. It can be accomplished by using the background property of the button that should be drawable.

Let see how we can do it by example-

First, create a button on an activity.

<Button    android:id="@+id/button"    android:layout_width="wrap_content"  
  android:layout_height="wrap_content"    android:text="Button" 
   tools:layout_editor_absoluteX="45dp"    tools:layout_editor_absoluteY="31dp" />


Now create a drawable resource file. To create this expand your app in project directory structure. Right click on res directory, then select New ----> Android resource file. Now put the file name (
I have used button1.xml) then Ok.



Now in this resource file (button1.xml), write the code for button customization.
See below the code-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
 <!--shape of button [You can use here rectangle, oval, line, Ring] -->
<solid android:color="#eeffeeee" /> <!--color of button -->
<corners android:bottomRightRadius="38dp" 
   android:bottomLeftRadius="38dp"  
  android:topRightRadius="38dp"   
 android:topLeftRadius="38dp"/> <!--corners of button -->
</shape>

Now in button, add the background property like -

android:background="@drawable/button1" 

button1 is drawable resource file name that you have created in res ---> drawable folder.

Some time we require image as well as test on button then -

Copy your image in res ---> drawable folder. Make sure that the file name should only contain characters and digits and file name should start only from small letter alphabets.

Now use another property in your button -

android:drawableLeft="@drawable/icon"

Where icon is image file name, which you have copied in res --> drawable.

Now See the complete activity code and structure.





Here is the complete reference of resource file (copied from
https://developer.android.com/guide/topics/resources/drawable-resource#StateList:-

 <?xml version="1.0" encoding="utf-8"?>
<shapexmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
<gradient
android:angle="integer"
android:centerX="float"
android:centerY="float"
android:centerColor="integer"
android:endColor="color"
android:gradientRadius="integer"
android:startColor="color"
android:type=["linear" | "radial" | "sweep"]
android:useLevel=["true" | "false"] />
<padding
android:left="integer"
android:top="integer"
android:right="integer"
android:bottom="integer" />
<size
android:width="integer"
android:height="integer" />
<solid
android:color="color" />
<stroke
android:width="integer"
android:color="color"
android:dashWidth="integer"
android:dashGap="integer" />
 </shape>

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape=["rectangle" | "oval" | "line" | "ring"] >
    <corners
        android:radius="integer"
        android:topLeftRadius="integer"
        android:topRightRadius="integer"
        android:bottomLeftRadius="integer"
        android:bottomRightRadius="integer" />
    <gradient
        android:angle="integer"
        android:centerX="float"
        android:centerY="float"
        android:centerColor="integer"
        android:endColor="color"
        android:gradientRadius="integer"
        android:startColor="color"
        android:type=["linear" | "radial" | "sweep"]
        android:useLevel=["true" | "false"] />
    <padding
        android:left="integer"
        android:top="integer"
        android:right="integer"
        android:bottom="integer" />
    <size
        android:width="integer"
        android:height="integer" />
    <solid
        android:color="color" />
    <stroke
        android:width="integer"
        android:color="color"
        android:dashWidth="integer"
        android:dashGap="integer" />
</shape>





Saturday, 3 September 2016

Dynamically create rows, checkboxes, buttons in JSP/JAVA

Generally we create rows, check boxes, buttons, hyperlink statically. But when we want it to create dynamically or when we have to associate it with row fetched from database and we don't know the exact result has to come.

If we want to create fixed number of elements dynamically, obviously we use loop like that

<table>
<%
int i=0;
while(i<=10)
{
%>
              <tr><td>your data</td></tr>   //it prints 10 table row
              <input  type="search"  />        // it prints 10 textfield
              <input type="submit" value="Search" />   // it prints 10 buttons
              ..... and so on 
<%
i++;
}
%>
</table>


If we get data from database and want these elements to associate with this then

just change the loop

while(rs.next())
{
// put your elements according to you//
}



Sunday, 18 October 2015

Customize Textbox, List/Menu, Button or Div border style

My title
You can customize Textbox, List/Menu or Div using CSS.

All input can be rounded and with border color:

CSS:-

input{
border-radius: 8px;
border-color: #98bf21;
}

HTML:-

<input type="text" name="username" placeholder="Enter Password" required />

<input type="submit" name="Submit" value="Login" style="width:150px;"/>

 <select name="select2" id="select2">
          <option>Select Scheduled Month</option>
          <option>January</option>
          <option>February</option>
          <option>March</option>
          <option>April</option>
          <option>May</option>
          <option>June</option>
          <option>July</option>
          <option>August</option>
          <option>September</option>
          <option>October</option>
          <option>November</option>
          <option>December</option>
        </select>


Output:-






Change image source dynamically on hyperlink

 Changing image source dynamically using JQuery. Here in this example I have created there hyperlink and stored all images in the same folde...